ee34371600f956358baa2879bd58d6e93f7fdf39
[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             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'])             
26              
27     @staticmethod 
28     def get_links(xml, namespaces=None):
29         links = []
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)
36             if len(cm) >  0:
37                 cm = cm[0]
38                 if  'name' in cm.attrib:
39                     link['component_manager'] = cm.attrib['name'] 
40             # set link type
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']
46           
47             # get capacity, latency and packet_loss and interfaces from first property  
48             props = link_elem.xpath('./default:property', namespaces=namespaces)
49             if len(props) > 0:
50                 prop = props[0]
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]
58             links.append(link)
59         return links 
60             
61