2 from sfa.rspecs.elements.link import Link
3 from sfa.rspecs.elements.interface import Interface
8 def add_links(xml, links):
10 link_elem = etree.SubElement(xml, 'link')
11 for attrib in ['component_name', 'component_id', 'client_id']:
12 if attrib in link and link[attrib]:
13 link_elem.set(attrib, link[attrib])
14 if 'component_manager' in link and link['component_manger']:
15 cm_element = etree.SubElement(xml, 'component_manager', name=link['component_manager'])
16 for if_ref in [link['interface1'], link['interface2']]:
17 if_ref_elem = etree.SubElement(xml, 'interface_ref')
18 for attrib in Interface.fields:
19 if attrib in if_ref and if_ref[attrib]:
20 if_ref_elem[attrib] = if_ref[attrib]
21 prop1 = etree.SubElement(xml, 'property', source_id = link['interface1']['component_id'],
22 dest_id = link['interface2']['component_id'], capacity=link['capacity'],
23 latency=link['latency'], packet_loss=link['packet_loss'])
24 prop2 = etree.SubElement(xml, 'property', source_id = link['interface2']['component_id'],
25 dest_id = link['interface1']['component_id'], capacity=link['capacity'],
26 latency=link['latency'], packet_loss=link['packet_loss'])
27 if 'type' in link and link['type']:
28 type_elem = etree.SubElement(xml, 'link_type', name=link['type'])
31 def get_links(xml, namespaces=None):
33 link_elems = xml.xpath('//default:link', namespaces=namespaces)
34 for link_elem in link_elems:
35 # set client_id, component_id, component_name
36 link = Link(link_elem.attrib)
37 # set component manager
38 cm = link_elem.xpath('./default:component_manager', namespaces=namespaces)
41 if 'name' in cm.attrib:
42 link['component_manager'] = cm.attrib['name']
44 link_types = link_elem.xpath('./default:link_type', namespaces=namespaces)
45 if len(link_types) > 0:
46 link_type = link_types[0]
47 if 'name' in link_type.attrib:
48 link['type'] = link_type.attrib['name']
50 # get capacity, latency and packet_loss from first property
51 props = link_elem.xpath('./default:property', namespaces=namespaces)
54 for attrib in ['capacity', 'latency', 'packet_loss']:
55 if attrib in prop.attrib:
56 link[attrib] = prop.attrib[attrib]
59 if_elems = link_elem.xpath('./default:interface_ref', namespaces=namespaces)
61 for if_elem in if_elems:
62 if_ref = Interface(if_elem.attrib)
65 link['interface1'] = ifs[0]
66 link['interface2'] = ifs[1]