Fix default slivers mgt in sfav1 rspecs
[sfa.git] / sfa / rspecs / versions / sfav1.py
1 from copy import deepcopy
2 from lxml import etree
3
4 from sfa.util.sfalogging import logger
5 from sfa.util.xrn import hrn_to_urn, urn_to_hrn
6 from sfa.rspecs.version import RSpecVersion
7 from sfa.rspecs.elements.element import Element
8 from sfa.rspecs.elements.versions.pgv2Link import PGv2Link
9 from sfa.rspecs.elements.versions.sfav1Node import SFAv1Node
10 from sfa.rspecs.elements.versions.sfav1Sliver import SFAv1Sliver
11 from sfa.rspecs.elements.versions.sfav1Lease import SFAv1Lease
12
13 class SFAv1(RSpecVersion):
14     enabled = True
15     type = 'SFA'
16     content_type = '*'
17     version = '1'
18     schema = None
19     namespace = None
20     extensions = {}
21     namespaces = None
22     template = '<RSpec type="%s"></RSpec>' % type
23
24     # Network 
25     def get_networks(self):
26         network_elems = self.xml.xpath('//network')
27         networks = [network_elem.get_instance(fields=['name', 'slice']) for \
28                     network_elem in network_elems]
29         return networks    
30
31
32     def add_network(self, network):
33         network_tags = self.xml.xpath('//network[@name="%s"]' % network)
34         if not network_tags:
35             network_tag = self.xml.add_element('network', name=network)
36         else:
37             network_tag = network_tags[0]
38         return network_tag
39
40
41     # Nodes
42     
43     def get_nodes(self, filter=None):
44         return SFAv1Node.get_nodes(self.xml, filter)
45
46     def get_nodes_with_slivers(self):
47         return SFAv1Node.get_nodes_with_slivers(self.xml)
48
49     def add_nodes(self, nodes, network = None, no_dupes=False):
50         SFAv1Node.add_nodes(self.xml, nodes)
51
52     def merge_node(self, source_node_tag, network, no_dupes=False):
53         if no_dupes and self.get_node_element(node['hostname']):
54             # node already exists
55             return
56
57         network_tag = self.add_network(network)
58         network_tag.append(deepcopy(source_node_tag))
59
60     # Slivers
61    
62     def add_slivers(self, hostnames, attributes=[], sliver_urn=None, append=False):
63         # add slice name to network tag
64         network_tags = self.xml.xpath('//network')
65         if network_tags:
66             network_tag = network_tags[0]
67             network_tag.set('slice', urn_to_hrn(sliver_urn)[0])
68
69         # add slivers
70         sliver = {'name':sliver_urn,
71                   'pl_tags': attributes}
72         for hostname in hostnames:
73             if sliver_urn:
74                 sliver['name'] = sliver_urn
75             node_elems = self.get_nodes({'component_id': '*%s*' % hostname})
76             if not node_elems:
77                 continue
78             node_elem = node_elems[0]
79             SFAv1Sliver.add_slivers(node_elem.element, sliver)
80
81         # remove all nodes without slivers
82         if not append:
83             for node_elem in self.get_nodes():
84                 if not node_elem['slivers']:
85                     parent = node_elem.element.getparent()
86                     parent.remove(node_elem.element)
87
88
89     def remove_slivers(self, slivers, network=None, no_dupes=False):
90         SFAv1Node.remove_slivers(self.xml, slivers)
91  
92     def get_slice_attributes(self, network=None):
93         attributes = []
94         nodes_with_slivers = self.get_nodes_with_slivers()
95         for default_attribute in self.get_default_sliver_attributes(network):
96             attribute = default_attribute.copy()
97             attribute['node_id'] = None
98             attributes.append(attribute)
99         for node in nodes_with_slivers:
100             nodename=node['component_name']
101             sliver_attributes = self.get_sliver_attributes(nodename, network)
102             for sliver_attribute in sliver_attributes:
103                 sliver_attribute['node_id'] = nodename
104                 attributes.append(sliver_attribute)
105         return attributes
106
107
108     def add_sliver_attribute(self, component_id, name, value, network=None):
109         nodes = self.get_nodes({'component_id': '*%s*' % component_id})
110         if nodes is not None and isinstance(nodes, list) and len(nodes) > 0:
111             node = nodes[0]
112             slivers = SFAv1Sliver.get_slivers(node)
113             if slivers:
114                 sliver = slivers[0]
115                 SFAv1Sliver.add_sliver_attribute(sliver, name, value)
116         else:
117             # should this be an assert / raise an exception?
118             logger.error("WARNING: failed to find component_id %s" % component_id)
119
120     def get_sliver_attributes(self, component_id, network=None):
121         nodes = self.get_nodes({'component_id': '*%s*' % component_id})
122         attribs = []
123         if nodes is not None and isinstance(nodes, list) and len(nodes) > 0:
124             node = nodes[0]
125             slivers = SFAv1Sliver.get_slivers(node.element)
126             if slivers is not None and isinstance(slivers, list) and len(slivers) > 0:
127                 sliver = slivers[0]
128                 attribs = SFAv1Sliver.get_sliver_attributes(sliver.element)
129         return attribs
130
131     def remove_sliver_attribute(self, component_id, name, value, network=None):
132         attribs = self.get_sliver_attributes(component_id)
133         for attrib in attribs:
134             if attrib['name'] == name and attrib['value'] == value:
135                 #attrib.element.delete()
136                 parent = attrib.element.getparent()
137                 parent.remove(attrib.element)
138
139     def add_default_sliver_attribute(self, name, value, network=None):
140         if network:
141             defaults = self.xml.xpath("//network[@name='%s']/sliver_defaults" % network)
142         else:
143             defaults = self.xml.xpath("//sliver_defaults")
144         if not defaults:
145             if network:
146                 network_tag = self.xml.xpath("//network[@name='%s']" % network)
147             else:
148                 network_tag = self.xml.xpath("//network") 
149             if len(network_tag) > 0:
150                 network_tag = network_tag[0]
151             elif network:
152                 network_tag = self.add_network(network)
153             else:
154                 network_tag = self.xml
155             defaults = network_tag.add_element('sliver_defaults')
156             
157         elif isinstance(defaults, list):
158             defaults = defaults[0]
159         SFAv1Sliver.add_sliver_attribute(defaults, name, value)
160
161     def get_default_sliver_attributes(self, network=None):
162         if network:
163             defaults = self.xml.xpath("//network[@name='%s']/sliver_defaults" % network)
164         else:
165             defaults = self.xml.xpath("//sliver_defaults")
166         if not defaults: return []
167         return SFAv1Sliver.get_sliver_attributes(defaults[0])
168     
169     def remove_default_sliver_attribute(self, name, value, network=None):
170         attribs = self.get_default_sliver_attributes(network)
171         for attrib in attribs:
172             if attrib['name'] == name and attrib['value'] == value:
173                 #attrib.element.delete()
174                 parent = attrib.element.getparent()
175                 parent.remove(attrib.element)
176
177     # Links
178
179     def get_links(self, network=None):
180         return PGv2Link.get_links(self.xml)
181
182     def get_link_requests(self):
183         return PGv2Link.get_link_requests(self.xml) 
184
185     def add_links(self, links):
186         networks = self.get_networks()
187         if len(networks) > 0:
188             xml = networks[0].element
189         else:
190             xml = self.xml
191         PGv2Link.add_links(xml, links)
192
193     def add_link_requests(self, links):
194         PGv2Link.add_link_requests(self.xml, links)
195
196     # utility
197
198     def merge(self, in_rspec):
199         """
200         Merge contents for specified rspec with current rspec
201         """
202
203         if not in_rspec:
204             return
205
206         from sfa.rspecs.rspec import RSpec
207         if isinstance(in_rspec, RSpec):
208             rspec = in_rspec
209         else:
210             rspec = RSpec(in_rspec)
211         if rspec.version.type.lower() == 'protogeni':
212             from sfa.rspecs.rspec_converter import RSpecConverter
213             in_rspec = RSpecConverter.to_sfa_rspec(rspec.toxml())
214             rspec = RSpec(in_rspec)
215
216         # just copy over all networks
217         current_networks = self.get_networks()
218         networks = rspec.version.get_networks()
219         for network in networks:
220             current_network = network.get('name')
221             if current_network and current_network not in current_networks:
222                 self.xml.append(network.element)
223                 current_networks.append(current_network)
224
225     # Leases
226
227     def get_leases(self, filter=None):
228         return SFAv1Lease.get_leases(self.xml, filter)
229
230     def add_leases(self, leases, network = None, no_dupes=False):
231         SFAv1Lease.add_leases(self.xml, leases)
232
233 if __name__ == '__main__':
234     from sfa.rspecs.rspec import RSpec
235     from sfa.rspecs.rspec_elements import *
236     r = RSpec('/tmp/resources.rspec')
237     r.load_rspec_elements(SFAv1.elements)
238     print r.get(RSpecElements.NODE)