def __init__(self, source_plugin, plugin_cfg): self._devices_auth = dict() self.logger = c.logger self._backendp = BackendClientProcessor( exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) cherrypy.config.update('{0}/{1}'.format(os.getcwd(), c.SVC_PHS_CONF)) phs = PhoneHomeServer(source_plugin, plugin_cfg) rd = cherrypy.dispatch.RoutesDispatcher() rd.connect('phs', c.PHS_INIT_URL, controller=phs, action='init') # rd.connect('phs', '/restconf/data/juniper-zerotouch-bootstrap-server:devices/device={uid}/activation-code', # controller=phs, action='activation') rd.connect('phs', c.PHS_NOTIFICATION_URL, controller=phs, action='notification') conf = { '/': { 'request.dispatch': rd, 'tools.auth_basic.checkpassword': self.validate_phc } } app = cherrypy.tree.mount(root=None, config='{0}/{1}'.format( os.getcwd(), c.SVC_PHS_CONF)) app.merge(conf) cherrypy.engine.start() cherrypy.engine.block()
def __init__(self, sample_device=None, shared=None): self.logger = c.logger self.task_state = c.TASK_STATE_INIT self.task_progress = 0.0 self.messages = { 'error': 'error', 'valid': 'valid', 'invalid': 'invalid', 'deactivated': 'deactivated' } self.task_name = self.__class__.__name__.split('Task')[0] self.sample_device = sample_device self.shared = shared self.grp_cfg = Tools.create_config_view( c.CONFIG_TYPE_GROUP, stream=sample_device.deviceGroupData) self.task_type = self.__class__.TASK_TYPE self.check_schema = self.__class__.CHECK_SCHEMA self.task_version = self.__class__.TASK_VERSION self._backendp = BackendClientProcessor( exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) self.logger.info( Tools.create_log_msg(self.task_name, self.sample_device.deviceSerial, 'Validating task configuration')) isValid, err = self.__validate_task_config( grp_cfg=sample_device.deviceGroupData) if isValid == self.messages['valid']: self.logger.info( Tools.create_log_msg(self.task_name, self.sample_device.deviceSerial, 'Task configuration is valid')) # self.pre_run_task() # self.run_task() # self.post_run_task() elif isValid == self.messages['invalid']: Tools.emit_log( task_name=self.task_name, sample_device=self.sample_device, message='Task configuration is invalid. <{0}>'.format(err)) self.update_task_state(new_task_state=c.TASK_STATE_FAILED, task_state_message=c.TASK_STATE_MSG_FAILED) return elif isValid == self.messages['error']: Tools.emit_log(task_name=self.task_name, sample_device=self.sample_device, message='Task configuration file not found') self.update_task_state(new_task_state=c.TASK_STATE_FAILED, task_state_message=c.TASK_STATE_MSG_FAILED) return else: Tools.emit_log(task_name=self.task_name, sample_device=self.sample_device, message='Task configuration validation deactivated')
def action(self, instance, task_name): from lib.processor import BackendClientProcessor backendp = BackendClientProcessor(exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) if isinstance(instance, TaskState): message = AMQPMessage(message_type=c.AMQP_MSG_TYPE_DEVICE_UPDATE_TASK_STATE, payload={'deviceSerial': getattr(instance, 'deviceSerial', None), 'isCallback': getattr(instance, 'is_callback', None), 'taskName': task_name, 'taskState': instance.taskState[task_name]}, source=task_name) backendp.call(message=message) else: self._logger.info('Unknown task type %s. Can\'t update backend', instance)
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None): super(UiProcessor, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs) self._logger.debug( Tools.create_log_msg( self.__class__.__name__, None, LogCommon.IS_SUBCLASS.format( self.__class__.__name__, issubclass(UiProcessor, AMQPBlockingServerAdapter)))) self.url = 'ws://{0}:{1}/yapt/ws?clientname={2}'.format( c.conf.YAPT.WebUiAddress, str(c.conf.YAPT.WebUiPort), c.conf.YAPT.WebUiPlugin) self.amqp2ws = Amqp2ws(name=c.conf.YAPT.WebUiPlugin, url=self.url) self.backendp = BackendClientProcessor( exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) LogViewer().run_service()
def __init__(self, normalizer, svc_cfg): self.svc_cfg = svc_cfg self.logger = c.logger self.normalizer = normalizer self.status = c.SVC_INIT self._backendp = BackendClientProcessor( exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE)
class PhsInitC(object): def __init__(self, source_plugin, plugin_cfg): self._devices_auth = dict() self.logger = c.logger self._backendp = BackendClientProcessor( exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) cherrypy.config.update('{0}/{1}'.format(os.getcwd(), c.SVC_PHS_CONF)) phs = PhoneHomeServer(source_plugin, plugin_cfg) rd = cherrypy.dispatch.RoutesDispatcher() rd.connect('phs', c.PHS_INIT_URL, controller=phs, action='init') # rd.connect('phs', '/restconf/data/juniper-zerotouch-bootstrap-server:devices/device={uid}/activation-code', # controller=phs, action='activation') rd.connect('phs', c.PHS_NOTIFICATION_URL, controller=phs, action='notification') conf = { '/': { 'request.dispatch': rd, 'tools.auth_basic.checkpassword': self.validate_phc } } app = cherrypy.tree.mount(root=None, config='{0}/{1}'.format( os.getcwd(), c.SVC_PHS_CONF)) app.merge(conf) cherrypy.engine.start() cherrypy.engine.block() def validate_phc(self, realm, username, password): message = AMQPMessage(message_type=c.AMQP_MSG_TYPE_SVC_PHS_VALIDATE, payload={ 'username': username, 'password': password }, source=c.AMQP_PROCESSOR_TASK) resp = self._backendp.call(message=message) resp = jsonpickle.decode(resp) if resp.payload[0]: self.logger.info( Tools.create_log_msg( logmsg.PHS_SERVICE, username, logmsg.PHS_VALIDATION_SUCCESS.format(username))) return True else: self.logger.info( Tools.create_log_msg( logmsg.PHS_SERVICE, username, logmsg.PHS_VALIDATION_FAILED.format(username))) return False
def run_normalizer(self, timestamp=None, device=None): if device: message = AMQPMessage( message_type=c.AMQP_MSG_TYPE_DEVICE_GET_BY_SN, payload=device, source=c.AMQP_PROCESSOR_SVC) backendp = BackendClientProcessor( exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) resp = jsonpickle.decode(backendp.call(message=message)) if resp.payload[0]: sample_device = resp.payload[1] sample_device.deviceSerial = device sample_device.deviceServicePlugin = c.SERVICEPLUGIN_WEBHOOK sample_device.deviceTimeStamp = timestamp return sample_device else: return False else: return False
def __init__(self): _backendp = BackendClientProcessor( exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) self._setattr_url_map(args=_backendp) _host = c.conf.YAPT.WebUiAddress _port = int(c.conf.YAPT.RestApiPort) yapt_rest_server = Server() yapt_rest_server.socket_host = _host yapt_rest_server.socket_port = _port yapt_rest_server.subscribe() cherrypy.tools.cors_tool = cherrypy.Tool('before_request_body', cors_tool, name='cors_tool', priority=50)
class Task(object): CHECK_SCHEMA = None TASK_TYPE = None TASK_VERSION = None TASK_DESCENT = None def __init__(self, sample_device=None, shared=None): self.logger = c.logger self.task_state = c.TASK_STATE_INIT self.task_progress = 0.0 self.messages = { 'error': 'error', 'valid': 'valid', 'invalid': 'invalid', 'deactivated': 'deactivated' } self.task_name = self.__class__.__name__.split('Task')[0] self.sample_device = sample_device self.shared = shared self.grp_cfg = Tools.create_config_view( c.CONFIG_TYPE_GROUP, stream=sample_device.deviceGroupData) self.task_type = self.__class__.TASK_TYPE self.check_schema = self.__class__.CHECK_SCHEMA self.task_version = self.__class__.TASK_VERSION self._backendp = BackendClientProcessor( exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) self.logger.info( Tools.create_log_msg(self.task_name, self.sample_device.deviceSerial, 'Validating task configuration')) isValid, err = self.__validate_task_config( grp_cfg=sample_device.deviceGroupData) if isValid == self.messages['valid']: self.logger.info( Tools.create_log_msg(self.task_name, self.sample_device.deviceSerial, 'Task configuration is valid')) # self.pre_run_task() # self.run_task() # self.post_run_task() elif isValid == self.messages['invalid']: Tools.emit_log( task_name=self.task_name, sample_device=self.sample_device, message='Task configuration is invalid. <{0}>'.format(err)) self.update_task_state(new_task_state=c.TASK_STATE_FAILED, task_state_message=c.TASK_STATE_MSG_FAILED) return elif isValid == self.messages['error']: Tools.emit_log(task_name=self.task_name, sample_device=self.sample_device, message='Task configuration file not found') self.update_task_state(new_task_state=c.TASK_STATE_FAILED, task_state_message=c.TASK_STATE_MSG_FAILED) return else: Tools.emit_log(task_name=self.task_name, sample_device=self.sample_device, message='Task configuration validation deactivated') # self.pre_run_task() # self.run_task() # self.post_run_task() @abc.abstractmethod def pre_run_task(self): raise NotImplementedError() @abc.abstractmethod def run_task(self): raise NotImplementedError() @abc.abstractmethod def post_run_task(self): raise NotImplementedError() def _update_backend(self): if self.task_type == c.TASK_TYPE_PROVISION or c.TASK_TYPE_VERIFICATION: if 100 % len(self.grp_cfg.TASKS.Sequence) == 0: self.sample_device.deviceTaskProgress += self.task_progress else: if self.task_state == c.TASK_STATE_DONE: if self.sample_device.deviceStatus == c.DEVICE_STATUS_DONE: diff = (100 - (self.task_progress + self.sample_device.deviceTaskProgress)) new = self.task_progress + self.sample_device.deviceTaskProgress self.sample_device.deviceTaskProgress = new + diff else: self.sample_device.deviceTaskProgress += self.task_progress else: self.sample_device.deviceTaskProgress += self.task_progress dev_conn = self.sample_device.deviceConnection self.sample_device.deviceConnection = hex( id(self.sample_device.deviceConnection)) message = AMQPMessage(message_type=c.AMQP_MSG_TYPE_DEVICE_UPDATE, payload=self.sample_device, source=c.AMQP_PROCESSOR_TASK) self._backendp.call(message=message) self.sample_device.deviceConnection = dev_conn def __validate_task_config(self, grp_cfg=None): if getattr(self, 'TASK_DESCENT', None): task_name = self.TASK_DESCENT else: task_name = self.task_name if self.check_schema: if os.path.exists('conf/schema/task/' + task_name.lower() + '.yml') and os.path.isfile('conf/schema/task/' + task_name.lower() + '.yml'): with open('conf/schema/task/' + task_name.lower() + '.yml', 'r') as stream: schema = yaml.safe_load(stream) v = Validator() v.allow_unknown = True if v.validate(document=grp_cfg['TASKS']['Provision'], schema=schema): return self.messages['valid'], None else: return self.messages['invalid'], v.errors else: return self.messages['error'], None else: return self.messages['deactivated'], None def update_task_state(self, new_task_state=c.TASK_STATE_INIT, task_state_message='empty'): Tools.emit_log( task_name=self.task_name, sample_device=self.sample_device, message='Current task state <{0}> --> New task state <{1}>'.format( self.task_state, new_task_state), level=c.LOGGER_LEVEL_DEBUG) if getattr(self, 'TASK_DESCENT', None): task_name = self.TASK_DESCENT else: task_name = self.task_name if new_task_state == c.TASK_STATE_DONE: if task_name == self.grp_cfg.TASKS.Sequence[-1:][0]: self.task_state = new_task_state self.sample_device.deviceStatus = c.DEVICE_STATUS_DONE self.task_progress = self.shared[c.TASK_SHARED_PROGRESS] self._update_backend() self.sample_device.deviceTasks.taskState[task_name] = { 'taskState': new_task_state, 'taskStateMsg': task_state_message } else: self.task_state = new_task_state self.sample_device.deviceStatus = c.DEVICE_STATUS_NEXT_TASK self.task_progress = self.shared[c.TASK_SHARED_PROGRESS] self._update_backend() self.sample_device.deviceTasks.taskState[task_name] = { 'taskState': new_task_state, 'taskStateMsg': task_state_message } elif new_task_state == c.TASK_STATE_FAILED: self.task_state = new_task_state self.sample_device.deviceStatus = c.DEVICE_STATUS_FAILED self._update_backend() self.sample_device.deviceTasks.taskState[task_name] = { 'taskState': new_task_state, 'taskStateMsg': task_state_message } elif new_task_state == c.TASK_STATE_REBOOTING: self.task_state = new_task_state self.sample_device.deviceStatus = c.DEVICE_STATUS_REBOOTED self._update_backend() self.sample_device.deviceTasks.taskState[task_name] = { 'taskState': new_task_state, 'taskStateMsg': task_state_message } elif new_task_state == c.TASK_STATE_PROGRESS: self.task_state = new_task_state self.sample_device.deviceStatus = c.DEVICE_STATUS_PROGRESS self._update_backend() self.sample_device.deviceTasks.taskState[task_name] = { 'taskState': new_task_state, 'taskStateMsg': task_state_message } else: Tools.emit_log(task_name=self.task_name, sample_device=self.sample_device, message='Unknown task state <{0}>'.format( self.task_state))
def __init__(self, normalizer=None, svc_cfg=None): self.logger = c.logger self._backendp = BackendClientProcessor(exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) cherrypy.tools.cors_tool = cherrypy.Tool('before_request_body', cors_tool, name='cors_tool', priority=50) self._source_plugin = normalizer self._plugin_cfg = svc_cfg
def get_config(self, lookup_type=None, **kvargs): """ obtain specific config type <lookup_type> from configured source in <DeviceConfSrcPlugins> :param lookup_type: defines which data we looking for e.g. device_data / group_data / template :return: """ osshid = None isRaw = None templateName = None groupName = None from lib.pluginfactory import StoragePlgFact storageFact = StoragePlgFact() storage_plgs = storageFact.init_plugins() c.logger.debug(Tools.create_log_msg('TOOLS', 'get_config', kvargs)) if 'sample_device' in kvargs: sample_device = kvargs.get('sample_device') sn = sample_device.deviceSerial osshid = sample_device.deviceOsshId groupName = sample_device.deviceGroup templateName = sample_device.deviceTemplate elif 'serialnumber' in kvargs and 'deviceOsshId' in kvargs: sn = kvargs.get('serialnumber') osshid = kvargs.get('deviceOsshId') elif 'templateName' in kvargs and 'groupName' in kvargs and 'isRaw' in kvargs: templateName = kvargs.get('templateName') groupName = kvargs.get('groupName') isRaw = kvargs.get('isRaw') sn = templateName elif 'groupName' in kvargs and 'isRaw' in kvargs: groupName = kvargs.get('groupName') isRaw = kvargs.get('isRaw') sn = groupName elif 'configSerial' in kvargs and 'isRaw' in kvargs: sn = kvargs.get('configSerial') isRaw = kvargs.get('isRaw') else: return False, 'Parameters not matching' c.logger.debug(Tools.create_log_msg(logmsg.STORAGE_PLG, sn if sn else osshid, logmsg.STORAGE_PLG_LOAD.format(c.conf.STORAGE.DeviceConfSrcPlugins))) if c.conf.STORAGE.DeviceConfSrcPlugins: for key, storage in storage_plgs.iteritems(): if lookup_type == c.CONFIG_LOOKUP_TYPE_GET_DEVICE_CFG: # check ooba if c.conf.STORAGE.DeviceConfOoba: c.logger.info(Tools.create_log_msg(logmsg.STORAGE_PLG, sn if sn else osshid, 'Checking config id mapping in asset database')) from lib.amqp.amqpmessage import AMQPMessage message = AMQPMessage(message_type=c.AMQP_MSG_TYPE_REST_ASSET_GET_BY_SERIAL, payload=osshid if osshid else sn, source=c.AMQP_PROCESSOR_REST) from lib.processor import BackendClientProcessor _backendp = BackendClientProcessor(exchange='', routing_key=c.AMQP_RPC_BACKEND_QUEUE) response = _backendp.call(message=message) response = jsonpickle.decode(response) # Check if mapping found if response.payload[0]: c.logger.info(Tools.create_log_msg(logmsg.STORAGE_PLG, sn if sn else osshid, 'Successfully retrieved config id mapping for {0}<-->{1}'.format( osshid if osshid else sn, response.payload[1]))) sn = response.payload[1] else: c.logger.info(Tools.create_log_msg(logmsg.STORAGE_PLG, sn if sn else osshid, 'Failed to retrieve config id mapping for {0}<-->{1}'.format( sn, response.payload))) return response.payload[0], response.payload[1] status, data = storage.get_device_config_data(serialnumber=sn, isRaw=isRaw) if status: return status, data else: continue elif lookup_type == c.CONFIG_LOOKUP_TYPE_GET_DEVICE_CFG_FILE: status, filename = storage.get_device_config_data_file(serialnumber=sn, deviceOsshId=osshid) c.logger.debug(Tools.create_log_msg(logmsg.STORAGE_PLG, sn if sn else osshid, logmsg.STORAGE_PLG_EXEC.format(storage))) if status: return status, filename else: continue elif lookup_type == c.CONFIG_LOOKUP_TYPE_GET_TEMPLATE: if sn and templateName and groupName: isFile, template = storage.get_config_template_data(serialnumber=sn, templateName=templateName, groupName=groupName, isRaw=isRaw) c.logger.debug(Tools.create_log_msg(logmsg.STORAGE_PLG, sn if sn else osshid, logmsg.STORAGE_PLG_EXEC.format(storage))) if isFile: return isFile, template else: c.logger.info(Tools.create_log_msg(logmsg.STORAGE_PLG, sn if sn else osshid, logmsg.STORAGE_TEMPLATE_NOK(templateName, templateName))) continue elif lookup_type == c.CONFIG_LOOKUP_TYPE_GET_TEMPLATE_FILE: if sn and templateName: isFile, template = storage.get_config_template_file(serialnumber=sn, templateName=templateName, groupName=groupName) c.logger.debug(logmsg.STORAGE_PLG, sn if sn else osshid, logmsg.STORAGE_PLG_EXEC.format(storage)) if isFile: return isFile, template else: continue elif lookup_type == c.CONFIG_LOOKUP_TYPE_TEMPLATE_BOOTSTRAP: if osshid: isFile, template = storage.get_bootstrap_config_template(serialnumber=osshid, path=kvargs.get('path'), file=kvargs.get('file')) c.logger.debug(logmsg.STORAGE_PLG, sn if sn else osshid, logmsg.STORAGE_PLG_EXEC.format(storage)) if isFile: return template else: continue elif lookup_type == c.CONFIG_LOOKUP_TYPE_GET_GROUP: if sn and groupName: status, groupvars = storage.get_group_data(serialnumber=sn, groupName=groupName, isRaw=isRaw) c.logger.debug(Tools.create_log_msg(logmsg.STORAGE_PLG, sn if sn else osshid, logmsg.STORAGE_PLG_EXEC.format(storage))) if status: return True, groupvars else: continue elif lookup_type == c.CONFIG_LOOKUP_TYPE_GET_GROUP_FILE: if sn and groupName: isFile, filename = storage.get_group_data_file(serialnumber=sn, group=groupName) c.logger.debug(logmsg.STORAGE_PLG, sn if sn else osshid, logmsg.STORAGE_PLG_EXEC.format(storage)) if isFile: return filename else: continue return False, 'No device or group or template data found' else: c.logger.info(logmsg.STORAGE_PLG, sn if sn else osshid, 'Config Source plugin sequence empty') return