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) """ number_of_blades = ConfigurationParser.get("driver_variable", "number_of_blades") ports_per_blade = ConfigurationParser.get("driver_variable", "ports_per_blade") # Step 1. Create root element (switch): depth = 0 resource_info = ResourceInfo() resource_info.set_depth(depth) resource_info.set_address(address) resource_info.set_index("L1Mock") # Step 2. Create child resources for the root element (blades): for blade_no in xrange(number_of_blades): blade_resource = ResourceInfo() blade_resource.set_depth(depth + 1) blade_resource.set_index(str(blade_no+1)) resource_info.add_child(blade_no+1, blade_resource) # Step 3. Create child resources for each root sub-resource (ports in blades) for port_no in xrange(ports_per_blade): port_resource = ResourceInfo() port_resource.set_depth(depth + 2) port_resource.set_index("%03d" % (port_no+1)) blade_resource.add_child(port_no+1, port_resource) return resource_info.convert_to_xml()
def __init__(self): DriverHandlerBase.__init__(self) self._port = ConfigurationParser.get("common_variable", "connection_port") self._driver_name = ConfigurationParser.get("common_variable", "driver_name") self._logger = get_qs_logger(log_group=self._driver_name + '_internal', log_file_prefix=self._driver_name + '_internal', log_category='INTERNAL')
def __init__(self): DriverHandlerBase.__init__(self) self._port = ConfigurationParser.get("common_variable", "connection_port") self._driver_name = ConfigurationParser.get("common_variable", "driver_name") self._logger = get_qs_logger(log_group=self._driver_name + '_internal', log_file_prefix=self._driver_name + '_internal', log_category='INTERNAL') self.log('Driver Version: 1.0.2') self._last_active_time = 0
def __init__(self): DriverHandlerBase.__init__(self) self._snmp_handler = None self._ctag = 1 self._switch_name = '' self._switch_size = 0 self._mapping_info = dict() self._resource_info = None self._service_mode = ConfigurationParser.get("driver_variable", "service_mode") self._port_logical_mode = ConfigurationParser.get( "driver_variable", "port_mode") self._custom_port_pairing = ConfigurationParser.get( "driver_variable", "custom_port_pairing") self._snmp_write_community = ConfigurationParser.get( "driver_variable", "snmp_write_community") self._snmp_version = ConfigurationParser.get("driver_variable", "snmp_version") self._snmp_v3_user = ConfigurationParser.get("driver_variable", "snmp_write_community") self._snmp_v3_password = ConfigurationParser.get( "driver_variable", "snmp_write_community") self._snmp_v3_private_key = ConfigurationParser.get( "driver_variable", "snmp_write_community")
def __init__(self): DriverHandlerBase.__init__(self) self._ctag = 1 self._switch_name = '' self._switch_size = 0 self._mapping_info = dict() self._resource_info = None self._service_mode = ConfigurationParser.get("driver_variable", "service_mode") self._port_logical_mode = ConfigurationParser.get("driver_variable", "port_mode") self._custom_port_pairing = ConfigurationParser.get("driver_variable", "custom_port_pairing") or dict() self._login_prompt = ConfigurationParser.get("common_variable", "device_login_prompt")
def __init__(self): DriverHandlerBase.__init__(self) self._switch_model = "{{cookiecutter.model_name}}" self._blade_model = "{{cookiecutter.model_name}} Blade" self._port_model = "{{cookiecutter.model_name}} Port" self.example_driver_setting = ConfigurationParser.get("driver_variable", "example_driver_setting")
def __init__(self): DriverHandlerBase.__init__(self) self._ctag = 1 self._switch_name = "" self._service_mode = ConfigurationParser.get("driver_variable", "service_mode")
def __init__(self): DriverHandlerBase.__init__(self) self._port_mode = ConfigurationParser.get("driver_variable", "port_mode") self._switch_name = None self.model_name = None self._software_version = None self._is_new_commands_format = None
def __init__(self): DriverHandlerBase.__init__(self) self._ctag = 1 self._switch_name = '' self._switch_size = 0 self._mapping_info = dict() self._resource_info = None self._service_mode = ConfigurationParser.get("driver_variable", "service_mode") self._port_logical_mode = ConfigurationParser.get( "driver_variable", "port_mode") self._custom_port_pairing = ConfigurationParser.get( "driver_variable", "custom_port_pairing")
def __init__(self): DriverHandlerBase.__init__(self) self._switch_model = "CgsDriver" self._blade_model = "CgsDriver Blade" self._port_model = "CgsDriver Port" self.example_driver_setting = ConfigurationParser.get( "driver_variable", "example_driver_setting")
def __init__(self): DriverHandlerBase.__init__(self) self._switch_model = "Rome" self._blade_model = "Rome Patch Panel" self._port_model = "Rome Port" self._driver_name = ConfigurationParser.get("common_variable", "driver_name") self._command_timeout = ConfigurationParser.get( "driver_variable", "command_timeout") if self._command_timeout is None: self._command_timeout = 121 self._logger = None self._connection = None self._device_address = '' self._device_user = '' self._device_password = '' self._port = ''
def _fake_operation_duration(self, command_logger): try: operation_duration = ConfigurationParser.get("driver_variable", "operation_duration_sec") command_logger.info("operation_duration_sec: %d" % operation_duration) except Exception as ex: command_logger.info("operation_duration_sec failed: " + ex.message) operation_duration = 5 time.sleep(operation_duration)
def init(self, host, username, password, port=None): self._host = host self._username = username self._password = password if port is not None: self._port = port else: self._port = ConfigurationParser.get("common_variable", "connection_port")
def create(session_type, **kwargs): session_data = ConfigurationParser.get('cli_variable', session_type) module_name = session_data[0] class_name = session_data[1] module = importlib.import_module(module_name) if hasattr(module, class_name): return getattr(module, class_name)(**kwargs) return None
def init_driver_handler(self): driver_module_data = ConfigurationParser.get('common_variable', 'driver_module') driver_module_name = driver_module_data[0] driver_class_name = driver_module_data[1] module = importlib.import_module(driver_module_name) if hasattr(module, driver_class_name): self._driver_handler = getattr(module, driver_class_name)() return raise Exception('RequestHandler', 'Can\'t found class in package!')
def __init__(self, host, port, request_manager, exe_folder_str): def __export_log_path(): os.environ['LOG_PATH'] = os.path.join(exe_folder_str, '..', 'Logs') __export_log_path() driver_name = ConfigurationParser.get("common_variable", "driver_name") self._xml_logger = get_qs_logger(log_group=driver_name + '_xml', log_file_prefix=driver_name + '_xml', log_category='XML') self._command_logger = get_qs_logger( log_group=driver_name + '_commands', log_file_prefix=driver_name + '_commands', log_category='COMMANDS') print 'Logger created with path {}'.format( self._command_logger.handlers[0]._handler.baseFilename) self._command_logger.info("Driver name: " + driver_name) self._command_logger.info("Driver host: " + host) self._command_logger.info("Driver port: " + str(port)) self._is_running = True self._host = host self._port = port self._request_manager = request_manager self._server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._command_logger.debug('New socket created') try: self._server_socket.bind((self._host, self._port)) except socket.error as error_object: # log will be here self._command_logger.error(str(error_object)) raise Exception( 'ServerConnection', 'Can\'t bind host and port: ' + self._host + ':' + self._port + '!') self._command_logger.debug('Start listenning ...') self._server_socket.listen(100)
def __init__(self, host, port, request_manager, exe_folder_str): def __export_log_path(): os.environ['LOG_PATH'] = os.path.join(exe_folder_str, '..', 'Logs') __export_log_path() driver_name = ConfigurationParser.get("common_variable", "driver_name") self._xml_logger = get_qs_logger(log_group=driver_name + '_xml', log_file_prefix=driver_name + '_xml', log_category='XML') self._command_logger = get_qs_logger(log_group=driver_name + '_commands', log_file_prefix=driver_name + '_commands', log_category='COMMANDS') print 'Logger created with path {}'.format(self._command_logger.handlers[0]._handler.baseFilename) self._command_logger.info("Driver name: " + driver_name) self._command_logger.info("Driver host: " + host) self._command_logger.info("Driver port: " + str(port)) self._is_running = True self._host = host self._port = port self._request_manager = request_manager self._server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._command_logger.debug('New socket created') try: self._server_socket.bind((self._host, self._port)) except socket.error as error_object: # log will be here self._command_logger.error(str(error_object)) raise Exception('ServerConnection', 'Can\'t bind host and port: ' + self._host + ':' + self._port + '!') self._command_logger.debug('Start listenning ...') self._server_socket.listen(100)
def login(self, address, username, password, command_logger=None): # Get port self._port = ConfigurationParser.get("common_variable", "connection_port") addr = address.split(":")[0] if self._device_address != addr or self._device_user != username or self._device_password != password: self._connection = None self._device_address = addr self._device_user = username self._device_password = password # Print out device login information self._logger = command_logger self._logger.info('Attempting to Connect') self._logger.info('Default command timeout: ' + str(self._command_timeout)) self._logger.info('Device address is: ' + str(addr) + ', port: ' + str(self._port)) self._logger.info('Username: '******'Device Prompt: ' + str(self._prompt)) self._create_connection()
def get_resource_attribute(self, name, index=None): resource_name_template = ConfigurationParser.get( 'driver_variable', name, self._depth) if index is not None: resource_name_template = resource_name_template.format(index) return resource_name_template
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()
def get_resource_description(self, address, command_logger=None): switchfamily = ConfigurationParser.get("driver_variable", "switch_family") switchmodel = ConfigurationParser.get("driver_variable", "switch_model") bladeprefix = ConfigurationParser.get("driver_variable", "blade_name_prefix") bladefamily = ConfigurationParser.get("driver_variable", "blade_family") blademodel = ConfigurationParser.get("driver_variable", "blade_model") portprefix = ConfigurationParser.get("driver_variable", "port_name_prefix") portfamily = ConfigurationParser.get("driver_variable", "port_family") portmodel = ConfigurationParser.get("driver_variable", "port_model") log2phy_in = ConfigurationParser.get("driver_variable", "dict_logical_port_to_physical_input_port") log2phy_out = ConfigurationParser.get("driver_variable", "dict_logical_port_to_physical_output_port") phy2log_in = {} phy2log_out = {} for log in log2phy_in: phy2log_in[log2phy_in[log]] = log for log in log2phy_out: phy2log_out[log2phy_out[log]] = log sw = ResourceInfo2('', address, switchfamily, switchmodel, serial=address) switchstate = self.send_command('switchstate') switchstate = re.sub(r'.\[\d+m', '', switchstate) self.log('CLEANED SWITCH STATE: (((' + switchstate + ')))') outaddr2inaddrstatus = {} max_global_row = -1 for line in switchstate.split('\n'): line = line.strip() matches = re.search(r"R(?P<global_row>\d+)#" r"(?P<state>[a-zA-Z])" r"\s+(?P<status0>[a-zA-Z]+)(?P<addr0>\d+)" r"\s+(?P<status1>[a-zA-Z]+)(?P<addr1>\d+)" r"\s+(?P<status2>[a-zA-Z]+)(?P<addr2>\d+)" r"\s+(?P<status3>[a-zA-Z]+)(?P<addr3>\d+)" r"\s+(?P<status4>[a-zA-Z]+)(?P<addr4>\d+)" r"\s+(?P<status5>[a-zA-Z]+)(?P<addr5>\d+)" r"\s+(?P<status6>[a-zA-Z]+)(?P<addr6>\d+)" r"\s+(?P<status7>[a-zA-Z]+)(?P<addr7>\d+)" r"\s+(?P<status8>[a-zA-Z]+)(?P<addr8>\d+)" r"\s+(?P<status9>[a-zA-Z]+)(?P<addr9>\d+)" r"\s+(?P<status10>[a-zA-Z]+)(?P<addr10>\d+)" r"\s+(?P<status11>[a-zA-Z]+)(?P<addr11>\d+)", line, re.DOTALL) if matches: d = matches.groupdict() global_row = int(d['global_row']) for col in range(0, 12): outaddr = global_row * 12 + col inaddr = d['addr' + str(col)] status = d['status' + str(col)] if not status.startswith('U'): outaddr2inaddrstatus[outaddr] = (inaddr, status) if global_row > max_global_row: max_global_row = global_row for module in range(0, (max_global_row + 1) / 8): bladeserial = address + '/' + str(module) blade = ResourceInfo2('%s%0.2d' % (bladeprefix, module), str(module), bladefamily, blademodel, serial=bladeserial) sw.subresources.append(blade) for row in range(0, 8): for col in range(0, 12): # portname = 'row_%d_col_%0.2d' % (row, col) # portaddr = '%d,%d' % (row, col) log_absaddr = (module * 8 + row) * 12 + col if str(log_absaddr) in log2phy_in: phy_absaddr_in = int(log2phy_in[str(log_absaddr)]) else: phy_absaddr_in = log_absaddr if str(log_absaddr) in log2phy_out: phy_absaddr_out = int(log2phy_out[str(log_absaddr)]) else: phy_absaddr_out = log_absaddr if phy_absaddr_in == phy_absaddr_out: phy_absaddr = '%d' % (phy_absaddr_in) else: phy_absaddr = '%d-%d' % (phy_absaddr_in, phy_absaddr_out) portname = '%s%0.4d' % (portprefix, log_absaddr) portaddr = '%s/%d/%s' % (address, module, phy_absaddr) portserial = portaddr if phy_absaddr_out in outaddr2inaddrstatus and outaddr2inaddrstatus[phy_absaddr_out][1].startswith( 'A'): conn_phy_absaddr = outaddr2inaddrstatus[phy_absaddr_out][0] if conn_phy_absaddr in phy2log_in: conn_log_absaddr = int(phy2log_in[conn_phy_absaddr]) else: conn_log_absaddr = int(conn_phy_absaddr) if str(conn_log_absaddr) in log2phy_in: a = log2phy_in[str(conn_log_absaddr)] else: a = str(conn_log_absaddr) if str(conn_log_absaddr) in log2phy_out: b = log2phy_out[str(conn_log_absaddr)] else: b = str(conn_log_absaddr) if a == b: ab = '%s' % (a) else: ab = '%s-%s' % (a, b) connglobrow = conn_log_absaddr / 12 connmodule = connglobrow / 8 mappath = '%s/%d/%s' % (address, connmodule, ab) else: mappath = None blade.subresources.append(ResourceInfo2( portname, portaddr, portfamily, portmodel, map_path=mappath, serial=portserial)) self.log('resource info xml: (((' + sw.to_string() + ')))') return XMLWrapper.parse_xml(sw.to_string())
def get_resource_attribute(self, name, index=None): resource_name_template = ConfigurationParser.get('driver_variable', name, self._depth) if index is not None: resource_name_template = resource_name_template.format(index) return resource_name_template
def get_resource_description(self, address, command_logger=None): switchfamily = ConfigurationParser.get("driver_variable", "switch_family") switchmodel = ConfigurationParser.get("driver_variable", "switch_model") bladeprefix = ConfigurationParser.get("driver_variable", "blade_name_prefix") bladefamily = ConfigurationParser.get("driver_variable", "blade_family") blademodel = ConfigurationParser.get("driver_variable", "blade_model") portprefix = ConfigurationParser.get("driver_variable", "port_name_prefix") portfamily = ConfigurationParser.get("driver_variable", "port_family") portmodel = ConfigurationParser.get("driver_variable", "port_model") sw = ResourceInfo2('', address, switchfamily, switchmodel, serial=address) switchstate = self.send_command('switchstate') switchstate = re.sub(r'.\[\d+m', '', switchstate) self.log('CLEANED SWITCH STATE: (((' + switchstate + ')))') outaddr2inaddrstatus = {} max_global_row = -1 for line in switchstate.split('\n'): line = line.strip() matches = re.search(r"R(?P<global_row>\d+)#" r"(?P<state>[a-zA-Z])" r"\s+(?P<status0>[a-zA-Z]+)(?P<addr0>\d+)" r"\s+(?P<status1>[a-zA-Z]+)(?P<addr1>\d+)" r"\s+(?P<status2>[a-zA-Z]+)(?P<addr2>\d+)" r"\s+(?P<status3>[a-zA-Z]+)(?P<addr3>\d+)" r"\s+(?P<status4>[a-zA-Z]+)(?P<addr4>\d+)" r"\s+(?P<status5>[a-zA-Z]+)(?P<addr5>\d+)" r"\s+(?P<status6>[a-zA-Z]+)(?P<addr6>\d+)" r"\s+(?P<status7>[a-zA-Z]+)(?P<addr7>\d+)" r"\s+(?P<status8>[a-zA-Z]+)(?P<addr8>\d+)" r"\s+(?P<status9>[a-zA-Z]+)(?P<addr9>\d+)" r"\s+(?P<status10>[a-zA-Z]+)(?P<addr10>\d+)" r"\s+(?P<status11>[a-zA-Z]+)(?P<addr11>\d+)", line, re.DOTALL) if matches: d = matches.groupdict() global_row = int(d['global_row']) for col in range(0, 12): outaddr = global_row*12 + col inaddr = d['addr' + str(col)] instatus = d['status' + str(col)] outaddr2inaddrstatus[outaddr] = (inaddr, instatus) if global_row > max_global_row: max_global_row = global_row for module in range(0, (max_global_row + 1) / 8): bladeserial = address + '/' + str(module) blade = ResourceInfo2('%s%0.2d' % (bladeprefix, module), str(module), bladefamily, blademodel, serial=bladeserial) sw.subresources.append(blade) for row in range(0,8): for col in range(0, 12): # portname = 'row_%d_col_%0.2d' % (row, col) # portaddr = '%d,%d' % (row, col) absaddr = (module * 8 + row) * 12 + col portname = '%s%0.4d' % (portprefix, absaddr) portaddr = '%d' % (absaddr) if absaddr in outaddr2inaddrstatus and outaddr2inaddrstatus[absaddr][1].startswith('A'): connabsaddr = int(outaddr2inaddrstatus[absaddr][0]) connglobrow = connabsaddr / 12 connmodule = connglobrow / 8 # connrow = connglobrow % 8 # conncol = connabsaddr % 12 mappath = '%s/%d/%d' % (address, connmodule, connabsaddr) else: mappath = None # todo Warning "An item with the same key has already been added." caused by nonblank mappath portserial = address + '/' + str(module) + '/' + portaddr blade.subresources.append(ResourceInfo2( portname, portaddr, portfamily, portmodel, map_path=mappath, serial=portserial)) self.log('resource info xml: (((' + sw.to_string() + ')))') return XMLWrapper.parse_xml(sw.to_string())