2 from sfa.rspecs.elements.link import Link
3 from sfa.rspecs.elements.interface import Interface
4 from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
9 'link': RSpecElement(RSpecElements.LINK, 'link', '//default:link | //link'),
10 #thierry:was incomplete.. 'component_manager': RSpecElement(RSpecElement),
14 def add_links(xml, links):
16 link_elem = etree.SubElement(xml, 'link')
17 for attrib in ['component_name', 'component_id', 'client_id']:
18 if attrib in link and link[attrib]:
19 link_elem.set(attrib, link[attrib])
20 if 'component_manager' in link and link['component_manager']:
21 cm_element = etree.SubElement(xml, 'component_manager', name=link['component_manager'])
22 for if_ref in [link['interface1'], link['interface2']]:
23 if_ref_elem = etree.SubElement(xml, 'interface_ref')
24 for attrib in Interface.fields:
25 if attrib in if_ref and if_ref[attrib]:
26 if_ref_elem.attrib[attrib] = if_ref[attrib]
27 prop1 = etree.SubElement(xml, 'property', source_id = link['interface1']['component_id'],
28 dest_id = link['interface2']['component_id'], capacity=link['capacity'],
29 latency=link['latency'], packet_loss=link['packet_loss'])
30 prop2 = etree.SubElement(xml, 'property', source_id = link['interface2']['component_id'],
31 dest_id = link['interface1']['component_id'], capacity=link['capacity'],
32 latency=link['latency'], packet_loss=link['packet_loss'])
33 if 'type' in link and link['type']:
34 type_elem = etree.SubElement(xml, 'link_type', name=link['type'])
37 def get_links(xml, namespaces=None):
39 link_elems = xml.xpath('//default:link', namespaces=namespaces)
40 for link_elem in link_elems:
41 # set client_id, component_id, component_name
42 link = Link(link_elem.attrib)
43 # set component manager
44 cm = link_elem.xpath('./default:component_manager', namespaces=namespaces)
47 if 'name' in cm.attrib:
48 link['component_manager'] = cm.attrib['name']
50 link_types = link_elem.xpath('./default:link_type', namespaces=namespaces)
51 if len(link_types) > 0:
52 link_type = link_types[0]
53 if 'name' in link_type.attrib:
54 link['type'] = link_type.attrib['name']
56 # get capacity, latency and packet_loss from first property
57 props = link_elem.xpath('./default:property', namespaces=namespaces)
60 for attrib in ['capacity', 'latency', 'packet_loss']:
61 if attrib in prop.attrib:
62 link[attrib] = prop.attrib[attrib]
65 if_elems = link_elem.xpath('./default:interface_ref', namespaces=namespaces)
67 for if_elem in if_elems:
68 if_ref = Interface(if_elem.attrib)
71 link['interface1'] = ifs[0]
72 link['interface2'] = ifs[1]