2 from sfa.util.plxrn import PlXrn
3 from sfa.util.xrn import Xrn
4 from sfa.rspecs.elements.link import Link
5 from sfa.rspecs.elements.interface import Interface
6 from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
11 'link': RSpecElement(RSpecElements.LINK, '//default:link | //link'),
12 'component_manager': RSpecElement(RSpecElements.COMPONENT_MANAGER, './default:component_manager | ./component_manager'),
13 'link_type': RSpecElement(RSpecElements.LINK_TYPE, './default:link_type | ./link_type'),
14 'property': RSpecElement(RSpecElements.PROPERTY, './default:property | ./property'),
15 'interface_ref': RSpecElement(RSpecElements.INTERFACE_REF, './default:interface_ref | ./interface_ref')
19 def add_links(xml, links):
22 link_elem = etree.SubElement(root, 'link')
23 for attrib in ['component_name', 'component_id', 'client_id']:
24 if attrib in link and link[attrib] is not None:
25 link_elem.set(attrib, link[attrib])
26 if 'component_manager' in link and link['component_manager']:
27 cm_element = etree.SubElement(link_elem, 'component_manager', name=link['component_manager'])
28 for if_ref in [link['interface1'], link['interface2']]:
29 if_ref_elem = etree.SubElement(link_elem, 'interface_ref')
30 for attrib in Interface.fields:
31 if attrib in if_ref and if_ref[attrib]:
32 if_ref_elem.attrib[attrib] = if_ref[attrib]
33 prop1 = etree.SubElement(link_elem, 'property', source_id = link['interface1']['component_id'],
34 dest_id = link['interface2']['component_id'], capacity=link['capacity'],
35 latency=link['latency'], packet_loss=link['packet_loss'])
36 prop2 = etree.SubElement(link_elem, 'property', source_id = link['interface2']['component_id'],
37 dest_id = link['interface1']['component_id'], capacity=link['capacity'],
38 latency=link['latency'], packet_loss=link['packet_loss'])
39 if 'type' in link and link['type']:
40 type_elem = etree.SubElement(link_elem, 'link_type', name=link['type'])
45 link_elems = xml.xpath(PGv2Link.elements['link'].path, namespaces=xml.namespaces)
46 for link_elem in link_elems:
47 # set client_id, component_id, component_name
48 link = Link(link_elem.attrib, link_elem)
49 # set component manager
50 cm = link_elem.xpath('./default:component_manager', namespaces=xml.namespaces)
53 if 'name' in cm.attrib:
54 link['component_manager'] = cm.attrib['name']
56 link_types = link_elem.xpath(PGv2Link.elements['link_type'].path, namespaces=xml.namespaces)
57 if len(link_types) > 0:
58 link_type = link_types[0]
59 if 'name' in link_type.attrib:
60 link['type'] = link_type.attrib['name']
62 # get capacity, latency and packet_loss from first property
63 props = link_elem.xpath(PGv2Link.elements['property'].path, namespaces=xml.namespaces)
66 for attrib in ['capacity', 'latency', 'packet_loss']:
67 if attrib in prop.attrib:
68 link[attrib] = prop.attrib[attrib]
71 if_elems = link_elem.xpath(PGv2Link.elements['interface_ref'].path, namespaces=xml.namespaces)
73 for if_elem in if_elems:
74 if_ref = Interface(if_elem.attrib, if_elem)
77 link['interface1'] = ifs[0]
78 link['interface2'] = ifs[1]
83 def add_link_requests(xml, link_tuples, append=False):
84 if not isinstance(link_tuples, set):
85 link_tuples = set(link_tuples)
87 available_links = PGv2Link.get_links(xml)
89 for link in available_links:
90 auth = Xrn(link['component_id']).get_authority_hrn()
91 if_name1 = Xrn(link['interface1']['component_id']).get_leaf()
92 if_name2 = Xrn(link['interface2']['component_id']).get_leaf()
95 l_tup_1 = (if_name1, if_name2)
96 l_tup_2 = (if_name2, if_name1)
97 if link_tuples.issuperset([(if_name1, if_name2)]):
98 requested_link = (if_name1, if_name2)
99 elif link_tuples.issuperset([(if_name2, if_name2)]):
100 requested_link = (if_name2, if_name1)
103 # add client id to link ane interface elements
104 link.element.set('client_id', link['component_name'])
105 link['interface1'].element.set('client_id', Xrn(link['interface1']['component_id']).get_leaf())
106 link['interface2'].element.set('client_id', Xrn(link['interface2']['component_id']).get_leaf())
107 recently_added.append(link['component_name'])
110 # remove all links that don't have a client id
111 for link in PGv2Link.get_links(xml):
112 if not link['client_id'] or link['component_name'] not in recently_added:
113 parent = link.element.getparent()
114 parent.remove(link.element)
117 def get_link_requests(xml):
119 for link in PGv2Link.get_links(xml):
120 if link['client_id'] != None:
121 link_requests.append(link)