diff --git a/examples/dell5k_edge_system_iotcc_websocket.py b/examples/dell5k_edge_system_iotcc_websocket.py index fb476441..40591824 100644 --- a/examples/dell5k_edge_system_iotcc_websocket.py +++ b/examples/dell5k_edge_system_iotcc_websocket.py @@ -100,7 +100,6 @@ def read_mem_free(): if __name__ == '__main__': - # create a data center object, IoTCC in this case, using websocket as a transport layer # this object encapsulates the formats and protocols necessary for the agent to interact with the dcc # UID/PASS login for now. diff --git a/examples/dell5k_edge_system_with_filter_iotcc.py b/examples/dell5k_edge_system_with_filter_iotcc.py index d429ca61..ebf9677f 100644 --- a/examples/dell5k_edge_system_with_filter_iotcc.py +++ b/examples/dell5k_edge_system_with_filter_iotcc.py @@ -107,10 +107,16 @@ def read_mem_free(): if __name__ == '__main__': # create a data center object, IoTCC in this case, using websocket as a transport layer - # this object encapsulates the formats and protocols neccessary for the agent to interact with the dcc + # this object encapsulates the formats and protocols necessary for the agent to interact with the dcc # UID/PASS login for now. - iotcc = IotControlCenter(config['IotCCUID'], config['IotCCPassword'], - WebSocketDccComms(url=config['WebSocketUrl'])) + identity = Identity(root_ca_cert=config['WebsocketCaCertFile'], username=config['IotCCUID'], + password=config['IotCCPassword'], + cert_file=config['ClientCertFile'], key_file=config['ClientKeyFile']) + + # Initialize DCC object with transport + iotcc = IotControlCenter( + WebSocketDccComms(url=config['WebSocketUrl'], verify_cert=config['VerifyServerCert'], identity=identity) + ) try: # create a System object encapsulating the particulars of a IoT System diff --git a/examples/mqtt/dcc_comms/iotcc/dell5k_edge_system_iotcc_mqtt.py b/examples/mqtt/dcc_comms/iotcc/dell5k_edge_system_iotcc_mqtt.py index ece48088..1fc9c30e 100644 --- a/examples/mqtt/dcc_comms/iotcc/dell5k_edge_system_iotcc_mqtt.py +++ b/examples/mqtt/dcc_comms/iotcc/dell5k_edge_system_iotcc_mqtt.py @@ -111,8 +111,7 @@ def read_mem_free(): # Encapsulate TLS parameters tls_conf = TLSConf(config['cert_required'], config['tls_version'], config['cipher']) - iotcc = IotControlCenter(config['broker_username'], config['broker_password'], - MqttDccComms(edge_system_name=edge_system.name, + iotcc = IotControlCenter(MqttDccComms(edge_system_name=edge_system.name, url=config['BrokerIP'], port=config['BrokerPort'], identity=identity, tls_conf=tls_conf, enable_authentication=True)) diff --git a/liota/dccs/iotcc.py b/liota/dccs/iotcc.py index 95cc9b48..bb0d0231 100755 --- a/liota/dccs/iotcc.py +++ b/liota/dccs/iotcc.py @@ -77,24 +77,26 @@ def __init__(self, con): # Liota internal entity file system path special for iotcc self.entity_file_path = self._get_file_storage_path("entity_file_path") self.file_ops_lock = Lock() - - def on_response(msg): - try: + try: + def on_response(msg): log.debug("Received msg: {0}".format(msg)) json_msg = json.loads(msg) self.proto.on_receive(json_msg) - if json_msg["type"] == "connection_response" and json_msg["body"]["result"] == "succeeded": - log.info("Connection verified") - return True + if json_msg["type"] == "connection_response": + if json_msg["body"]["result"] == "succeeded": + log.info("Connection verified") + else: + raise Exception("Helix Protocol Version mismatch") else: log.debug("Processed msg: {0}".format(json_msg["type"])) - on_response(self.recv_msg_queue.get(True,300)) - except Exception as error: - log.error("HelixProtocolException: " + repr(error)) + on_response(self.recv_msg_queue.get(True, 300)) - # Block on Queue for not more then 300 seconds else it will raise an exception - on_response(self.recv_msg_queue.get(True,300)) - log.info("Logged in to DCC successfully") + # Block on Queue for not more then 300 seconds else it will raise an exception + on_response(self.recv_msg_queue.get(True, 300)) + except Exception as error: + self.comms.client.disconnect() + log.error("HelixProtocolException: " + repr(error)) + raise Exception("HelixProtocolException") def register(self, entity_obj): """ Register the objects @@ -119,7 +121,7 @@ def on_response(msg): self.reg_entity_id = json_msg["body"]["uuid"] else: log.info("Waiting for resource creation") - on_response(self.recv_msg_queue.get(True,300)) + on_response(self.recv_msg_queue.get(True, 300)) except: raise Exception("Exception while registering resource") @@ -127,19 +129,21 @@ def on_response(msg): entity_obj.entity_type = "HelixGateway" self.comms.send(json.dumps( self._registration(self.next_id(), entity_obj.entity_id, entity_obj.name, entity_obj.entity_type))) - on_response(self.recv_msg_queue.get(True,300)) + on_response(self.recv_msg_queue.get(True, 300)) if not self.reg_entity_id: raise RegistrationFailure() log.info("Resource Registered {0}".format(entity_obj.name)) if entity_obj.entity_type == "HelixGateway": - self.store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id, entity_obj.entity_id) + self.store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id, + entity_obj.entity_id) store_edge_system_uuid(entity_name=entity_obj.name, entity_id=entity_obj.entity_id, reg_entity_id=self.reg_entity_id) with self.file_ops_lock: self.store_reg_entity_attributes("EdgeSystem", entity_obj.name, self.reg_entity_id, None, None) else: - self.store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id, entity_obj.entity_id) + self.store_reg_entity_details(entity_obj.entity_type, entity_obj.name, self.reg_entity_id, + entity_obj.entity_id) # get dev_type, and prop_dict if possible with self.file_ops_lock: self.store_reg_entity_attributes("Devices", entity_obj.name, self.reg_entity_id, @@ -158,17 +162,18 @@ def on_response(msg): json_msg = json.loads(msg) log.debug("Processed msg: {0}".format(json_msg["type"])) if json_msg["type"] == "remove_resource_response" and json_msg["body"]["result"] == "succeeded": - log.info("Unregistration of resource {0} with IoTCC succeeded".format(entity_obj.ref_entity.name)) + log.info("Unregistration of resource {0} with IoTCC succeeded".format(entity_obj.ref_entity.name)) else: - log.info("Unregistration of resource {0} with IoTCC failed".format(entity_obj.ref_entity.name)) + log.info("Unregistration of resource {0} with IoTCC failed".format(entity_obj.ref_entity.name)) except: raise Exception("Exception while unregistering resource") self.comms.send(json.dumps(self._unregistration(self.next_id(), entity_obj.ref_entity))) - on_response(self.recv_msg_queue.get(True,20)) + on_response(self.recv_msg_queue.get(True, 20)) self.remove_reg_entity_details(entity_obj.ref_entity.name, entity_obj.reg_entity_id) if entity_obj.ref_entity.entity_type != "HelixGateway": - self.store_device_info(entity_obj.reg_entity_id, entity_obj.ref_entity.name, entity_obj.ref_entity.entity_type, None, True) + self.store_device_info(entity_obj.reg_entity_id, entity_obj.ref_entity.name, + entity_obj.ref_entity.entity_type, None, True) else: self.store_device_info(entity_obj.reg_entity_id, entity_obj.ref_entity.name, None, None, True) @@ -262,7 +267,8 @@ def _format_data(self, reg_metric): }] }) - def set_organization_group_properties(self, reg_entity_name, reg_entity_id, reg_entity_type, entity_local_uuid, properties): + def set_organization_group_properties(self, reg_entity_name, reg_entity_id, reg_entity_type, entity_local_uuid, + properties): log.info("Organization Group Properties defined for resource {0}".format(reg_entity_name)) self.comms.send(json.dumps( self._properties(self.next_id(), reg_entity_type, entity_local_uuid, reg_entity_name, @@ -351,7 +357,8 @@ def store_reg_entity_details(self, entity_type, entity_name, reg_entity_id, enti break if not entity_exist: msg["iotcc"]["Devices"].append( - {"DeviceName": entity_name, "uuid": reg_entity_id, "EntityType": entity_type, "LocalUuid": entity_local_uuid}) + {"DeviceName": entity_name, "uuid": reg_entity_id, "EntityType": entity_type, + "LocalUuid": entity_local_uuid}) if msg != '': with open(self._iotcc_json, 'w') as f: json.dump(msg, f, sort_keys=True, indent=4, ensure_ascii=False) @@ -368,13 +375,14 @@ def remove_reg_entity_details(self, entity_name, reg_entity_id): except IOError, err: log.error('Could not open {0} file '.format(self._iotcc_json) + str(err)) log.debug('Remove {0}:{1} from iotcc.json'.format(entity_name, reg_entity_id)) - if msg["iotcc"]["EdgeSystem"]["SystemName"]==entity_name and msg["iotcc"]["EdgeSystem"]["uuid"]==reg_entity_id: + if msg["iotcc"]["EdgeSystem"]["SystemName"] == entity_name and msg["iotcc"]["EdgeSystem"][ + "uuid"] == reg_entity_id: del msg["iotcc"]["EdgeSystem"] log.info("Removed {0} edge-system from iotcc.json".format(entity_name)) else: entity_exist = False for device in msg["iotcc"]["Devices"]: - if device["uuid"] == reg_entity_id and device["uuid"] == reg_entity_id : + if device["uuid"] == reg_entity_id and device["uuid"] == reg_entity_id: entity_exist = True try: msg["iotcc"]["Devices"].remove(device) diff --git a/liota/lib/utilities/utility.py b/liota/lib/utilities/utility.py index 56eb3240..60dacbf5 100644 --- a/liota/lib/utilities/utility.py +++ b/liota/lib/utilities/utility.py @@ -47,7 +47,7 @@ import stat import json import subprocess - +import time log = logging.getLogger(__name__) @@ -66,15 +66,21 @@ def __init__(self): def _getMacAddrIfaceHash(self): mac = uuid.getnode() if (mac >> 40) % 2: - log.warn( - 'could not find a mac address, an unlikely potential exists for uuid collisions with liota instances on other IoT gateways') - # generate a 48-bit random integer from this seed - # always returns the same random integer - # however in get_uuid below a unique uuid for each resource name will be created - # this allows us not to have to store any uuid on the persistent storage yet - # create a unique system uuid - random.seed(1234567) - mac = random.randint(0, 281474976710655) + # retry after 30 seconds to get the mac address + # if not able to detect mac address after this try then go ahead with alternate mechanism + log.info('Retrying getting the mac address') + time.sleep(30) + mac = uuid.getnode() + if (mac >> 40) % 2: + log.warn( + 'could not find a mac address, an unlikely potential exists for uuid collisions with liota instances on other IoT gateways') + # generate a 48-bit random integer from this seed + # always returns the same random integer + # however in get_uuid below a unique uuid for each resource name will be created + # this allows us not to have to store any uuid on the persistent storage yet + # create a unique system uuid + random.seed(1234567) + mac = random.randint(0, 281474976710655) m = hashlib.md5() m.update(str(mac)) self.macHash = m.hexdigest() @@ -144,7 +150,7 @@ def get_disk_name(): def getUTCmillis(): return long(1000 * ((datetime.utcnow() - datetime(1970, 1, 1)).total_seconds())) - + def mkdir(path): if not os.path.exists(path): try: @@ -155,6 +161,7 @@ def mkdir(path): else: raise + def store_edge_system_uuid(entity_name, entity_id, reg_entity_id): """ Utility function to store EdgeSystem's Name, local-uuid and registered-uuid in the @@ -179,6 +186,7 @@ def store_edge_system_uuid(entity_name, entity_id, reg_entity_id): except ConfigParser.ParsingError, err: log.error('Could not open config file ' + str(err)) + def sha1sum(path_file): """ This method calculates SHA-1 checksum of file. @@ -195,6 +203,7 @@ def sha1sum(path_file): sha1.update(data) return sha1 + class LiotaConfigPath: path_liota_config = '' syswide_path = '/etc/liota/conf/' @@ -264,7 +273,7 @@ def setup_logging(self, default_level=logging.WARNING): # missing config file log.warn('liota.conf file missing') - + def read_liota_config(section, name): """ Returns the value of name within the specified section. @@ -287,6 +296,7 @@ def read_liota_config(section, name): log.warn('liota.conf file missing') return value + def read_user_config(config_file_path): """ Returns the user defined configuration as a dictionary from DEFAULT section. diff --git a/packages/iotcc_mqtt.py b/packages/iotcc_mqtt.py index 1e2a6edd..75721648 100644 --- a/packages/iotcc_mqtt.py +++ b/packages/iotcc_mqtt.py @@ -67,8 +67,7 @@ def run(self, registry): tls_conf = TLSConf(config['cert_required'], config['tls_version'], config['cipher']) # Initialize DCC object with MQTT transport - self.iotcc = IotControlCenter(config['broker_username'], config['broker_password'], - MqttDccComms(edge_system_name=edge_system.name, + self.iotcc = IotControlCenter(MqttDccComms(edge_system_name=edge_system.name, url=config['BrokerIP'], port=config['BrokerPort'], identity=identity, tls_conf=tls_conf, enable_authentication=True)) diff --git a/setup.py b/setup.py index 590be44f..9e3d9e19 100644 --- a/setup.py +++ b/setup.py @@ -54,7 +54,7 @@ setup( name='liota', - version='0.2.6', + version='0.2.8', packages=find_packages(exclude=["*.json", "*.txt"]), description='IoT Agent', long_description=long_description,