def test_parse_vlans(): """Test parse_vlans""" # Have to import the method here, otherwise anything starting with # "hil.ext" pollutes the env. from hil.ext.switches.common import parse_vlans assert parse_vlans('12,14') == ['12', '14'] assert parse_vlans('20-22') == ['20', '21', '22'] assert parse_vlans('1512') == ['1512'] assert parse_vlans('12,21-24,250,511-514') == [ '12', '21', '22', '23', '24', '250', '511', '512', '513', '514' ]
def _get_vlans(self, interface): """ Return the vlans of a trunk port. Does not include the native vlan. Use _get_native_vlan. Args: interface: interface to return the vlans of Returns: List containing the vlans of the form: [('vlan/vlan1', vlan1), ('vlan/vlan2', vlan2)] """ # It uses the REST API CLI which is slow but it is the only way # because the switch is VLAN centric. Doing a GET on interface won't # return the VLANs on it, we would have to do get on all vlans (if that # worked reliably in the first place) and then find our interface there # which is not feasible. if not self._is_port_on(interface): return [] response = self._get_port_info(interface) # finds a comma separated list of integers and/or ranges starting with # T. Sample T12,14-18,23,28,80-90 or T20 or T20,22 or T20-22 match = re.search(r'T(\d+(-\d+)?)(,\d+(-\d+)?)*', response) if match is None: return [] vlan_list = parse_vlans(match.group().replace('T', '')) return [('vlan/%s' % x, x) for x in vlan_list]
def _get_vlans(self, interface): """ Return the vlans of a trunk port. Does not include the native vlan. Use _get_native_vlan. Args: interface: interface to return the vlans of Returns: List containing the vlans of the form: [('vlan/vlan1', vlan1), ('vlan/vlan2', vlan2)] """ try: url = self._construct_url(interface, suffix='trunk') response = self._make_request('GET', url) root = etree.fromstring(response.text) vlans = root. \ find(self._construct_tag('allowed')).\ find(self._construct_tag('vlan')).\ find(self._construct_tag('add')).text # finds a comma separated list of integers and/or ranges. # Sample: 12,14-18,23,28,80-90 or 20 or 20,22 or 20-22 match = re.search(r'(\d+(-\d+)?)(,\d+(-\d+)?)*', vlans) if match is None: return [] vlan_list = parse_vlans(match.group()) return [('vlan/%s' % x, x) for x in vlan_list] except AttributeError: return []
def get_port_networks(self, ports): '''Returns dictionary of ports, each containing a list of related VLANs.''' port_configs = self._port_configs(ports) result = {} for k, v in port_configs.iteritems(): network_list = [] non_native_list = [] if 'Trunking Native Mode VLAN' not in v: # XXX (probable BUG): For some reason the last port on the # switch sometimes isn't read correctly. For now just don't use # that port for the test suite, and will skip it if this # happens. continue # Get native vlan then remove junk if native is not None or Dummy native_vlan = v['Trunking Native Mode VLAN'].strip() if int(native_vlan.split(' ')[0]) == int(self.switch.dummy_vlan): native_vlan = None elif native_vlan != 'none': native_vlan = native_vlan.split(' ')[0] else: native_vlan = None # Get other vlans trunk_vlans = v['Trunking VLANs Allowed'].strip() if trunk_vlans != 'none': non_native_list = parse_vlans(trunk_vlans) if native_vlan is not None: # Ensure that native vlan is not in the non-native list non_native_list.remove(native_vlan) network_list.append(('vlan/native', int(native_vlan))) for v in (non_native_list): network_list.append(('vlan/%s' % v, int(v))) result[k] = network_list return result
def _make_vlan_list(dirty_list): '''Create vlan list from switch config vlan ranges.''' ranges = dirty_list.replace(' (Inactive)', '') ranges = ranges.split('\r\n') ranges = ','.join(ranges) return parse_vlans(ranges)