Ejemplo n.º 1
0
    def handle_lwm2m_put(self, msg, remote, rx_record):
        uri_query = msg.findOption(options.UriQuery)

        method = None
        try:
            method = uri_query[0].value.split("=")[1]
        except:
            pass

        if method == "write":
            self.logger.info("Updating the Resources in the Client")

            path = msg.findOption(URI_PATH_VALUE)
            content_type_number = msg.findOption(options.ContentType)
            if content_type_number is None:
                content_type = "text/plain"
            else:
                content_type = constants.media_types[content_type_number.value]
            self.write.write_resource(msg.payload, path, content_type)

            payload_forward = msg.payload

            msg = connection.Message(connection.Message.ACK,
                                     code=constants.CHANGED,
                                     payload="CHANGED")
            self.local_server.sendto(msg._pack(rx_record.transaction_id),
                                     remote)

            endpoint_name, object_id, object_inst_id, res_id, res_inst_id, _, _ = OperationRequest(
            ).find_elements(path, remote)

            self.dm_adapter.update_resources(endpoint_name, object_id, object_inst_id, \
                                    payload_forward, content_type=content_type)

        elif method == "write_attributes":
            path = msg.findOption(URI_PATH_VALUE)
            content_type_number = msg.findOption(options.ContentType)
            if content_type_number is None:
                content_type = "text/plain"
            else:
                content_type = constants.media_types[content_type_number.value]
            payload = loads(msg.payload)
            self.write_attributes.set_attributes(path, remote, payload)

            msg = connection.Message(connection.Message.ACK,
                                     code=constants.CHANGED,
                                     payload="Resource Attributes Changed")
            self.local_server.sendto(msg._pack(rx_record.transaction_id),
                                     remote)
Ejemplo n.º 2
0
    def handle_lwm2m_post(self, msg, uri_query, remote, rx_record):
        method = None
        try:
            method = uri_query[0].value.split("=")[1]
        except:
            pass

        if method == "create":
            path = msg.findOption(URI_PATH_VALUE)
            content_type_number = msg.findOption(options.ContentType)
            if content_type_number is None:
                content_type = "text/plain"
            else:
                content_type = constants.media_types[content_type_number.value]
            self.create_object_instance.create_instance(
                path, remote, content_type, loads(msg.payload))

            msg = connection.Message(connection.Message.ACK,
                                     code=constants.CREATED,
                                     payload="Resource Created")
            self.local_server.sendto(msg._pack(rx_record.transaction_id),
                                     remote)

        elif method == "execute":
            path = msg.findOption(URI_PATH_VALUE)
            content_type_number = msg.findOption(options.ContentType)
            if content_type_number is None:
                content_type = "text/plain"
            else:
                content_type = constants.media_types[content_type_number.value]

            endpoint_name, object_id, object_inst_id, res_id, res_value = \
                                self.execution.execute_resource(path, remote, msg.payload)

            msg = connection.Message(connection.Message.ACK,
                                     code=constants.CHANGED,
                                     payload="Resource Executed")
            self.local_server.sendto(msg._pack(rx_record.transaction_id),
                                     remote)

            resource = {}
            resource[res_id] = {"res_value": res_value}
            content_type = "application/json"
            self.dm_adapter.update_resources(endpoint_name,
                                             object_id,
                                             object_inst_id,
                                             dumps(resource),
                                             content_type=content_type)
Ejemplo n.º 3
0
    def handle_lwm2m_cancel_observe(self, msg, remote, rx_record):
        self.logger.info("Cancel Observation Request Received")
        path = msg.findOption(URI_PATH_VALUE)

        endpoint_name, object_id, object_inst_id, res_id, res_inst_id, _, _ = OperationRequest(
        ).find_elements(path, remote)

        token_id = msg.token
        payload = msg.payload

        message = self.observation.cancel_observation(
            endpoint_name,
            object_id,
            object_inst_id,
            res_id,
            token_id,
            payload,
            self.lwm2m_dm_server_ip,
            self.lwm2m_dm_server_port,
            res_inst_id=res_inst_id)

        msg = connection.Message(connection.Message.ACK,
                                 code=constants.CONTENT,
                                 payload=message)
        self.local_server.sendto(msg._pack(rx_record.transaction_id), remote)
Ejemplo n.º 4
0
    def handle_lwm2m_observe(self, msg, remote, rx_record):
        path = msg.findOption(URI_PATH_VALUE)
        if len(path) == 1:
            token_id = self.set_generation_observation_params(msg)
            payload = "General Observation Started at the Client"
            content_type = "text/plain"
        else:
            self.logger.info("Specific Observation Received")

            endpoint_name, object_id, object_inst_id, res_id, res_inst_id, _, _ = OperationRequest(
            ).find_elements(path, remote)

            token_id = msg.token
            payload = msg.payload

            self.observation.set_observation(endpoint_name,
                                             object_id,
                                             object_inst_id,
                                             res_id,
                                             token_id,
                                             payload,
                                             self.lwm2m_dm_server_ip,
                                             self.lwm2m_dm_server_port,
                                             res_inst_id=res_inst_id)

        msg = connection.Message(connection.Message.ACK,
                                 code=constants.CONTENT)
        self.local_server.sendto(msg._pack(rx_record.transaction_id, token_id),
                                 remote)
Ejemplo n.º 5
0
    def process(self, rx_record, remote, uri_query):
        """ Processes various requests like CON (POST, PUT, GET) or NON.
        POST requests : Generally used for Registration and Execution
        PUT requests : Generally used for updating the resources
        GET requests : Generally used for Discovery, Observation, Cancel Observation """

        msg = rx_record.message
        self.uri_query = uri_query

        if msg.transaction_type == connection.Message.CON:
            if constants.POST == msg.code:
                """ Used for Registration requests, Execution Operation Request """
                self.handle_lwm2m_post(msg, uri_query, remote, rx_record)

            elif constants.PUT == msg.code:
                """ It consists of Normal Update, Write Operation, Write Attribute Operation.
                Write Operation is used to update the resource(s) as per the request. Write
                Attributes operation is used to update the attributes of the object, object
                instance or resource. """

                self.handle_lwm2m_put(msg, remote, rx_record)

            elif constants.GET == msg.code:
                """ Handles Requests like Discovery, Observation """
                try:
                    observe_value = msg.findOption(options.Observe).value
                except:
                    observe_value = ""
                if observe_value == OBSERVE_OPTION_VALUE_OBSERVATION:
                    """ Sets the Observation. Two types of observations. General Observation
                    and Specific Observation. General Observation is used for anything that is
                    not observed and updates are sent as general notifications using a general
                    token. Specific observation is implicitly defined by the observer(as request) 
                    and handled as specific notification with a specific token """
                    self.handle_lwm2m_observe(msg, remote, rx_record)

                elif observe_value == OBSERVE_OPTION_VALUE_CANCEL_OBSERVATION:
                    """ Removes the observation from the List """

                    self.handle_lwm2m_cancel_observe(msg, remote, rx_record)

                else:
                    uri_query = msg.findOption(options.UriQuery)

                    method = None
                    try:
                        method = uri_query[0].value.split("=")[1]
                    except:
                        pass

                    if method == "discover":
                        path = msg.findOption(URI_PATH_VALUE)
                        payload = self.discover.get_resource(path, remote)

                        msg = connection.Message(connection.Message.ACK,
                                                 code=constants.CONTENT,
                                                 payload=dumps(payload))
                        self.local_server.sendto(
                            msg._pack(rx_record.transaction_id), remote)
    def process(self, rx_record, remote, uri_query):
        if rx_record.message.transaction_type == connection.Message.CON:
            if constants.POST == rx_record.message.code:
                if self.general_notification_token == rx_record.message.token:
                    self.logger.info("General Notification received")
                    msg = connection.Message(connection.Message.ACK,
                                             code=constants.CREATED)
                    self.local_server.sendto(
                        msg._pack(rx_record.transaction_id), remote)

                    self.process_resources(
                        json.loads(rx_record.message.payload))
                else:
                    self.logger.info("Specific Notification received")
                    msg = connection.Message(connection.Message.ACK,
                                             code=constants.CREATED)
                    self.local_server.sendto(
                        msg._pack(rx_record.transaction_id), remote)

                    payload = json.loads(rx_record.message.payload)
                    observer_ip = payload["observer_ip"]
                    observer_port = payload["observer_port"]
                    del payload["observer_ip"]
                    del payload["observer_port"]
                    self.process_resources(payload,
                                           observer_ip=observer_ip,
                                           observer_port=observer_port)

        elif rx_record.message.transaction_type == connection.Message.NON:
            if self.general_notification_token == rx_record.message.token:
                self.logger.info("General Notification received")
                self.process_resources(json.loads(rx_record.message.payload))
            else:
                self.logger.info("Specific Notification received")
                payload = json.loads(rx_record.message.payload)
                observer_ip = payload["observer_ip"]
                observer_port = payload["observer_port"]
                del payload["observer_ip"]
                del payload["observer_port"]
                self.process_resources(payload,
                                       observer_ip=observer_ip,
                                       observer_port=observer_port)
    def set_m2m_server_adapter_params(self, rx_record, remote):
        msg = rx_record.message
        #content_type is application/json
        listener_ip = loads(msg.payload)["listener_ip"]
        listener_port = loads(msg.payload)["listener_port"]
        token_id = msg.token

        self.general_observation = GeneralObservationInformation(
            listener_ip, listener_port, token_id)

        response = "Observation Started on the LWM2M Server"
        msg = connection.Message(connection.Message.ACK,
                                 code=constants.CONTENT,
                                 payload=response)
        self.dm_server.sendto(msg._pack(rx_record.transaction_id, token_id),
                              remote)
    def handle_lwm2m_put(self, msg, uri_query, remote, rx_record):
        """ It consists of Normal Update, Write Operation, Write Attribute Operation.
        Write Operation is used to update the resource(s) as per the request. Write
        Attributes operation is used to update the attributes of the object, object
        instance or resource. """

        method = None
        try:
            method = uri_query[0].value.split("=")[1]
        except:
            pass

        if method == "write":
            path = msg.findOption(URI_PATH_VALUE)
            content_type_number = msg.findOption(options.ContentType)
            if content_type_number is None:
                content_type = "text/plain"
            else:
                content_type = constants.media_types[content_type_number.value]
            self.write.write_resource(msg.payload, path, content_type)

            payload_forward = msg.payload

            msg = connection.Message(connection.Message.ACK,
                                     code=constants.CHANGED,
                                     payload="Resource Updated")
            self.dm_server.sendto(msg._pack(rx_record.transaction_id), remote)

            client_port = self.generate_client_port()
            self.write.forward_write_request(path, payload_forward, \
                                        content_type, remote, client_port)

        elif method == "write_attributes":
            path = msg.findOption(URI_PATH_VALUE)
            content_type_number = msg.findOption(options.ContentType)
            if content_type_number is None:
                content_type = "text/plain"
            else:
                content_type = constants.media_types[content_type_number.value]
            payload = loads(msg.payload)
            self.write_attributes.set_attributes(path, remote, payload)
            msg = connection.Message(connection.Message.ACK,
                                     code=constants.CHANGED,
                                     payload="Resource Attributes Updated")
            self.dm_server.sendto(msg._pack(rx_record.transaction_id), remote)

            client_port = self.generate_client_port()
            self.write_attributes.forward_request(path, remote, payload,
                                                  content_type, client_port)

        else:

            endpoint_location = msg.findOption(URI_PATH_VALUE)[0].value
            if msg.payload == "":
                self.logger.info("Updating the Registration Params")
                endpoint_object = self.lwm2m_resources.return_endpoint_object(
                    endpoint_location=endpoint_location)
                endpoint_object.listener_ip = uri_query[6].value.split("=")[1]
                endpoint_object.local_ip = uri_query[6].value.split("=")[1]

                msg = connection.Message(connection.Message.ACK,
                                         code=constants.CHANGED,
                                         payload="Resource Updated")
                self.dm_server.sendto(msg._pack(rx_record.transaction_id),
                                      remote)
            else:
                self.logger.info("Adding/Updating the Resources")
                payload = self.update_resource(
                    loads(msg.payload), endpoint_location=endpoint_location)

                msg = connection.Message(connection.Message.ACK,
                                         code=constants.CHANGED,
                                         payload="Resource Updated")
                self.dm_server.sendto(msg._pack(rx_record.transaction_id),
                                      remote)

                self.logger.info("Forwarding the Notification")
                request = lwm2m_api()
                client_port = self.generate_client_port()

                response = request.send_notification(self.general_observation.listener_ip, self.general_observation.listener_port, \
                                                     self.general_observation.token_id, payload, content_type="application/json", client_port=client_port)
 def _handle_response(payload):
     msg = connection.Message(connection.Message.ACK, code=constants.CONTENT, \
                          payload=payload, content_type="application/json")
     self.dm_server.sendto(
         msg._pack(rx_record.transaction_id), remote)
 def _handle_response(response):
     msg = connection.Message(connection.Message.ACK,
                              code=constants.CONTENT,
                              payload=response)
     self.dm_server.sendto(
         msg._pack(rx_record.transaction_id), remote)
    def process(self, rx_record, remote, uri_query):
        """ Processes various requests like CON (POST, PUT, GET) or NON.
        POST requests : Generally used for Registration and Execution
        PUT requests : Generally used for Updating the resources
        GET requests : Generally used for Discovery, Observation, Cancel Observation """

        msg = rx_record.message
        self.uri_query = uri_query
        if msg.transaction_type == connection.Message.CON:
            if constants.POST == msg.code:
                method = None
                try:
                    method = uri_query[0].value.split("=")[1]
                except:
                    pass
                if method == "create":
                    path = msg.findOption(URI_PATH_VALUE)
                    content_type_number = msg.findOption(options.ContentType)
                    if content_type_number is None:
                        content_type = "text/plain"
                    else:
                        content_type = constants.media_types[
                            content_type_number.value]
                    self.create_object_instance.create_instance(
                        path, remote, content_type, loads(msg.payload))
                    resources = loads(msg.payload)
                    msg = connection.Message(connection.Message.ACK,
                                             code=constants.CREATED,
                                             payload="Resource Created")
                    self.dm_server.sendto(msg._pack(rx_record.transaction_id),
                                          remote)

                    client_port = self.generate_client_port()
                    self.create_object_instance.forward_request(
                        path, remote, resources, content_type, client_port)

                elif method == "execute":
                    path = msg.findOption(URI_PATH_VALUE)
                    content_type_number = msg.findOption(options.ContentType)
                    if content_type_number is None:
                        content_type = "text/plain"
                    else:
                        content_type = constants.media_types[
                            content_type_number.value]
                    self.execution.execute_resource(path, remote, msg.payload)
                    execute_payload = msg.payload
                    msg = connection.Message(connection.Message.ACK,
                                             code=constants.CHANGED,
                                             payload="Resource Executed")
                    self.dm_server.sendto(msg._pack(rx_record.transaction_id),
                                          remote)

                    client_port = self.generate_client_port()
                    self.execution.forward_request(path, remote,
                                                   execute_payload,
                                                   client_port)

                elif method == "notify":
                    self.logger.info("Notification Received")
                    client_port = self.generate_client_port()
                    for item1, item2 in loads(msg.payload).iteritems():
                        if item1 == "observer_ip":
                            observer_ip = item2
                        elif item1 == "observer_port":
                            observer_port = item2
                        elif item1 != "observer_ip" and item1 != "observer_port":
                            endpoint_name = item1
                            for item3, item4 in item2.iteritems():
                                for item5, item6 in item4[
                                        "resources"].iteritems():
                                    pass

                    res = {
                        item3: {
                            "resources": {
                                item5.split("_")[0]: {
                                    "res_value": item6,
                                    "res_inst_id": item5.split("_")[1]
                                }
                            }
                        }
                    }
                    payload = {}
                    payload = self.update_resource(res,
                                                   endpoint_name=endpoint_name)

                    payload["observer_ip"] = observer_ip
                    payload["observer_port"] = observer_port

                    token_id = msg.token
                    observe_value = msg.findOption(options.Observe).value
                    self.logger.info("Forwarding Notification")

                    content_type = "application/json"
                    request = lwm2m_api()
                    response = request.send_notification(self.general_observation.listener_ip, self.general_observation.listener_port, token_id, payload, \
                                content_type=content_type, time_elapse=observe_value, client_port=client_port)

                    msg = connection.Message(connection.Message.ACK,
                                             code=constants.CREATED,
                                             payload="Notification Received")
                    self.dm_server.sendto(msg._pack(rx_record.transaction_id),
                                          remote)

                else:
                    """ Handles the Client Registration Request """

                    self.logger.info(
                        "Registering Client Endpoint in the LWM2M DM Server")

                    endpoint = self.registration.process_registration(
                        msg, uri_query)

                    response = self.registration.register_client(endpoint)
                    registered_client_location = response
                    if registered_client_location is not None:
                        self.logger.info(
                            "Client Endpoint Registration Successful for Endpoint : %s",
                            endpoint.endpoint_name)
                        self.logger.info("The registered location is %s",
                                         registered_client_location)
                        payload = self.set_general_observation_params()
                    else:
                        self.logger.info("Client Endpoint Registration Failed")

                    msg = connection.Message(
                        connection.Message.ACK,
                        code=constants.CREATED,
                        location=registered_client_location)
                    self.dm_server.sendto(msg._pack(rx_record.transaction_id),
                                          remote)

                    #Send the General Observation to the Registered Client
                    #self.send_general_observation(registered_client_location)

            elif constants.PUT == msg.code:
                """ It consists of Normal Update, Write Operation, Write Attribute Operation.
                Write Operation is used to update the resource(s) as per the request. Write
                Attributes operation is used to update the attributes of the object, object
                instance or resource. """
                self.handle_lwm2m_put(msg, uri_query, remote, rx_record)

            elif constants.GET == msg.code:
                """ Handles Requests like Discovery, Observation """
                try:
                    observe_value = msg.findOption(options.Observe).value
                except:
                    observe_value = ""

                if observe_value == OBSERVE_OPTION_VALUE_OBSERVATION:
                    """ Sets the Observation. Two types of observations. General Observation
                    and Specific Observation. General Observation is used for anything that is
                    not observed and updates are sent as general notifications using a general
                    token. Specific observation is implicitly defined by the observer(as request) 
                    and handled as specific notification with a specific token """

                    path = msg.findOption(URI_PATH_VALUE)
                    if len(path) == 1:
                        self.set_m2m_server_adapter_params(rx_record, remote)
                    else:
                        self.logger.info(
                            "Specific Observation Request Received")

                        content_type_number = msg.findOption(
                            options.ContentType)
                        if content_type_number is None:
                            content_type = "text/plain"
                        else:
                            content_type = constants.media_types[
                                content_type_number.value]

                        token_id = msg.token
                        app_ip = loads(msg.payload)["app_ip"]
                        app_port = loads(msg.payload)["app_port"]
                        client_port = self.generate_client_port()

                        response = self.observation.forward_request(path, remote, observe_value, \
                                            self.lwm2m_dm_server_ip, self.lwm2m_dm_server_port, \
                                            app_ip, app_port, token_id, client_port)

                        msg = connection.Message(connection.Message.ACK, code=constants.CONTENT, \
                                                 payload="test") #todo: payload to be replaced
                        self.dm_server.sendto(
                            msg._pack(rx_record.transaction_id, token_id),
                            remote)

                elif observe_value == OBSERVE_OPTION_VALUE_CANCEL_OBSERVATION:
                    """ Removes the observation from the List """
                    self.logger.info("Cancel Observation Request Received")
                    path = msg.findOption(URI_PATH_VALUE)
                    token_id = msg.token
                    app_ip = loads(msg.payload)["app_ip"]
                    app_port = loads(msg.payload)["app_port"]
                    client_port = self.generate_client_port()

                    response = self.observation.forward_request(path, remote, observe_value, \
                                self.lwm2m_dm_server_ip, self.lwm2m_dm_server_port, \
                                app_ip, app_port, token_id, client_port)

                    def _handle_response(response):
                        msg = connection.Message(connection.Message.ACK,
                                                 code=constants.CONTENT,
                                                 payload=response)
                        self.dm_server.sendto(
                            msg._pack(rx_record.transaction_id), remote)

                    response.then(_handle_response)

                else:
                    method = None
                    try:
                        method = uri_query[0].value.split("=")[1]
                    except:
                        pass

                    if method == "read":
                        path = msg.findOption(URI_PATH_VALUE)
                        self.read.read_resource(path, remote)
                        msg = connection.Message(connection.Message.ACK, code=constants.CONTENT, \
                                             payload="info read", content_type="text/plain")
                        self.dm_server.sendto(
                            msg._pack(rx_record.transaction_id), remote)

                    elif method == "discover":
                        if msg.payload == "/.well-known/core":
                            payload = dumps(self.discover.get_all_resources())
                        else:
                            path = msg.findOption(URI_PATH_VALUE)
                            client_port = self.generate_client_port()
                            payload = self.discover.forward_request(
                                path, remote, client_port)

                        def _handle_response(payload):
                            msg = connection.Message(connection.Message.ACK, code=constants.CONTENT, \
                                                 payload=payload, content_type="application/json")
                            self.dm_server.sendto(
                                msg._pack(rx_record.transaction_id), remote)

                        if payload is Promise:
                            payload.then(_handle_response)
                        else:
                            _handle_response(payload)

        elif msg.transaction_type == connection.Message.NON:
            print "reached msg non"
            payload = msg.payload
            print payload