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 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 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 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 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 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 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 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 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)
示例#12
0
    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',
                                            'ion.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 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 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 get_resource(self, resource_id='', params=None):
        """Return the value of the given resource parameter.
        """
        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 get_capabilities(self, resource_id='', current_state=True):
        """Introspect for agent capabilities.
        """
        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 get_resource(self, resource_id='', params=None):
        """Return the value of the given resource parameter.
        """
        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 get_capabilities(self, resource_id='', current_state=True):
        """Introspect for agent capabilities.
        """
        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 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
示例#20
0
    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', 'ion.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)