def get_lamp_topology(): # GFR: When using the lamp@slice certificate # there's no need for credential # #slice_cred = get_slice_cred(cred_file) #body = make_UNISQueryCred_message(slice_cred) # Note that UNIS will return only the slice view for the Query All body = make_UNISQueryAll_message() try: client = SimpleClient(host=host, port=port, uri=uri, cert=cert_file, key=key_file) response = client.send_request(body, useSSL=True) except: print "Error contacting UNIS" return print response #t = create_from_xml_file(tfile) t = create_from_xml_string(response) if not t: print "Could not parse any topologies" return save_parsed_elements(t[0]) types = ['in', 'out', 'cpu', 'mem'] slices = [] for d in t[0].get_domains(): id_fields = get_urn_value(d.unis_id, 'domain').split('+') if len(id_fields) == 3 and id_fields[1] == 'slice': slices.append(id_fields[2] + '@' + id_fields[0]) for n in d.get_nodes(): for p in n.get_ports(): pa = PortAddresses.objects.filter(port__unis_id=p.unis_id, address__type='ipv4') #pn = NetworkObjectNames.objects.filter(networkobject=p) for type in types: urn = UrnStub(urn=pa[0].address.value + '-' + type, ifName=get_urn_value(p.unis_id, 'port'), ifHost=get_urn_value(n.unis_id, 'node'), ifAddress=pa[0].address.value, type=type, source='MA') urn.save() t[0].unis_id = "Topology for slice " + slices[0] t[0].save()
def process_domain(domain): domainID = get_urn_value(domain.getAttribute('id'), 'domain') node = domain.getElementsByTagNameNS(TOPOLOGY_NS, "node") d = Domain(domain=domainID) d.save() for n in node: process_node(n, d)
def process_node(node, domain): nodeID = get_urn_value(node.getAttribute('id'), 'node') address = node.getElementsByTagNameNS(TOPOLOGY_NS, "address") ports = node.getElementsByTagNameNS(TOPOLOGY_NS, "port") n = Node(domain=domain, node=nodeID, address=address[0].firstChild.data) n.save() for p in ports: process_port(p, n)
def process_port(port, node): portID = get_urn_value(port.getAttribute('id'), 'port') capacity = port.getElementsByTagNameNS(TOPOLOGY_NS, "capacity") maximumReservableCapacity = port.getElementsByTagNameNS(TOPOLOGY_NS, "maximumReservableCapacity") minimumReservableCapacity = port.getElementsByTagNameNS(TOPOLOGY_NS, "minimumReservableCapacity") granularity = port.getElementsByTagNameNS(TOPOLOGY_NS, "granularity") links = port.getElementsByTagNameNS(TOPOLOGY_NS, "link") p = Port(node=node, port=portID, capacity=capacity[0].firstChild.data, maximumReservableCapacity=maximumReservableCapacity[0].firstChild.data, minimumReservableCapacity=minimumReservableCapacity[0].firstChild.data, granularity=granularity[0].firstChild.data, ) p.save() for l in links: process_link(l, p)
def process_link(link, port): """ AH: Switching Capability Descriptors is NOT handled yet """ linkID = get_urn_value(link.getAttribute('id'), 'link') capacity = link.getElementsByTagNameNS(TOPOLOGY_NS, "capacity") maximumReservableCapacity = link.getElementsByTagNameNS(TOPOLOGY_NS, "maximumReservableCapacity") minimumReservableCapacity = link.getElementsByTagNameNS(TOPOLOGY_NS, "minimumReservableCapacity") granularity = link.getElementsByTagNameNS(TOPOLOGY_NS, "granularity") trafficEngineeringMetric = link.getElementsByTagNameNS(TOPOLOGY_NS, "trafficEngineeringMetric") remoteLink = link.getElementsByTagNameNS(TOPOLOGY_NS, "remoteLinkId") l = Link(port=port, link=linkID, capacity=capacity[0].firstChild.data, maximumReservableCapacity=maximumReservableCapacity[0].firstChild.data, minimumReservableCapacity=minimumReservableCapacity[0].firstChild.data, granularity=granularity[0].firstChild.data, trafficEngineeringMetric=trafficEngineeringMetric[0].firstChild.data, remoteLink=remoteLink[0].firstChild.data, ) l.save()
def handle(self, *args, **options): window = 24 * 60 * 60 if Metadata.objects.count() == 0: if create_measurement_mds() == None: return mds = Metadata.objects.filter(poll=True) # TODO: Assumes all mds use the same service, and single domain ma = mds[0].service if ma is None or not is_psservice_available(ma): xquery = GANGLIA_MA_XQUERY % create_urn( get_urn_value(mds[0].subject.unis_id, 'domain')) response = query_hls(settings.UNIS_INSTANCE, xquery, useSSL=True) service = etree.fromstring(response).find(".//{%s}service" % PSSERVICE_NS) if service is None: return accessPoint = service.find("{%s}accessPoint" % PSSERVICE_NS).text serviceNode = service.find("{%s}serviceNode" % PSSERVICE_NS).text serviceName = service.find("{%s}serviceName" % PSSERVICE_NS).text serviceType = service.find("{%s}serviceType" % PSSERVICE_NS).text serviceDescription = service.find("{%s}serviceDescription" % PSSERVICE_NS).text ma = create_psservice(serviceNode, serviceName, accessPoint, serviceType, serviceDescription) # Update all mds (remember we assume single MA for now) Metadata.objects.update(service=ma) # Update metadata keys if we have any missing if mds.filter(key=None).count() > 0: node_mds = [] port_mds = [] for md in mds.filter(key=None): if hasattr(md.subject, 'node'): node_mds.append(md) elif hasattr(md.subject, 'port'): port_mds.append(md) if len(node_mds): response = query_psservice(ma, NODE_MDK_REQUEST) root = etree.fromstring(response) for md in node_mds: addresses = [] for node_address in NodeAddresses.objects.filter( node=md.subject): addresses.append(node_address.address.value) addresses = ','.join(addresses) xpath = NODE_KEY_MD_XPATH % (addresses, md.event_type.value) metadata_ids = root.xpath(xpath) if not len(metadata_ids): continue keys = root.xpath(KEY_XPATH % metadata_ids[0]) if not len(keys): continue md.key = keys[0] md.save() if len(port_mds): response = query_psservice(ma, PORT_MDK_REQUEST) root = etree.fromstring(response) for md in port_mds: addresses = [] for node_address in NodeAddresses.objects.filter( node=md.subject.parent): addresses.append(node_address.address.value) addresses = ','.join(addresses) names = [] for port_names in NetworkObjectNames.objects.filter( networkobject=md.subject): names.append(port_names.name.value) names = '~^~'.join(names) event_type = md.event_type.value # Special cases direction = '' if md.event_type.value == \ 'http://ggf.org/ns/nmwg/characteristic/network/utilization/bytes/received/2.0': direction = 'in' event_type = 'http://ggf.org/ns/nmwg/characteristic/utilization/2.0' elif md.event_type.value == \ 'http://ggf.org/ns/nmwg/characteristic/network/utilization/bytes/sent/2.0': direction = 'out' event_type = 'http://ggf.org/ns/nmwg/characteristic/utilization/2.0' xpath = PORT_KEY_MD_XPATH % (addresses, names, direction, direction, event_type) metadata_ids = root.xpath(xpath) if not len(metadata_ids): continue keys = root.xpath(KEY_XPATH % metadata_ids[0]) if not len(keys): continue md.key = keys[0] md.save() for md in mds: last_datum = Data.objects.filter( metadata=md).order_by('time').reverse()[:1] if len(last_datum): last_time = int(time.mktime( last_datum[0].time.timetuple())) + 1 else: last_time = int(time.time()) - window response = query_psservice(ma, SETUPDATA_REQUEST % (md.key, last_time)) if response is None: # TODO: Check for errors continue for v in parse_snmp_response(response): Data.objects.create(metadata=md, time=v['timeValue'], value=v['value'], units=v['valueUnits'])
def make_snmp_request(port, start_time, end_time): if len(port.addresses.filter(type='ipv4')) > 0: address = port.addresses.filter(type='ipv4')[0] ifname = None else: address = None ifname = get_urn_value(port.unis_id, 'port') hostname = get_urn_value(port.unis_id, 'node') msg_address = """ <nmwg:metadata xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="metadataMETAID"> <netutil:subject xmlns:netutil="http://ggf.org/ns/nmwg/characteristic/utilization/2.0/" id="s-in-netutil-METAID"> <nmwgt:interface xmlns:nmwgt="http://ggf.org/ns/nmwg/topology/2.0/"> <nmwgt:ifAddress>%s</nmwgt:ifAddress> </nmwgt:interface> </netutil:subject> <nmwg:eventType>http://ggf.org/ns/nmwg/characteristic/utilization/2.0</nmwg:eventType> </nmwg:metadata> <nmwg:metadata xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="metadataMETAIDc"> <select:subject xmlns:select="http://ggf.org/ns/nmwg/ops/select/2.0/" id="subjectMETAIDc" metadataIdRef="metadataMETAID" /> <select:parameters id="param2c" xmlns:select="http://ggf.org/ns/nmwg/ops/select/2.0/"> <nmwg:parameter name="startTime">%s</nmwg:parameter> <nmwg:parameter name="endTime">%s</nmwg:parameter> </select:parameters> <nmwg:eventType>http://ggf.org/ns/nmwg/ops/select/2.0</nmwg:eventType> </nmwg:metadata> <nmwg:data xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="dataMETAID" metadataIdRef="metadataMETAIDc"/> """ msg_ifname = """ <nmwg:metadata xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="metadataMETAID"> <netutil:subject xmlns:netutil="http://ggf.org/ns/nmwg/characteristic/utilization/2.0/" id="s-in-netutil-METAID"> <nmwgt:interface xmlns:nmwgt="http://ggf.org/ns/nmwg/topology/2.0/"> <nmwgt:hostName>%s</nmwgt:hostName> <nmwgt:ifName>%s</nmwgt:ifName> </nmwgt:interface> </netutil:subject> <nmwg:eventType>http://ggf.org/ns/nmwg/characteristic/utilization/2.0</nmwg:eventType> </nmwg:metadata> <nmwg:metadata xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="metadataMETAIDc"> <select:subject xmlns:select="http://ggf.org/ns/nmwg/ops/select/2.0/" id="subjectMETAIDc" metadataIdRef="metadataMETAID" /> <select:parameters id="param2c" xmlns:select="http://ggf.org/ns/nmwg/ops/select/2.0/"> <nmwg:parameter name="startTime">%s</nmwg:parameter> <nmwg:parameter name="endTime">%s</nmwg:parameter> </select:parameters> <nmwg:eventType>http://ggf.org/ns/nmwg/ops/select/2.0</nmwg:eventType> </nmwg:metadata> <nmwg:data xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="dataMETAID" metadataIdRef="metadataMETAIDc"/> """ if address is None: msg = msg_ifname % (hostname, ifname, start_time, end_time) else: msg = msg_address % (address, start_time, end_time) metaid = random.randint(1, 1000000) msg = msg.replace('METAID', metaid.__str__()) return {'metaid': "metadata%ic" % metaid, 'xml':msg}
def handle(self, *args, **options): window = 24*60*60 if Metadata.objects.count() == 0: if create_measurement_mds() == None: return mds = Metadata.objects.filter(poll=True) # TODO: Assumes all mds use the same service, and single domain ma = mds[0].service if ma is None or not is_psservice_available(ma): xquery = GANGLIA_MA_XQUERY % create_urn(get_urn_value(mds[0].subject.unis_id, 'domain')) response = query_hls(settings.UNIS_INSTANCE, xquery, useSSL=True) service = etree.fromstring(response).find(".//{%s}service" % PSSERVICE_NS) if service is None: return accessPoint = service.find("{%s}accessPoint" % PSSERVICE_NS).text serviceNode = service.find("{%s}serviceNode" % PSSERVICE_NS).text serviceName = service.find("{%s}serviceName" % PSSERVICE_NS).text serviceType = service.find("{%s}serviceType" % PSSERVICE_NS).text serviceDescription = service.find("{%s}serviceDescription" % PSSERVICE_NS).text ma = create_psservice(serviceNode, serviceName, accessPoint, serviceType, serviceDescription) # Update all mds (remember we assume single MA for now) Metadata.objects.update(service=ma) # Update metadata keys if we have any missing if mds.filter(key=None).count() > 0: node_mds = [] port_mds = [] for md in mds.filter(key=None): if hasattr(md.subject, 'node'): node_mds.append(md) elif hasattr(md.subject, 'port'): port_mds.append(md) if len(node_mds): response = query_psservice(ma, NODE_MDK_REQUEST) root = etree.fromstring(response) for md in node_mds: addresses = [] for node_address in NodeAddresses.objects.filter(node=md.subject): addresses.append(node_address.address.value) addresses = ','.join(addresses) xpath = NODE_KEY_MD_XPATH % (addresses, md.event_type.value) metadata_ids = root.xpath( xpath ) if not len(metadata_ids): continue keys = root.xpath(KEY_XPATH % metadata_ids[0]) if not len(keys): continue md.key = keys[0] md.save() if len(port_mds): response = query_psservice(ma, PORT_MDK_REQUEST) root = etree.fromstring(response) for md in port_mds: addresses = [] for node_address in NodeAddresses.objects.filter(node=md.subject.parent): addresses.append(node_address.address.value) addresses = ','.join(addresses) names = [] for port_names in NetworkObjectNames.objects.filter(networkobject=md.subject): names.append(port_names.name.value) names = '~^~'.join(names) event_type = md.event_type.value # Special cases direction = '' if md.event_type.value == \ 'http://ggf.org/ns/nmwg/characteristic/network/utilization/bytes/received/2.0': direction = 'in' event_type = 'http://ggf.org/ns/nmwg/characteristic/utilization/2.0' elif md.event_type.value == \ 'http://ggf.org/ns/nmwg/characteristic/network/utilization/bytes/sent/2.0': direction = 'out' event_type = 'http://ggf.org/ns/nmwg/characteristic/utilization/2.0' xpath = PORT_KEY_MD_XPATH % (addresses, names, direction, direction, event_type) metadata_ids = root.xpath( xpath ) if not len(metadata_ids): continue keys = root.xpath(KEY_XPATH % metadata_ids[0]) if not len(keys): continue md.key = keys[0] md.save() for md in mds: last_datum = Data.objects.filter(metadata=md).order_by('time').reverse()[:1] if len(last_datum): last_time = int(time.mktime(last_datum[0].time.timetuple())) + 1 else: last_time = int(time.time()) - window response = query_psservice(ma, SETUPDATA_REQUEST % (md.key, last_time)) if response is None: # TODO: Check for errors continue for v in parse_snmp_response(response): Data.objects.create(metadata=md, time=v['timeValue'], value=v['value'], units=v['valueUnits'])
def make_snmp_request(port, start_time, end_time): if len(port.addresses.filter(type='ipv4')) > 0: address = port.addresses.filter(type='ipv4')[0] ifname = None else: address = None ifname = get_urn_value(port.unis_id, 'port') hostname = get_urn_value(port.unis_id, 'node') msg_address = """ <nmwg:metadata xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="metadataMETAID"> <netutil:subject xmlns:netutil="http://ggf.org/ns/nmwg/characteristic/utilization/2.0/" id="s-in-netutil-METAID"> <nmwgt:interface xmlns:nmwgt="http://ggf.org/ns/nmwg/topology/2.0/"> <nmwgt:ifAddress>%s</nmwgt:ifAddress> </nmwgt:interface> </netutil:subject> <nmwg:eventType>http://ggf.org/ns/nmwg/characteristic/utilization/2.0</nmwg:eventType> </nmwg:metadata> <nmwg:metadata xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="metadataMETAIDc"> <select:subject xmlns:select="http://ggf.org/ns/nmwg/ops/select/2.0/" id="subjectMETAIDc" metadataIdRef="metadataMETAID" /> <select:parameters id="param2c" xmlns:select="http://ggf.org/ns/nmwg/ops/select/2.0/"> <nmwg:parameter name="startTime">%s</nmwg:parameter> <nmwg:parameter name="endTime">%s</nmwg:parameter> </select:parameters> <nmwg:eventType>http://ggf.org/ns/nmwg/ops/select/2.0</nmwg:eventType> </nmwg:metadata> <nmwg:data xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="dataMETAID" metadataIdRef="metadataMETAIDc"/> """ msg_ifname = """ <nmwg:metadata xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="metadataMETAID"> <netutil:subject xmlns:netutil="http://ggf.org/ns/nmwg/characteristic/utilization/2.0/" id="s-in-netutil-METAID"> <nmwgt:interface xmlns:nmwgt="http://ggf.org/ns/nmwg/topology/2.0/"> <nmwgt:hostName>%s</nmwgt:hostName> <nmwgt:ifName>%s</nmwgt:ifName> </nmwgt:interface> </netutil:subject> <nmwg:eventType>http://ggf.org/ns/nmwg/characteristic/utilization/2.0</nmwg:eventType> </nmwg:metadata> <nmwg:metadata xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="metadataMETAIDc"> <select:subject xmlns:select="http://ggf.org/ns/nmwg/ops/select/2.0/" id="subjectMETAIDc" metadataIdRef="metadataMETAID" /> <select:parameters id="param2c" xmlns:select="http://ggf.org/ns/nmwg/ops/select/2.0/"> <nmwg:parameter name="startTime">%s</nmwg:parameter> <nmwg:parameter name="endTime">%s</nmwg:parameter> </select:parameters> <nmwg:eventType>http://ggf.org/ns/nmwg/ops/select/2.0</nmwg:eventType> </nmwg:metadata> <nmwg:data xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/" id="dataMETAID" metadataIdRef="metadataMETAIDc"/> """ if address is None: msg = msg_ifname % (hostname, ifname, start_time, end_time) else: msg = msg_address % (address, start_time, end_time) metaid = random.randint(1, 1000000) msg = msg.replace('METAID', metaid.__str__()) return {'metaid': "metadata%ic" % metaid, 'xml': msg}