def receive_message(self, ch, method, properties, body): if body is not None: ch.basic_ack(delivery_tag=method.delivery_tag) body_decoded = jsonpickle.decode(body) Tools.amqp_receive_to_logger(routing_key=method.routing_key, body_decoded=body_decoded) if isinstance(body_decoded, AMQPMessage) and c.AMQP_MSG_TYPE_DEVICE_ADD == body_decoded.message_type: sample_device = body_decoded.payload status, sample_device = Tools.create_dev_conn(sample_device=sample_device) if status: sample_device = Tools.get_device_facts(sample_device=sample_device) status, data = Tools.get_config(lookup_type=c.CONFIG_LOOKUP_TYPE_GET_DEVICE_CFG, sample_device=sample_device) if status: sample_device.deviceConfigData = data try: sample_device.deviceGroup = data['yapt']['device_group'] except KeyError as ke: self._logger.info(Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, logmsg.TASKP_STOP_NO_DEVGRP.format(ke.message))) return status, grp_cfg = Tools.get_config(lookup_type=c.CONFIG_LOOKUP_TYPE_GET_GROUP, sample_device=sample_device) if status: sample_device.deviceGroupData = grp_cfg grp_cfg = Tools.create_config_view(config_type=c.CONFIG_TYPE_GROUP, stream=grp_cfg) sample_device.deviceTemplate = grp_cfg.TASKS.Provision.Configuration.DeviceConfTemplateFile sample_device.deviceTaskSeq = list(grp_cfg.TASKS.Sequence) dev_conn = sample_device.deviceConnection sample_device.deviceConnection = hex(id(sample_device.deviceConnection)) message = AMQPMessage(message_type=c.AMQP_MSG_TYPE_DEVICE_ADD, payload=sample_device, source=c.AMQP_PROCESSOR_TASK) resp = self._backendp.call(message=message) resp = jsonpickle.decode(resp) sample_device = resp.payload[1] sample_device.deviceConnection = dev_conn self._logger.info(Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, logmsg.TASKP_LOAD_TASK_SEQ.format( sample_device.deviceTaskSeq))) c.taskq.add_device_task_q(sample_device=sample_device, grp_cfg=grp_cfg) taskq = c.taskq.get_device_task_q(sn=sample_device.deviceSerial) self._logger.debug(Tools.create_log_msg(self.name, sample_device.deviceSerial, 'Device status: <{0}>'.format( sample_device.deviceStatus))) if taskq: for task in taskq: self._logger.debug(Tools.create_log_msg(self.name, sample_device.deviceSerial, 'Task: <{0}> status: <{1}>'.format( task.task_name, task.task_state))) if task.task_state == c.TASK_STATE_INIT: task.pre_run_task() task.run_task() task.post_run_task() if task.task_state == c.TASK_STATE_REBOOTING: self._logger.debug( Tools.create_log_msg(self.name, sample_device.deviceSerial, 'Task: <{0}> status: <{1}>'.format( task.task_name, task.task_state))) self._logger.info( Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, logmsg.TASKP_STOP_DEV_REBOOT.format( sample_device.deviceSerial))) break elif task.task_state == c.TASK_STATE_FAILED: self._logger.debug( Tools.create_log_msg(self.name, sample_device.deviceSerial, 'Task: <{0}> status: <{1}>'.format( task.task_name, task.task_state))) self._logger.info( Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, logmsg.TASKP_TASK_ERROR.format( task.task_name, ))) self.close_dev_conn(sample_device=sample_device) break elif task.task_state == c.TASK_STATE_REBOOTING: task.pre_run_task() task.run_task() task.post_run_task() if task.task_state == c.TASK_STATE_FAILED: self._logger.debug( Tools.create_log_msg(self.name, sample_device.deviceSerial, 'Task: <{0}> status: <{1}>'.format( task.task_name, task.task_state))) self._logger.info( Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, logmsg.TASKP_TASK_ERROR.format( task.task_name, ))) self.close_dev_conn(sample_device=sample_device) break elif task.task_state == c.TASK_STATE_DONE: pass elif task.task_state == c.TASK_STATE_FAILED: self._logger.debug( Tools.create_log_msg(self.name, sample_device.deviceSerial, 'Task: <{0}> status: <{1}>'.format( task.task_name, task.task_state))) self._logger.info( Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, logmsg.TASKP_TASK_ERROR.format( task.task_name, ))) self.close_dev_conn(sample_device=sample_device) break else: self._logger.info(Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, 'Task <{0}> state: <{1}> unknown'.format( task.task_name, task.task_state))) break # print 'After running all tasks device status is: <{0}>'.format( # sample_device.deviceStatus) if sample_device.deviceStatus == c.DEVICE_STATUS_DONE: c.taskq.del_device_task_q(sample_device.deviceSerial) else: self._logger.info(Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, logmsg.TASKP_STOP_NO_DEVGRP_CFG.format(grp_cfg))) self.close_dev_conn(sample_device=sample_device) return else: self._logger.info(Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, logmsg.TASKP_STOP_NO_DEV_CFG.format(data))) self.close_dev_conn(sample_device=sample_device) return else: self._logger.info(Tools.create_log_msg(logmsg.TASKP, sample_device.deviceSerial, logmsg.TASKP_CONN_ERR.format(sample_device.deviceSerial))) return else: Tools.amqp_receive_error_to_logger(routing_key=method.routing_key, body_decoded=body_decoded) else: self._logger.info(Tools.create_log_msg(logmsg.TASKP, None, logmsg.TASKP_DEFECT_MSG))
def on_request(self, ch, method, props, body): if body is not None: body_decoded = jsonpickle.decode(body) Tools.amqp_receive_to_logger(routing_key=method.routing_key, body_decoded=body_decoded) if isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_DEVICE_ADD == body_decoded.message_type: response = self.add_device(body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_DEVICE_CFG_ADD == body_decoded.message_type: response = self.add_device_config( configSerial=body_decoded.payload['configSerial'], configDescr=body_decoded.payload['configDescr'], configConfigSource=body_decoded. payload['configConfigSource']) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_DEVICE_CFG_DEL == body_decoded.message_type: response = self.del_device_config( configSerial=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_DEVICE_GET_CFG_BY_SERIAL == body_decoded.message_type: response = self.get_device_config_by_sn( configSerial=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_DEVICE_GET_CFG_ALL == body_decoded.message_type: response = self.get_device_configs() self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_DEVICE_UPDATE == body_decoded.message_type: response = self.update_device(body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_DEVICE_UPDATE_TASK_STATE == body_decoded.message_type: device_serial = body_decoded.payload['deviceSerial'] is_callback = body_decoded.payload['isCallback'] task_name = body_decoded.payload['taskName'] task_state = body_decoded.payload['taskState'] response = self.update_device_task_state( device_serial=device_serial, is_callback=is_callback, task_name=task_name, task_state=task_state) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_DEVICE_GET_BY_SN == body_decoded.message_type: response = self.get_device(body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_DEVICE_GET_ALL == body_decoded.message_type: response = self.get_devices() self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_SITE_ADD == body_decoded.message_type: response = self.add_site( siteId=body_decoded.payload['siteId'], siteName=body_decoded.payload['siteName'], siteDescr=body_decoded.payload['siteDescr']) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_SITE_DEL == body_decoded.message_type: response = self.del_site(siteId=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_ASSET_ADD == body_decoded.message_type: response = self.add_asset_to_site( assetSiteId=body_decoded.payload['assetSiteId'], assetSerial=body_decoded.payload['assetSerial'], assetConfigId=body_decoded.payload['assetConfigId'], assetDescr=body_decoded.payload['assetDescr']) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_ASSET_GET_BY_SITE == body_decoded.message_type: response = self.get_asset_by_site_id( assetSiteId=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_ASSET_GET_BY_SERIAL == body_decoded.message_type: response = self.get_asset_by_serial( assetSerial=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_ASSET_UPDATE == body_decoded.message_type: response = self.update_asset_site_mapping( assetSerial=body_decoded.payload['assetSerial'], assetConfigId=body_decoded.payload['assetConfigId']) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_SITE_GET_BY_ID == body_decoded.message_type: response = self.get_site_by_id(siteId=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_SITE_GET_ALL == body_decoded.message_type: response = self.get_sites() self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_GROUP_ADD == body_decoded.message_type: response = self.add_group( groupName=body_decoded.payload['groupName'], groupConfig=body_decoded.payload['groupConfig'], groupDescr=body_decoded.payload['groupDescr'], groupConfigSource=body_decoded.payload['groupConfigSource'] ) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_GROUP_GET_BY_NAME == body_decoded.message_type: response = self.get_group_by_name( groupName=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_GROUP_GET_ALL == body_decoded.message_type: response = self.get_groups() self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_GROUP_DEL == body_decoded.message_type: response = self.del_group_by_name( groupName=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_TEMPLATE_ADD == body_decoded.message_type: response = self.add_template( templateName=body_decoded.payload['templateName'], templateConfig=body_decoded.payload['templateConfig'], templateDescr=body_decoded.payload['templateDescr'], templateConfigSource=body_decoded. payload['templateConfigSource'], templateDevGrp=body_decoded.payload['templateDevGrp']) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_TEMPLATE_GET_BY_NAME == body_decoded.message_type: response = self.get_template_by_name( templateName=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_TEMPLATE_GET_ALL == body_decoded.message_type: response = self.get_templates() self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_TEMPLATE_DEL == body_decoded.message_type: response = self.del_template_by_name( templateName=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_IMAGE_ADD == body_decoded.message_type: response = self.add_image( imageName=body_decoded.payload['imageName'], imageDescr=body_decoded.payload['imageDescr']) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_IMAGE_GET_BY_NAME == body_decoded.message_type: response = self.get_image_by_name( imageName=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_IMAGE_GET_ALL == body_decoded.message_type: response = self.get_images() self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_IMAGE_DEL == body_decoded.message_type: response = self.del_image_by_name( imageName=body_decoded.payload['imageName']) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_SVC_GET_BY_NAME == body_decoded.message_type: response = self.get_service_by_name( serviceName=body_decoded.payload) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_SVC_GET_ALL == body_decoded.message_type: response = self.get_services() self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_SVC_PHS_VALIDATE == body_decoded.message_type: response = self.validate_phc( username=body_decoded.payload['username'], password=body_decoded.payload['password']) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_VAL_GET_ALL == body_decoded.message_type: response = self.get_validation_all() self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_VAL_VAL_ADD == body_decoded.message_type: response = self.add_asset_to_validation_db( username=body_decoded.payload['username'], password=body_decoded.payload['password']) self.processRequest(ch=ch, method=method, props=props, response=response) elif isinstance( body_decoded, AMQPMessage ) and c.AMQP_MSG_TYPE_REST_VAL_VAL_DEL == body_decoded.message_type: response = self.del_asset_from_validation_db( username=body_decoded.payload['username']) self.processRequest(ch=ch, method=method, props=props, response=response) else: Tools.amqp_receive_error_to_logger( routing_key=method.routing_key, body_decoded=body_decoded) else: self._logger.info('YAPTBACKEND: Recevied empty message')