def execute_resource(self, resource_id='', command=None): """Execute command on the resource represented by agent. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.execute_resource(resource_id=resource_id, command=command) cmd_res = None res_interface = self._get_type_interface(res_type) target = get_safe(res_interface, "commands.%s.execute" % command.command, None) if target: res = self._call_execute(target, resource_id, res_type, command.args, command.kwargs) cmd_res = AgentCommandResult(command_id=command.command_id, command=command.command, ts_execute=get_ion_ts(), status=0) else: log.warn("execute_resource(): command %s not defined", command.command) return cmd_res
def execute_resource(self, resource_id='', command=None): """Execute command on the resource represented by agent. @param resource_id The id of the resource agennt. @param command An AgentCommand containing the command. @retval result An AgentCommandResult containing the result. @throws BadRequest if the command was malformed. @throws NotFound if the command is not available in current state. @throws ResourceError if the resource produced an error during execution. @param resource_id str @param command AgentCommand @retval result AgentCommandResult @throws BadRequest if the command was malformed. @throws NotFound if the command is not implemented in the agent. @throws ResourceError if the resource produced an error. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.execute_resource(resource_id=resource_id, command=command) cmd_res = None res_interface = self._get_type_interface(res_type) target = get_safe(res_interface, "commands.%s.execute" % command.command, None) if target: res = self._call_execute(target, resource_id, res_type, command.args, command.kwargs) cmd_res = AgentCommandResult(command_id=command.command_id, command=command.command, ts_execute=get_ion_ts(), status=0) else: log.warn("execute_resource(): command %s not defined", command.command) return cmd_res
def get_capabilities(self, resource_id='', current_state=True): """Introspect for agent capabilities. @param resource_id The id of the resource agent. @param current_state Flag indicating to return capabilities for current state only (default True). @retval List of AgentCapabilities objects. @param resource_id str @param current_state bool @retval capability_list list """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.get_capabilities(resource_id=resource_id, current_state=current_state) res_interface = self._get_type_interface(res_type) cap_list = [] for param in res_interface['params'].keys(): cap = AgentCapability(name=param, cap_type=CapabilityType.RES_PAR) cap_list.append(cap) for cmd in res_interface['commands'].keys(): cap = AgentCapability(name=cmd, cap_type=CapabilityType.RES_CMD) cap_list.append(cap) return cap_list
def do_read_dataset(self): self.dams.start_external_dataset_agent_instance( self.agent_instance._id) # # should i wait for process (above) to start # before launching client (below)? # self.client = None end = time.time() + MAX_AGENT_START_TIME while not self.client and time.time() < end: try: self.client = ResourceAgentClient(self.device._id, process=FakeProcess()) except NotFound: time.sleep(2) if not self.client: self.fail( msg='external dataset agent process did not start in %d seconds' % MAX_AGENT_START_TIME) self.client.execute_agent( AgentCommand(command=ResourceAgentEvent.INITIALIZE)) self.client.execute_agent( AgentCommand(command=ResourceAgentEvent.GO_ACTIVE)) self.client.execute_agent(AgentCommand(command=ResourceAgentEvent.RUN)) self.client.execute_resource(command=AgentCommand( command=DriverEvent.START_AUTOSAMPLE))
def set_resource(self, resource_id='', params=None): """Set the value of the given resource parameters. @param resource_id The id of the resource agent. @param params A dict of resource parameter name-value pairs. @throws BadRequest if the command was malformed. @throws NotFound if a parameter is not supported by the resource. @throws ResourceError if the resource encountered an error while setting the parameters. @param resource_id str @param params dict @throws BadRequest if the command was malformed. @throws NotFound if the parameter does not exist. @throws ResourceError if the resource failed while trying to set the parameter. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.set_resource(resource_id=resource_id, params=params) res_interface = self._get_type_interface(res_type) for param in params: setter = get_safe(res_interface, "params.%s.set" % param, None) if setter: self._call_setter(setter, params[param], resource_id, res_type) else: log.warn("set_resource(): param %s not defined", param)
def get_resource(self, resource_id='', params=None): """Return the value of the given resource parameter. @param resource_id The id of the resource agennt. @param params A list of parameters names to query. @retval A dict of parameter name-value pairs. @throws BadRequest if the command was malformed. @throws NotFound if the resource does not support the parameter. @param resource_id str @param params list @retval result AgentCommandResult @throws NotFound if the parameter does not exist. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.get_resource(resource_id=resource_id, params=params) res_interface = self._get_type_interface(res_type) get_result = {} for param in params: getter = get_safe(res_interface, "params.%s.get" % param, None) if getter: get_res = self._call_getter(getter, resource_id, res_type) get_result['param'] = get_res else: get_result['param'] = None return get_result
def test_instrument_simple(self): instrument_model_id = self.create_instrument_model() instrument_agent_id = self.create_instrument_agent(instrument_model_id) instrument_device_id = self.create_instrument_device(instrument_model_id) instrument_agent_instance_id = self.create_instrument_agent_instance(instrument_agent_id, instrument_device_id) raw_dp_id, parsed_dp_id = self.create_instrument_data_products(instrument_device_id) self.start_instrument_agent_instance(instrument_agent_instance_id) agent_process_id = self.poll_instrument_agent_instance(instrument_agent_instance_id, instrument_device_id) agent_client = ResourceAgentClient(instrument_device_id, to_name=agent_process_id, process=FakeProcess()) self.agent_state_transition(agent_client, ResourceAgentEvent.INITIALIZE, ResourceAgentState.INACTIVE) self.agent_state_transition(agent_client, ResourceAgentEvent.GO_ACTIVE, ResourceAgentState.IDLE) self.agent_state_transition(agent_client, ResourceAgentEvent.RUN, ResourceAgentState.COMMAND) dataset_id = self.RR2.find_dataset_id_of_data_product_using_has_dataset(parsed_dp_id) for i in xrange(10): monitor = DatasetMonitor(dataset_id=dataset_id) agent_client.execute_resource(AgentCommand(command=SBE37ProtocolEvent.ACQUIRE_SAMPLE)) if not monitor.wait(): raise AssertionError('Failed on the %ith granule' % i) monitor.stop() rdt = RecordDictionaryTool.load_from_granule(self.data_retriever.retrieve(dataset_id)) self.assertEquals(len(rdt), 10)
def recover_data(self, agent_instance_id, resource_id): res_obj = self.rr.read(resource_id) if res_obj.type_ != RT.InstrumentDevice: log.warn("Ignoring resource because it is not an instrument: %s - %s", res_obj.name, res_obj.type_) self._recover_data_status['ignored'].append("%s (%s)" % (res_obj.name, res_obj.type_)) return self.recover_start = self.CFG.get("recover_start", None) self.recover_end = self.CFG.get("recover_end", None) if self.recover_end is None: raise BadRequest("Missing recover_end parameter") if self.recover_start is None: raise BadRequest("Missing recover_start parameter") try: ia_client = ResourceAgentClient(resource_id, process=self) log.info('Got ia client %s.', str(ia_client)) ia_client.execute_resource(command=AgentCommand(command=DriverEvent.GAP_RECOVERY, args=[self.recover_start, self.recover_end])) self._recover_data_status['success'].append(res_obj.name) except Exception as e: log.warn("Failed to start recovery process for %s", res_obj.name) log.warn("Exception: %s", e) self._recover_data_status['fail'].append("%s (%s)" % (res_obj.name, e))
def test_instrument_simple(self): instrument_model_id = self.create_instrument_model() instrument_agent_id = self.create_instrument_agent(instrument_model_id) instrument_device_id = self.create_instrument_device(instrument_model_id) instrument_agent_instance_id = self.create_instrument_agent_instance(instrument_agent_id, instrument_device_id) raw_dp_id, parsed_dp_id = self.create_instrument_data_products(instrument_device_id) self.start_instrument_agent_instance(instrument_agent_instance_id) agent_process_id = self.poll_instrument_agent_instance(instrument_agent_instance_id, instrument_device_id) agent_client = ResourceAgentClient(instrument_device_id, to_name=agent_process_id, process=FakeProcess()) self.agent_state_transition(agent_client, ResourceAgentEvent.INITIALIZE, ResourceAgentState.INACTIVE) self.agent_state_transition(agent_client, ResourceAgentEvent.GO_ACTIVE, ResourceAgentState.IDLE) self.agent_state_transition(agent_client, ResourceAgentEvent.RUN, ResourceAgentState.COMMAND) dataset_id = self.RR2.find_dataset_id_of_data_product_using_has_dataset(parsed_dp_id) for i in xrange(10): monitor = DatasetMonitor(dataset_id=dataset_id) agent_client.execute_resource(AgentCommand(command=SBE37ProtocolEvent.ACQUIRE_SAMPLE)) if not monitor.event.wait(30): raise AssertionError('Failed on the %ith granule' % i) monitor.stop() rdt = RecordDictionaryTool.load_from_granule(self.data_retriever.retrieve(dataset_id)) self.assertEquals(len(rdt), 10)
def start_agent(self): agent_config = { 'driver_config': self.get_dvr_config(), 'stream_config': {}, 'agent': {'resource_id': self.external_dataset_id}, 'test_mode': True } self._ia_pid = self.container.spawn_process( name=self.EDA_NAME, module=self.EDA_MOD, cls=self.EDA_CLS, config=agent_config) self._ia_client = ResourceAgentClient(self.external_dataset_id, process=FakeProcess()) cmd = AgentCommand(command=ResourceAgentEvent.INITIALIZE) self._ia_client.execute_agent(cmd) cmd = AgentCommand(command=ResourceAgentEvent.GO_ACTIVE) self._ia_client.execute_agent(cmd) cmd = AgentCommand(command=ResourceAgentEvent.RUN) self._ia_client.execute_agent(cmd) cmd = AgentCommand(command=DriverEvent.START_AUTOSAMPLE) self._ia_client.execute_resource(command=cmd) self.start_listener(self.dataset_id)
def setUp(self): self._start_container() self.container.start_rel_from_url(rel_url='res/deploy/r2eoi.yml') # self.eoas_cli = ExternalObservatoryAgentServiceClient() # self.rr_cli = ResourceRegistryServiceClient() self.dams_cli = DataAcquisitionManagementServiceClient() self.dpms_cli = DataProductManagementServiceClient() self._setup_ncom() self._setup_hfr() # eoas_proc = self.container.proc_manager.procs_by_name['external_data_agent_management'] # log.debug("Got EOAS Process: %s" % eoas_proc) self._ncom_agt_cli = ResourceAgentClient( resource_id=self.ncom_ds_id, name='external_observatory_agent', process=FakeProcess()) log.debug("Got a ResourceAgentClient: res_id=%s" % self._ncom_agt_cli.resource_id) self._hfr_agt_cli = ResourceAgentClient( resource_id=self.hfr_ds_id, name='external_observatory_agent', process=FakeProcess()) log.debug("Got a ResourceAgentClient: res_id=%s" % self._hfr_agt_cli.resource_id)
def test_platform_with_instrument_streaming(self): # # The following is with just a single platform and the single # instrument "SBE37_SIM_08", which corresponds to the one on port 4008. # #load the paramaters and the param dicts necesssary for the VEL3D self._load_params() #create the instrument device/agent/mode self._create_instrument_resources() #create the platform device, agent and instance self._create_platform_configuration('LPJBox_CI_Ben_Hall') self.rrclient.create_association(subject=self.platform_device, predicate=PRED.hasDevice, object=self.instrument_device) self._start_platform() # self.addCleanup(self._stop_platform, p_root) # get everything in command mode: self._ping_agent() self._initialize() _ia_client = ResourceAgentClient(self.instrument_device, process=FakeProcess()) state = _ia_client.get_agent_state() log.info("TestPlatformInstrument get_agent_state %s", state) self._go_active() # self._run() gevent.sleep(3) # note that this includes the instrument also getting to the command state # self._stream_instruments() # get client to the instrument: # the i_obj is a DotDict with various pieces captured during the # set-up of the instrument, in particular instrument_device_id #i_obj = self._get_instrument(instr_key) # log.debug("KK creating ResourceAgentClient") # ia_client = ResourceAgentClient(i_obj.instrument_device_id, # process=FakeProcess()) # log.debug("KK got ResourceAgentClient: %s", ia_client) # # # verify the instrument is command state: # state = ia_client.get_agent_state() # log.debug("KK instrument state: %s", state) # self.assertEqual(state, ResourceAgentState.COMMAND) self._reset() self._shutdown()
def start_agent(self): """ Start an instrument agent and client. """ log.info('Creating driver integration test support:') log.info('driver module: %s', DRV_MOD) log.info('driver class: %s', DRV_CLS) log.info('device address: %s', DEV_ADDR) log.info('device port: %s', DEV_PORT) log.info('log delimiter: %s', DELIM) log.info('work dir: %s', WORK_DIR) self._support = DriverIntegrationTestSupport(DRV_MOD, DRV_CLS, DEV_ADDR, DEV_PORT, DATA_PORT, CMD_PORT, PA_BINARY, DELIM, WORK_DIR) # Start port agent, add stop to cleanup. port = self._support.start_pagent() log.info('Port agent started at port %i',port) # Configure driver to use port agent port number. DVR_CONFIG['comms_config'] = { 'addr' : 'localhost', 'port' : port, 'cmd_port' : CMD_PORT } self.addCleanup(self._support.stop_pagent) # Create agent config. agent_config = { 'driver_config' : DVR_CONFIG, 'stream_config' : {}, 'agent' : {'resource_id': IA_RESOURCE_ID}, 'test_mode' : True } # Start instrument agent. log.debug("Starting IA.") container_client = ContainerAgentClient(node=self.container.node, name=self.container.name) ia_pid = container_client.spawn_process(name=IA_NAME, module=IA_MOD, cls=IA_CLS, config=agent_config) log.info('Agent pid=%s.', str(ia_pid)) # Start a resource agent client to talk with the instrument agent. self._ia_client = ResourceAgentClient(IA_RESOURCE_ID, process=FakeProcess()) log.info('Got ia client %s.', str(self._ia_client))
def setUp(self): # Start container self._start_container() self.container.start_rel_from_url('res/deploy/r2deploy.yml') # Now create client to DataAcquisitionManagementService self.client = DataAcquisitionManagementServiceClient(node=self.container.node) self.rrclient = ResourceRegistryServiceClient(node=self.container.node) self.dataproductclient = DataProductManagementServiceClient(node=self.container.node) self.dams_client = DataAcquisitionManagementServiceClient(node=self.container.node) self.pubsub_client = PubsubManagementServiceClient(node=self.container.node) self.processdispatchclient = ProcessDispatcherServiceClient(node=self.container.node) self.data_retriever = DataRetrieverServiceClient(node=self.container.node) self._container_client = ContainerAgentClient(node=self.container.node, name=self.container.name) # Data async and subscription TODO: Replace with new subscriber self._finished_count = None #TODO: Switch to gevent.queue.Queue self._async_finished_result = AsyncResult() self._finished_events_received = [] self._finished_event_subscriber = None self._start_finished_event_subscriber() self.addCleanup(self._stop_finished_event_subscriber) self.DVR_CONFIG = {} self.DVR_CONFIG = { 'dvr_mod' : 'ion.agents.data.handlers.slocum_data_handler', 'dvr_cls' : 'SlocumDataHandler', } self._setup_resources() self.agent_config = { 'driver_config' : self.DVR_CONFIG, 'stream_config' : {}, 'agent' : {'resource_id': self.EDA_RESOURCE_ID}, 'test_mode' : True } datasetagent_instance_obj = IonObject(RT.ExternalDatasetAgentInstance, name='ExternalDatasetAgentInstance1', description='external data agent instance', handler_module=self.EDA_MOD, handler_class=self.EDA_CLS, dataset_driver_config=self.DVR_CONFIG, dataset_agent_config=self.agent_config ) self.dataset_agent_instance_id = self.dams_client.create_external_dataset_agent_instance(external_dataset_agent_instance=datasetagent_instance_obj, external_dataset_agent_id=self.datasetagent_id, external_dataset_id=self.EDA_RESOURCE_ID) #TG: Setup/configure the granule logger to log granules as they're published pid = self.dams_client.start_external_dataset_agent_instance(self.dataset_agent_instance_id) dataset_agent_instance_obj= self.dams_client.read_external_dataset_agent_instance(self.dataset_agent_instance_id) print 'TestBulkIngest: Dataset agent instance obj: = ', dataset_agent_instance_obj # Start a resource agent client to talk with the instrument agent. self._ia_client = ResourceAgentClient('datasetagentclient', name=pid, process=FakeProcess()) log.debug(" test_createTransformsThenActivateInstrument:: got ia client %s", str(self._ia_client))
def ping_agent(self, resource_id=''): """Ping the agent. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.ping_agent(resource_id=resource_id) raise BadRequest("Not implemented for resource type %s" % res_type)
def on_start(self): log.info("Known agents: "+ str(agent_instances)) target_name = agent_instances['user_agent_1'] resource_id = agent_resources[target_name] self.rac = ResourceAgentClient(resource_id=resource_id, process=self) self.trigger_func = threading.Thread(target=self._trigger_func) self.trigger_func.start()
def set_agent(self, resource_id='', params=None): """Set the value of the given agent parameters. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.set_agent(resource_id=resource_id, params=params) raise BadRequest("Not implemented for resource type %s" % res_type)
def get_agent_state(self, resource_id=''): """Return the current resource agent common state. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.get_agent_state(resource_id=resource_id) raise BadRequest("Not implemented for resource type %s" % res_type)
def execute_agent(self, resource_id='', command=None): """Execute command on the agent. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.execute_agent(resource_id=resource_id, command=command) raise BadRequest("Not implemented for resource type %s" % res_type)
def get_resource_state(self, resource_id=''): """Return the current resource specific state, if available. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.get_resource_state(resource_id=resource_id) raise BadRequest("Not implemented for resource type %s", res_type)
def _process_cmd_agent_execute(resource_id, res_obj=None): agent_cmd = get_arg('agentcmd') from pyon.agent.agent import ResourceAgentClient from interface.objects import AgentCommand rac = ResourceAgentClient(process=containerui_instance, resource_id=resource_id) ac = AgentCommand(command=agent_cmd) res = rac.execute_agent(ac) res_dict = get_value_dict(res) res_str = get_formatted_value(res_dict, fieldtype="dict") return res_str
def test_agent_registration(self): self._start_container() idev = IonObject("TestInstrument", name="any_resource") idev_id, _ = self.container.resource_registry.create(idev) config = dict(agent=dict(resource_id=idev_id)) pid1 = self.container.spawn_process('agent1', 'pyon.agent.test.test_agent', 'SampleAgent', config) rac = ResourceAgentClient(idev_id) rac_pid = rac.get_agent_process_id() rac_de = rac.get_agent_directory_entry() self.assertEquals(rac_pid, pid1) # Now fake a second agent directory entry that wasn't cleaned up self.container.directory.register( "/Agents", "fake_pid", **dict(name="agent1", container=self.container.id, resource_id=idev_id, agent_id="fake")) entries = self.container.directory.find_by_value( '/Agents', 'resource_id', idev_id) self.assertEquals(len(entries), 2) rac = ResourceAgentClient(idev_id) rac_pid1 = rac.get_agent_process_id() self.assertEquals(rac_pid1, "fake_pid") # Check cleanup side effect of agent client entries = self.container.directory.find_by_value( '/Agents', 'resource_id', idev_id) self.assertEquals(len(entries), 1) # Now restore the original process id self.container.directory.register( "/Agents", pid1, **dict(name="agent1", container=self.container.id, resource_id=idev_id, agent_id=rac_de.attributes["agent_id"])) rac = ResourceAgentClient(idev_id) rac_pid1 = rac.get_agent_process_id() self.assertEquals(rac_pid1, pid1) # Check cleanup side effect of agent client entries = self.container.directory.find_by_value( '/Agents', 'resource_id', idev_id) self.assertEquals(len(entries), 1) self.container.terminate_process(pid1)
def start_agent(self, agent_instance_id, resource_id): if not agent_instance_id or not resource_id: log.warn("Could not %s agent %s for device %s", self.op, agent_instance_id, resource_id) return res_obj = self.rr.read(resource_id) log.info('Starting agent...') if res_obj.type_ == RT.ExternalDatasetAgentInstance or res_obj == RT.ExternalDataset: dams = DataAcquisitionManagementServiceProcessClient(process=self) dams.start_external_dataset_agent_instance(agent_instance_id) elif res_obj.type_ == RT.InstrumentDevice: ims = InstrumentManagementServiceClient() ims.start_instrument_agent_instance(agent_instance_id) elif res_obj.type_ == RT.PlatformDevice: ims = InstrumentManagementServiceClient() ims.start_platform_agent_instance(agent_instance_id) else: BadRequest("Attempt to start unsupported agent type: %s", res_obj.type_) log.info('Agent started!') activate = self.CFG.get("activate", True) if activate: log.info('Activating agent...') client = ResourceAgentClient(resource_id, process=self) client.execute_agent(AgentCommand(command=ResourceAgentEvent.INITIALIZE)) client.execute_agent(AgentCommand(command=ResourceAgentEvent.GO_ACTIVE)) client.execute_agent(AgentCommand(command=ResourceAgentEvent.RUN)) client.execute_resource(command=AgentCommand(command=DriverEvent.START_AUTOSAMPLE)) log.info('Agent active!')
def rcmd_makesay(self, who, what, *args, **kwargs): log.info("Makesay: Relaying command %s to %s" % (what, who)) target_name = agent_instances.get(str(who), None) if target_name: self.rac = ResourceAgentClient(resource_id='R', name=target_name, process=self) cmd = AgentCommand(command='say', args=[what]) res = self.rac.execute(cmd) return "OK" else: return "UNKNOWN AGENT"
def start_agent(self, agent_instance_id, resource_id): if not agent_instance_id or not resource_id: log.warn("Could not op=%s agent %s for device %s", self.op, agent_instance_id, resource_id) return res_obj = self.rr.read(resource_id) ai_obj = self.rr.read(agent_instance_id) try: client = ResourceAgentClient(resource_id, process=self) if self.force: log.warn("Agent for resource %s seems running - continuing", resource_id) if self.autoclean: self.cleanup_agent(agent_instance_id, resource_id) else: log.warn("Agent for resource %s seems running", resource_id) return except NotFound: pass # This is expected log.info('Starting agent...') if ai_obj.type_ == RT.ExternalDatasetAgentInstance: dams = DataAcquisitionManagementServiceProcessClient(process=self) dams.start_external_dataset_agent_instance(agent_instance_id, headers=self._get_system_actor_headers(), timeout=self.timeout) elif ai_obj.type_ == RT.InstrumentAgentInstance: ims = InstrumentManagementServiceProcessClient(process=self) ims.start_instrument_agent_instance(agent_instance_id, headers=self._get_system_actor_headers(), timeout=self.timeout) elif ai_obj.type_ == RT.PlatformAgentInstance: ims = InstrumentManagementServiceProcessClient(process=self) ims.start_platform_agent_instance(agent_instance_id, headers=self._get_system_actor_headers(), timeout=self.timeout) else: BadRequest("Attempt to start unsupported agent type: %s", ai_obj.type_) log.info('Agent started!') activate = self.CFG.get("activate", True) if activate: log.info('Activating agent...') client = ResourceAgentClient(resource_id, process=self) client.execute_agent(AgentCommand(command=ResourceAgentEvent.INITIALIZE), headers=self._get_system_actor_headers(), timeout=self.timeout) client.execute_agent(AgentCommand(command=ResourceAgentEvent.GO_ACTIVE), headers=self._get_system_actor_headers(), timeout=self.timeout) client.execute_agent(AgentCommand(command=ResourceAgentEvent.RUN), headers=self._get_system_actor_headers(), timeout=self.timeout) client.execute_resource(command=AgentCommand(command=DriverEvent.START_AUTOSAMPLE), headers=self._get_system_actor_headers(), timeout=self.timeout) log.info('Agent in auto-sample mode!')
def get_agent_state(self, resource_id=''): """Return the current resource agent common state. @param resource_id The id of the resource agennt. @retval A str containing the current agent state. @param resource_id str @retval result str """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.get_agent_state(resource_id=resource_id) raise BadRequest("Not implemented for resource type %s" % res_type)
def ping_agent(self, resource_id=''): """Ping the agent. @param resource_id The id of the resource agennt. @retval A str containing a string representation of the agent and a timestamp. @param resource_id str @retval result str """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.ping_agent(resource_id=resource_id) raise BadRequest("Not implemented for resource type %s" % res_type)
def _start_agent(self): """ """ container_client = ContainerAgentClient(node=self.container.node, name=self.container.name) self._ia_pid = container_client.spawn_process( name=IA_NAME, module=IA_MOD, cls=IA_CLS, config=self._agent_config) log.info('Started instrument agent pid=%s.', str(self._ia_pid)) # Start a resource agent client to talk with the instrument agent. self._ia_client = None self._ia_client = ResourceAgentClient(IA_RESOURCE_ID, process=FakeProcess()) log.info('Got instrument agent client %s.', str(self._ia_client))
def get_resource_state(self, resource_id=''): """Return the current resource specific state, if available. @param resource_id The id of the resource agennt. @retval A str containing the current resource specific state. @param resource_id str @retval result str @throws NotFound if the resource does not utilize a specific state machine. @throws ResourceError if the resource failed while trying to get the state. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.get_resource_state(resource_id=resource_id) raise BadRequest("Not implemented for resource type %s", res_type)
def on_start(self): log.info("Known agents: " + str(agent_instances)) target_name = agent_instances["user_agent_1"] self.rac = ResourceAgentClient(resource_id="res_id", name=target_name, process=self) self.trigger_func = threading.Thread(target=self._trigger_func) self.trigger_func.start()
def start_agent(self): agent_config = { 'driver_config': self.get_dvr_config(), 'stream_config': {}, 'agent': {'resource_id': self.external_dataset_id}, 'test_mode': True } _ia_pid = self.container.spawn_process( name=self.EDA_NAME, module=self.EDA_MOD, cls=self.EDA_CLS, config=agent_config) self._ia_client = ResourceAgentClient(self.external_dataset_id, process=FakeProcess()) cmd = AgentCommand(command=ResourceAgentEvent.INITIALIZE) self._ia_client.execute_agent(cmd) cmd = AgentCommand(command=ResourceAgentEvent.GO_ACTIVE) self._ia_client.execute_agent(cmd) cmd = AgentCommand(command=ResourceAgentEvent.RUN) self._ia_client.execute_agent(cmd) cmd = AgentCommand(command=DriverEvent.START_AUTOSAMPLE) self._ia_client.execute_resource(command=cmd) self.start_listener(self.dataset_id)
def setUp(self): self._start_container() self.container.start_rel_from_url(rel_url='res/deploy/r2eoi.yml') self.dams_cli = DataAcquisitionManagementServiceClient() self.dpms_cli = DataProductManagementServiceClient() eda = ExternalDatasetAgent() self.eda_id = self.dams_cli.create_external_dataset_agent(eda) eda_inst = ExternalDatasetAgentInstance() self.eda_inst_id = self.dams_cli.create_external_dataset_agent_instance(eda_inst, external_dataset_agent_id=self.eda_id) self._setup_ncom() proc_name = self.ncom_ds_id+'_worker' config = {} config['process']={'name':proc_name,'type':'agent'} config['process']['eoa']={'dataset_id':self.ncom_ds_id} pid = self.container.spawn_process(name=proc_name, module='ion.agents.eoi.external_observatory_agent', cls='ExternalObservatoryAgent', config=config) queue_id = "%s.%s" % (self.container.id, pid) log.debug("Spawned worker process ==> proc_name: %s\tproc_id: %s\tqueue_id: %s" % (proc_name, pid, queue_id)) self._agent_cli = ResourceAgentClient(self.ncom_ds_id, name=pid, process=FakeProcess()) log.debug("Got a ResourceAgentClient: res_id=%s" % self._agent_cli.resource_id)
def _start_platform(self): """ Starts the given platform waiting for it to transition to the UNINITIALIZED state (note that the agent starts in the LAUNCHING state). More in concrete the sequence of steps here are: - prepares subscriber to receive the UNINITIALIZED state transition - launches the platform process - waits for the start of the process - waits for the transition to the UNINITIALIZED state """ ############################################################## # prepare to receive the UNINITIALIZED state transition: async_res = AsyncResult() def consume_event(evt, *args, **kwargs): log.debug("Got ResourceAgentStateEvent %s from origin %r", evt.state, evt.origin) if evt.state == PlatformAgentState.UNINITIALIZED: async_res.set(evt) # start subscriber: sub = EventSubscriber(event_type="ResourceAgentStateEvent", origin=self.platform_device_id, callback=consume_event) sub.start() log.info( "registered event subscriber to wait for state=%r from origin %r", PlatformAgentState.UNINITIALIZED, self.platform_device_id) #self._event_subscribers.append(sub) sub._ready_event.wait(timeout=EVENT_TIMEOUT) ############################################################## # now start the platform: agent_instance_id = self.platform_agent_instance_id log.debug("about to call start_platform_agent_instance with id=%s", agent_instance_id) pid = self.imsclient.start_platform_agent_instance( platform_agent_instance_id=agent_instance_id) log.debug("start_platform_agent_instance returned pid=%s", pid) #wait for start agent_instance_obj = self.imsclient.read_platform_agent_instance( agent_instance_id) gate = AgentProcessStateGate(self.processdispatchclient.read_process, self.platform_device_id, ProcessStateEnum.RUNNING) self.assertTrue( gate. await (90), "The platform agent instance did not spawn in 90 seconds") # Start a resource agent client to talk with the agent. self._pa_client = ResourceAgentClient(self.platform_device_id, name=gate.process_id, process=FakeProcess()) log.debug("got platform agent client %s", str(self._pa_client)) ############################################################## # wait for the UNINITIALIZED event: async_res.get(timeout=self._receive_timeout)
def set_resource(self, resource_id='', params=None): """Set the value of the given resource parameters. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.set_resource(resource_id=resource_id, params=params) res_interface = self._get_type_interface(res_type) for param in params: setter = get_safe(res_interface, "params.%s.set" % param, None) if setter: self._call_setter(setter, params[param], resource_id, res_type) else: log.warn("set_resource(): param %s not defined", param)
def setUp(self): """ Initialize test members. Start port agent. Start container and client. Start streams and subscribers. Start agent, client. """ TrhphTestCase.setUp(self) # Start port agent, add stop to cleanup. self._pagent = None self._start_pagent() self.addCleanup(self._stop_pagent) # Start container. self._start_container() # Bring up services in a deploy file (no need to message) self.container.start_rel_from_url("res/deploy/r2dm.yml") # Start data suscribers, add stop to cleanup. # Define stream_config. self._no_samples = None self._async_data_result = AsyncResult() self._data_greenlets = [] self._stream_config = {} self._samples_received = [] self._data_subscribers = [] self._start_data_subscribers() self.addCleanup(self._stop_data_subscribers) # Start event subscribers, add stop to cleanup. self._no_events = None self._async_event_result = AsyncResult() self._events_received = [] self._event_subscribers = [] self._start_event_subscribers() self.addCleanup(self._stop_event_subscribers) # Create agent config. agent_config = { "driver_config": DVR_CONFIG, "stream_config": self._stream_config, "agent": {"resource_id": IA_RESOURCE_ID}, "test_mode": True, } # Start instrument agent. self._ia_pid = None log.debug("TestInstrumentAgent.setup(): starting IA.") container_client = ContainerAgentClient(node=self.container.node, name=self.container.name) self._ia_pid = container_client.spawn_process(name=IA_NAME, module=IA_MOD, cls=IA_CLS, config=agent_config) log.info("Agent pid=%s.", str(self._ia_pid)) # Start a resource agent client to talk with the instrument agent. self._ia_client = None self._ia_client = ResourceAgentClient(IA_RESOURCE_ID, process=FakeProcess()) log.info("Got ia client %s.", str(self._ia_client))
def start_instrument_agent_process(container, stream_config={}, resource_id=IA_RESOURCE_ID, resource_name=IA_NAME, org_name=None, message_headers=None): log.info("foobar") # Create agent config. agent_config = { 'driver_config' : DVR_CONFIG, 'stream_config' : stream_config, 'agent' : {'resource_id': resource_id}, 'test_mode' : True } if org_name is not None: agent_config['org_name'] = org_name # Start instrument agent. log.info("TestInstrumentAgent.setup(): starting IA.") container_client = ContainerAgentClient(node=container.node, name=container.name) log.info("Agent setup") ia_pid = container_client.spawn_process(name=resource_name, module=IA_MOD, cls=IA_CLS, config=agent_config, headers=message_headers) log.info('Agent pid=%s.', str(ia_pid)) # Start a resource agent client to talk with the instrument agent. ia_client = ResourceAgentClient(resource_id, process=FakeProcess()) log.info('Got ia client %s.', str(ia_client)) return ia_client
def setup_platform_simulator_and_instruments(self): self._set_receive_timeout() # Create a platform in the test environment p_root = self._create_single_platform() # Create instruments and assign to platform for mission in self.mission.mission_entries: for instrument_id in mission['instrument_id']: # create only if not already created: if instrument_id not in self._setup_instruments: i_obj = self._create_instrument(instrument_id, start_port_agent=True) self._assign_instrument_to_platform(i_obj, p_root) # Start the platform self._start_platform(p_root) self.addCleanup(self._stop_platform, p_root) # self.addCleanup(self._run_shutdown_commands) self._instruments = {} self._instruments.update({self.PLATFORM_ID: self._pa_client}) # Now get instrument clients for each instrument for mission in self.mission.mission_entries: for instrument_id in mission['instrument_id']: instrument_device_id = self._setup_instruments[instrument_id][ 'instrument_device_id'] # Start a resource agent client to talk with each instrument agent. ia_client = ResourceAgentClient(instrument_device_id, process=FakeProcess()) # make a dictionary storing the instrument ids and client objects self._instruments.update({instrument_id: ia_client})
def prepare(self, will_launch=True): """ Prepare (validate) an agent for launch, fetching all associated resources @param will_launch - whether the running status should be checked -- set false if just generating config """ assert self.agent_instance_obj # fetch caches just in time if any([ not self.RR2.has_cached_predicate(x) for x in self._predicates_to_cache() ]): self._update_cached_predicates() if any([ not self.RR2.has_cached_resource(x) for x in self._resources_to_cache() ]): self._update_cached_resources() # validate the associations, then pick things up self._collect_agent_instance_associations() if will_launch: # if there is an agent pid then assume that a drive is already started agent_process_id = ResourceAgentClient._get_agent_process_id( self._get_device()._id) if agent_process_id: raise BadRequest( "Agent Instance already running for this device pid: %s" % str(agent_process_id)) self.will_launch = will_launch return self.generate_config()
def prepare(self, will_launch=True): """ Prepare (validate) an agent for launch, fetching all associated resources @param will_launch - whether the running status should be checked -- set false if just generating config """ assert self.agent_instance_obj # fetch caches just in time if any([not self.RR2.has_cached_predicate(x) for x in self._predicates_to_cache()]): self._update_cached_predicates() if any([not self.RR2.has_cached_resource(x) for x in self._resources_to_cache()]): self._update_cached_resources() # validate the associations, then pick things up self._collect_agent_instance_associations() if will_launch: # if there is an agent pid then assume that a drive is already started agent_process_id = ResourceAgentClient._get_agent_process_id(self._get_device()._id) if agent_process_id: raise BadRequest("Agent Instance already running for this device pid: %s" % str(agent_process_id)) self.will_launch = will_launch config = self.generate_config() return config
def setUp(self): self._start_container() self.container.start_rel_from_url("res/deploy/r2deploy.yml") self._pubsub_client = PubsubManagementServiceClient(node=self.container.node) self.PLATFORM_CONFIG = {"platform_id": self.PLATFORM_ID, "driver_config": DVR_CONFIG} # Start data suscribers, add stop to cleanup. # Define stream_config. self._async_data_result = AsyncResult() self._data_greenlets = [] self._stream_config = {} self._samples_received = [] self._data_subscribers = [] self._start_data_subscribers() self.addCleanup(self._stop_data_subscribers) self._agent_config = { "agent": {"resource_id": PA_RESOURCE_ID}, "stream_config": self._stream_config, # pass platform config here "platform_config": self.PLATFORM_CONFIG, } log.debug("launching with agent_config=%s", str(self._agent_config)) self._launcher = LauncherFactory.createLauncher() self._pid = self._launcher.launch(self.PLATFORM_ID, self._agent_config) log.debug("LAUNCHED PLATFORM_ID=%r", self.PLATFORM_ID) # Start a resource agent client to talk with the agent. self._pa_client = ResourceAgentClient(PA_RESOURCE_ID, process=FakeProcess()) log.info("Got pa client %s." % str(self._pa_client))
def on_start(self): rr = self.container.resource_registry name = self.CFG.get("dataset") if name: # We are looking for an external dataset! log.trace("Looking for an external dataset with name %s", name) objects,_ = rr.find_resources(RT.ExternalDataset) filtered_objs = [obj for obj in objects if obj.name == name] if (filtered_objs) == []: raise ConfigNotFound("No appropriate ExternalDataset objects loaded") external_dataset = filtered_objs[0] instance_id = rr.read_object(subject=external_dataset._id, predicate=PRED.hasAgentInstance, object_type=RT.ExternalDatasetAgentInstance, id_only=True) dams = get_service_registry().services['data_acquisition_management'].client(process=self) dams.start_external_dataset_agent_instance(instance_id) client = ResourceAgentClient(external_dataset._id, process=FakeProcess()) else: name = self.CFG.get("instrument") if name: # We are looking for an instrument device! log.trace("Looking for an instrument device with name %s", name) objects,_ = rr.find_resources(RT.InstrumentDevice) filtered_objs = [obj for obj in objects if obj.name == name] if (filtered_objs) == []: raise ConfigNotFound("No appropriate InstrumentDevice objects loaded") instrument_device = filtered_objs[0] log.trace("Found instrument device: %s", instrument_device) instance_id = rr.read_object(subject=instrument_device._id, predicate=PRED.hasAgentInstance, object_type=RT.ExternalDatasetAgentInstance, id_only=True) dams = get_service_registry().services['data_acquisition_management'].client(process=self) proc_id = dams.start_external_dataset_agent_instance(instance_id) client = ResourceAgentClient(instrument_device._id, process=FakeProcess()) else: raise ConfigNotFound("Could not find dataset or instrument_device value name") log.info('Activating agent...') client.execute_agent(AgentCommand(command=ResourceAgentEvent.INITIALIZE)) client.execute_agent(AgentCommand(command=ResourceAgentEvent.GO_ACTIVE)) client.execute_agent(AgentCommand(command=ResourceAgentEvent.RUN)) client.execute_resource(command=AgentCommand(command=DriverEvent.START_AUTOSAMPLE)) log.info('Now reading: %s', name)
def __init__(self, read_process_fn=None, resource_id='', desired_state=None, *args, **kwargs): agent_process_id = ResourceAgentClient._get_agent_process_id(resource_id) super(AgentProcessStateGate, self).__init__(read_process_fn, agent_process_id, desired_state, *args, **kwargs)
def set_agent(self, resource_id='', params=None): """Set the value of the given agent parameters. @param resource_id The id of the resource agennt. @param params A dict of resource parameter name-value pairs. @throws BadRequest if the command was malformed. @throws NotFound if a parameter is not supported by the resource. @param resource_id str @param params dict @throws BadRequest if the command was malformed. @throws NotFound if the parameter does not exist. """ res_type = self._get_resource_type(resource_id) if self._has_agent(res_type): rac = ResourceAgentClient(resource_id=resource_id) return rac.set_agent(resource_id=resource_id, params=params) raise BadRequest("Not implemented for resource type %s" % res_type)