def _get_client_id_from_originator(originator): _, cse_id, ae_id = split_onem2m_address(originator) if cse_id: client_id = cse_id[1:] + ('/' + ae_id if ae_id else '') elif ae_id: client_id = ae_id else: # TODO: make this configurable client_id = 'ae0' return client_id
def send_onem2m_request(self, onem2m_request): path = onem2m_request.to sp_id, cse_id, _ = split_onem2m_address(path) if not cse_id: poa_list = [] else: if sp_id and sp_id[2:] != self.sp_id: # inter-domain routing raise CSENotImplemented() else: # intra-domain routing poa_list = self._poa_lists.get(cse_id[1:], []) return self._send_request_to_endpoints(onem2m_request, poa_list)
def __init__(self, poas, ep, onem2m_mapper, ca_certs=None, cert_file=None, key_file=None): """ :param list poas: :param str ep: :param openmtc_onem2m.mapper.OneM2MMapper onem2m_mapper: """ self.mapper = onem2m_mapper self.sp_id, self.cse_id, _ = split_onem2m_address( onem2m_mapper.originator) self.ssl_certs = { 'ca_certs': ca_certs, 'cert_file': cert_file, 'key_file': key_file } for poa in map(urlparse, poas): if poa.hostname == 'auto': poa = poa._replace(netloc="%s:%s" % (self._get_auto_host(ep), poa.port)) if not poa.scheme: poa = poa._replace(scheme='http') try: self.handlers.append( get_handler(poa.scheme, poa, self._handle_callback, self.ssl_certs)) self.endpoints.append(poa.geturl()) except: pass self.logger.debug('Available POAs: %s' % ', '.join(self.endpoints)) super(NotificationManager, self).__init__()
def send_onem2m_request(self, request): """ :param openmtc_onem2m.transport.OneM2MRequest request: :return Promise: """ p = Promise() client_id = self._get_client_id_from_originator(request.originator) if request.ty and request.op == OneM2MOperation.create: request.ty = ResourceTypeE[request.resource_type.typename].value else: request.ty = None request.op = 1 + OneM2MOperation._member_map_.keys().index( OneM2MOperation[request.op].name) if request.pc: request.pc = self._decode( encode_onem2m_content(request.pc, 'application/json', path=request.to)[1]) if request.fc: request.fc = encode_onem2m_content(request.fc, 'application/json', path=request.to)[1] if self._default_target_id: target_id = self._default_target_id else: _, cse_id, suffix = split_onem2m_address(request.to) if cse_id: target_id = cse_id[1:] + ('/' + suffix if request.ae_notifying else '') else: raise CSETargetNotReachable() self.logger.debug('Preparing request for transit: %s' % (request, )) promises_key = (OneM2MMQTTClient._mqtt_mask(target_id), request.rqi) def cleanup(_): self.logger.debug('Clearing request id %s ...' % (promises_key, )) del self._request_promises[promises_key] p.addCallback(cleanup) p.addErrback(cleanup) self._request_promises[promises_key] = p gevent.spawn_later(MQTT_RESPONSE_TIMEOUT, self._cancel_request, promises_key) self._publish_message( self._encode({ str(k): getattr(request, k) for k in self.__request_fields if getattr(request, k) is not None }), self._build_topic(client_id, target_id) + '/json', ) return p
def handle_request(): originator = message.topic.split('/')[3] try: request = self._decode(message.payload) except JSONDecodeError as e: self.logger.warn('Got rubbish request from client %s: %s' % ( originator, e.message, )) return try: if request['rqi'] in self._processed_request_ids: self.logger.info( 'Request %s already processed; discarding duplicate.' % (request['rqi'], )) return else: rqi = request['rqi'] except KeyError: self.logger.warn( 'Special treatment for special request w/o request id from %s.' % (originator, )) return try: request['pc'] = decode_onem2m_content( self._encode(request['pc']), 'application/json') request['ty'] = type(request['pc']) except KeyError: # No content, eh? request['ty'] = None self.logger.debug('Decoded JSON request: %s' % (request, )) op = OneM2MOperation._member_map_.values()[request['op'] - 1] to = request['to'] del request['op'], request['to'] try: response = self._handle_request_func( OneM2MRequest(op, to, **request)) try: response = response.get() except AttributeError: pass except OneM2MErrorResponse as response: self.logger.debug('OneM2MError: %s' % response) except CSEError as e: response = OneM2MErrorResponse( status_code=e.response_status_code, rqi=rqi) if not response.rqi: # This really should not happen. No, really, it shouldn't. self.logger.debug( 'FIXUP! FIXUP! FIXUP! Adding missing request identifier to response: %s' % (rqi, )) response.rqi = rqi if response.content: sp_id, cse_id, _ = split_onem2m_address(response.to) response.content = self._decode( encode_onem2m_content(response.content, 'application/json', path=sp_id + cse_id)[1]) self._publish_message( self._encode({ k: getattr(response, k) for k in self.__response_fields if getattr(response, k) is not None }), self._build_topic(originator, self._client_id, type='resp'), ) self._processed_request_ids.append(rqi)