Merge branch 'master' of ssh://git.f-lab.fr/git/sfa
[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 from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
5
6 class PGv2Link:
7
8     elements = {
9         'link': RSpecElement(RSpecElements.LINK, '//default:link | //link'),
10         'component_manager': RSpecElement(RSpecElements.COMPONENT_MANAGER, './default:component_manager | ./component_manager')
11     }
12     
13     @staticmethod
14     def add_links(xml, links):
15         for link in 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'])             
35    
36     @staticmethod 
37     def get_links(xml, namespaces=None):
38         links = []
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)
45             if len(cm) >  0:
46                 cm = cm[0]
47                 if  'name' in cm.attrib:
48                     link['component_manager'] = cm.attrib['name'] 
49             # set link type
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']
55           
56             # get capacity, latency and packet_loss from first property  
57             props = link_elem.xpath('./default:property', namespaces=namespaces)
58             if len(props) > 0:
59                 prop = props[0]
60                 for attrib in ['capacity', 'latency', 'packet_loss']:
61                     if attrib in prop.attrib:
62                         link[attrib] = prop.attrib[attrib]
63                              
64             # get interfaces 
65             if_elems = link_elem.xpath('./default:interface_ref', namespaces=namespaces)
66             ifs = []
67             for if_elem in if_elems:
68                 if_ref = Interface(if_elem.attrib)                 
69                 ifs.append(if_ref)
70             if len(ifs) > 1:
71                 link['interface1'] = ifs[0]
72                 link['interface2'] = ifs[1] 
73             links.append(link)
74         return links 
75