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