Example #1
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
Example #2
0
    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, dic['network'])
                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
Example #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):
            assert not platform_id in ndef.pnodes
            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)
                assert 'monitor_cycle_seconds' in attr_defn
                assert 'units' in attr_defn
                pn.add_attribute(AttrNode(attr_id, attr_defn))

        def _add_ports_to_platform_node(ports, pn):
            for port_info in ports:
                assert 'port_id' in port_info
                assert 'network' in port_info
                port_id = port_info['port_id']
                network = port_info['network']
                port = PortNode(port_id, network)
                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:
            _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
Example #4
0
    def deserialize_network_definition(ser):
        """
        Creates a NetworkDefinition object by deserializing the given argument.

        @param ser representation of the given serialization
        @return A NetworkDefinition object
        """

        ndef = NetworkDefinition()

        def _get_platform_types(pyobj):
            """
            Constructs:
              - ndef._platform_types, {platform_type : description} dict
            """
            assert 'platform_types' in pyobj
            ndef._platform_types = {}
            for ptypeObj in pyobj["platform_types"]:
                assert 'platform_type' in ptypeObj
                assert 'description' in ptypeObj
                platform_type = ptypeObj['platform_type']
                description = ptypeObj['description']
                ndef._platform_types[platform_type] = description

        def _build_network(pyobj):
            """
            Constructs:
              - ndef._pnodes: {platform_id : PlatformNode} dict
            """
            assert 'network' in pyobj

            def create_node(platform_id, platform_types=None):
                assert not platform_id in ndef.pnodes
                pn = PlatformNode(platform_id, platform_types)
                ndef.pnodes[platform_id] = pn
                return pn

            def build_and_add_ports_to_node(ports, pn):
                for port_info in ports:
                    assert 'port_id' in port_info
                    assert 'network' in port_info
                    port_id = port_info['port_id']
                    network = port_info['network']
                    port = PortNode(port_id, network)
                    port.set_on(port_info.get('is_on', False))
                    if 'instruments' in port_info:
                        for instrument in port_info['instruments']:
                            instrument_id = instrument['instrument_id']
                            if instrument_id in port.instruments:
                                raise Exception(
                                    'port_id=%r: duplicate instrument ID %r' %
                                    (port_id, instrument_id))
                            port.add_instrument(InstrumentNode(instrument_id))
                    pn.add_port(port)

            def build_and_add_attrs_to_node(attrs, pn):
                for attr_defn in attrs:
                    assert 'attr_id' in attr_defn
                    assert 'monitorCycleSeconds' in attr_defn
                    assert 'units' in attr_defn
                    attr_id = attr_defn['attr_id']
                    pn.add_attribute(AttrNode(attr_id, attr_defn))

            def build_node(platObj, parent_node):
                assert 'platform_id' in platObj
                assert 'platform_types' in platObj
                platform_id = platObj['platform_id']
                platform_types = platObj['platform_types']
                for platform_type in platform_types:
                    assert platform_type in ndef._platform_types
                ports = platObj['ports'] if 'ports' in platObj else []
                attrs = platObj['attrs'] if 'attrs' in platObj else []
                pn = create_node(platform_id, platform_types)
                parent_node.add_subplatform(pn)
                build_and_add_ports_to_node(ports, pn)
                build_and_add_attrs_to_node(attrs, pn)
                if 'subplatforms' in platObj:
                    for subplat in platObj['subplatforms']:
                        subplat_id = subplat['platform_id']
                        if subplat_id in pn.subplatforms:
                            raise Exception(
                                '%s: duplicate subplatform ID for parent %s' %
                                (subplat_id, platform_id))
                        build_node(subplat, pn)
                return pn

            ndef._pnodes = {}

            ndef._dummy_root = create_node(platform_id='')

            for platObj in pyobj["network"]:
                build_node(platObj, ndef._dummy_root)

        pyobj = yaml.load(ser)
        _get_platform_types(pyobj)
        _build_network(pyobj)

        return ndef
Example #5
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)
        _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)
                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:
            # 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)

            _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
Example #6
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):
            assert not platform_id in ndef.pnodes
            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)
                assert 'monitor_cycle_seconds' in attr_defn
                assert 'units' in attr_defn
                pn.add_attribute(AttrNode(attr_id, attr_defn))

        def _add_ports_to_platform_node(ports, pn):
            for port_info in ports:
                assert 'port_id' in port_info
                assert 'network' in port_info
                port_id = port_info['port_id']
                network = port_info['network']
                port = PortNode(port_id, network)
                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:
            _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
Example #7
0
    def deserialize_network_definition(ser):
        """
        Creates a NetworkDefinition object by deserializing the given argument.

        @param ser representation of the given serialization
        @return A NetworkDefinition object
        """

        ndef = NetworkDefinition()

        def _build_network(pyobj):
            """
            Constructs:
              - ndef._pnodes: {platform_id : PlatformNode} dict
            """
            def create_node(platform_id):
                _require(not platform_id in ndef.pnodes)
                pn = PlatformNode(platform_id)
                ndef.pnodes[platform_id] = pn
                return pn

            def build_and_add_ports_to_node(ports, pn):
                for port_info in ports:
                    _require('port_id' in port_info)
                    port_id = port_info['port_id']
                    port = PortNode(port_id)
                    if 'instruments' in port_info:
                        for instrument in port_info['instruments']:
                            instrument_id = instrument['instrument_id']
                            _require(
                                not instrument_id in port.instrument_ids,
                                'port_id=%r: duplicate instrument_id=%r' %
                                (port_id, instrument_id))
                            port.add_instrument_id(instrument_id)
                    pn.add_port(port)

            def build_and_add_attrs_to_node(attrs, pn):
                for attr_defn in attrs:
                    attr_id = _get_attr_id(attr_defn)
                    _require('monitor_cycle_seconds' in attr_defn)
                    _require('units' in attr_defn)
                    if isinstance(attr_defn, OrderedDict):
                        attr_defn = dict(attr_defn)
                    pn.add_attribute(AttrNode(attr_id, attr_defn))

            def build_node(platObj, parent_node):
                _require('platform_id' in platObj)
                platform_id = platObj['platform_id']
                ports = platObj['ports'] if 'ports' in platObj else []
                attrs = platObj['attrs'] if 'attrs' in platObj else []
                pn = create_node(platform_id)
                parent_node.add_subplatform(pn)
                build_and_add_ports_to_node(ports, pn)
                build_and_add_attrs_to_node(attrs, pn)
                if 'subplatforms' in platObj:
                    for subplat in platObj['subplatforms']:
                        subplat_id = subplat['platform_id']
                        _require(
                            not subplat_id in pn.subplatforms,
                            '%s: duplicate subplatform ID for parent %s' %
                            (subplat_id, platform_id))
                        build_node(subplat, pn)
                return pn

            ndef._pnodes = {}

            ndef._dummy_root = create_node(platform_id='')

            _require('network' in pyobj, "'network' undefined")
            for platObj in pyobj["network"]:
                build_node(platObj, ndef._dummy_root)

        pyobj = yaml.load(ser)
        _build_network(pyobj)

        return ndef
Example #8
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
Example #9
0
    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.get_platform_attributes(platform_id)
            if not isinstance(attr_infos, dict):
                log.warn("%r: get_platform_attributes returned: %s",
                         platform_id, attr_infos)
                return

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

            assert platform_id in 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.get_platform_ports(platform_id)
            if not isinstance(port_infos, dict):
                log.warn("%r: get_platform_ports returned: %s", platform_id,
                         port_infos)
                return

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

            assert platform_id in port_infos
            ports = port_infos[platform_id]
            for port_id, dic in ports.iteritems():
                port = PortNode(port_id, dic['network'])
                pnode.add_port(port)

                # add connected instruments:
                instrs_res = rsn_oms.get_connected_instruments(
                    platform_id, port_id)
                if not isinstance(instrs_res, dict):
                    log.warn("%r: port_id=%r: get_connected_instruments "
                             "returned: %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))
                assert platform_id in instrs_res
                assert port_id in 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