2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from nepi.util.logger import Logger
21 from sfa.rspecs.rspec import RSpec
22 from sfa.util.xrn import Xrn, get_leaf, get_authority, hrn_to_urn, urn_to_hrn
24 log = Logger("SFA RSpec Processing")
25 log.debug("Package sfa-common not installed.\
26 Could not import sfa.rspecs.rspec and sfa.util.xrn")
28 from types import StringTypes, ListType
31 class SfaRSpecProcessing(object):
33 Class to process SFA RSpecs, parse the RSpec replies such as Advertisement RSpecs,
34 and build in the case of Request RSpecs.
36 def __init__(self, config=None):
37 self._log = Logger("SFA RSpec Processing")
40 def make_dict_rec(self, obj):
41 if not obj or isinstance(obj, (StringTypes, bool)):
43 if isinstance(obj, list):
46 objcopy.append(self.make_dict_rec(x))
48 # We thus suppose we have a child of dict
50 for k, v in obj.items():
51 objcopy[k] = self.make_dict_rec(v)
54 def parse_sfa_rspec(self, rspec_string):
56 Parse the RSpec XML as a string.
58 # rspec_type and rspec_version should be set in the config of the platform,
59 # we use GENIv3 as default one if not
61 if 'rspec_type' and 'rspec_version' in self.config:
62 rspec_version = self.config['rspec_type'] + ' ' + self.config['rspec_version']
64 rspec_version = 'GENI 3'
65 self._log.debug(rspec_version)
66 rspec = RSpec(rspec_string, version=rspec_version)
69 nodes = rspec.version.get_nodes()
71 self._log.warn("Could not retrieve nodes in RSpec: %s" % e)
73 leases = rspec.version.get_leases()
75 self._log.warn("Could not retrieve leases in RSpec: %s" % e)
77 links = rspec.version.get_links()
79 self._log.warn("Could not retrieve links in RSpec: %s" % e)
81 channels = rspec.version.get_channels()
83 self._log.warn("Could not retrieve channels in RSpec: %s" % e)
86 # Extend object and Format object field's name
89 node['network_hrn'] = Xrn(node['component_id']).authority[0] # network ? XXX
90 node['hrn'] = urn_to_hrn(node['component_id'])[0]
91 node['urn'] = node['component_id']
92 node['hostname'] = node['component_name']
93 node['initscripts'] = node.pop('pl_initscripts')
94 if 'exclusive' in node and node['exclusive']:
95 node['exclusive'] = node['exclusive'].lower() == 'true'
97 # XXX This should use a MAP as before
98 if 'position' in node: # iotlab
99 node['x'] = node['position']['posx']
100 node['y'] = node['position']['posy']
101 node['z'] = node['position']['posz']
104 if 'location' in node:
106 node['latitude'] = node['location']['latitude']
107 node['longitude'] = node['location']['longitude']
113 for tag in node['tags']:
114 node[tag['tagname']] = tag['value']
118 # We suppose we have children of dict that cannot be serialized
119 # with xmlrpc, let's make dict
120 resources.append(self.make_dict_rec(node))
122 # NOTE a channel is a resource and should not be treated independently
125 # +----+------+-------+
127 # node link channel etc.
128 #resources.extend(nodes)
129 #resources.extend(channels)
131 return {'resource': resources, 'lease': leases }
132 # 'channel': channels \
136 def build_sfa_rspec(self, slice_id, resources, properties, leases):
138 Build the XML RSpec from list of resources' urns.
139 eg. resources = ["urn:publicid:IDN+ple:modenaple+node+planetlab-1.ing.unimo.it"]
141 #if isinstance(resources, str):
142 # resources = eval(resources)
143 # rspec_type and rspec_version should be set in the config of the platform,
144 # we use GENIv3 as default one if not
146 if 'rspec_type' and 'rspec_version' in self.config:
147 rspec_version = self.config['rspec_type'] + ' ' + self.config['rspec_version']
149 rspec_version = 'GENI 3'
151 # extend rspec version with "content_type"
152 rspec_version += ' request'
154 rspec = RSpec(version=rspec_version)
159 self._log.debug("Building RSpec for resources %s" % resources)
162 for urn in resources:
163 # XXX TO BE CORRECTED, this handles None values
168 # TODO: take into account the case where we send a dict of URNs without keys
169 #resource['component_id'] = resource.pop('urn')
170 resource['component_id'] = urn
171 resource_hrn, resource_type = urn_to_hrn(resource['component_id'])
172 # build component_manager_id
173 top_auth = resource_hrn.split('.')[0]
175 resource['component_manager_id'] = "%s+%s+authority+cm" % (cm[0],top_auth)
177 if resource_type == 'node':
178 # XXX dirty hack WiLab !!!
179 # Commented Lucia, doesn't work for wilabt
181 # if 'wilab2' in self.config['sm']:
182 # resource['client_id'] = "PC"
183 # resource['sliver_type'] = "raw-pc"
186 resource['client_id'] = "node%s" % cardinal
187 resource['sliver_type'] = "raw-pc"
188 resource['disk_image'] = "hola"
189 top_auth = resource_hrn.replace("\\", "").split('.')
191 top_auth = '.'.join(top_auth)
193 resource['component_manager_id'] = "%s+%s+authority+cm" % (cm[0],top_auth)
195 nodes.append(resource)
196 elif resource_type == 'link':
197 links.append(resource)
198 elif resource_type == 'channel':
199 channels.append(resource)
201 raise Exception, "Not supported type of resource"
203 rspec.version.add_nodes(nodes, rspec_content_type="request")
204 #rspec.version.add_leases(leases)
205 #rspec.version.add_links(links)
206 #rspec.version.add_channels(channels)
208 #self._log.debug("request rspec: %s"%rspec.toxml())
209 string = rspec.toxml()
210 if wilab and properties is not None:
211 ## dirty hack for the f4f demo
212 b = string.split('\n')
213 for i, n in enumerate(b):
214 if 'sliver_type name="raw-pc"' in n:
215 b[i] = '<sliver_type name="raw-pc">'
216 b.insert(i+1, '<disk_image name="urn:publicid:IDN+wall2.ilabt.iminds.be+image+emulab-ops//%s"/>' % properties['disk_image'])
217 #b.insert(i+1, '<disk_image name="urn:publicid:IDN+wilab2.ilabt.iminds.be+image+nepi:%s"/>' % properties['disk_image'])
218 b.insert(i+2, '</sliver_type>')
220 self._log.debug("request rspec : %s" % string)