Advertisement
Guest User

Untitled

a guest
Aug 9th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##----------------------------------------------------------------------
  3. ## Huawei.VRP.get_interfaces
  4. ##----------------------------------------------------------------------
  5. ## Copyright (C) 2007-2012 The NOC Project
  6. ## See LICENSE for details
  7. ##----------------------------------------------------------------------
  8. """
  9. """
  10. # Python modules
  11. import re
  12. from collections import defaultdict
  13. # NOC modules
  14. from noc.lib.ip import IPv4
  15. from noc.sa.script import Script as NOCScript
  16. from noc.sa.interfaces import IGetInterfaces, InterfaceTypeError, MACAddressParameter
  17.  
  18. class Script(NOCScript):
  19. name = "Huawei.VRP.get_interfaces"
  20. implements = [IGetInterfaces]
  21.  
  22. TIMEOUT = 240
  23. cache = True
  24. types = {
  25. "Eth": "physical",
  26. "Trunk": "aggregated",
  27. "VLAN": "SVI",
  28. "Loopback": "loopback"
  29. }
  30.  
  31.  
  32. rx_ip_int_descr_2326 = re.compile(r".*(?P<name>Vlanif\d+)\s+(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(?P<mask>\d{2})\s+(?P<oper_status>U|D)\s+(?P<proto_status>U|D)\s+(?P<description>\S*)\n",
  33. re.MULTILINE | re.IGNORECASE | re.DOTALL)
  34.  
  35. rx_if_descr_block = re.compile(
  36. r"Interface\s+Description$",
  37. re.MULTILINE | re.DOTALL | re.IGNORECASE)
  38. rx_if_descr = re.compile(
  39. r"^\s*(?P<interface>[^ ]+)\s+(?P<description>).*$",
  40. re.MULTILINE | re.DOTALL | re.IGNORECASE)
  41. rx_ifc_status = re.compile(
  42. r"^\s*(?P<interface>[^ ]+) current state :.*?(?P<status>up|down)",
  43. re.IGNORECASE)
  44. rx_ifc_block = re.compile(
  45. r"Interface\s+(PHY|Physical)\s+Protocol[^\n]+\n(?P<block>.*)$",
  46. re.MULTILINE | re.DOTALL | re.IGNORECASE)
  47. rx_ifc_br_status = re.compile(
  48. r"^\s*(?P<interface>[^ ]+)\s+(?P<status>up|down|\*down).*$",
  49. re.IGNORECASE)
  50.  
  51.  
  52. def execute(self):
  53. ifaces = {}
  54. current = None
  55. is_bundle = False
  56. is_svi = False
  57. vlan_ids = []
  58. mac_svi = ""
  59. ipv4_svi = ""
  60. name_ = {}
  61. mac_ = {}
  62. descr_ = {}
  63. status_ = {}
  64. operstatus_ = {}
  65. adminstatus_ = {}
  66. tagged_ = {}
  67. untagged_ = {}
  68. loopback_ = {}
  69. iftype_ = {}
  70. end_if = False
  71. ipv4_ = {}
  72.  
  73. # Tested only on 2326 3328 5300 5328
  74. if (not (self.match_version(platform__contains="2326") or self.match_version(platform__contains="5328")
  75. or self.match_version(platform__contains="5300") or self.match_version(platform__contains="3328"))):
  76. raise self.NotSupportedError()
  77.  
  78. # Get switch MAC, for Huawei it is the same for all Ethernet ports, see 'display interface' for example
  79. for p in self.scripts.get_arp():
  80. intf = p["interface"]
  81. if "Vlanif" in intf:
  82. mac_svi = p["mac"]
  83. ipv4_svi = p["ip"]
  84. # mac_[intf] = p["mac"]
  85. # ipv4_[intf] = p["ip"]
  86.  
  87. # Get interface status, fill MAC and status lists
  88. for p in self.scripts.get_interface_status():
  89. intf = p["interface"]
  90. if "LoopBack" in intf:
  91. iftype_[intf] = "loopback"
  92. elif "Ethernet" in intf:
  93. iftype_[intf] = "physical"
  94. name_[intf] = intf
  95. elif "Trunk" in intf:
  96. iftype_[intf] = "aggregated"
  97. name_[intf] = intf
  98. elif "Vlanif" in intf:
  99. iftype_[intf] = "SVI"
  100. mac_[intf] = mac_svi
  101. operstatus_[intf] = p["status"]
  102. adminstatus_[intf] = p["adminstatus"]
  103.  
  104.  
  105. # Get switchports and fill tagged/untagged/description lists if they are not empty
  106. for p in self.scripts.get_switchport():
  107. intf = p["interface"]
  108. if "tagged" in p and p["tagged"]:
  109. tagged_[intf] = p["tagged"]
  110. if "untagged" in p and p["untagged"]:
  111. untagged_[intf] = p["untagged"]
  112. if "description" in p and p["description"]:
  113. descr_[intf] = p["description"]
  114.  
  115.  
  116. # try:
  117. # interface_descr = self.cli("display interface description", cached=True)
  118. # except self.CLISyntaxError:
  119. # raise self.NotSupportedError()
  120.  
  121. # ip = match.group("ip")
  122. # mask = match.group("mask")
  123. # ip_addr += [IPv4(ip/mask).prefix]
  124. #
  125. # if (ls.strip().startswith("Vlanif")):
  126. # if not ip_addr:
  127. # continue
  128. # type = "SVI"
  129. # vlan_ids = [int(namesviif[7:])]
  130. # mac_svi = mac_[namesviif]
  131. # sub = {
  132. # "name": namesviif,
  133. # "admin_status": stat == "up",
  134. # "oper_status": stat == "up",
  135. # "is_ipv4": True,
  136. # "ipv4_addresses": ip_addr,
  137. # "vlan_ids": vlan_ids,
  138. # "mac": mac_svi,
  139. # }
  140. # ifaces[namesviif] = {
  141. # "name": namesviif,
  142. # "admin_status": stat == "up",
  143. # "oper_status": stat == "up",
  144. # "type": type,
  145. # "mac": mac_svi,
  146. # "subinterfaces": [sub],
  147. # }
  148.  
  149. # Pre-process portchannel members
  150. portchannel_members = {}
  151. for pc in self.scripts.get_portchannel():
  152. i = pc["interface"]
  153. t = pc["type"] == "L"
  154. for m in pc["members"]:
  155. portchannel_members[m] = (i, t)
  156.  
  157.  
  158. # Simulate hard working
  159.  
  160. # set name ifaces and skip SVI's
  161. for current in name_:
  162. is_svi = current.startswith("Vlanif")
  163. if is_svi:
  164. continue
  165. ifaces[current] = {
  166. "name": current
  167. }
  168. # other
  169. for current in ifaces:
  170. is_svi = current.startswith("Vlanif")
  171. if is_svi:
  172. continue
  173. is_bundle = current.startswith("Eth-Trunk")
  174. if is_bundle:
  175. ifaces[current]["mac"] = mac_[current]
  176. ifaces[current]["admin_status"] = adminstatus_[current]
  177. ifaces[current]["oper_status"] = operstatus_[current]
  178. ifaces[current]["type"] = "aggregated"
  179. # Sub-interface
  180. sub = {
  181. "name": current,
  182. "admin_status": adminstatus_[current],
  183. "oper_status": operstatus_[current],
  184. "is_bridge": True,
  185. "tagged_vlan": tagged_[current],
  186. "mac": mac_[current],
  187. }
  188. if current in untagged_:
  189. sub["untagged_vlans"] = untagged_[current]
  190. if current in descr_:
  191. ifaces[current]["description"] = descr_[current]
  192. sub["description"] = descr_[current]
  193. ifaces[current]["subinterfaces"] = [sub]
  194.  
  195. else:
  196. ifaces[current]["mac"] = mac_[current]
  197. ifaces[current]["admin_status"] = adminstatus_[current]
  198. ifaces[current]["oper_status"] = operstatus_[current]
  199. ifaces[current]["type"] = iftype_[current]
  200. sub = {
  201. "name": current,
  202. "admin_status": adminstatus_[current],
  203. "oper_status": operstatus_[current],
  204. "is_bridge": True,
  205. "mac": mac_[current],
  206. }
  207.  
  208. if current in descr_:
  209. ifaces[current]["description"] = descr_[current]
  210. sub["description"] = descr_[current]
  211. ifaces[current]["subinterfaces"] = [sub]
  212. if current in tagged_:
  213. sub["tagged_vlans"] = tagged_[current]
  214. if current in untagged_:
  215. sub["untagged_vlan"] = untagged_[current]
  216.  
  217.  
  218. # Portchannel member
  219. if current in portchannel_members:
  220. ai, is_lacp = portchannel_members[current]
  221. ifaces[current]["aggregated_interface"] = ai
  222. ifaces[current]["is_lacp"] = is_lacp
  223.  
  224.  
  225. # Get VRFs and "default" VRF interfaces
  226. r = []
  227. seen = set()
  228. vpns = [{
  229. "name": "default",
  230. "type": "ip",
  231. "interfaces": []
  232. }]
  233. for fi in vpns:
  234. # Forwarding instance
  235. rr = {
  236. "forwarding_instance": fi["name"],
  237. "type": fi["type"],
  238. "interfaces": []
  239. }
  240. rd = fi.get("rd")
  241. if rd:
  242. rr["rd"] = rd
  243. # create ifaces
  244.  
  245. rr["interfaces"] = ifaces.values()
  246. r += [rr]
  247. # Return result
  248. return r
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement