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 if_ref1 = etree.SubElement(xml, 'interface_ref', component_id=link['interface1']['component_id'])
17 if_ref2 = etree.SubElement(xml, 'interface_ref', component_id=link['interface2']['component_id'])
18 prop1 = etree.SubElement(xml, 'property', source_id = link['interface1']['component_id'],
19 dest_id = link['interface2']['component_id'], capacity=link['capacity'],
20 latency=link['latency'], packet_loss=link['packet_loss'])
21 prop2 = etree.SubElement(xml, 'property', source_id = link['interface2']['component_id'],
22 dest_id = link['interface1']['component_id'], capacity=link['capacity'],
23 latency=link['latency'], packet_loss=link['packet_loss'])
24 if 'type' in link and link['type']:
25 type_elem = etree.SubElement(xml, 'link_type', name=link['type'])
28 def get_links(xml, namespaces=None):
30 link_elems = xml.xpath('//default:link', namespaces=namespaces)
31 for link_elem in link_elems:
32 # set client_id, component_id, component_name
33 link = Link(link_elem.attrib)
34 # set component manager
35 cm = link_elem.xpath('./default:component_manager', namespaces=namespaces)
38 if 'name' in cm.attrib:
39 link['component_manager'] = cm.attrib['name']
41 link_types = link_elem.xpath('./default:link_type', namespaces=namespaces)
42 if len(link_types) > 0:
43 link_type = link_types[0]
44 if 'name' in link_type.attrib:
45 link['type'] = link_type.attrib['name']
47 # get capacity, latency and packet_loss and interfaces from first property
48 props = link_elem.xpath('./default:property', namespaces=namespaces)
51 if 'source_id' in prop.attrib:
52 link['interface1'] = Interface({'component_id': prop.attrib['source_id']})
53 if 'dest_id' in prop.attrib:
54 link['interface2'] = Interface({'component_id': prop.attrib['dest_id']})
55 for attrib in ['capacity', 'latency', 'packet_loss']:
56 if attrib in prop.attrib:
57 link[attrib] = prop.attrib[attrib]