oops
[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         'link_type': RSpecElement(RSpecElements.LINK_TYPE, './default:link_type | ./link_type'),
12         'property': RSpecElement(RSpecElements.PROPERTY, './default:property | ./property'),
13         'interface_ref': RSpecElement(RSpecElements.INTERFACE_REF, './default:interface_ref | ./interface_ref') 
14     }
15     
16     @staticmethod
17     def add_links(xml, links):
18         for link in links:
19             link_elem = etree.SubElement(xml, 'link')
20             for attrib in ['component_name', 'component_id', 'client_id']:
21                 if attrib in link and link[attrib]:
22                     link_elem.set(attrib, link[attrib])
23             if 'component_manager' in link and link['component_manager']:
24                 cm_element = etree.SubElement(link_elem, 'component_manager', name=link['component_manager'])
25             for if_ref in [link['interface1'], link['interface2']]:
26                 if_ref_elem = etree.SubElement(link_elem, 'interface_ref')
27                 for attrib in Interface.fields:
28                     if attrib in if_ref and if_ref[attrib]:
29                         if_ref_elem.attrib[attrib] = if_ref[attrib]  
30             prop1 = etree.SubElement(link_elem, 'property', source_id = link['interface1']['component_id'],
31                 dest_id = link['interface2']['component_id'], capacity=link['capacity'], 
32                 latency=link['latency'], packet_loss=link['packet_loss'])
33             prop2 = etree.SubElement(link_elem, 'property', source_id = link['interface2']['component_id'],
34                 dest_id = link['interface1']['component_id'], capacity=link['capacity'], 
35                 latency=link['latency'], packet_loss=link['packet_loss'])
36             if 'type' in link and link['type']:
37                 type_elem = etree.SubElement(link_elem, 'link_type', name=link['type'])             
38    
39     @staticmethod 
40     def get_links(xml, namespaces=None):
41         links = []
42         link_elems = xml.xpath(PGv2Link.elements['link'].path, namespaces=namespaces)
43         for link_elem in link_elems:
44             # set client_id, component_id, component_name
45             link = Link(link_elem.attrib)
46             # set component manager
47             cm = link_elem.xpath('./default:component_manager', namespaces=namespaces)
48             if len(cm) >  0:
49                 cm = cm[0]
50                 if  'name' in cm.attrib:
51                     link['component_manager'] = cm.attrib['name'] 
52             # set link type
53             link_types = link_elem.xpath(PGv2Link.elements['link_type'].path, namespaces=namespaces)
54             if len(link_types) > 0:
55                 link_type = link_types[0]
56                 if 'name' in link_type.attrib:
57                     link['type'] = link_type.attrib['name']
58           
59             # get capacity, latency and packet_loss from first property  
60             props = link_elem.xpath(PGv2Link.elements['property'].path, namespaces=namespaces)
61             if len(props) > 0:
62                 prop = props[0]
63                 for attrib in ['capacity', 'latency', 'packet_loss']:
64                     if attrib in prop.attrib:
65                         link[attrib] = prop.attrib[attrib]
66                              
67             # get interfaces 
68             if_elems = link_elem.xpath(PGv2Link.elements['interface_ref'].path, namespaces=namespaces)
69             ifs = []
70             for if_elem in if_elems:
71                 if_ref = Interface(if_elem.attrib)                 
72                 ifs.append(if_ref)
73             if len(ifs) > 1:
74                 link['interface1'] = ifs[0]
75                 link['interface2'] = ifs[1] 
76             links.append(link)
77         return links 
78