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.elements.link_type import LinkType
7 from sfa.rspecs.elements.component_manager import ComponentManager
8 from sfa.rspecs.elements.property import Property
9 from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
13 'link': RSpecElement(RSpecElements.LINK, '//default:link | //link'),
14 'component_manager': RSpecElement(RSpecElements.COMPONENT_MANAGER, './default:component_manager | ./component_manager'),
15 'link_type': RSpecElement(RSpecElements.LINK_TYPE, './default:link_type | ./link_type'),
16 'property': RSpecElement(RSpecElements.PROPERTY, './default:property | ./property'),
17 'interface_ref': RSpecElement(RSpecElements.INTERFACE_REF, './default:interface_ref | ./interface_ref'),
21 def add_links(xml, links):
23 link_elem = etree.SubElement(xml, 'link')
24 for attrib in ['component_name', 'component_id', 'client_id']:
25 if attrib in link and link[attrib] is not None:
26 link_elem.set(attrib, link[attrib])
27 if 'component_manager' in link and link['component_manager']:
28 cm_element = etree.SubElement(link_elem, 'component_manager', name=link['component_manager'])
29 for if_ref in [link['interface1'], link['interface2']]:
30 if_ref_elem = etree.SubElement(link_elem, 'interface_ref')
31 for attrib in Interface.fields:
32 if attrib in if_ref and if_ref[attrib]:
33 if_ref_elem.attrib[attrib] = if_ref[attrib]
34 prop1 = etree.SubElement(link_elem, 'property', source_id = link['interface1']['component_id'],
35 dest_id = link['interface2']['component_id'], capacity=link['capacity'],
36 latency=link['latency'], packet_loss=link['packet_loss'])
37 prop2 = etree.SubElement(link_elem, 'property', source_id = link['interface2']['component_id'],
38 dest_id = link['interface1']['component_id'], capacity=link['capacity'],
39 latency=link['latency'], packet_loss=link['packet_loss'])
40 if 'type' in link and link['type']:
41 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 if_name1 = Xrn(link['interface1']['component_id']).get_leaf()
91 if_name2 = Xrn(link['interface2']['component_id']).get_leaf()
94 l_tup_1 = (if_name1, if_name2)
95 l_tup_2 = (if_name2, if_name1)
96 if link_tuples.issuperset([(if_name1, if_name2)]):
97 requested_link = (if_name1, if_name2)
98 elif link_tuples.issuperset([(if_name2, if_name2)]):
99 requested_link = (if_name2, if_name1)
101 # add client id to link ane interface elements
102 link.element.set('client_id', link['component_name'])
103 link['interface1'].element.set('client_id', Xrn(link['interface1']['component_id']).get_leaf())
104 link['interface2'].element.set('client_id', Xrn(link['interface2']['component_id']).get_leaf())
105 recently_added.append(link['component_name'])
108 # remove all links that don't have a client id
109 for link in PGv2Link.get_links(xml):
110 if not link['client_id'] or link['component_name'] not in recently_added:
111 parent = link.element.getparent()
112 parent.remove(link.element)
115 def get_link_requests(xml):
117 for link in PGv2Link.get_links(xml):
118 if link['client_id'] != None:
119 link_requests.append(link)