Advertisement
Guest User

Untitled

a guest
May 5th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.83 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. #import xmpp
  4. from time import sleep
  5.  
  6. ## NOC modules
  7. from noc.lib.app import ExtApplication, view
  8. from noc.inv.models import *
  9. from noc.sa.models import *
  10. from noc.ip.models import *
  11. from noc.lib.text import split_alnum
  12. from noc.vc.models import *
  13. from noc.project.models import *
  14. from noc.main.models import *
  15.  
  16. class INTERNETAppplication(ExtApplication):
  17. """
  18. sa.IZ-INTERNET application
  19. """
  20. title = "IZ-INTERNET"
  21. menu = "IZ-INTERNET"
  22. @view(method=["POST"], url=r"^$", access="read", api=True)
  23. def api_main(self, request):
  24. req = request.POST
  25. po = req['project']
  26. customer = "_".join(req['customer'].strip().split(" "))
  27. bid = req['bid']
  28. mo = ManagedObject.objects.get(id = req['managedobject'])
  29. speed = req['speed']
  30. user = request.user
  31. up = UserProfile.objects.get(user = user)
  32. upc = UserProfileContact.objects.filter(user_profile = up, notification_method = "xmpp")
  33. jids = []
  34. for c in upc:
  35. jids += [c.params]
  36. xmpp_jid = 'noc-notifier@bca.ru'
  37. xmpp_pwd = 'noc-notifier'
  38. GlobalVRF = VRF.objects.get(name="Global")
  39.  
  40. try:
  41. confirm = req['confirm']
  42. except:
  43. confirm = "off"
  44.  
  45. r = []
  46.  
  47. try:
  48. int(speed)
  49. speed = speed + "M"
  50. except:
  51. pass
  52.  
  53. try:
  54. int(bid)
  55. bd = "id-" + bid
  56. description = customer + " " + bd + " po " + po
  57. fqdn_cl="id-" + bid + ".kis.ru"
  58. except:
  59. bd = bid
  60. description = customer + " " + bd + " po " + po
  61. fqdn_cl = bid + ".kis.ru"
  62. try:
  63. Project.objects.get(code = po)
  64. return { "config": "PROJECT ALREADY EXISTS!!!", "success": "True" }
  65. except:
  66. pass
  67.  
  68. interfaces = Interface.objects.filter(managed_object = mo.id, type = "physical").order_by('name')
  69. free_interface = ""
  70. if mo.profile_name == "Alcatel.7324RU":
  71. for interface in sorted(interfaces):
  72. if not interface.name.startswith("enet"):
  73. if interface.description == "" or interface.description.split(" ")[0] == "was" or interface.description.split(" ")[1] == "was":
  74. free_interface = interface
  75. break
  76. else:
  77. for interface in sorted(interfaces, key = lambda x: split_alnum(x.name)):
  78. if "/" in interface.name:
  79. if interface.description == None or interface.description.split(" ")[0] == "was":
  80. free_interface = interface
  81. break
  82. try:
  83. UP = SubInterface.objects.get(managed_object = mo.id, name = free_interface.name).untagged_vlan
  84. except:
  85. UP = 0
  86. if free_interface=="" or UP==1:
  87. return { "config": "NO FREE INTERFACE OR FREE INTERFACE IN DEFAULT (1) VLAN!!!", "success": "True" }
  88.  
  89. config = ""
  90.  
  91. if mo.profile_name == "Alcatel.7324RU":
  92. uplink1 = Interface.objects.get(managed_object = mo.id, name = "enet1")
  93. uplink2 = Interface.objects.get(managed_object = mo.id, name = "enet2")
  94. sub_uplink1 = SubInterface.objects.get(interface = uplink1)
  95. sub_uplink2 = SubInterface.objects.get(interface = uplink2)
  96. vlans = set()
  97. vlans.update(sub_uplink1.tagged_vlans)
  98. vlans.update(sub_uplink2.tagged_vlans)
  99. for v in range(901,999):
  100. if v not in vlans:
  101. vlan = str(v)
  102. break
  103.  
  104. config += "switch vlan set " + str(vlan) + " " + free_interface.name + ":FU enet1~enet2:FT " + bd + "\n"
  105. config += "adsl pvc set " + free_interface.name + " 8 35 " + str(vlan) + " 0 General,General\n"
  106. config += "adsl profile map " + free_interface.name + " FAST-8M-1M auto\n"
  107. config += "adsl enable " + free_interface.name + "\n"
  108. config += "adsl name " + free_interface.name + " " + customer + " " + bd + "\n"
  109. config += "adsl tel " + free_interface.name + " po" + po + "\n"
  110. config += "config save"
  111. else:
  112. config += "configure terminal\n"
  113. config += "interface " + free_interface.name + "\n"
  114. config += " description " + description + "\n"
  115. config += " no shutdown\n"
  116. config += " end\n"
  117. config += "write"
  118. r += [{
  119. "interface" : free_interface.name,
  120. "mo" : mo,
  121. "config" : config
  122. }]
  123.  
  124. config = ""
  125.  
  126. p = Prefix.objects.filter(vrf = GlobalVRF.id, Prefix_group="Clients-p2p", afi=4).order_by('prefix')
  127. free = []
  128. for pref in p:
  129. for free_preffix in pref.iter_free():
  130. free += {free_preffix}
  131. try:
  132. if free[0]:
  133. break
  134. except:
  135. pass
  136. if free == []:
  137. return { "config": "NO FREE SUBNET!!!", "success": "True" }
  138.  
  139. ip_prefix = str(free[0]).split('/')[0].split('.')
  140. ip_gw = str(ip_prefix[0]) + "." + str(ip_prefix[1]) + "." + str(ip_prefix[2]) + "." + str(int(ip_prefix[3])+1)
  141. ip_cl = str(ip_prefix[0]) + "." + str(ip_prefix[1]) + "." + str(ip_prefix[2]) + "." + str(int(ip_prefix[3])+2)
  142.  
  143. new_prefix=str(free[0]).split('/')[0]+"/30"
  144. fqdn_gw=mo.name + "-" + "-".join("-".join(free_interface.name.split("/")).split(" ")) + ".kis.ru"
  145.  
  146. if mo.profile_name == "Alcatel.7324RU":
  147. vcd = mo.vc_domain
  148. if uplink1.link:
  149. l1tag = str(SubInterface.objects.get(managed_object = uplink1.link.other(uplink1)[0].managed_object.id, name = uplink1.link.other(uplink1)[0].name).untagged_vlan)
  150. elif uplink2.link:
  151. l1tag = str(SubInterface.objects.get(managed_object = uplink2.link.other(uplink2)[0].managed_object.id, name = uplink2.link.other(uplink2)[0].name).untagged_vlan)
  152. else:
  153. return { "config": "CONNECT DSLAM TO MEN SWITCH\n\n 1. Go to Inventory > Interfaces\n 2. Select switch or DSLAM in dropdown menu\n 3. Click link button on proper interface\n 4. Set remote interface", "success": "True" }
  154.  
  155. else:
  156. vcd = mo.vc_domain
  157. l1tag = str(SubInterface.objects.get(managed_object = mo.id, name = free_interface.name).untagged_vlan)
  158.  
  159. if vcd == None:
  160. return { "config": "Set proper VC_DOMAIN for Managed Object\n\n 1. Go to Service ACtivation > Managed Objects\n 2. Find Managed Object\n 3. Click Edit\n 4. Select proper VC_DOMAIN in dropdown menu", "success": "True" }
  161. elif vcd.name == "NN":
  162. return { "config": "For this Managed Object VC_DOMAIN set to NN. Change VC_DOMAIN or use Commands Snippets to configuring INTERNET service", "success": "True" }
  163.  
  164. pe_num = 1
  165. for i in Interface.objects.filter(vc_domain=vcd.id).order_by("managed_object"):
  166. pe = i.managed_object
  167. config = "configure\n"
  168. if mo.profile_name == "Alcatel.7324RU":
  169. sub = i.name + "." + l1tag + "0" + vlan
  170. config += "interface " + sub +" l2transport\n"
  171. config += " description " + description + "\n"
  172. config += " encapsulation dot1q " + l1tag + " second-dot1q " + vlan + "\n"
  173. config += " rewrite ingress tag pop 2\n"
  174. config += "exit\n"
  175.  
  176. else:
  177. sub = i.name + "." + l1tag
  178. config += "interface " + sub + " l2transport\n"
  179. config += " description " + description + "\n"
  180. config += " encapsulation dot1q " + l1tag + "\n"
  181. config += " rewrite ingress tag pop 1\n"
  182. config += "exit\n"
  183. if pe_num == 1:
  184. if len(set([z.managed_object.name for z in Interface.objects.filter(vc_domain=vcd.id)])) > 1:
  185. vpnid = str(VCDomain.objects.get(name="VPN-ID").get_free_label())
  186. bvi = "BV" + vpnid
  187. else:
  188. vpnid = ""
  189. vl = SubInterface.objects.filter(managed_object=i.managed_object.id, enabled_afi=["IPv4"])
  190. bv = set()
  191. for o in vl:
  192. if o.name.startswith("BV"):
  193. bv.add(o.name)
  194. for k in range(60000, 65000):
  195. bvi60k = "BV" + str(k)
  196. if bvi60k not in bv:
  197. bvi = bvi60k
  198. break
  199.  
  200. config += "interface " + bvi + "\n"
  201. config += " description " + description + "\n"
  202. config += " ipv4 address " + ip_gw + "/30\n"
  203.  
  204. if speed:
  205. config += " service-policy input " + speed + "-inet\n"
  206. config += " service-policy output " + speed + "-inet\n"
  207. else:
  208. config += " service-policy input SET-DSCP-0\n"
  209. config += "root\n"
  210. config += "l2vpn\n"
  211. config += " bridge group 1\n"
  212. config += " bridge-domain " + bd + "\n"
  213. config += " interface " + sub + "\n"
  214. config += " routed interface " + bvi + "\n"
  215. if len(set([k.managed_object for k in Interface.objects.filter(vc_domain=vcd.id)])) > 1:
  216. config += " vfi " + vpnid + "\n"
  217. config += " vpn-id " + vpnid + "\n"
  218. config += " autodiscovery bgp route-target 8371:" + vpnid + "\n"
  219. config += "root\n"
  220. config += "commit"
  221.  
  222. r += [{
  223. "mo": pe,
  224. "interface": i,
  225. "subif": sub,
  226. "BVI": bvi,
  227. "config": config
  228. }]
  229.  
  230. else:
  231. config += "l2vpn\n"
  232. config += " bridge group 1\n"
  233. config += " bridge-domain " + bd + "\n"
  234. config += " interface " + sub + "\n"
  235. if len(set([k.managed_object for k in Interface.objects.filter(vc_domain=vcd.id)])) > 1:
  236. config += " vfi " + vpnid + "\n"
  237. config += " vpn-id " + vpnid + "\n"
  238. config += " autodiscovery bgp route-target 8371:" + vpnid + "\n"
  239. config += "root\n"
  240. config += "commit"
  241. r += [{
  242. "mo": pe,
  243. "interface": i,
  244. "subif": sub,
  245. "config": config
  246. }]
  247.  
  248. pe_num += 1
  249.  
  250. if confirm == "on":
  251. result = ""
  252. project = Project(code = po, name = bid, description = "INTERNET for " + customer + " on " + mo.name )
  253. project.save()
  254.  
  255. if not vpnid == "":
  256. VC(vc_domain = VCDomain.objects.get(name="VPN-ID"), l1 = int(vpnid), name = bd, project = project, description = customer).save()
  257.  
  258. Prefix(prefix = new_prefix, vrf = GlobalVRF, description = customer, project = project).save()
  259. Address(address=ip_cl, fqdn=fqdn_cl, vrf=GlobalVRF, project = project).save()
  260. free_interface.description = description
  261. free_interface.profile = InterfaceProfile.objects.get(name="UNI").id
  262. free_interface.project = project
  263. free_interface.save()
  264.  
  265. for m in r:
  266. mo = m['mo']
  267. commands = m['config'].split('\n')
  268. task = ReduceTask.create_task(object_selector=mo,
  269. map_script="commands", map_script_params={"commands":commands},
  270. reduce_script="pyrule:mrt_result", reduce_script_params={},
  271. timeout=180)
  272.  
  273. ############### "WHILE" DO NOT WORK. WHY??? #####
  274. # while not task.complete:
  275. # sleep(0.1)
  276. ##################################################
  277.  
  278. if "BVI" in m:
  279. fqdn_gw=m['mo'].name + "-" + bvi + ".kis.ru"
  280. Address(address=ip_gw, fqdn=fqdn_gw, vrf = GlobalVRF, project = project).save()
  281. iface = Interface(managed_object=mo, type="SVI", name=bvi, project = project, description = description)
  282. siface = SubInterface(managed_object=mo, interface=iface, name=bvi, enabled_afi=["IPv4"], project = project, description = description, ipv4_addresses = [ip_gw + '/30'])
  283. iface.save()
  284. siface.save()
  285. if "subif" in m:
  286. SubInterface(managed_object=mo.id, interface=m['interface'], name=m['subif'], project = project, description = description).save()
  287.  
  288. ## Send cinfig via jabber. TODO import xmpp in virtualenv
  289. if len(jids)>0:
  290. msg = m['config']
  291. # for to in jids:
  292. # jid = xmpp.protocol.JID(xmpp_jid)
  293. # client = xmpp.Client(jid.getDomain(),debug=[])
  294. # client.connect()
  295. # client.auth(jid.getNode(),str(xmpp_pwd),resource='xmpppy')
  296. # client.send(xmpp.protocol.Message(to,msg))
  297. # client.disconnect()
  298. result += m['mo'].name + "\n\n" + m['config'] + "\n\n\n"
  299. result = "DEVICES CONFIGURED\n\n\n" + result
  300. else:
  301. result = ""
  302. for q in r:
  303. result += q['mo'].name + "\n\n" + q['config'] + "\n\n\n"
  304.  
  305. return { "config": result, "success" : True }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement