Guest User

Untitled

a guest
Jan 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import unittest
  4.  
  5. def vlan_parser(vlan_list):
  6. '''
  7. Input: Unsorted list of vlan integers
  8. Output: Sorted list of integers according to Cisco IOS vlan list rules
  9.  
  10. 1. Vlans are listed in ascending order
  11. 2. Runs of 3 or more consecutive vlans are listed with a dash
  12. 3. The first line of the list can be 48 characters long
  13. 4. Subsequents list line can be 44 characters
  14.  
  15. Example 1:
  16. interface GigabitEthernet1/36
  17. description EXAMPLE_1
  18. switchport trunk encapsulation dot1q
  19. switchport trunk allowed vlan 100,1688,3002-3005,3102-3105,3802,3900,3998,3999
  20. switchport mode trunk
  21.  
  22. Example 2:
  23. interface GigabitEthernet1/37
  24. description EXAMPLE_2
  25. switchport trunk encapsulation dot1q
  26. switchport trunk allowed vlan 100,1688,3002,3004,3005,3050,3102,3104,3105,3151
  27. switchport trunk allowed vlan add 3802,3900,3998,3999
  28. switchport mode trunk
  29. '''
  30.  
  31. #Sort and remove duplicates
  32. sorted_list = sorted(set(vlan_list))
  33.  
  34. parse_list = []
  35. idx = 0
  36. while idx < len(sorted_list):
  37. start = idx
  38. end = start
  39. while end < len(sorted_list) - 1:
  40. if sorted_list[end + 1] - sorted_list[end] == 1:
  41. end += 1
  42. else:
  43. break
  44. #print start, end
  45. if start == end:
  46. # Single VLAN
  47. parse_list.append(str(sorted_list[idx]))
  48. elif start + 1 == end:
  49. # Run of 2 VLANs
  50. parse_list.append(str(sorted_list[start]))
  51. parse_list.append(str(sorted_list[end]))
  52. else:
  53. # Run of 3 or more VLANs
  54. parse_list.append(str(sorted_list[start]) + '-' + str(sorted_list[end]))
  55. idx = end + 1
  56.  
  57. line_count = 0
  58. result = ['']
  59. for vlans in parse_list:
  60. #First line (" switchport trunk allowed vlan ")
  61. if line_count == 0:
  62. if len(result[line_count] + vlans) > 48:
  63. result.append('')
  64. line_count += 1
  65. result[line_count] += vlans + ','
  66. else:
  67. result[line_count] += vlans + ','
  68.  
  69. #Subsequent lines (" switchport trunk allowed vlan add ")
  70. else:
  71. if len(result[line_count] + vlans) > 44:
  72. result.append('')
  73. line_count += 1
  74. result[line_count] += vlans + ','
  75. else:
  76. result[line_count] += vlans + ','
  77.  
  78. #Remove trailing orphan commas
  79. for idx in range(0, len(result)):
  80. result[idx] = result[idx].rstrip(',')
  81.  
  82. #Sometimes text wraps to next line, but there are no remaining VLANs
  83. if '' in result:
  84. result.remove('')
  85.  
  86. return result
  87.  
  88. class VLANParserMethods(unittest.TestCase):
  89. '''
  90. Example1: Sorted single line with compression
  91. Example2: Sorted multiple line, no compression
  92. Example3: Unsorted duplicates, single line with compression
  93. '''
  94.  
  95. line1 = ' switchport trunk allowed vlan '
  96. line2 = ' switchport trunk allowed vlan add '
  97. ex1 = [100, 1688, 3002, 3003, 3004, 3005, 3102, 3103, 3104, 3105, 3802, 3900, 3998, 3999]
  98. ex2 = [100, 1688, 3002, 3004, 3005, 3050, 3102, 3104, 3105, 3151, 3802, 3900, 3998, 3999]
  99. ex3 = [3, 2, 1, 1, 2, 3, 1, 2, 3]
  100.  
  101. def test_example1(self):
  102. '''
  103. switchport trunk allowed vlan 100,1688,3002-3005,3102-3105,3802,3900,3998,3999
  104. '''
  105.  
  106. #Check number of lines in result
  107. self.assertTrue(len(vlan_parser(self.ex1)) == 1)
  108.  
  109. #Verify VLAN list isn't too long
  110. self.assertTrue(len(vlan_parser(self.ex1)[0]) <= 48)
  111. for i in range(1, len(vlan_parser(self.ex1))):
  112. self.assertTrue(len(vlan_parser(self.ex1)[i]) <= 44)
  113.  
  114. #Verify entire command less than 80 chars
  115. self.assertTrue(len(self.line1 + vlan_parser(self.ex1)[0]) <= 80)
  116.  
  117. #Validate parsing rules
  118. self.assertTrue(vlan_parser(self.ex1)
  119. == ['100,1688,3002-3005,3102-3105,3802,3900,3998,3999'])
  120.  
  121. def test_example2(self):
  122. '''
  123. switchport trunk allowed vlan 100,1688,3002,3004,3005,3050,3102,3104,3105,3151
  124. switchport trunk allowed vlan add 3802,3900,3998,3999
  125. '''
  126.  
  127. #Check number of lines in result
  128. self.assertTrue(len(vlan_parser(self.ex2)) == 2)
  129.  
  130. #Verify VLAN list isn't too long
  131. self.assertTrue(len(vlan_parser(self.ex2)[0]) <= 48)
  132. for i in range(1, len(vlan_parser(self.ex2))):
  133. self.assertTrue(len(vlan_parser(self.ex2)[i]) <= 44)
  134.  
  135. #Verify entire command less than 80 chars
  136. self.assertTrue(len(self.line1 + vlan_parser(self.ex2)[0]) <= 80)
  137. for i in range(1, len(vlan_parser(self.ex2))):
  138. self.assertTrue(len(self.line2 + vlan_parser(self.ex2)[i]) <= 80)
  139.  
  140. #Validate parsing rules
  141. self.assertTrue(vlan_parser(self.ex2)
  142. == ['100,1688,3002,3004,3005,3050,3102,3104,3105,3151',
  143. '3802,3900,3998,3999'])
  144.  
  145. def test_example3(self):
  146. '''
  147. switchport trunk allowed vlan 1,2,3
  148. '''
  149.  
  150. #Check number of lines in result
  151. self.assertTrue(len(vlan_parser(self.ex3)) == 1)
  152.  
  153. #Verify VLAN list isn't too long
  154. self.assertTrue(len(vlan_parser(self.ex3)[0]) <= 48)
  155. for i in range(1, len(vlan_parser(self.ex3))):
  156. self.assertTrue(len(vlan_parser(self.ex3)[i]) <= 44)
  157.  
  158. #Verify entire command less than 80 chars
  159. self.assertTrue(len(self.line1 + vlan_parser(self.ex3)[0]) <= 80)
  160.  
  161. #Validate parsing rules
  162. self.assertTrue(vlan_parser(self.ex3) == ['1-3'])
  163.  
  164. if __name__ == '__main__':
  165. unittest.main()
Add Comment
Please, Sign In to add comment