1 from copy import deepcopy
2 from StringIO import StringIO
3 from sfa.util.xrn import Xrn, urn_to_sliver_id
4 from sfa.util.plxrn import hostname_to_urn, xrn_to_hostname
5 from sfa.rspecs.baseversion import BaseVersion
7 from sfa.rspecs.elements.versions.slabv1Node import Slabv1Node
8 from sfa.rspecs.elements.versions.slabv1Sliver import Slabv1Sliver
9 from sfa.rspecs.elements.versions.slabv1Timeslot import Slabv1Timeslot
11 class Slabv1(BaseVersion):
16 #template = '<RSpec type="%s"></RSpec>' % type
18 schema = 'http://senslab.info/resources/rspec/1/ad.xsd'
19 namespace = 'http://www.geni.net/resources/rspec/3'
21 'flack': "http://www.protogeni.net/resources/rspec/ext/flack/1",
22 'planetlab': "http://www.planet-lab.org/resources/sfa/ext/planetlab/1",
24 namespaces = dict(extensions.items() + [('default', namespace)])
28 def get_networks(self):
29 network_elems = self.xml.xpath('//network')
30 networks = [network_elem.get_instance(fields=['name', 'slice']) for \
31 network_elem in network_elems]
35 def add_network(self, network):
36 network_tags = self.xml.xpath('//network[@name="%s"]' % network)
38 network_tag = self.xml.add_element('network', name=network)
40 network_tag = network_tags[0]
46 def get_nodes(self, filter=None):
47 return Slabv1Node.get_nodes(self.xml, filter)
49 def get_nodes_with_slivers(self):
50 return Slabv1Node.get_nodes_with_slivers(self.xml)
52 def get_slice_timeslot(self ):
53 return Slabv1Timeslot.get_slice_timeslot(self.xml)
55 def add_nodes(self, nodes, check_for_dupes=False):
56 return Slabv1Node.add_nodes(self.xml, nodes)
58 def merge_node(self, source_node_tag, network, no_dupes = False):
59 if no_dupes and self.get_node_element(node['hostname']):
62 network_tag = self.add_network(network)
63 network_tag.append(deepcopy(source_node_tag))
67 def get_sliver_attributes(self, hostname, node, network=None):
68 print>>sys.stderr, "\r\n \r\n \r\n \t\t SLABV1.PY get_sliver_attributes hostname %s " %(hostname)
69 nodes = self.get_nodes({'component_id': '*%s*' %hostname})
71 print>>sys.stderr, "\r\n \r\n \r\n \t\t SLABV1.PY get_sliver_attributes-----------------nodes %s " %(nodes)
72 if nodes is not None and isinstance(nodes, list) and len(nodes) > 0:
75 #sliver = node.xpath('./default:sliver | ./sliver')
76 #sliver = node.xpath('./default:sliver', namespaces=self.namespaces)
77 sliver = node['slivers']
79 if sliver is not None and isinstance(sliver, list) and len(sliver) > 0:
82 #attribs = self.attributes_list(sliver)
83 print>>sys.stderr, "\r\n \r\n \r\n \t\t SLABV1.PY get_sliver_attributes----------NN------- sliver %s self.namespaces %s attribs %s " %(sliver, self.namespaces,attribs)
86 def get_slice_attributes(self, network=None):
89 slot = self.get_slice_timeslot()
90 nodes_with_slivers = self.get_nodes_with_slivers()
91 slice_attributes.append({'timeslot':slot})
92 #slice_attributes.append({'name': 'timeslot', 'value' : slot})
93 print>>sys.stderr, "\r\n \r\n \r\n \t\t SLABV1.PY get_slice_attributes -----------------nodes_with_slivers %s "%(nodes_with_slivers)
94 # TODO: default sliver attributes in the PG rspec?
95 default_ns_prefix = self.namespaces['default']
96 for node in nodes_with_slivers:
97 sliver_attributes = self.get_sliver_attributes(node['component_id'],node, network)
98 for sliver_attribute in sliver_attributes:
99 name=str(sliver_attribute[0])
100 text =str(sliver_attribute[1])
101 attribs = sliver_attribute[2]
102 # we currently only suppor the <initscript> and <flack> attributes
104 #attribute = {'name': 'flack_info', 'value': str(attribs), 'node_id': node}
105 #slice_attributes.append(attribute)
106 #elif 'initscript' in name:
107 if 'initscript' in name:
108 if attribs is not None and 'name' in attribs:
109 value = attribs['name']
112 attribute = {'name': 'initscript', 'value': value, 'node_id': node}
113 slice_attributes.append(attribute)
116 print>>sys.stderr, "\r\n \r\n \r\n \t\t SLABV1.PY get_slice_attributes ----------------- slice_attributes %s "%(slice_attributes)
117 return slice_attributes
119 def attributes_list(self, elem):
123 opts.append((e.tag, str(e.text).strip(), e.attrib))
126 def get_default_sliver_attributes(self, network=None):
129 def add_default_sliver_attribute(self, name, value, network=None):
132 def add_slivers(self, hostnames, attributes=[], sliver_urn=None, append=False):
133 # all nodes hould already be present in the rspec. Remove all
134 # nodes that done have slivers
135 print>>sys.stderr, "\r\n \r\n \r\n \t\t\t SLABv1.PY add_slivers ----->get_node "
136 for hostname in hostnames:
137 node_elems = self.get_nodes({'component_id': '*%s*' % hostname})
140 node_elem = node_elems[0]
142 # determine sliver types for this node
143 valid_sliver_types = ['slab-node', 'emulab-openvz', 'raw-pc', 'plab-vserver', 'plab-vnode']
144 #valid_sliver_types = ['emulab-openvz', 'raw-pc', 'plab-vserver', 'plab-vnode']
145 requested_sliver_type = None
146 for sliver_type in node_elem.get('slivers', []):
147 if sliver_type.get('type') in valid_sliver_types:
148 requested_sliver_type = sliver_type['type']
150 if not requested_sliver_type:
152 sliver = {'type': requested_sliver_type,
153 'pl_tags': attributes}
154 print>>sys.stderr, "\r\n \r\n \r\n \t\t\t SLABv1.PY add_slivers node_elem %s sliver_type %s \r\n \r\n " %(node_elem, sliver_type)
155 # remove available element
156 for available_elem in node_elem.xpath('./default:available | ./available'):
157 node_elem.remove(available_elem)
159 # remove interface elements
160 for interface_elem in node_elem.xpath('./default:interface | ./interface'):
161 node_elem.remove(interface_elem)
163 # remove existing sliver_type elements
164 for sliver_type in node_elem.get('slivers', []):
165 node_elem.element.remove(sliver_type.element)
168 node_elem.element.set('client_id', hostname)
173 #slice_id = sliver_info.get('slice_id', -1)
174 #node_id = sliver_info.get('node_id', -1)
175 #sliver_id = urn_to_sliver_id(sliver_urn, slice_id, node_id)
176 #node_elem.set('sliver_id', sliver_id)
178 # add the sliver type elemnt
179 Slabv1SliverType.add_slivers(node_elem.element, sliver)
181 # remove all nodes without slivers
183 for node_elem in self.get_nodes():
184 if not node_elem['client_id']:
185 parent = node_elem.element.getparent()
186 parent.remove(node_elem.element)
188 def remove_slivers(self, slivers, network=None, no_dupes=False):
189 Slabv1Node.remove_slivers(self.xml, slivers)
195 def merge(self, in_rspec):
197 Merge contents for specified rspec with current rspec
199 from sfa.rspecs.rspec import RSpec
200 # just copy over all the child elements under the root element
201 if isinstance(in_rspec, basestring):
202 in_rspec = RSpec(in_rspec)
204 nodes = in_rspec.version.get_nodes()
205 # protogeni rspecs need to advertise the availabel sliver types
207 if not node.has_key('sliver') or not node['sliver']:
208 node['sliver'] = {'name': 'slab-node'}
210 self.add_nodes(nodes)
211 #self.add_links(in_rspec.version.get_links())
214 #rspec = RSpec(in_rspec)
215 #for child in rspec.xml.iterchildren():
216 # self.xml.root.append(child)
221 # remove unncecessary elements, attributes
222 if self.type in ['request', 'manifest']:
223 # remove 'available' element from remaining node elements
224 self.xml.remove_element('//default:available | //available')
227 class Slabv1Ad(Slabv1):
230 schema = 'http://senslab.info/resources/rspec/1/ad.xsd'
231 #http://www.geni.net/resources/rspec/3/ad.xsd'
232 template = '<rspec type="advertisement" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://senslab.info/resources/rspec/1" xmlns:flack="http://senslab.info/resources/rspec/ext/flack/1" xmlns:planetlab="http://www.planet-lab.org/resources/sfa/ext/planetlab/1" xsi:schemaLocation="http://senslab.info/resources/rspec/1 http://senslab.info/resources/rspec/1/ad.xsd http://www.planet-lab.org/resources/sfa/ext/planetlab/1 http://www.planet-lab.org/resources/sfa/ext/planetlab/1/planetlab.xsd"/>'
234 class Slabv1Request(Slabv1):
236 content_type = 'request'
237 schema = 'http://senslab.info/resources/rspec/1/request.xsd'
238 #http://www.geni.net/resources/rspec/3/request.xsd
239 template = '<rspec type="request" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://senslab.info/resources/rspec/1" xmlns:flack="http://senslab.info/resources/rspec/ext/flack/1" xmlns:planetlab="http://www.planet-lab.org/resources/sfa/ext/planetlab/1" xsi:schemaLocation="http://senslab.info/resources/rspec/1 http://senslab.info/resources/rspec/1/request.xsd http://www.planet-lab.org/resources/sfa/ext/planetlab/1 http://www.planet-lab.org/resources/sfa/ext/planetlab/1/planetlab.xsd"/>'
241 class Slabv1Manifest(Slabv1):
243 content_type = 'manifest'
244 schema = 'http://senslab.info/resources/rspec/1/manifest.xsd'
245 #http://www.geni.net/resources/rspec/3/manifest.xsd
246 template = '<rspec type="manifest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://senslab.info/resources/rspec/1" xmlns:flack="http://senslab.info/resources/rspec/ext/flack/1" xmlns:planetlab="http://www.planet-lab.org/resources/sfa/ext/planetlab/1" xsi:schemaLocation="http://senslab.info/resources/rspec/1 http://senslab.info/resources/rspec/1/manifest.xsd http://www.planet-lab.org/resources/sfa/ext/planetlab/1 http://www.planet-lab.org/resources/sfa/ext/planetlab/1/planetlab.xsd"/>'
249 if __name__ == '__main__':
250 from sfa.rspecs.rspec import RSpec
251 from sfa.rspecs.rspec_elements import *
252 r = RSpec('/tmp/slab.rspec')
253 r.load_rspec_elements(Slabv1.elements)
254 r.namespaces = Slabv1.namespaces
255 print r.get(RSpecElements.NODE)