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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Lucia Guevgeozian Odizzio <lucia.guevgeozian_odizzio@inria.fr>
26 class MANIFOLDAPI(object):
28 API to query different data platforms as SFA, TopHat, OML Central Server,
29 using Manifold Framework, the backend of MySlice.
31 def __init__(self, username, password, hostname, urlpattern):
33 self.auth_pwd = dict(AuthMethod='password', Username=username,
35 self._url = urlpattern % {'hostname':hostname}
36 self.lock = threading.Lock()
37 self.auth = self.get_session_key()
41 return xmlrpc.client.Server(self._url, allow_none = True)
43 def get_session_key(self):
45 Retrieves the session key, in order to use the same session for
48 query = {'timestamp' : 'now', 'object': 'local:session',
49 'filters' : [], 'fields' : [], 'action' : 'create'}
51 session = self.api.forward(self.auth_pwd, query)
53 if not session['value']:
54 msg = "Can not authenticate in Manifold API"
55 raise RuntimeError(msg)
57 session_key = session['value'][0]['session']
58 return dict(AuthMethod='session', session=session_key)
60 def get_resource_info(self, filters=None, fields=None):
62 Create and execute the Manifold API Query to get the resources
63 according fields and filters.
64 :param filters: resource's constraints for the experiment
66 :param fields: desire fields in the result of the query
69 query = {'action' : 'get', 'object' : 'resource'}
72 filters = self._map_attr_to_resource_filters(filters)
75 for filtername, filtervalue in filters.items():
76 newfilter = [filtername, "==", filtervalue]
77 qfilters.append(newfilter)
79 query['filters'] = qfilters
82 fields = self._check_valid_fields(fields)
85 query['fields'] = fields
87 return self.api.forward(self.auth, query)['value']
89 def get_resource_urn(self, filters=None):
91 Retrieves the resources urn of the resources matching filters.
93 return self.get_resource_info(filters, 'urn')
95 def get_slice_resources(self, slicename):
97 Retrieves resources attached to user's slice.
98 return value: list of resources' urn
101 query = {'action' : 'get', 'object' : 'resource',
102 'filters' : [['slice','==', slicename]],
106 value = self.api.forward(self.auth, query)['value']
108 for resource in value:
109 result.append(resource['urn'])
113 def add_resource_to_slice(self, slicename, resource_urn):
115 Add resource to user's slice. The query needs to specify the new
116 resource plus the previous resources already in the slice.
118 resources = self.get_slice_resources(slicename)
119 resources.append(resource_urn)
125 urn_list.append(urn_dict)
127 query = {'action' : 'update', 'object' : 'slice',
128 'filters' : [['slice_hrn','==', slicename]],
129 'params' : {'resource' : urn_list}}
132 self.api.forward(self.auth, query)
134 resources = self.get_slice_resources(slicename)
135 if resource_urn in resources:
138 msg = "Failed while trying to add %s to slice" % resource_urn
140 # check how to do warning
143 def remove_resource_from_slice(self, slicename, resource_urn):
145 Remove resource from user's slice. The query needs to specify the list
146 of previous resources in the slice without the one to be remove.
148 resources = self.get_slice_resources(slicename)
149 resources.remove(resource_urn)
155 urn_list.append(urn_dict)
157 query = {'action' : 'update', 'object' : 'slice',
158 'filters' : [['slice_hrn','==', slicename]],
159 'params' : {'resource' : urn_list}}
162 self.api.forward(self.auth, query)
164 resources = self.get_slice_resources(slicename)
165 if resource_urn not in resources:
168 msg = "Failed while trying to remove %s to slice" % resource_urn
169 # check how to do warning
172 def _get_metadata(self):
174 This method is useful to retrive metadata from different platforms
175 in order to update fields and possible filters.
177 query = {'action' : 'get', 'object' : 'local:object',
178 'filters' : [['table','=','resource']]}
180 res = self.api.forward(self.auth, query)
182 valid_fields = list()
183 for i in res['value'][0]['column']:
184 valid_fields.append(i['name'])
188 def _map_attr_to_resource_filters(self, filters):
190 Depending on the object used for the Manifold query, the filters and
191 fields can change its sintaxis. A resource field in a slice object
192 query adds 'resource.' to the field. Other changes don't follow any
193 particular convention.
195 #TODO: find out useful filters
197 'hostname' : 'hostname',
198 'longitude' : 'longitude',
199 'latitude' : 'latitude',
200 'network' : 'network',
201 'component_id' : 'component_id'
204 mapped_filters = dict()
205 for filtername, filtervalue in filters.items():
206 if attr_to_filter[filtername]:
207 new_filtername = attr_to_filter[filtername]
208 mapped_filters[new_filtername] = filtervalue
210 return mapped_filters
212 def _check_valid_fields(self, fields):
214 The fields can be a predefine set, define in the Manifold metadata.
216 valid_fields = self._get_metadata()
218 if not isinstance(fields, list):
222 if field not in valid_fields:
224 #self.warning(" Invalid Manifold field or filter ")
229 class MANIFOLDAPIFactory(object):
231 API Factory to manage a map of MANIFOLDAPI instances as key-value pairs, it
232 instanciate a single instance per key. The key represents the same SFA,
233 MF (ManiFold) credentials.
236 _lock = threading.Lock()
240 def get_api(cls, username, password,
241 #hostname = "manifold.pl.sophia.inria.fr",
242 hostname ="test.myslice.info",
243 urlpattern = "http://%(hostname)s:7080"):
245 :param username: Manifold user (also used for MySlice web login)
247 :param password: Manifold password (also used for MySlice web login)
249 :param hostname: Hostname of the Manifold API to query SFA, TopHat, etc
251 :param urlpattern: Url of the Manifold API to query SFA, TopHat, etc
252 :type urlpattern: str
255 if username and password:
256 key = cls.make_key(username, password)
258 api = cls._apis.get(key)
261 api = MANIFOLDAPI(username, password, hostname, urlpattern)
269 def make_key(cls, *args):
270 skey = "".join(map(str, args))
271 return hashlib.md5(skey).hexdigest()