b10e5667a3494bc99908ac55ce9ed8d75e131a62
[sfa.git] / sfa / rspecs / elements / versions / pgv2Link.py
1 from lxml import etree
2 from sfa.rspecs.elements.link import Link
3 from sfa.rspecs.elements.interface import Interface
4
5 class PGv2Link:
6     
7     @staticmethod
8     def add_links(xml, links):
9         for link in 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'])             
29              
30     @staticmethod 
31     def get_links(xml, namespaces=None):
32         links = []
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)
39             if len(cm) >  0:
40                 cm = cm[0]
41                 if  'name' in cm.attrib:
42                     link['component_manager'] = cm.attrib['name'] 
43             # set link type
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']
49           
50             # get capacity, latency and packet_loss from first property  
51             props = link_elem.xpath('./default:property', namespaces=namespaces)
52             if len(props) > 0:
53                 prop = props[0]
54                 for attrib in ['capacity', 'latency', 'packet_loss']:
55                     if attrib in prop.attrib:
56                         link[attrib] = prop.attrib[attrib]
57                              
58             # get interfaces 
59             if_elems = link_elem.xpath('./default:interface_ref', namespaces=namespaces)
60             ifs = []
61             for if_elem in if_elems:
62                 if_ref = Interface(if_elem.attrib)                 
63                 ifs.append(if_ref)
64             if len(ifs) > 1:
65                 link['interface1'] = ifs[0]
66                 link['interface2'] = ifs[1] 
67             links.append(link)
68         return links 
69             
70