Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Commit

Permalink
Code refactor and Mac address retry logic (#134)
Browse files Browse the repository at this point in the history
* Code_Refactor

* MAC_Address Fix

* Liota version
  • Loading branch information
KohliDev authored May 20, 2017
1 parent 0a93f9f commit 5c70493
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 45 deletions.
1 change: 0 additions & 1 deletion examples/dell5k_edge_system_iotcc_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions examples/dell5k_edge_system_with_filter_iotcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
56 changes: 32 additions & 24 deletions liota/dccs/iotcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -119,27 +121,29 @@ 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")

if entity_obj.entity_type == "EdgeSystem":
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,
Expand All @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
34 changes: 22 additions & 12 deletions liota/lib/utilities/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import stat
import json
import subprocess

import time

log = logging.getLogger(__name__)

Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -195,6 +203,7 @@ def sha1sum(path_file):
sha1.update(data)
return sha1


class LiotaConfigPath:
path_liota_config = ''
syswide_path = '/etc/liota/conf/'
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions packages/iotcc_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5c70493

Please sign in to comment.