Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- #import xmpp
- from time import sleep
- ## NOC modules
- from noc.lib.app import ExtApplication, view
- from noc.inv.models import *
- from noc.sa.models import *
- from noc.ip.models import *
- from noc.lib.text import split_alnum
- from noc.vc.models import *
- from noc.project.models import *
- from noc.main.models import *
- class INTERNETAppplication(ExtApplication):
- """
- sa.IZ-INTERNET application
- """
- title = "IZ-INTERNET"
- menu = "IZ-INTERNET"
- @view(method=["POST"], url=r"^$", access="read", api=True)
- def api_main(self, request):
- req = request.POST
- po = req['project']
- customer = "_".join(req['customer'].strip().split(" "))
- bid = req['bid']
- mo = ManagedObject.objects.get(id = req['managedobject'])
- speed = req['speed']
- user = request.user
- up = UserProfile.objects.get(user = user)
- upc = UserProfileContact.objects.filter(user_profile = up, notification_method = "xmpp")
- jids = []
- for c in upc:
- jids += [c.params]
- xmpp_jid = 'noc-notifier@bca.ru'
- xmpp_pwd = 'noc-notifier'
- GlobalVRF = VRF.objects.get(name="Global")
- try:
- confirm = req['confirm']
- except:
- confirm = "off"
- r = []
- try:
- int(speed)
- speed = speed + "M"
- except:
- pass
- try:
- int(bid)
- bd = "id-" + bid
- description = customer + " " + bd + " po " + po
- fqdn_cl="id-" + bid + ".kis.ru"
- except:
- bd = bid
- description = customer + " " + bd + " po " + po
- fqdn_cl = bid + ".kis.ru"
- try:
- Project.objects.get(code = po)
- return { "config": "PROJECT ALREADY EXISTS!!!", "success": "True" }
- except:
- pass
- interfaces = Interface.objects.filter(managed_object = mo.id, type = "physical").order_by('name')
- free_interface = ""
- if mo.profile_name == "Alcatel.7324RU":
- for interface in sorted(interfaces):
- if not interface.name.startswith("enet"):
- if interface.description == "" or interface.description.split(" ")[0] == "was" or interface.description.split(" ")[1] == "was":
- free_interface = interface
- break
- else:
- for interface in sorted(interfaces, key = lambda x: split_alnum(x.name)):
- if "/" in interface.name:
- if interface.description == None or interface.description.split(" ")[0] == "was":
- free_interface = interface
- break
- try:
- UP = SubInterface.objects.get(managed_object = mo.id, name = free_interface.name).untagged_vlan
- except:
- UP = 0
- if free_interface=="" or UP==1:
- return { "config": "NO FREE INTERFACE OR FREE INTERFACE IN DEFAULT (1) VLAN!!!", "success": "True" }
- config = ""
- if mo.profile_name == "Alcatel.7324RU":
- uplink1 = Interface.objects.get(managed_object = mo.id, name = "enet1")
- uplink2 = Interface.objects.get(managed_object = mo.id, name = "enet2")
- sub_uplink1 = SubInterface.objects.get(interface = uplink1)
- sub_uplink2 = SubInterface.objects.get(interface = uplink2)
- vlans = set()
- vlans.update(sub_uplink1.tagged_vlans)
- vlans.update(sub_uplink2.tagged_vlans)
- for v in range(901,999):
- if v not in vlans:
- vlan = str(v)
- break
- config += "switch vlan set " + str(vlan) + " " + free_interface.name + ":FU enet1~enet2:FT " + bd + "\n"
- config += "adsl pvc set " + free_interface.name + " 8 35 " + str(vlan) + " 0 General,General\n"
- config += "adsl profile map " + free_interface.name + " FAST-8M-1M auto\n"
- config += "adsl enable " + free_interface.name + "\n"
- config += "adsl name " + free_interface.name + " " + customer + " " + bd + "\n"
- config += "adsl tel " + free_interface.name + " po" + po + "\n"
- config += "config save"
- else:
- config += "configure terminal\n"
- config += "interface " + free_interface.name + "\n"
- config += " description " + description + "\n"
- config += " no shutdown\n"
- config += " end\n"
- config += "write"
- r += [{
- "interface" : free_interface.name,
- "mo" : mo,
- "config" : config
- }]
- config = ""
- p = Prefix.objects.filter(vrf = GlobalVRF.id, Prefix_group="Clients-p2p", afi=4).order_by('prefix')
- free = []
- for pref in p:
- for free_preffix in pref.iter_free():
- free += {free_preffix}
- try:
- if free[0]:
- break
- except:
- pass
- if free == []:
- return { "config": "NO FREE SUBNET!!!", "success": "True" }
- ip_prefix = str(free[0]).split('/')[0].split('.')
- ip_gw = str(ip_prefix[0]) + "." + str(ip_prefix[1]) + "." + str(ip_prefix[2]) + "." + str(int(ip_prefix[3])+1)
- ip_cl = str(ip_prefix[0]) + "." + str(ip_prefix[1]) + "." + str(ip_prefix[2]) + "." + str(int(ip_prefix[3])+2)
- new_prefix=str(free[0]).split('/')[0]+"/30"
- fqdn_gw=mo.name + "-" + "-".join("-".join(free_interface.name.split("/")).split(" ")) + ".kis.ru"
- if mo.profile_name == "Alcatel.7324RU":
- vcd = mo.vc_domain
- if uplink1.link:
- l1tag = str(SubInterface.objects.get(managed_object = uplink1.link.other(uplink1)[0].managed_object.id, name = uplink1.link.other(uplink1)[0].name).untagged_vlan)
- elif uplink2.link:
- l1tag = str(SubInterface.objects.get(managed_object = uplink2.link.other(uplink2)[0].managed_object.id, name = uplink2.link.other(uplink2)[0].name).untagged_vlan)
- else:
- 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" }
- else:
- vcd = mo.vc_domain
- l1tag = str(SubInterface.objects.get(managed_object = mo.id, name = free_interface.name).untagged_vlan)
- if vcd == None:
- 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" }
- elif vcd.name == "NN":
- return { "config": "For this Managed Object VC_DOMAIN set to NN. Change VC_DOMAIN or use Commands Snippets to configuring INTERNET service", "success": "True" }
- pe_num = 1
- for i in Interface.objects.filter(vc_domain=vcd.id).order_by("managed_object"):
- pe = i.managed_object
- config = "configure\n"
- if mo.profile_name == "Alcatel.7324RU":
- sub = i.name + "." + l1tag + "0" + vlan
- config += "interface " + sub +" l2transport\n"
- config += " description " + description + "\n"
- config += " encapsulation dot1q " + l1tag + " second-dot1q " + vlan + "\n"
- config += " rewrite ingress tag pop 2\n"
- config += "exit\n"
- else:
- sub = i.name + "." + l1tag
- config += "interface " + sub + " l2transport\n"
- config += " description " + description + "\n"
- config += " encapsulation dot1q " + l1tag + "\n"
- config += " rewrite ingress tag pop 1\n"
- config += "exit\n"
- if pe_num == 1:
- if len(set([z.managed_object.name for z in Interface.objects.filter(vc_domain=vcd.id)])) > 1:
- vpnid = str(VCDomain.objects.get(name="VPN-ID").get_free_label())
- bvi = "BV" + vpnid
- else:
- vpnid = ""
- vl = SubInterface.objects.filter(managed_object=i.managed_object.id, enabled_afi=["IPv4"])
- bv = set()
- for o in vl:
- if o.name.startswith("BV"):
- bv.add(o.name)
- for k in range(60000, 65000):
- bvi60k = "BV" + str(k)
- if bvi60k not in bv:
- bvi = bvi60k
- break
- config += "interface " + bvi + "\n"
- config += " description " + description + "\n"
- config += " ipv4 address " + ip_gw + "/30\n"
- if speed:
- config += " service-policy input " + speed + "-inet\n"
- config += " service-policy output " + speed + "-inet\n"
- else:
- config += " service-policy input SET-DSCP-0\n"
- config += "root\n"
- config += "l2vpn\n"
- config += " bridge group 1\n"
- config += " bridge-domain " + bd + "\n"
- config += " interface " + sub + "\n"
- config += " routed interface " + bvi + "\n"
- if len(set([k.managed_object for k in Interface.objects.filter(vc_domain=vcd.id)])) > 1:
- config += " vfi " + vpnid + "\n"
- config += " vpn-id " + vpnid + "\n"
- config += " autodiscovery bgp route-target 8371:" + vpnid + "\n"
- config += "root\n"
- config += "commit"
- r += [{
- "mo": pe,
- "interface": i,
- "subif": sub,
- "BVI": bvi,
- "config": config
- }]
- else:
- config += "l2vpn\n"
- config += " bridge group 1\n"
- config += " bridge-domain " + bd + "\n"
- config += " interface " + sub + "\n"
- if len(set([k.managed_object for k in Interface.objects.filter(vc_domain=vcd.id)])) > 1:
- config += " vfi " + vpnid + "\n"
- config += " vpn-id " + vpnid + "\n"
- config += " autodiscovery bgp route-target 8371:" + vpnid + "\n"
- config += "root\n"
- config += "commit"
- r += [{
- "mo": pe,
- "interface": i,
- "subif": sub,
- "config": config
- }]
- pe_num += 1
- if confirm == "on":
- result = ""
- project = Project(code = po, name = bid, description = "INTERNET for " + customer + " on " + mo.name )
- project.save()
- if not vpnid == "":
- VC(vc_domain = VCDomain.objects.get(name="VPN-ID"), l1 = int(vpnid), name = bd, project = project, description = customer).save()
- Prefix(prefix = new_prefix, vrf = GlobalVRF, description = customer, project = project).save()
- Address(address=ip_cl, fqdn=fqdn_cl, vrf=GlobalVRF, project = project).save()
- free_interface.description = description
- free_interface.profile = InterfaceProfile.objects.get(name="UNI").id
- free_interface.project = project
- free_interface.save()
- for m in r:
- mo = m['mo']
- commands = m['config'].split('\n')
- task = ReduceTask.create_task(object_selector=mo,
- map_script="commands", map_script_params={"commands":commands},
- reduce_script="pyrule:mrt_result", reduce_script_params={},
- timeout=180)
- ############### "WHILE" DO NOT WORK. WHY??? #####
- # while not task.complete:
- # sleep(0.1)
- ##################################################
- if "BVI" in m:
- fqdn_gw=m['mo'].name + "-" + bvi + ".kis.ru"
- Address(address=ip_gw, fqdn=fqdn_gw, vrf = GlobalVRF, project = project).save()
- iface = Interface(managed_object=mo, type="SVI", name=bvi, project = project, description = description)
- siface = SubInterface(managed_object=mo, interface=iface, name=bvi, enabled_afi=["IPv4"], project = project, description = description, ipv4_addresses = [ip_gw + '/30'])
- iface.save()
- siface.save()
- if "subif" in m:
- SubInterface(managed_object=mo.id, interface=m['interface'], name=m['subif'], project = project, description = description).save()
- ## Send cinfig via jabber. TODO import xmpp in virtualenv
- if len(jids)>0:
- msg = m['config']
- # for to in jids:
- # jid = xmpp.protocol.JID(xmpp_jid)
- # client = xmpp.Client(jid.getDomain(),debug=[])
- # client.connect()
- # client.auth(jid.getNode(),str(xmpp_pwd),resource='xmpppy')
- # client.send(xmpp.protocol.Message(to,msg))
- # client.disconnect()
- result += m['mo'].name + "\n\n" + m['config'] + "\n\n\n"
- result = "DEVICES CONFIGURED\n\n\n" + result
- else:
- result = ""
- for q in r:
- result += q['mo'].name + "\n\n" + q['config'] + "\n\n\n"
- return { "config": result, "success" : True }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement