コード例 #1
0
    def get_resource_description(self, address, command_logger=None):

        depth = 0
        resource_info = ResourceInfo()
        resource_info.set_depth(depth)
        resource_info.set_address(address)

        error_map = OrderedDict([("% Invalid", 'Can\'t get system details')])

        command = 'show system details | nomore'
        system_details = self._session.send_command(command,
                                                    re_string=self._prompt,
                                                    error_map=error_map)
        model_name = re.search(r"Device Model[ ]*(.*?)\n", system_details,
                               re.DOTALL).group(1)
        resource_info.set_model_name(model_name)
        resource_info.set_index(model_name)
        serial_number = re.search(r"Serial Number[ ]*(.*?)\n", system_details,
                                  re.DOTALL).group(1)
        resource_info.set_serial_number(serial_number)

        command = 'show ports | nomore'
        ports_details = self._session.send_command(command,
                                                   re_string=self._prompt,
                                                   error_map=error_map)

        blade_resource = ResourceInfo()
        blade_resource.set_depth(depth + 1)
        blade_resource.set_index(str(1))
        resource_info.add_child(1, blade_resource)

        src_ports_set = self.parse_filters_source_ports(command_logger)

        ports_list = re.search(r"Port.*?\n====.*?\n(.*)\n", ports_details,
                               re.DOTALL).group(1).split("\n")

        for port in ports_list:

            port_id = re.search(r"(.*?)[ ]{1,}.*", port, re.DOTALL).group(1)

            port_resource = ResourceInfo()
            port_resource.set_depth(depth + 2)
            port_resource.set_index(str(port_id))

            if port_id in src_ports_set:
                port_resource.set_mapping("{}/1/{}".format(
                    address, src_ports_set[port_id]))

            blade_resource.add_child(port_id, port_resource)

        return resource_info.convert_to_xml()
    def get_resource_description(self, address, command_logger=None):
        autoload_helper = IxiaXstreamAutoloadHelper(command_logger)
        if self._service_mode.lower() == "ssh":
            device_data = autoload_helper.get_ssh_device_structure(
                self._session)
        elif self._service_mode.lower() == "snmp":
            snmp_handler = self.__get_snmp_handler(address, command_logger)
            device_data = autoload_helper.get_snmp_device_structure(
                snmp_handler)
        else:
            if not self._service_mode.lower():
                raise Exception(self.__class__.__name__,
                                "Service mode is empty")
            raise Exception(
                self.__class__.__name__,
                "Service mode {0} is not supported".format(
                    self._service_mode.lower()))

        address_prefix = address + "/"
        self._resource_info = ResourceInfo()
        self._resource_info.set_depth(0)
        self._resource_info.set_index(1)

        self._resource_info.set_address(address)
        self._resource_info.add_attribute("Vendor", device_data["vendor"])
        self._resource_info.add_attribute("Type", device_data["type"])
        self._resource_info.add_attribute("Version", device_data["version"])
        self._resource_info.add_attribute("Model", device_data["model"])
        model_name = device_data["model"]
        self._resource_info.set_model_name(model_name)
        self._resource_info.set_serial_number(device_data["serial"])

        # if self._port_logical_mode.lower() == "logical":
        for port_id, port_data in device_data["ports"].iteritems():
            port_resource_info = ResourceInfo()
            port_resource_info.set_depth(1)
            port_resource_info.set_index(port_id)
            port_resource_info.set_model_name(model_name)
            mapped_to = port_data.get("mapped_to")
            if mapped_to:
                port_resource_info.set_mapping(address_prefix + mapped_to)
            port_resource_info.add_attribute("State", port_data['state'])
            port_resource_info.add_attribute("Protocol Type", 0)
            self._resource_info.add_child(port_id, port_resource_info)
        return self._resource_info.convert_to_xml()
コード例 #3
0
    def get_resource_description(self, address, command_logger=None):
        """ Auto-load function to retrieve all information from the device

        :param address: (str) address attribute from the CloudShell portal
        :param command_logger: logging.Logger instance
        :return: xml.etree.ElementTree.Element instance with all switch sub-resources (blades, ports)
        """

        resource_info = ResourceInfo()
        resource_info.set_depth(0)
        resource_info.set_address(address)

        if self._service_mode.lower() == "rest":
            raise NotImplementedError
        elif self._service_mode.lower() == "tl1":

            dev_info = self._get_device_info()
            resource_info.add_attribute("Vendor", "Calient")
            resource_info.add_attribute("Version", dev_info.get("version", ""))
            model_name = dev_info.get("model", "")
            resource_info.set_model_name(model_name)
            resource_info.set_serial_number(dev_info.get("serial", ""))

            connection_info = self._get_crossconnections()

            for port_info in self._get_ports():
                port_resource_info = ResourceInfo()
                port_resource_info.set_depth(1)

                port_name = port_info.get("port_name")
                port_resource_info.set_index(port_name)
                if port_name in connection_info:
                    port_resource_info.set_mapping(
                        "{address}/{dst_port}".format(
                            address=address,
                            dst_port=connection_info[port_name]))

                port_resource_info.add_attribute("Protocol Type", 0)
                port_resource_info.set_model_name(model_name)

                resource_info.add_child(port_name, port_resource_info)

        return resource_info.convert_to_xml()
    def get_resource_description(self, address, command_logger=None):
        device_data = self._get_device_data()

        self._resource_info = ResourceInfo()
        self._resource_info.set_depth(0)
        self._resource_info.set_index(1)

        self._resource_info.set_address(address)

        if self._service_mode.lower() == "tl1":
            model_info_match = re.search(
                'SerialNumber=(?P<serial>\S+)".*SystemType=(?P<type>\S+)".*"(?P<vendor>\S+):'
                +
                'ChassisType=(?P<model>\S+)".*SoftwareActiveVersion=(?P<version>\S+)"',
                device_data["system_info"], re.DOTALL)

            # add chassis info
            if model_info_match is not None:
                model_info_dict = model_info_match.groupdict()

                self._resource_info.add_attribute("Vendor",
                                                  model_info_dict["vendor"])
                self._resource_info.add_attribute("Type",
                                                  model_info_dict["type"])
                self._resource_info.add_attribute("Version",
                                                  model_info_dict["version"])
                self._resource_info.add_attribute("Model",
                                                  model_info_dict["model"])

                model_name = model_info_dict["model"]

                self._resource_info.set_model_name(model_info_dict["model"])
                self._resource_info.set_serial_number(
                    model_info_dict["serial"])
            else:
                raise Exception(self.__class__.__name__,
                                "Can't parse model info!")

            # get port mappings and port info
            address_prefix = address + "/"
            port_map_list = device_data["connections_map"].split("\n")
            port_list = device_data['port_list'].split("\n")

            if self._port_logical_mode.lower() == "logical":
                logical_port_map = dict()
                for port_data in port_list:
                    port_info_match = re.search(
                        r"PORTID=(?P<id>\d+).*PORTNAME=(?P<name>(IN|OUT)\d+)" +
                        ".*PORTHEALTH=(?P<state>good|bad)", port_data,
                        re.DOTALL)
                    if port_info_match is not None:
                        port_info_dict = port_info_match.groupdict()
                        logical_port_id = re.sub('(IN|OUT)', '',
                                                 port_info_dict["name"])
                        if logical_port_id not in logical_port_map.keys():
                            logical_port_map[logical_port_id] = {}
                        if port_info_dict["state"].lower() == "good":
                            port_state = "Enable"
                        else:
                            port_state = "Disable"

                        logical_port_map[logical_port_id]['state'] = port_state

                        if 'in' in port_info_dict["name"].lower():
                            logical_port_map[logical_port_id][
                                'in'] = logical_port_id
                        else:
                            if logical_port_id in self._custom_port_pairing.values(
                            ):
                                for key, value in self._custom_port_pairing.iteritems(
                                ):
                                    if value == logical_port_id and key in logical_port_map:
                                        logical_port_map[key][
                                            'out'] = logical_port_id
                            else:
                                logical_port_map[logical_port_id][
                                    'out'] = logical_port_id

                for port_id, port_data in logical_port_map.iteritems():
                    if 'in' in port_data and 'out' in port_data:
                        logical_port_map[port_id][
                            'port_address'] = '{0}-{1}'.format(
                                logical_port_map[port_id]['in'],
                                logical_port_map[port_id]['out'])

                for port_data in port_map_list:
                    port_map_match = re.search(
                        r"IPORTID=(?P<src_port>\d+).*IPORTNAME=(?P<src_port_name>\S+),IP.*"
                        +
                        "OPORTID=(?P<dst_port>\d+).*OPORTNAME=(?P<dst_port_name>\S+),OP.*",
                        port_data, re.DOTALL)
                    if port_map_match is not None:
                        port_map_dict = port_map_match.groupdict()
                        src_logical_port_id = re.sub(
                            '(IN|OUT)', '', port_map_dict["src_port_name"])
                        dst_logical_port_id = re.sub(
                            '(IN|OUT)', '', port_map_dict["dst_port_name"])
                        if src_logical_port_id in logical_port_map.keys() \
                                and dst_logical_port_id in logical_port_map.keys():
                            self._mapping_info[
                                dst_logical_port_id] = src_logical_port_id

                for logical_port_index, logical_port_data in logical_port_map.iteritems(
                ):
                    port_resource_info = ResourceInfo()
                    port_resource_info.set_depth(1)
                    if 'port_address' not in logical_port_data:
                        continue
                    port_resource_info.set_index(
                        logical_port_data['port_address'])
                    port_resource_info.set_model_name(model_name)
                    if logical_port_index in self._mapping_info:
                        port_resource_info.set_mapping(
                            address_prefix + logical_port_map[
                                self._mapping_info[logical_port_index]]
                            ['port_address'])
                    port_resource_info.add_attribute(
                        "State", logical_port_data['state'])
                    port_resource_info.add_attribute("Protocol Type", 0)
                    self._resource_info.add_child(
                        logical_port_data['port_address'], port_resource_info)
            else:
                for port_data in port_map_list:
                    port_map_match = re.search(
                        r".*IPORTID=(?P<src_port>\d+).*IPORTNAME=(?P<src_port_name>\S*),IP.*"
                        +
                        "OPORTID=(?P<dst_port>\d+).*OPORTNAME=(?P<dst_port_name>\S*),OP.*",
                        port_data, re.DOTALL)

                    if port_map_match is not None:
                        port_map_dict = port_map_match.groupdict()
                        if int(port_map_dict['src_port']) > 0 and \
                                int(port_map_dict['dst_port']) > 0:
                            src_port = port_map_dict["src_port"]
                            dst_port = port_map_dict["dst_port"]
                            # self._mapping_info[dst_port] = src_port
                            self._mapping_info[src_port] = dst_port

                for port_data in port_list:
                    port_info_match = re.search(
                        r"PORTID=(?P<id>\d+).*PORTNAME=(?P<name>(IN|OUT)\d+)" +
                        ".*PORTHEALTH=(?P<state>good|bad)", port_data,
                        re.DOTALL)

                    if port_info_match is not None:
                        port_info_dict = port_info_match.groupdict()

                        port_resource_info = ResourceInfo()
                        port_resource_info.set_depth(1)

                        port_id = port_info_dict["id"]
                        port_resource_info.set_index(port_id)
                        port_resource_info.set_model_name(model_name)
                        # port_resource_info.set_name(port_info_dict["name"])

                        if port_id in self._mapping_info:
                            port_resource_info.set_mapping(
                                address_prefix + self._mapping_info[port_id])

                        if port_info_dict["state"].lower() == "good":
                            port_resource_info.add_attribute("State", "Enable")
                        else:
                            port_resource_info.add_attribute(
                                "State", "Disable")

                        port_resource_info.add_attribute("Protocol Type", 0)

                        self._resource_info.add_child(port_info_dict["id"],
                                                      port_resource_info)
        else:
            raise Exception(
                self.__class__.__name__,
                "Selected '{}' connection type is not supported".format(
                    self._service_mode))

        return self._resource_info.convert_to_xml()
コード例 #5
0
    def get_resource_description(self, address, command_logger):
        """Auto-load function to retrieve all information from the device

        :param address: (str) address in the format <host>:<port>?TestStream=<switch_name> (port is optional)
        :param command_logger: logging.Logger instance
        :return: common.resource_info.ResourceInfo instance with all switch sub-resources (chassis, blades, ports)
        """

        depth = 0
        resource_info = ResourceInfo()
        resource_info.set_depth(depth)
        resource_info.set_address(address)

        device_info = self._disp_switch_info()

        info_match = re.search(
            r"PHYSICAL INFORMATION(?P<physical_info>.*)"
            r"SWITCH COMPONENTS(?P<switch_components>.*)", device_info,
            re.DOTALL)

        self.model_name = re.search(r"Switch Model:[ ]*(.*?)\n",
                                    info_match.group("physical_info"),
                                    re.DOTALL).group(1)
        resource_info.set_model_name(self.model_name)
        resource_info.set_index(self.model_name)
        ip_addr = re.search(r"IP Address:[ ]*(.*?)\n",
                            info_match.group("physical_info"),
                            re.DOTALL).group(1)
        resource_info.add_attribute("Switch Address", ip_addr)

        if not self._software_version:
            self._software_version = self._get_software_version()

        resource_info.add_attribute("Software Version", self._software_version)

        info_list = info_match.group("switch_components").split("\n")

        ports_info = self._show_ports_info()

        ports_list = re.search(r".*-\n(.*)\n\n", ports_info,
                               re.DOTALL).group(1).split("\n")
        all_ports = {}

        port_mappings = self._get_port_mappings(command_logger)

        for port in ports_list:
            info = re.search(
                r"(?P<phys_addr>.*?)[ ]{2,}"
                r"(?P<status>.*?)[ ]{2,}"
                r"(?P<name>.*?)[ ]{2,}"
                r".*?[ ]{2,}"  # Lock*/Rx Pwr(dBm)
                r".*?[ ]{2,}"  # Tx Pwr(dBm)
                r".*?[ ]{2,}"  # Rx Pwr(dBm)
                r"(?P<speed>.*?)[ ]{2,}"
                r"(?P<protocol>.*)",
                port)

            chassis, blade, port_no = info.group("phys_addr").split('.')
            chassis_ports = all_ports.setdefault(int(chassis), {})
            blade_ports = chassis_ports.setdefault(int(blade), [])
            blade_ports.append(info.groupdict())

        for info_str in info_list:
            if info_str.lower().startswith("chassis"):
                chassis_no = int(re.search(r"(\d+):", info_str).group(1))
                chassis_resource = ResourceInfo()
                chassis_resource.set_depth(depth + 1)
                chassis_resource.set_index(str(chassis_no))
                resource_info.add_child(info_str, chassis_resource)

            elif info_str.startswith(" " * 4):
                # add ports

                blade_info = re.search(
                    r"(?P<vendor>.*),(?P<model>.*),(?P<uboot_rev>.*),(?P<serial_number>.*)",
                    info_str, re.DOTALL)

                if not blade_info:
                    blade_info = re.search(
                        r"(?P<model>.*?)\s{2,}(?P<uboot_rev>.*?)\s{2,}(?P<serial_number>.*?)(\s{2,}|$)",
                        info_str.strip(), re.DOTALL)

                # blade_resource.add_attribute("Vendor", blade_info.group("vendor"))
                # blade_type = blade_info.group("model").replace(" ", "-").upper()
                blade_resource.set_model_name(blade_type)
                # blade_resource.add_attribute("Model", blade_info.group("model"))
                blade_resource.add_attribute("Uboot Rev.",
                                             blade_info.group("uboot_rev"))
                blade_resource.set_serial_number(
                    blade_info.group("serial_number"))

                chassis_ports = all_ports.get(chassis_no, {})
                blade_ports = chassis_ports.get(blade_no, [])

                for port_data in blade_ports:
                    phys_addr = port_data["phys_addr"]
                    port_no = int(phys_addr.split(".")[-1])

                    if self.is_logical_port_mode:
                        port_resource = ResourceInfo()
                        port_resource.set_model_name(blade_type)
                        port_resource.set_depth(depth + 3)
                        port_resource.set_index(str(port_no))
                        port_resource.add_attribute("Protocol Type", 0)

                        connected_resource = self._get_connected_resource(
                            phys_addr, port_mappings)
                        if connected_resource:
                            port_resource.set_mapping("{}/{}".format(
                                address, connected_resource))

                        # if port_data["status"].lower() == "not connected":
                        #     port_resource.add_attribute("State", "Enable")
                        # else:
                        #     port_resource.add_attribute("State", "Disable")

                        blade_resource.add_child(port_no, port_resource)
                    else:
                        for subport in (self.TX_SUBPORT_INDEX,
                                        self.RX_SUBPORT_INDEX):
                            subport_idx = "{}-{}".format(port_no, subport)
                            subport_resource = ResourceInfo()
                            subport_resource.set_model_name(blade_type)
                            subport_resource.set_depth(depth + 3)
                            subport_resource.set_index(subport_idx)
                            subport_resource.add_attribute("Protocol Type", 0)
                            blade_resource.add_child(subport_idx,
                                                     subport_resource)

                            if subport == self.RX_SUBPORT_INDEX:
                                connected_resource = self._get_connected_resource(
                                    phys_addr, port_mappings)
                                if connected_resource:
                                    subport_resource.set_mapping(
                                        "{}/{}".format(address,
                                                       connected_resource))

            elif info_str.startswith(" " * 2):
                # blade type is the last word in the sequence
                blade_base_info = re.search(
                    r"(?P<blade_id>\d+)\s+(?P<model>.*)", info_str)

                blade_type = blade_base_info.group("model").strip().replace(
                    " ", "-").title()
                blade_no = int(blade_base_info.group("blade_id"))

                blade_resource = ResourceInfo()
                blade_resource.set_model_name(blade_type)
                blade_resource.set_depth(depth + 2)
                blade_resource.set_index(str(blade_no))
                chassis_resource.add_child(info_str, blade_resource)

        return resource_info.convert_to_xml()
コード例 #6
0
    def get_resource_description(self, address, command_logger):
        """Auto-load function to retrieve all information from the device

        :param address: (str) address in the format <host>:<port>?TestStream=<switch_name> (port is optional)
        :param command_logger: logging.Logger instance
        :return: common.resource_info.ResourceInfo instance with all switch sub-resources (chassis, blades, ports)
        """

        depth = 0
        resource_info = ResourceInfo()
        resource_info.set_depth(depth)
        resource_info.set_address(address)

        device_info = self._disp_switch_info()

        info_match = re.search(
            r"PHYSICAL INFORMATION(?P<physical_info>.*)"
            r"SWITCH COMPONENTS(?P<switch_components>.*)",
            device_info, re.DOTALL)

        self.model_name = re.search(r"Switch Model:[ ]*(.*?)\n", info_match.group("physical_info"), re.DOTALL).group(1)
        resource_info.set_model_name(self.model_name)
        resource_info.set_index(self.model_name)
        ip_addr = re.search(r"IP Address:[ ]*(.*?)\n", info_match.group("physical_info"), re.DOTALL).group(1)
        resource_info.add_attribute("Switch Address", ip_addr)

        if not self._software_version:
            self._software_version = self._get_software_version()

        resource_info.add_attribute("Software Version", self._software_version)

        info_list = info_match.group("switch_components").split("\n")

        ports_info = self._show_ports_info()

        ports_list = re.search(r".*-\n(.*)\n\n", ports_info, re.DOTALL).group(1).split("\n")
        all_ports = {}

        port_mappings = self._get_port_mappings(command_logger)

        for port in ports_list:
            info = re.search(r"(?P<phys_addr>.*?)[ ]{2,}"
                             r"(?P<status>.*?)[ ]{2,}"
                             r"(?P<name>.*?)[ ]{2,}"
                             r".*?[ ]{2,}"  # Lock*/Rx Pwr(dBm)
                             r".*?[ ]{2,}"  # Tx Pwr(dBm)
                             r".*?[ ]{2,}"  # Rx Pwr(dBm)
                             r"(?P<speed>.*?)[ ]{2,}"
                             r"(?P<protocol>.*)", port)

            chassis, blade, port_no = info.group("phys_addr").split('.')
            chassis_ports = all_ports.setdefault(int(chassis), {})
            blade_ports = chassis_ports.setdefault(int(blade), [])
            blade_ports.append(info.groupdict())

        for info_str in info_list:
            if info_str.lower().startswith("chassis"):
                chassis_no = int(re.search(r"(\d+):", info_str).group(1))
                chassis_resource = ResourceInfo()
                chassis_resource.set_depth(depth + 1)
                chassis_resource.set_index(str(chassis_no))
                resource_info.add_child(info_str, chassis_resource)

            elif info_str.startswith(" " * 4):
                # add ports

                blade_info = re.search(
                    r"(?P<vendor>.*),(?P<model>.*),(?P<uboot_rev>.*),(?P<serial_number>.*)", info_str, re.DOTALL)

                if not blade_info:
                    blade_info = re.search(
                        r"(?P<model>.*?)\s{2,}(?P<uboot_rev>.*?)\s{2,}(?P<serial_number>.*?)(\s{2,}|$)",
                        info_str.strip(),
                        re.DOTALL)

                # blade_resource.add_attribute("Vendor", blade_info.group("vendor"))
                # blade_type = blade_info.group("model").replace(" ", "-").upper()
                blade_resource.set_model_name(blade_type)
                # blade_resource.add_attribute("Model", blade_info.group("model"))
                blade_resource.add_attribute("Uboot Rev.", blade_info.group("uboot_rev"))
                blade_resource.set_serial_number(blade_info.group("serial_number"))

                chassis_ports = all_ports.get(chassis_no, {})
                blade_ports = chassis_ports.get(blade_no, [])

                for port_data in blade_ports:
                    phys_addr = port_data["phys_addr"]
                    port_no = int(phys_addr.split(".")[-1])

                    if self.is_logical_port_mode:
                        port_resource = ResourceInfo()
                        port_resource.set_model_name(blade_type)
                        port_resource.set_depth(depth + 3)
                        port_resource.set_index(str(port_no))
                        port_resource.add_attribute("Protocol Type", 0)

                        connected_resource = self._get_connected_resource(phys_addr, port_mappings)
                        if connected_resource:
                            port_resource.set_mapping("{}/{}".format(address, connected_resource))

                        # if port_data["status"].lower() == "not connected":
                        #     port_resource.add_attribute("State", "Enable")
                        # else:
                        #     port_resource.add_attribute("State", "Disable")

                        blade_resource.add_child(port_no, port_resource)
                    else:
                        for subport in (self.TX_SUBPORT_INDEX, self.RX_SUBPORT_INDEX):
                            subport_idx = "{}-{}".format(port_no, subport)
                            subport_resource = ResourceInfo()
                            subport_resource.set_model_name(blade_type)
                            subport_resource.set_depth(depth + 3)
                            subport_resource.set_index(subport_idx)
                            subport_resource.add_attribute("Protocol Type", 0)
                            blade_resource.add_child(subport_idx, subport_resource)

                            if subport == self.RX_SUBPORT_INDEX:
                                connected_resource = self._get_connected_resource(phys_addr, port_mappings)
                                if connected_resource:
                                    subport_resource.set_mapping("{}/{}".format(address, connected_resource))

            elif info_str.startswith(" " * 2):
                # blade type is the last word in the sequence
                blade_base_info = re.search(r"(?P<blade_id>\d+)\s+(?P<model>.*)", info_str)

                blade_type = blade_base_info.group("model").strip().replace(" ", "-").title()
                blade_no = int(blade_base_info.group("blade_id"))

                blade_resource = ResourceInfo()
                blade_resource.set_model_name(blade_type)
                blade_resource.set_depth(depth + 2)
                blade_resource.set_index(str(blade_no))
                chassis_resource.add_child(info_str, blade_resource)

        return resource_info.convert_to_xml()
コード例 #7
0
                logical_port_map[port_id]['in'],
                logical_port_map[port_id]['out'])

for port_data in port_map_list:
    port_map_match = re.search(r"IPORTID=(?P<src_port>\d+).*IPORTNAME=(?P<src_port_name>\S+),IP.*" +
                               "OPORTID=(?P<dst_port>\d+).*OPORTNAME=(?P<dst_port_name>\S+),OP.*",
                               port_data, re.DOTALL)
    if port_map_match is not None:
        port_map_dict = port_map_match.groupdict()
        src_logical_port_id = re.sub('(IN|OUT)', '', port_map_dict["src_port_name"])
        dst_logical_port_id = re.sub('(IN|OUT)', '', port_map_dict["dst_port_name"])
        if src_logical_port_id in logical_port_map.keys() \
                and dst_logical_port_id in logical_port_map.keys():
            _mapping_info[src_logical_port_id] = dst_logical_port_id

for logical_port_index, logical_port_data in logical_port_map.iteritems():
    port_resource_info = ResourceInfo()
    port_resource_info.set_depth(1)
    if 'port_address' not in logical_port_data:
        continue
    port_resource_info.set_index(logical_port_data['port_address'])
    port_resource_info.set_model_name('adadadad')
    if logical_port_index in _mapping_info:
        port_resource_info.set_mapping(
            address_prefix + logical_port_map[_mapping_info[logical_port_index]]['port_address'])
    port_resource_info.add_attribute("State", logical_port_data['state'])
    port_resource_info.add_attribute("Protocol Type", 0)
    result_list.append((logical_port_index, port_resource_info))

print result_list
    def get_resource_description(self, address, command_logger=None):
        self._session.send_command("", re_string=self._login_prompt)
        device_data = self._get_device_data()

        self._resource_info = ResourceInfo()
        self._resource_info.set_depth(0)
        self._resource_info.set_index(1)

        self._resource_info.set_address(address)

        if self._service_mode.lower() == "tl1":
            model_info_match = re.search('SerialNumber=(?P<serial>\S+)".*SystemType=(?P<type>\S+)".*"(?P<vendor>\S+):' +
                                         'ChassisType=(?P<model>\S+)".*SoftwareActiveVersion=(?P<version>\S+)"',
                                         device_data["system_info"], re.DOTALL)

            # add chassis info
            if model_info_match is not None:
                model_info_dict = model_info_match.groupdict()

                self._resource_info.add_attribute("Vendor", model_info_dict["vendor"])
                self._resource_info.add_attribute("Type", model_info_dict["type"])
                self._resource_info.add_attribute("Version", model_info_dict["version"])
                self._resource_info.add_attribute("Model", model_info_dict["model"])

                model_name = model_info_dict["model"]

                self._resource_info.set_model_name(model_info_dict["model"])
                self._resource_info.set_serial_number(model_info_dict["serial"])
            else:
                raise Exception(self.__class__.__name__, "Can't parse model info!")

            # get port mappings and port info
            address_prefix = address + "/"
            port_map_list = device_data["connections_map"].split("\n")
            port_list = device_data['port_list'].split("\n")

            if self._port_logical_mode.lower() == "logical":
                logical_port_map = dict()
                for port_data in port_list:
                    port_info_match = re.search(r"PORTID=(?P<id>\d+).*PORTNAME=(?P<name>(IN|OUT)\d+)" +
                                                ".*PORTHEALTH=(?P<state>good|bad)", port_data, re.DOTALL)
                    if port_info_match is not None:
                        port_info_dict = port_info_match.groupdict()
                        logical_port_id = re.sub('(IN|OUT)', '', port_info_dict["name"])
                        if logical_port_id not in logical_port_map.keys():
                            logical_port_map[logical_port_id] = {}
                        if port_info_dict["state"].lower() == "good":
                            port_state = "Enable"
                        else:
                            port_state = "Disable"

                        logical_port_map[logical_port_id]['state'] = port_state

                        if 'in' in port_info_dict["name"].lower():
                            logical_port_map[logical_port_id]['in'] = logical_port_id
                        else:
                            if logical_port_id in self._custom_port_pairing.values():
                                for key, value in self._custom_port_pairing.iteritems():
                                    if value == logical_port_id and key in logical_port_map:
                                        logical_port_map[key]['out'] = logical_port_id
                            else:
                                logical_port_map[logical_port_id]['out'] = logical_port_id

                for port_id, port_data in logical_port_map.iteritems():
                    if 'in' in port_data and 'out' in port_data:
                        logical_port_map[port_id]['port_address'] = '{0}-{1}'.format(
                            logical_port_map[port_id]['in'],
                            logical_port_map[port_id]['out'])

                for port_data in port_map_list:
                    port_map_match = re.search(r"IPORTID=(?P<src_port>\d+).*IPORTNAME=(?P<src_port_name>\S+),IP.*" +
                                               "OPORTID=(?P<dst_port>\d+).*OPORTNAME=(?P<dst_port_name>\S+),OP.*",
                                               port_data, re.DOTALL)
                    if port_map_match is not None:
                        port_map_dict = port_map_match.groupdict()
                        src_logical_port_id = re.sub('(IN|OUT)', '', port_map_dict["src_port_name"])
                        dst_logical_port_id = re.sub('(IN|OUT)', '', port_map_dict["dst_port_name"])
                        if src_logical_port_id in logical_port_map.keys() \
                                and dst_logical_port_id in logical_port_map.keys():
                            self._mapping_info[dst_logical_port_id] = src_logical_port_id

                for logical_port_index, logical_port_data in logical_port_map.iteritems():
                    port_resource_info = ResourceInfo()
                    port_resource_info.set_depth(1)
                    if 'port_address' not in logical_port_data:
                        continue
                    port_resource_info.set_index(logical_port_data['port_address'])
                    port_resource_info.set_model_name(model_name)
                    if logical_port_index in self._mapping_info:
                        port_resource_info.set_mapping(address_prefix +
                                                       logical_port_map[self._mapping_info[logical_port_index]][
                                                           'port_address'])
                    port_resource_info.add_attribute("State", logical_port_data['state'])
                    port_resource_info.add_attribute("Protocol Type", 0)
                    self._resource_info.add_child(logical_port_data['port_address'], port_resource_info)
            else:
                for port_data in port_map_list:
                    port_map_match = re.search(r".*IPORTID=(?P<src_port>\d+).*IPORTNAME=(?P<src_port_name>\S*),IP.*" +
                                               "OPORTID=(?P<dst_port>\d+).*OPORTNAME=(?P<dst_port_name>\S*),OP.*",
                                               port_data, re.DOTALL)

                    if port_map_match is not None:
                        port_map_dict = port_map_match.groupdict()
                        if int(port_map_dict['src_port']) > 0 and \
                                int(port_map_dict['dst_port']) > 0:
                            src_port = port_map_dict["src_port"]
                            dst_port = port_map_dict["dst_port"]
                            # self._mapping_info[dst_port] = src_port
                            self._mapping_info[src_port] = dst_port

                for port_data in port_list:
                    port_info_match = re.search(r"PORTID=(?P<id>\d+).*PORTNAME=(?P<name>(IN|OUT)\d+)" +
                                                ".*PORTHEALTH=(?P<state>good|bad)", port_data, re.DOTALL)

                    if port_info_match is not None:
                        port_info_dict = port_info_match.groupdict()

                        port_resource_info = ResourceInfo()
                        port_resource_info.set_depth(1)

                        port_id = port_info_dict["id"]
                        port_resource_info.set_index(port_id)
                        port_resource_info.set_model_name(model_name)
                        # port_resource_info.set_name(port_info_dict["name"])

                        if port_id in self._mapping_info:
                            port_resource_info.set_mapping(address_prefix + self._mapping_info[port_id])

                        if port_info_dict["state"].lower() == "good":
                            port_resource_info.add_attribute("State", "Enable")
                        else:
                            port_resource_info.add_attribute("State", "Disable")

                        port_resource_info.add_attribute("Protocol Type", 0)

                        self._resource_info.add_child(port_info_dict["id"], port_resource_info)
        else:
            raise Exception(self.__class__.__name__,
                            "Selected '{}' connection type is not supported".format(self._service_mode))

        return self._resource_info.convert_to_xml()
コード例 #9
0
    def get_resource_description(self, address, command_logger=None):
        """Auto-load function to retrieve all information from the device
        :param address: (str) address attribute from the CloudShell portal
        :param command_logger: logging.Logger instance
        :return: xml.etree.ElementTree.Element instance with all switch sub-resources (blades, ports)
        """
        #Validate properIP address
        if ":" not in address:
            self._logger.error('Invalid address entry found. Address found: ' +
                               address)
            raise Exception(
                'Invalid address found. Please check IP and matrix letter. Format: [IP]:[Matrix Letter]'
            )

        self._logger = command_logger
        self._create_connection()

        # Step 1. Create root element (switch):
        depth = 0
        # switch_Family = ConfigurationParser.get("driver_variable", "switch_family")
        switch_Model = ConfigurationParser.get("driver_variable",
                                               "switch_model")

        # blade_Family = ConfigurationParser.get("driver_variable", "blade_family")
        blade_Model = "Unit"

        # port_Family = ConfigurationParser.get("driver_variable", "port_family")
        port_Model = ConfigurationParser.get("driver_variable", "port_model")

        self._logger.info('switch: %s' % (str(switch_Model)))
        self._logger.info('Patch Panel: %s' % (str(blade_Model)))
        self._logger.info('port: %s' % (str(port_Model)))

        resource_info = ResourceInfo()
        resource_info.set_depth(depth)
        resource_info.set_address(address)
        resource_info.set_index("Rome")
        resource_info.add_attribute("Software Version", "1.0.0")
        resource_info.set_model_name(switch_Model)

        letter = ""

        self._connection.write('cc' + "\n!@#$")
        self._connection.read_until('cc')
        message = self._connection.expect(['!@#$'], 1)
        self._connection.write('\b\b\b\b\b\b\b')
        self._logger.info('cc output:\r\n' + message[2])
        lines = message[2].split('\r\n')
        mappings = {}
        if len(lines) > 4:
            for line in lines[4:-2]:
                values = re.match('^E(\d+)?.*?W(\d+)?.*', line).groups()
                mappings[values[1]] = values[0]

        #parse the string and only take the right side of the address
        parsed_string = address.split(":")
        try:
            #If true place remove white space and look for a single letter notation.
            matrix_letter = parsed_string[1].lower()
            string = matrix_letter
            matrix_letter = matrix_letter.replace(" ", "")
        except IndexError:
            self._logger.error(
                'Resource address should specify MatrixA or MatrixB. Current address: '
                + address)
            raise Exception(
                'Resource address should specify MatrixA or MatrixB. Format [IP]:[Matrix Letter].'
            )
        self._logger.info("Matrix Letter is: " + matrix_letter)

        # Validating the matrix address a/b
        pattern = re.compile(r"(?i)(matrix)?(.+)?(A|B)")
        mat = pattern.match(string)
        f = ['a', 'a ', 'b', 'b ', 'm']
        valid = False

        #Check for validity of format
        if mat:
            self.g3 = mat.group(3)
            self.g2 = mat.group(2)
            self.g1 = mat.group(1)
            if self.g3 == 'a' or self.g3 == 'b':
                if self.g2 not in f:
                    valid = True
        if not valid:
            if self.g3 != 'a' or self.g3 != 'b':
                self._logger.error(
                    'Resource address should specify MatrixA or MatrixB. Current address: '
                    + address)
                raise Exception(
                    'Resource address should specify MatrixA or MatrixB')

            if self.g2 in f:
                self._logger.error('Multiple matrix letters found')
                raise Exception(
                    'Resource address should only contain one matrix letter. Either A or B'
                )

        if self.g3 == 'a':
            letter = "A"
        elif self.g3 == 'b':
            letter = "B"
        else:
            self._logger.error(
                'Resource address should specify MatrixA or MatrixB. Current address: '
                + address)
            raise Exception(
                'Resource address should specify MatrixA or MatrixB')

        #Step 2. Create child resources for the root element (blades):
        for blade_no in range(1, 2):
            blade_resource = ResourceInfo()
            blade_resource.set_depth(depth + 1)
            blade_resource.set_index(str(blade_no))
            blade_resource.set_model_name(blade_Model)
            blade_resource.set_address(address + ":" + ("Matrix%s" % letter))
            resource_info.add_child(blade_no, blade_resource)

            # Step 3. Create child resources for each root sub-resource (ports in blades)
            if letter is "A":
                for port_no in range(1, 129):
                    port_resource = ResourceInfo()
                    port_resource.set_depth(depth + 2)
                    port_resource.set_index(str(port_no).zfill(3))
                    port_resource.set_model_name(port_Model)
                    if str(port_no) in mappings:
                        mapped_to = mappings[str(port_no)]
                        self._logger.info('found mapping for port ' +
                                          str(port_no) + ', mapped to: ' +
                                          mapped_to)
                        port_resource.set_mapping(address + '/1/' +
                                                  mapped_to.zfill(3))
                    blade_resource.add_child(port_no, port_resource)
            elif letter is "B":
                for port_no in range(129, 257):
                    port_resource = ResourceInfo()
                    port_resource.set_depth(depth + 2)
                    port_resource.set_index(str(port_no).zfill(3))
                    port_resource.set_model_name(port_Model)
                    if str(port_no) in mappings:
                        mapped_to = mappings[str(port_no)]
                        self._logger.info('found mapping for port ' +
                                          str(port_no) + ', mapped to: ' +
                                          mapped_to)
                        port_resource.set_mapping(address + '/1/' +
                                                  mapped_to.zfill(3))
                    blade_resource.add_child(port_no, port_resource)

        self._close_connection()
        return resource_info.convert_to_xml()