1 from copy import deepcopy
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
13 class SFAv1(RSpecVersion):
22 template = '<RSpec type="%s"></RSpec>' % type
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]
32 def add_network(self, network):
33 network_tags = self.xml.xpath('//network[@name="%s"]' % network)
35 network_tag = self.xml.add_element('network', name=network)
37 network_tag = network_tags[0]
43 def get_nodes(self, filter=None):
44 return SFAv1Node.get_nodes(self.xml, filter)
46 def get_nodes_with_slivers(self):
47 return SFAv1Node.get_nodes_with_slivers(self.xml)
49 def add_nodes(self, nodes, network = None, no_dupes=False):
50 SFAv1Node.add_nodes(self.xml, nodes)
52 def merge_node(self, source_node_tag, network, no_dupes=False):
53 if no_dupes and self.get_node_element(node['hostname']):
57 network_tag = self.add_network(network)
58 network_tag.append(deepcopy(source_node_tag))
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')
66 network_tag = network_tags[0]
67 network_tag.set('slice', urn_to_hrn(sliver_urn)[0])
70 sliver = {'name':sliver_urn,
71 'pl_tags': attributes}
72 for hostname in hostnames:
74 sliver['name'] = sliver_urn
75 node_elems = self.get_nodes({'component_id': '*%s*' % hostname})
78 node_elem = node_elems[0]
79 SFAv1Sliver.add_slivers(node_elem.element, sliver)
81 # remove all nodes without slivers
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)
89 def remove_slivers(self, slivers, network=None, no_dupes=False):
90 SFAv1Node.remove_slivers(self.xml, slivers)
92 def get_slice_attributes(self, network=None):
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)
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:
112 slivers = SFAv1Sliver.get_slivers(node)
115 SFAv1Sliver.add_sliver_attribute(sliver, name, value)
117 # should this be an assert / raise an exception?
118 logger.error("WARNING: failed to find component_id %s" % component_id)
120 def get_sliver_attributes(self, component_id, network=None):
121 nodes = self.get_nodes({'component_id': '*%s*' % component_id})
123 if nodes is not None and isinstance(nodes, list) and len(nodes) > 0:
125 slivers = SFAv1Sliver.get_slivers(node.element)
126 if slivers is not None and isinstance(slivers, list) and len(slivers) > 0:
128 attribs = SFAv1Sliver.get_sliver_attributes(sliver.element)
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)
139 def add_default_sliver_attribute(self, name, value, network=None):
141 defaults = self.xml.xpath("//network[@name='%s']/sliver_defaults" % network)
143 defaults = self.xml.xpath("//sliver_defaults")
146 network_tag = self.xml.xpath("//network[@name='%s']" % network)
148 network_tag = self.xml.xpath("//network")
149 if len(network_tag) > 0:
150 network_tag = network_tag[0]
152 network_tag = self.add_network(network)
154 network_tag = self.xml
155 defaults = network_tag.add_element('sliver_defaults')
157 elif isinstance(defaults, list):
158 defaults = defaults[0]
159 SFAv1Sliver.add_sliver_attribute(defaults, name, value)
161 def get_default_sliver_attributes(self, network=None):
163 defaults = self.xml.xpath("//network[@name='%s']/sliver_defaults" % network)
165 defaults = self.xml.xpath("//sliver_defaults")
166 if not defaults: return []
167 return SFAv1Sliver.get_sliver_attributes(defaults[0])
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)
179 def get_links(self, network=None):
180 return PGv2Link.get_links(self.xml)
182 def get_link_requests(self):
183 return PGv2Link.get_link_requests(self.xml)
185 def add_links(self, links):
186 networks = self.get_networks()
187 if len(networks) > 0:
188 xml = networks[0].element
191 PGv2Link.add_links(xml, links)
193 def add_link_requests(self, links):
194 PGv2Link.add_link_requests(self.xml, links)
198 def merge(self, in_rspec):
200 Merge contents for specified rspec with current rspec
206 from sfa.rspecs.rspec import RSpec
207 if isinstance(in_rspec, RSpec):
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)
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)
227 def get_leases(self, filter=None):
228 return SFAv1Lease.get_leases(self.xml, filter)
230 def add_leases(self, leases, network = None, no_dupes=False):
231 SFAv1Lease.add_leases(self.xml, leases)
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)