コード例 #1
0
ファイル: oms_util.py プロジェクト: n1ywb/mi-instrument
    def build_network_definition(rsn_oms):
        """
        Creates and returns a NetworkDefinition object reflecting the platform
        network definition reported by the RSN OMS Client object.
        The returned object will have as root the PlatformNode corresponding to the
        actual root of the whole newtork. You can use the `pnodes` property to
        access any node.

        @param rsn_oms RSN OMS Client object.
        @return NetworkDefinition object
        """
        if log.isEnabledFor(logging.DEBUG):
            log.debug("build_network_definition. rsn_oms class: %s",
                      rsn_oms.__class__.__name__)

        # platform types:
        platform_types = rsn_oms.config.get_platform_types()
        if log.isEnabledFor(logging.DEBUG):
            log.debug("got platform_types %s", str(platform_types))

        # platform map:
        map = rsn_oms.config.get_platform_map()
        if log.isEnabledFor(logging.DEBUG):
            log.debug("got platform map %s", str(map))

        # build topology:
        pnodes = NetworkUtil.create_node_network(map)
        dummy_root = pnodes['']
        root_pnode = pnodes[dummy_root.subplatforms.keys()[0]]
        if log.isEnabledFor(logging.DEBUG):
            log.debug("topology's root platform_id=%r", root_pnode.platform_id)

        # now, populate the attributes and ports for the platforms

        def build_attributes_and_ports(pnode):
            """
            Recursive routine to call set_attributes and set_ports on each pnode.
            """
            set_attributes(pnode)
            set_ports(pnode)

            for sub_platform_id, sub_pnode in pnode.subplatforms.iteritems():
                build_attributes_and_ports(sub_pnode)

        def set_attributes(pnode):
            platform_id = pnode.platform_id
            attr_infos = rsn_oms.attr.get_platform_attributes(platform_id)
            if not isinstance(attr_infos, dict):
                raise PlatformDriverException(
                    "%r: get_platform_attributes returned: %s" % (
                    platform_id, attr_infos))

            if log.isEnabledFor(logging.TRACE):
                log.trace("%r: attr_infos: %s", platform_id, attr_infos)

            if not platform_id in attr_infos:
                raise PlatformDriverException(
                    "%r: get_platform_attributes response does not "
                    "include entry for platform_id: %s" %(
                    platform_id, attr_infos))

            ret_infos = attr_infos[platform_id]
            for attrName, attr_defn in ret_infos.iteritems():
                attr = AttrNode(attrName, attr_defn)
                pnode.add_attribute(attr)

        def set_ports(pnode):
            platform_id = pnode.platform_id
            port_infos = rsn_oms.port.get_platform_ports(platform_id)
            if not isinstance(port_infos, dict):
                raise PlatformDriverException(
                    "%r: get_platform_ports response is not a dict: %s" % (
                    platform_id, port_infos))

            if log.isEnabledFor(logging.TRACE):
                log.trace("%r: port_infos: %s", platform_id, port_infos)

            if not platform_id in port_infos:
                raise PlatformDriverException(
                    "%r: get_platform_ports response does not include "
                    "platform_id: %s" % (platform_id, port_infos))

            ports = port_infos[platform_id]

            if not isinstance(ports, dict):
                raise PlatformDriverException(
                    "%r: get_platform_ports: entry for platform_id is "
                    "not a dict: %s" % (platform_id, ports))

            for port_id, dic in ports.iteritems():
                port = PortNode(port_id)
                port.set_state(dic['state'])
                pnode.add_port(port)

                # add connected instruments:
                instrs_res = rsn_oms.instr.get_connected_instruments(platform_id, port_id)
                if not isinstance(instrs_res, dict):
                    log.warn("%r: port_id=%r: get_connected_instruments "
                             "response is not a dict: %s" % (platform_id, port_id, instrs_res))
                    continue

                if log.isEnabledFor(logging.TRACE):
                    log.trace("%r: port_id=%r: get_connected_instruments "
                              "returned: %s" % (platform_id, port_id, instrs_res))

                if not platform_id in instrs_res:
                    raise PlatformDriverException(
                        "%r: port_id=%r: get_connected_instruments response"
                        "does not have entry for platform_id: %s" % (
                        platform_id, ports))

                if not port_id in instrs_res[platform_id]:
                    raise PlatformDriverException(
                        "%r: port_id=%r: get_connected_instruments response "
                        "for platform_id does not have entry for port_id: %s" % (
                        platform_id, port_id, instrs_res[platform_id]))

                instr = instrs_res[platform_id][port_id]
                for instrument_id, attrs in instr.iteritems():
                    port.add_instrument(InstrumentNode(instrument_id, attrs))

        # call the recursive routine
        build_attributes_and_ports(root_pnode)

        # we got our whole network including platform attributes and ports.

        # and finally create and return NetworkDefinition:
        ndef = NetworkDefinition()
        ndef._platform_types = platform_types
        ndef._pnodes = pnodes
        ndef._dummy_root = dummy_root
        return ndef
コード例 #2
0
    def create_network_definition_from_ci_config(CFG):
        """
        Creates a NetworkDefinition object by traversing the given CI agent
        configuration dictionary.

        @param CFG CI agent configuration
        @return A NetworkDefinition object

        @raise PlatformDefinitionException device_type is not 'PlatformDevice'
        """

        # verify CFG corresponds to PlatformDevice:
        device_type = CFG.get("device_type", None)
        if 'PlatformDevice' != device_type:
            raise PlatformDefinitionException("Expecting device_type to be "
                                              "'PlatformDevice'. Got %r" %
                                              device_type)

        ndef = NetworkDefinition()
        ndef._pnodes = {}

        def create_platform_node(platform_id, platform_types=None, CFG=None):
            if platform_id in ndef.pnodes:
                raise PlatformDefinitionException(
                    "create_platform_node(): platform_id %r not in ndef.pnodes"
                    % platform_id)
            pn = PlatformNode(platform_id, platform_types, CFG)
            ndef.pnodes[platform_id] = pn
            return pn

        ndef._dummy_root = create_platform_node(platform_id='')

        def _get_platform_types(CFG):
            """
            Constructs:
              - ndef._platform_types, {platform_type : description} dict
            """
            ndef._platform_types = {}
            #
            # TODO implement once this information is provided in the CI config

        _get_platform_types(CFG)

        def _add_attrs_to_platform_node(attrs, pn):
            for attr_defn in attrs:
                attr_id = _get_attr_id(attr_defn)
                if not 'monitor_cycle_seconds' in attr_defn:
                    raise PlatformDefinitionException(
                        "_add_attrs_to_platform_node(): 'monitor_cycle_seconds' not in attr_defn"
                    )
                if not 'units' in attr_defn:
                    raise PlatformDefinitionException(
                        "_add_attrs_to_platform_node(): 'units' not in attr_defn"
                    )
                pn.add_attribute(AttrNode(attr_id, attr_defn))

        def _add_ports_to_platform_node(ports, pn):
            for port_info in ports:
                if not 'port_id' in port_info:
                    raise PlatformDefinitionException(
                        "_add_ports_to_platform_node(): 'port_id' not in port_info"
                    )
                port_id = port_info['port_id']
                port = PortNode(port_id)
                pn.add_port(port)

        def build_platform_node(CFG, parent_node):
            platform_config = CFG.get('platform_config', {})
            platform_id = platform_config.get('platform_id', None)
            platform_types = platform_config.get('platform_types', [])

            driver_config = CFG.get('driver_config', {})
            attributes = driver_config.get('attributes', {})
            ports = driver_config.get('ports', {})

            if not platform_id:
                raise PlatformDefinitionException(
                    "missing CFG.platform_config.platform_id")

            if not driver_config:
                raise PlatformDefinitionException("missing CFG.driver_config")

            for platform_type in platform_types:
                if not platform_type in ndef._platform_types:
                    raise PlatformDefinitionException(
                        "%r not in defined platform types: %s" %
                        (platform_type, ndef._platform_types))

            pn = create_platform_node(platform_id, platform_types, CFG)
            parent_node.add_subplatform(pn)

            # attributes:
            _add_attrs_to_platform_node(attributes.itervalues(), pn)

            # ports:
            # TODO(OOIION-1495) the following was commented out,
            # but we need to capture the ports, at least under the current logic.
            # remove until network checkpoint needs are defined.
            # port info can be retrieve from active deployment
            _add_ports_to_platform_node(ports.itervalues(), pn)

            # children:
            for child_CFG in CFG.get("children", {}).itervalues():
                device_type = child_CFG.get("device_type", None)
                if device_type == 'PlatformDevice':
                    build_platform_node(child_CFG, pn)

                elif device_type == 'InstrumentDevice':
                    build_instrument_node(child_CFG, pn)

            return pn

        def build_instrument_node(CFG, parent_node):
            # use CFG.agent.resource_id as the instrument_id:
            agent = CFG.get('agent', {})
            instrument_id = agent.get('resource_id', None)

            if not instrument_id:
                raise PlatformDefinitionException(
                    "missing CFG.agent.resource_id for instrument")

            inn = InstrumentNode(instrument_id, CFG=CFG)
            parent_node.add_instrument(inn)

        # Now, build the whole network:
        build_platform_node(CFG, ndef._dummy_root)

        return ndef
コード例 #3
0
    def create_network_definition_from_ci_config(CFG):
        """
        Creates a NetworkDefinition object by traversing the given CI agent
        configuration dictionary.

        @param CFG CI agent configuration
        @return A NetworkDefinition object

        @raise PlatformDefinitionException device_type is not 'PlatformDevice'
        """

        # verify CFG corresponds to PlatformDevice:
        device_type = CFG.get("device_type", None)
        if 'PlatformDevice' != device_type:
            raise PlatformDefinitionException("Expecting device_type to be "
                                              "'PlatformDevice'. Got %r" % device_type)

        ndef = NetworkDefinition()
        ndef._pnodes = {}

        def create_platform_node(platform_id, platform_types=None, CFG=None):
            if platform_id in ndef.pnodes:
                raise PlatformDefinitionException("create_platform_node(): platform_id %r not in ndef.pnodes" % platform_id)
            pn = PlatformNode(platform_id, platform_types, CFG)
            ndef.pnodes[platform_id] = pn
            return pn

        ndef._dummy_root = create_platform_node(platform_id='')

        def _get_platform_types(CFG):
            """
            Constructs:
              - ndef._platform_types, {platform_type : description} dict
            """
            ndef._platform_types = {}
            #
            # TODO implement once this information is provided in the CI config

        _get_platform_types(CFG)

        def _add_attrs_to_platform_node(attrs, pn):
            for attr_defn in attrs:
                attr_id = _get_attr_id(attr_defn)
                if not 'monitor_cycle_seconds' in attr_defn:
                    raise PlatformDefinitionException("_add_attrs_to_platform_node(): 'monitor_cycle_seconds' not in attr_defn")
                if not 'units' in attr_defn:
                    raise PlatformDefinitionException("_add_attrs_to_platform_node(): 'units' not in attr_defn")
                pn.add_attribute(AttrNode(attr_id, attr_defn))

        def _add_ports_to_platform_node(ports, pn):
            for port_info in ports:
                if not 'port_id' in port_info:
                    raise PlatformDefinitionException("_add_ports_to_platform_node(): 'port_id' not in port_info")
                port_id = port_info['port_id']
                port = PortNode(port_id)
                pn.add_port(port)

        def build_platform_node(CFG, parent_node):
            platform_config = CFG.get('platform_config', {})
            platform_id     = platform_config.get('platform_id', None)
            platform_types  = platform_config.get('platform_types', [])

            driver_config  = CFG.get('driver_config', {})
            attributes     = driver_config.get('attributes', {})
            ports          = driver_config.get('ports', {})

            if not platform_id:
                raise PlatformDefinitionException("missing CFG.platform_config.platform_id")

            if not driver_config:
                raise PlatformDefinitionException("missing CFG.driver_config")

            for platform_type in platform_types:
                if not platform_type in ndef._platform_types:
                    raise PlatformDefinitionException(
                        "%r not in defined platform types: %s" %(
                        platform_type, ndef._platform_types))

            pn = create_platform_node(platform_id, platform_types, CFG)
            parent_node.add_subplatform(pn)

            # attributes:
            _add_attrs_to_platform_node(attributes.itervalues(), pn)

            # ports:
            # TODO(OOIION-1495) the following was commented out,
            # but we need to capture the ports, at least under the current logic.
            # remove until network checkpoint needs are defined.
            # port info can be retrieve from active deployment
            _add_ports_to_platform_node(ports.itervalues(), pn)

            # children:
            for child_CFG in CFG.get("children", {}).itervalues():
                device_type = child_CFG.get("device_type", None)
                if device_type == 'PlatformDevice':
                    build_platform_node(child_CFG, pn)

                elif device_type == 'InstrumentDevice':
                    build_instrument_node(child_CFG, pn)

            return pn

        def build_instrument_node(CFG, parent_node):
            # use CFG.agent.resource_id as the instrument_id:
            agent = CFG.get('agent', {})
            instrument_id = agent.get('resource_id', None)

            if not instrument_id:
                raise PlatformDefinitionException("missing CFG.agent.resource_id for instrument")

            inn = InstrumentNode(instrument_id, CFG=CFG)
            parent_node.add_instrument(inn)

        # Now, build the whole network:
        build_platform_node(CFG, ndef._dummy_root)

        return ndef
コード例 #4
0
    def create_network_definition_from_ci_config(CFG):
        """
        Creates a NetworkDefinition object by traversing the given CI agent
        configuration dictionary.

        @param CFG CI agent configuration
        @return A NetworkDefinition object

        @raise NetworkDefinitionException device_type is not 'PlatformDevice'
        """

        # verify CFG corresponds to PlatformDevice:
        device_type = CFG.get("device_type", None)
        _require('PlatformDevice' == device_type,
                 "Expecting device_type to be 'PlatformDevice'. Got %r" % device_type)

        ndef = NetworkDefinition()
        ndef._pnodes = {}

        def create_platform_node(platform_id, CFG=None):
            _require(not platform_id in ndef.pnodes,
                     "create_platform_node(): platform_id %r not in ndef.pnodes" % platform_id)
            pn = PlatformNode(platform_id, CFG)
            ndef.pnodes[platform_id] = pn
            return pn

        ndef._dummy_root = create_platform_node(platform_id='')

        def _add_attrs_to_platform_node(attrs, pn):
            for attr_defn in attrs:
                attr_id = _get_attr_id(attr_defn)
                _require('monitor_cycle_seconds' in attr_defn,
                         "_add_attrs_to_platform_node(): 'monitor_cycle_seconds' not in attr_defn")
                _require('units' in attr_defn,
                         "_add_attrs_to_platform_node(): 'units' not in attr_defn")
                pn.add_attribute(AttrNode(attr_id, attr_defn))

        def _add_ports_to_platform_node(ports, pn):
            for port_info in ports:
                _require('port_id' in port_info,
                         "_add_ports_to_platform_node(): 'port_id' not in port_info")
                port_id = port_info['port_id']
                port = PortNode(port_id)
                for instrument_id in port_info.get('instrument_ids', []):
                    port.add_instrument_id(instrument_id)
                pn.add_port(port)

        def build_platform_node(CFG, parent_node):
            platform_config = CFG.get('platform_config', {})
            platform_id     = platform_config.get('platform_id', None)

            driver_config  = CFG.get('driver_config', {})
            attributes     = driver_config.get('attributes', {})
            ports          = driver_config.get('ports', {})

            _require(platform_id, "missing CFG.platform_config.platform_id")

            _require(driver_config, "missing CFG.driver_config")

            pn = create_platform_node(platform_id, CFG)
            parent_node.add_subplatform(pn)

            # attributes:
            _add_attrs_to_platform_node(attributes.itervalues(), pn)

            # ports:
            _add_ports_to_platform_node(ports.itervalues(), pn)

            # children:
            for child_CFG in CFG.get("children", {}).itervalues():
                device_type = child_CFG.get("device_type", None)
                if device_type == 'PlatformDevice':
                    build_platform_node(child_CFG, pn)

                elif device_type == 'InstrumentDevice':
                    build_instrument_node(child_CFG, pn)

            return pn

        def build_instrument_node(CFG, parent_node):
            # use CFG.agent.resource_id as the instrument_id:
            agent = CFG.get('agent', {})
            instrument_id = agent.get('resource_id', None)

            _require(instrument_id, "missing CFG.agent.resource_id for instrument")

            inn = InstrumentNode(instrument_id, CFG=CFG)
            parent_node.add_instrument(inn)

        # Now, build the whole network:
        build_platform_node(CFG, ndef._dummy_root)

        return ndef