def _handle_subscription_deleted(self, subscription, req): # only when subscription is deleted directly if not req.cascading: try: del self.subscriptions_info[subscription.resourceID] except KeyError: pass # 7.5.1.2.4 Notification for Subscription Deletion # Originator: # When the <subscription> resource is deleted and subscriberURI of the # <subscription> resource is configured, the Originator shall send a # Notify request primitive with subscriptionDeletion element of the # notification data object set as TRUE and subscriptionReference element # set as the URI of the <subscription> resource to the entity indicated # in subscriberURI. su = subscription.subscriberURI if not su: return try: self.api.handle_onem2m_request(OneM2MRequest( OneM2MOperation.notify, su, pc=Notification( subscriptionDeletion=True, subscriptionReference=self._get_subscription_reference(su, subscription.path), # TODO(rst): check if this is the sub creator or the creator of the notification # TODO in this case the CSE creator=subscription.creator, ), )) except CSENotFound: self.logger.debug("subscription target %s already deleted or not existing." % su)
def create(self, path, instance): for a in [a.name for a in type(instance).attributes if a.accesstype == a.RO]: try: setattr(instance, a, None) except AttributeError: pass # TODO(rst): add resource_type response = self._send_request(OneM2MRequest( OneM2MOperation.create, path, self.originator, ty=type(instance), pc=instance )).get() if response.content: for k, v in response.content.values.items(): try: setattr(instance, k, v) except AttributeError: pass instance.path = '/'.join([path, response.content.resourceName]) else: instance.path = path self.logger.debug("Set instance path: %s" % (instance.path, )) instance._synced = False return instance
def _do_update(self, instance, fields=None): if fields: for a in [a.name for a in type(instance).attributes if a.name not in fields]: try: setattr(instance, a, None) except AttributeError: pass instance.childResource = [] # remove NP attributes for a in [a.name for a in type(instance).attributes if a.accesstype in (a.WO, a.RO)]: try: setattr(instance, a, None) except AttributeError: pass response = self._send_request(OneM2MRequest( OneM2MOperation.update, instance.path, self.originator, pc=instance )).get() try: response.content.path = instance.path except AttributeError: pass return response.content
def _do_update(self, instance, fields=None): try: fields = list(fields or []) except TypeError: fields = [] attributes = type(instance).attributes values_to_clear = {a.name: None if a.type is not list else [] for a in attributes if a.accesstype in (a.WO, a.RO) or a.name not in fields and len(fields)} # remove NP attributes instance.set_values(values_to_clear) response = self._send_request(OneM2MRequest( OneM2MOperation.update, instance.path, self.originator, pc=instance )).get() try: response.content.path = instance.path except AttributeError: pass return response.content
def _send_notification(self, resource, sub): self.logger.debug("sending notification for resource: %s", resource) def get_subscription_reference(to, path): if to.startswith('//'): return self._abs_cse_id + '/' + path elif to.startswith('/'): return self._rel_cse_id + '/' + path else: return path for uri in sub.notificationURI: self.api.handle_onem2m_request( OneM2MRequest( op=OneM2MOperation.notify, to=uri, pc=Notification( notificationEvent=NotificationEventC( representation=resource), subscriptionReference=get_subscription_reference( uri, sub.path), # TODO(rst): check if this is the sub creator or the creator of the notification # TODO in this case the CSE creator=sub.creator), ))
def create(self, path, instance): instance.__dict__.update({ attribute.name: None for attribute in type(instance).attributes if attribute.accesstype == attribute.RO }) # TODO(rst): add resource_type response = self._send_request( OneM2MRequest(OneM2MOperation.create, path, self.originator, ty=type(instance), pc=instance, rvi='2a')).get() try: instance.__dict__.update(response.content.values) instance.path = path + '/' + response.content.resourceName except (AttributeError, ): instance.path = path self.logger.debug("Set instance path: %s" % (instance.path, )) instance._synced = False return instance
def _do_update(self, instance, fields=None): attributes = type(instance).attributes fields_to_be_cleared = [ a.name for a in attributes if a.accesstype in (a.WO, a.RO) ] if fields: fields_to_be_cleared.extend( [a.name for a in attributes if a.name not in fields]) instance.childResource = [] # remove NP attributes instance.__dict__.update({a: None for a in fields_to_be_cleared}) response = self._send_request( OneM2MRequest(OneM2MOperation.update, instance.path, self.originator, pc=instance)).get() try: response.content.path = instance.path except AttributeError: pass return response.content
def _get_data(self, path, fc=None, **request_options): return self._send_request(OneM2MRequest( OneM2MOperation.retrieve, path, self.originator, filter_criteria=fc, **request_options )).get()
def delete(self, instance): """ DUNNO, somethings gets deleted :param instance: instance/object or a string representing its path """ self._send_request(OneM2MRequest( OneM2MOperation.delete, getattr(instance, "path", instance), self.originator ))
def retrieve_request(self): app = AE(appName="appName") onem2m_request = OneM2MRequest("update", to="onem2m/ipe_ae", pc=app) promise = self.client.send_onem2m_request(onem2m_request) content_request = self.discover() for resource in content_request: onem2m_request = OneM2MRequest("retrieve", to=resource) promise = self.client.send_onem2m_request(onem2m_request) onem2m_response = promise.get() response = onem2m_response.content res_builded = self.resource_retrieved_builder(response) self.resourceDiscovered.append(res_builded) self.uri_resource_dict[resource] = res_builded # remove None values in list self.resourceDiscovered = [i for i in self.resourceDiscovered if i] self.update_label_request() #self.start_subscription() self.sub_state = True
def _send_notification(self, resource, sub): self.logger.debug("sending notification for resource: %s", resource) for uri in sub.notificationURI: self.api.handle_onem2m_request(OneM2MRequest( op=OneM2MOperation.notify, to=uri, pc=Notification( notificationEvent=NotificationEventC( representation=resource ), subscriptionReference=self._get_subscription_reference(uri, sub.path), # TODO(rst): check if this is the sub creator or the creator of the notification # TODO in this case the CSE creator=sub.creator ), ))
def _send_notification(self, resource, sub, req): self.logger.debug("sending notification for resource: %s", resource) notification = Notification( notificationEvent=NotificationEventC( representation=resource ), subscriptionReference=sub.path, creator=sub.creator ) for uri in sub.notificationURI: send_notification_request = OneM2MRequest(OneM2MOperation.notify, uri, req.originator, pc=notification) self.api.send_onem2m_request(send_notification_request)
def _get_data(self, path): return self._send_request(OneM2MRequest( OneM2MOperation.retrieve, path, self.originator )).get()
def update_label_request(self): labels_ = [{"Exposed-Resource-IDs": self.exposed_ids}] app = AE(labels=labels_) onem2m_request = OneM2MRequest("update", to="onem2m/ipe_ae", pc=app) promise = self.client.send_onem2m_request(onem2m_request)
def delete(self, instance): self._send_request(OneM2MRequest( OneM2MOperation.delete, getattr(instance, "path", instance), self.originator ))