Source code for smia.logic.inter_aas_interactions_utils
"""This class groups the methods related to the Inter AAS interactions between I4.0 SMIA entities."""importjsonimportloggingimportjsonschemafromjsonschema.exceptionsimportValidationErrorfromspade.messageimportMessagefromsmia.logic.exceptionsimportRequestDataErrorfromsmia.utilities.smia_infoimportSMIAInteractionInfofromsmia.utilities.general_utilsimportGeneralUtils_logger=logging.getLogger(__name__)
[docs]defcreate_svc_json_data_from_acl_msg(acl_msg):""" This method creates the dictionary with all the required data of a service related to an ACL message. Args: acl_msg (spade.message.Message): ACL message where to get the information Returns: dict: dictionary with all the information about the service """svc_req_data_json={'performative':acl_msg.get_metadata('performative'),'ontology':acl_msg.get_metadata('ontology'),'thread':acl_msg.thread,'sender':GeneralUtils.get_sender_from_acl_msg(acl_msg),'receiver':str(acl_msg.to),}# The body of the ACL message contains the rest of the informationsvc_req_data_json.update(json.loads(acl_msg.body))returnsvc_req_data_json
[docs]defcreate_inter_smia_response_msg(receiver,thread,performative,ontology,service_id=None,service_type=None,service_params=None):""" This method creates the Inter AAS interaction response object. Args: receiver (str): the JID of the receiver of the ACL message from which the service is requested. thread (str): the thread of the ACL message. performative (str): the performative of the ACL message. ontology (str): the ontology of the ACL message. service_id (str): the serviceID of the ACL message. service_type (str): the serviceType of the ACL message. service_params (str): the serviceParams of the "serviceData" section of the ACL message. Returns: spade.message.Message: SPADE message object FIPA-ACL-compliant. """request_msg=Message(to=receiver,thread=thread)request_msg.set_metadata('performative',performative)request_msg.set_metadata('ontology',ontology)# request_msg.set_metadata('ontology', 'SvcResponse')request_msg_body_json={'serviceID':service_id,'serviceType':service_type,'serviceData':{'serviceCategory':'service-response','timestamp':GeneralUtils.get_current_timestamp(),}}ifservice_paramsisnotNone:request_msg_body_json['serviceData']['serviceParams']=service_paramsrequest_msg.body=json.dumps(request_msg_body_json)returnrequest_msg
[docs]asyncdefcheck_received_request_data_structure(received_data,json_schema):""" This method checks if the received data for a request is valid. The JSON object with the specific data is also validated against the given associated JSON Schema. In any case, if it is invalid, it raises a RequestDataError exception. Args: received_data (dict): received data in form of a JSON object. json_schema (dict): JSON Schema in form of a JSON object. """# TODO modificarlo cuando se piense la estructura del lenguaje I4.0if'serviceData'notinreceived_data:raiseRequestDataError("The received request is invalid due to missing #serviceData field in the""request message.")if'serviceParams'notinreceived_data['serviceData']:raiseRequestDataError("The received request is invalid due to missing #serviceParams field within ""the #serviceData section of the request message.")# The received JSON object is also validated against the associated JSON Schematry:jsonschema.validate(instance=received_data['serviceData']['serviceParams'],schema=json_schema)exceptValidationErrorase:raiseRequestDataError("The received JSON data within the request message is invalid against the required ""JSON schema. Invalid part: {}. Reason: {}.".format(e.instance,e.message))