id ipaddress attribute to interface tag
[sfa.git] / sfa / rspecs / elements / versions / pgv2Link.py
1 from lxml import etree
2 from sfa.util.plxrn import PlXrn
3 from sfa.util.xrn import Xrn
4 from sfa.rspecs.elements.link import Link
5 from sfa.rspecs.elements.interface import Interface
6 from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
7
8 class PGv2Link:
9
10     elements = {
11         'link': RSpecElement(RSpecElements.LINK, '//default:link | //link'),
12         'component_manager': RSpecElement(RSpecElements.COMPONENT_MANAGER, './default:component_manager | ./component_manager'),
13         'link_type': RSpecElement(RSpecElements.LINK_TYPE, './default:link_type | ./link_type'),
14         'property': RSpecElement(RSpecElements.PROPERTY, './default:property | ./property'),
15         'interface_ref': RSpecElement(RSpecElements.INTERFACE_REF, './default:interface_ref | ./interface_ref') 
16     }
17     
18     @staticmethod
19     def add_links(xml, links):
20         root = xml.root
21         for link in links:
22             link_elem = etree.SubElement(root, 'link')
23             for attrib in ['component_name', 'component_id', 'client_id']:
24                 if attrib in link and link[attrib] is not None:
25                     link_elem.set(attrib, link[attrib])
26             if 'component_manager' in link and link['component_manager']:
27                 cm_element = etree.SubElement(link_elem, 'component_manager', name=link['component_manager'])
28             for if_ref in [link['interface1'], link['interface2']]:
29                 if_ref_elem = etree.SubElement(link_elem, 'interface_ref')
30                 for attrib in Interface.fields:
31                     if attrib in if_ref and if_ref[attrib]:
32                         if_ref_elem.attrib[attrib] = if_ref[attrib]  
33             prop1 = etree.SubElement(link_elem, 'property', source_id = link['interface1']['component_id'],
34                 dest_id = link['interface2']['component_id'], capacity=link['capacity'], 
35                 latency=link['latency'], packet_loss=link['packet_loss'])
36             prop2 = etree.SubElement(link_elem, 'property', source_id = link['interface2']['component_id'],
37                 dest_id = link['interface1']['component_id'], capacity=link['capacity'], 
38                 latency=link['latency'], packet_loss=link['packet_loss'])
39             if 'type' in link and link['type']:
40                 type_elem = etree.SubElement(link_elem, 'link_type', name=link['type'])             
41     @staticmethod 
42     def get_links(xml):
43         links = []
44         link_elems = xml.xpath(PGv2Link.elements['link'].path, namespaces=xml.namespaces)
45         for link_elem in link_elems:
46             # set client_id, component_id, component_name
47             link = Link(link_elem.attrib, 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, if_elem)
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     @staticmethod
82     def add_link_requests(xml, link_tuples, append=False):
83         if not isinstance(link_tuples, set):
84             link_tuples = set(link_tuples)
85
86         available_links = PGv2Link.get_links(xml)
87         recently_added = []
88         for link in available_links:
89             auth = Xrn(link['component_id']).get_authority_hrn()
90             if_name1 =  Xrn(link['interface1']['component_id']).get_leaf()
91             if_name2 =  Xrn(link['interface2']['component_id']).get_leaf()
92             
93             requested_link = None
94             l_tup_1 = (if_name1, if_name2)
95             l_tup_2 = (if_name2, if_name1) 
96             if link_tuples.issuperset([(if_name1, if_name2)]):
97                 requested_link = (if_name1, if_name2)        
98             elif link_tuples.issuperset([(if_name2, if_name2)]):
99                 requested_link = (if_name2, if_name1)
100             
101             if requested_link:
102                 # add client id to link ane interface elements 
103                 link.element.set('client_id', link['component_name'])
104                 link['interface1'].element.set('client_id', Xrn(link['interface1']['component_id']).get_leaf()) 
105                 link['interface2'].element.set('client_id', Xrn(link['interface2']['component_id']).get_leaf()) 
106                 recently_added.append(link['component_name'])
107
108         if not append:
109             # remove all links that don't have a client id 
110             for link in PGv2Link.get_links(xml):
111                 if not link['client_id'] or link['component_name'] not in recently_added:
112                     parent = link.element.getparent()
113                     parent.remove(link.element)                  
114              
115     @staticmethod
116     def get_link_requests(xml):
117         link_requests = []
118         for link in PGv2Link.get_links(xml):
119             if link['client_id'] != None:
120                 link_requests.append(link)
121         return link_requests