1 # -*- coding: utf-8 -*-
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 HTTP server implementing the openmano API. It will answer to POST, PUT, GET methods in the appropriate URLs
26 and will use the nfvo.py module to run the appropriate method.
27 Every YAML/JSON file is checked against a schema in openmano_schemas.py module.
29 __author__
="Alfonso Tierno, Gerardo Garcia"
30 __date__
="$17-sep-2014 09:07:15$"
39 from jsonschema
import validate
as js_v
, exceptions
as js_e
40 from openmano_schemas
import vnfd_schema_v01
, vnfd_schema_v02
, \
41 nsd_schema_v01
, nsd_schema_v02
, nsd_schema_v03
, scenario_edit_schema
, \
42 scenario_action_schema
, instance_scenario_action_schema
, instance_scenario_create_schema_v01
, \
43 tenant_schema
, tenant_edit_schema
,\
44 datacenter_schema
, datacenter_edit_schema
, datacenter_action_schema
, datacenter_associate_schema
,\
45 object_schema
, netmap_new_schema
, netmap_edit_schema
48 from db_base
import db_base_Exception
49 from functools
import wraps
56 HTTP_Bad_Request
= 400
57 HTTP_Unauthorized
= 401
60 HTTP_Method_Not_Allowed
= 405
61 HTTP_Not_Acceptable
= 406
62 HTTP_Service_Unavailable
= 503
63 HTTP_Internal_Server_Error
= 500
65 def delete_nulls(var
):
68 if var
[k
] is None: del var
[k
]
69 elif type(var
[k
]) is dict or type(var
[k
]) is list or type(var
[k
]) is tuple:
70 if delete_nulls(var
[k
]): del var
[k
]
71 if len(var
) == 0: return True
72 elif type(var
) is list or type(var
) is tuple:
74 if type(k
) is dict: delete_nulls(k
)
75 if len(var
) == 0: return True
78 def convert_datetime2str(var
):
79 '''Converts a datetime variable to a string with the format '%Y-%m-%dT%H:%i:%s'
80 It enters recursively in the dict var finding this kind of variables
83 for k
,v
in var
.items():
84 if type(v
) is float and k
in ("created_at", "modified_at"):
85 var
[k
] = time
.strftime("%Y-%m-%dT%H:%M:%S", time
.localtime(v
) )
86 elif type(v
) is dict or type(v
) is list or type(v
) is tuple:
87 convert_datetime2str(v
)
88 if len(var
) == 0: return True
89 elif type(var
) is list or type(var
) is tuple:
91 convert_datetime2str(v
)
93 def log_to_logger(fn
):
95 Wrap a Bottle request so that a log line is emitted after it's handled.
96 (This decorator can be extended to take the desired logger as a param.)
99 def _log_to_logger(*args
, **kwargs
):
100 actual_response
= fn(*args
, **kwargs
)
101 # modify this to log exactly what you need:
102 logger
.info('FROM %s %s %s %s' % (bottle
.request
.remote_addr
,
103 bottle
.request
.method
,
105 bottle
.response
.status
))
106 return actual_response
107 return _log_to_logger
109 class httpserver(threading
.Thread
):
110 def __init__(self
, db
, admin
=False, host
='localhost', port
=9090):
115 logger
= logging
.getLogger('openmano.http')
116 threading
.Thread
.__init
__(self
)
118 self
.port
= port
#Port where the listen service must be started
120 self
.name
= "http_admin"
123 #self.url_preffix = 'http://' + host + ':' + str(port) + url_base
125 #self.first_usable_connection_index = 10
126 #self.next_connection_index = self.first_usable_connection_index #The next connection index to be used
127 #Ensure that when the main program exits the thread will also exit
132 bottle
.install(log_to_logger
)
133 bottle
.run(host
=self
.host
, port
=self
.port
, debug
=False, quiet
=True)
135 def run_bottle(db
, host_
='localhost', port_
=9090):
136 '''used for launching in main thread, so that it can be debugged'''
139 bottle
.run(host
=host_
, port
=port_
, debug
=True) #quiet=True
142 @bottle.route(url_base
+ '/', method
='GET')
145 return 'works' #TODO: to be completed
151 def change_keys_http2db(data
, http_db
, reverse
=False):
152 '''Change keys of dictionary data acording to the key_dict values
153 This allow change from http interface names to database names.
154 When reverse is True, the change is otherwise
156 data: can be a dictionary or a list
157 http_db: is a dictionary with hhtp names as keys and database names as value
158 reverse: by default change is done from http api to database. If True change is done otherwise
159 Return: None, but data is modified'''
160 if type(data
) is tuple or type(data
) is list:
162 change_keys_http2db(d
, http_db
, reverse
)
163 elif type(data
) is dict or type(data
) is bottle
.FormsDict
:
165 for k
,v
in http_db
.items():
166 if v
in data
: data
[k
]=data
.pop(v
)
168 for k
,v
in http_db
.items():
169 if k
in data
: data
[v
]=data
.pop(k
)
171 def format_out(data
):
172 '''return string of dictionary data according to requested json, yaml, xml. By default json'''
173 logger
.debug(yaml
.safe_dump(data
, explicit_start
=True, indent
=4, default_flow_style
=False, tags
=False, encoding
='utf-8', allow_unicode
=True) )
174 if 'application/yaml' in bottle
.request
.headers
.get('Accept'):
175 bottle
.response
.content_type
='application/yaml'
176 return yaml
.safe_dump(data
, explicit_start
=True, indent
=4, default_flow_style
=False, tags
=False, encoding
='utf-8', allow_unicode
=True) #, canonical=True, default_style='"'
177 else: #by default json
178 bottle
.response
.content_type
='application/json'
179 #return data #json no style
180 return json
.dumps(data
, indent
=4) + "\n"
182 def format_in(default_schema
, version_fields
=None, version_dict_schema
=None):
183 ''' Parse the content of HTTP request against a json_schema
185 default_schema: The schema to be parsed by default if no version field is found in the client data
186 version_fields: If provided it contains a tuple or list with the fields to iterate across the client data to obtain the version
187 version_dict_schema: It contains a dictionary with the version as key, and json schema to apply as value
188 It can contain a None as key, and this is apply if the client data version does not match any key
190 user_data, used_schema: if the data is successfully decoded and matches the schema
191 launch a bottle abort if fails
193 #print "HEADERS :" + str(bottle.request.headers.items())
195 error_text
= "Invalid header format "
196 format_type
= bottle
.request
.headers
.get('Content-Type', 'application/json')
197 if 'application/json' in format_type
:
198 error_text
= "Invalid json format "
199 #Use the json decoder instead of bottle decoder because it informs about the location of error formats with a ValueError exception
200 client_data
= json
.load(bottle
.request
.body
)
201 #client_data = bottle.request.json()
202 elif 'application/yaml' in format_type
:
203 error_text
= "Invalid yaml format "
204 client_data
= yaml
.load(bottle
.request
.body
)
205 elif 'application/xml' in format_type
:
206 bottle
.abort(501, "Content-Type: application/xml not supported yet.")
208 print 'Content-Type ' + str(format_type
) + ' not supported.'
209 bottle
.abort(HTTP_Not_Acceptable
, 'Content-Type ' + str(format_type
) + ' not supported.')
211 #if client_data == None:
212 # bottle.abort(HTTP_Bad_Request, "Content error, empty")
215 #logger.debug('client-data: %s', client_data)
216 #look for the client provider version
217 error_text
= "Invalid content "
218 client_version
= None
220 if version_fields
!= None:
221 client_version
= client_data
222 for field
in version_fields
:
223 if field
in client_version
:
224 client_version
= client_version
[field
]
228 if client_version
==None:
229 used_schema
=default_schema
230 elif version_dict_schema
!=None:
231 if client_version
in version_dict_schema
:
232 used_schema
= version_dict_schema
[client_version
]
233 elif None in version_dict_schema
:
234 used_schema
= version_dict_schema
[None]
235 if used_schema
==None:
236 bottle
.abort(HTTP_Bad_Request
, "Invalid schema version or missing version field")
238 js_v(client_data
, used_schema
)
239 return client_data
, used_schema
240 except (ValueError, yaml
.YAMLError
) as exc
:
241 error_text
+= str(exc
)
243 bottle
.abort(HTTP_Bad_Request
, error_text
)
244 except js_e
.ValidationError
as exc
:
245 print "validate_in error, jsonschema exception ", exc
.message
, "at", exc
.path
247 if len(exc
.path
)>0: error_pos
=" at " + ":".join(map(json
.dumps
, exc
.path
))
248 bottle
.abort(HTTP_Bad_Request
, error_text
+ exc
.message
+ error_pos
)
250 # bottle.abort(HTTP_Bad_Request, "Content error: Failed to parse Content-Type", error_pos)
253 def filter_query_string(qs
, http2db
, allowed
):
254 '''Process query string (qs) checking that contains only valid tokens for avoiding SQL injection
256 'qs': bottle.FormsDict variable to be processed. None or empty is considered valid
257 'http2db': dictionary with change from http API naming (dictionary key) to database naming(dictionary value)
258 'allowed': list of allowed string tokens (API http naming). All the keys of 'qs' must be one of 'allowed'
259 Return: A tuple with the (select,where,limit) to be use in a database query. All of then transformed to the database naming
260 select: list of items to retrieve, filtered by query string 'field=token'. If no 'field' is present, allowed list is returned
261 where: dictionary with key, value, taken from the query string token=value. Empty if nothing is provided
262 limit: limit dictated by user with the query string 'limit'. 100 by default
263 abort if not permited, using bottel.abort
268 #if type(qs) is not bottle.FormsDict:
269 # bottle.abort(HTTP_Internal_Server_Error, '!!!!!!!!!!!!!!invalid query string not a dictionary')
270 # #bottle.abort(HTTP_Internal_Server_Error, "call programmer")
273 select
+= qs
.getall(k
)
276 bottle
.abort(HTTP_Bad_Request
, "Invalid query string at 'field="+v
+"'")
281 bottle
.abort(HTTP_Bad_Request
, "Invalid query string at 'limit="+qs
[k
]+"'")
284 bottle
.abort(HTTP_Bad_Request
, "Invalid query string at '"+k
+"="+qs
[k
]+"'")
285 if qs
[k
]!="null": where
[k
]=qs
[k
]
287 if len(select
)==0: select
+= allowed
288 #change from http api to database naming
289 for i
in range(0,len(select
)):
291 if http2db
and k
in http2db
:
292 select
[i
] = http2db
[k
]
294 change_keys_http2db(where
, http2db
)
295 #print "filter_query_string", select,where,limit
297 return select
,where
,limit
299 @bottle.hook('after_request')
301 '''Don't know yet if really needed. Keep it just in case'''
302 bottle
.response
.headers
['Access-Control-Allow-Origin'] = '*'
308 @bottle.route(url_base
+ '/tenants', method
='GET')
309 def http_get_tenants():
310 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
311 select_
,where_
,limit_
= filter_query_string(bottle
.request
.query
, None,
312 ('uuid','name','description','created_at') )
314 tenants
= mydb
.get_rows(FROM
='nfvo_tenants', SELECT
=select_
,WHERE
=where_
,LIMIT
=limit_
)
315 #change_keys_http2db(content, http2db_tenant, reverse=True)
316 convert_datetime2str(tenants
)
317 data
={'tenants' : tenants
}
318 return format_out(data
)
319 except db_base_Exception
as e
:
320 logger
.error("http_get_tenants error {}: {}".format(e
.http_code
, str(e
)))
321 bottle
.abort(e
.http_code
, str(e
))
323 @bottle.route(url_base
+ '/tenants/<tenant_id>', method
='GET')
324 def http_get_tenant_id(tenant_id
):
325 '''get tenant details, can use both uuid or name'''
327 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
329 tenant
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
, "tenant")
330 #change_keys_http2db(content, http2db_tenant, reverse=True)
331 convert_datetime2str(tenant
)
332 data
={'tenant' : tenant
}
333 return format_out(data
)
334 except db_base_Exception
as e
:
335 logger
.error("http_get_tenant_id error {}: {}".format(e
.http_code
, str(e
)))
336 bottle
.abort(e
.http_code
, str(e
))
338 @bottle.route(url_base
+ '/tenants', method
='POST')
339 def http_post_tenants():
340 '''insert a tenant into the catalogue. '''
342 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
343 http_content
,_
= format_in( tenant_schema
)
344 r
= utils
.remove_extra_items(http_content
, tenant_schema
)
345 if r
is not None: print "http_post_tenants: Warning: remove extra items ", r
347 data
= nfvo
.new_tenant(mydb
, http_content
['tenant'])
348 return http_get_tenant_id(data
)
349 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
350 logger
.error("http_post_tenants error {}: {}".format(e
.http_code
, str(e
)))
351 bottle
.abort(e
.http_code
, str(e
))
353 @bottle.route(url_base
+ '/tenants/<tenant_id>', method
='PUT')
354 def http_edit_tenant_id(tenant_id
):
355 '''edit tenant details, can use both uuid or name'''
357 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
358 http_content
,_
= format_in( tenant_edit_schema
)
359 r
= utils
.remove_extra_items(http_content
, tenant_edit_schema
)
360 if r
is not None: print "http_edit_tenant_id: Warning: remove extra items ", r
362 #obtain data, check that only one exist
364 tenant
= mydb
.get_table_by_uuid_name('nfvo_tenants', tenant_id
)
366 tenant_id
= tenant
['uuid']
367 where
={'uuid': tenant
['uuid']}
368 mydb
.update_rows('nfvo_tenants', http_content
['tenant'], where
)
369 return http_get_tenant_id(tenant_id
)
370 except db_base_Exception
as e
:
371 logger
.error("http_edit_tenant_id error {}: {}".format(e
.http_code
, str(e
)))
372 bottle
.abort(e
.http_code
, str(e
))
374 @bottle.route(url_base
+ '/tenants/<tenant_id>', method
='DELETE')
375 def http_delete_tenant_id(tenant_id
):
376 '''delete a tenant from database, can use both uuid or name'''
377 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
379 data
= nfvo
.delete_tenant(mydb
, tenant_id
)
380 return format_out({"result":"tenant " + data
+ " deleted"})
381 except db_base_Exception
as e
:
382 logger
.error("http_delete_tenant_id error {}: {}".format(e
.http_code
, str(e
)))
383 bottle
.abort(e
.http_code
, str(e
))
386 @bottle.route(url_base
+ '/<tenant_id>/datacenters', method
='GET')
387 def http_get_datacenters(tenant_id
):
388 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
390 if tenant_id
!= 'any':
391 #check valid tenant_id
392 nfvo
.check_tenant(mydb
, tenant_id
)
393 select_
,where_
,limit_
= filter_query_string(bottle
.request
.query
, None,
394 ('uuid','name','vim_url','type','created_at') )
395 if tenant_id
!= 'any':
396 where_
['nfvo_tenant_id'] = tenant_id
397 if 'created_at' in select_
:
398 select_
[ select_
.index('created_at') ] = 'd.created_at as created_at'
399 if 'created_at' in where_
:
400 where_
['d.created_at'] = where_
.pop('created_at')
401 datacenters
= mydb
.get_rows(FROM
='datacenters as d join tenants_datacenters as td on d.uuid=td.datacenter_id',
402 SELECT
=select_
,WHERE
=where_
,LIMIT
=limit_
)
404 datacenters
= mydb
.get_rows(FROM
='datacenters',
405 SELECT
=select_
,WHERE
=where_
,LIMIT
=limit_
)
406 #change_keys_http2db(content, http2db_tenant, reverse=True)
407 convert_datetime2str(datacenters
)
408 data
={'datacenters' : datacenters
}
409 return format_out(data
)
410 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
411 logger
.error("http_get_datacenters error {}: {}".format(e
.http_code
, str(e
)))
412 bottle
.abort(e
.http_code
, str(e
))
414 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>', method
='GET')
415 def http_get_datacenter_id(tenant_id
, datacenter_id
):
416 '''get datacenter details, can use both uuid or name'''
417 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
419 if tenant_id
!= 'any':
420 #check valid tenant_id
421 nfvo
.check_tenant(mydb
, tenant_id
)
423 what
= 'uuid' if utils
.check_valid_uuid(datacenter_id
) else 'name'
425 where_
[what
] = datacenter_id
426 select_
=['uuid', 'name','vim_url', 'vim_url_admin', 'type', 'config', 'description', 'd.created_at as created_at']
427 if tenant_id
!= 'any':
428 select_
.append("datacenter_tenant_id")
429 where_
['td.nfvo_tenant_id']= tenant_id
430 from_
='datacenters as d join tenants_datacenters as td on d.uuid=td.datacenter_id'
432 from_
='datacenters as d'
433 datacenters
= mydb
.get_rows(
438 if len(datacenters
)==0:
439 bottle
.abort( HTTP_Not_Found
, "No datacenter found for tenant with {} '{}'".format(what
, datacenter_id
) )
440 elif len(datacenters
)>1:
441 bottle
.abort( HTTP_Bad_Request
, "More than one datacenter found for tenant with {} '{}'".format(what
, datacenter_id
) )
442 datacenter
= datacenters
[0]
443 if tenant_id
!= 'any':
445 vim_tenants
= mydb
.get_rows(
446 SELECT
=("vim_tenant_name", "vim_tenant_id", "user"),
447 FROM
="datacenter_tenants",
448 WHERE
={"uuid": datacenters
[0]["datacenter_tenant_id"]},
449 ORDER_BY
=("created", ) )
450 del datacenter
["datacenter_tenant_id"]
451 datacenter
["vim_tenants"] = vim_tenants
453 if datacenter
['config'] != None:
455 config_dict
= yaml
.load(datacenter
['config'])
456 datacenter
['config'] = config_dict
458 print "Exception '%s' while trying to load config information" % str(e
)
459 #change_keys_http2db(content, http2db_datacenter, reverse=True)
460 convert_datetime2str(datacenter
)
461 data
={'datacenter' : datacenter
}
462 return format_out(data
)
463 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
464 logger
.error("http_get_datacenter_id error {}: {}".format(e
.http_code
, str(e
)))
465 bottle
.abort(e
.http_code
, str(e
))
467 @bottle.route(url_base
+ '/datacenters', method
='POST')
468 def http_post_datacenters():
469 '''insert a tenant into the catalogue. '''
471 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
472 http_content
,_
= format_in( datacenter_schema
)
473 r
= utils
.remove_extra_items(http_content
, datacenter_schema
)
474 if r
is not None: print "http_post_datacenters: Warning: remove extra items ", r
476 data
= nfvo
.new_datacenter(mydb
, http_content
['datacenter'])
477 return http_get_datacenter_id('any', data
)
478 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
479 logger
.error("http_post_datacenters error {}: {}".format(e
.http_code
, str(e
)))
480 bottle
.abort(e
.http_code
, str(e
))
482 @bottle.route(url_base
+ '/datacenters/<datacenter_id_name>', method
='PUT')
483 def http_edit_datacenter_id(datacenter_id_name
):
484 '''edit datacenter details, can use both uuid or name'''
485 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
487 http_content
,_
= format_in( datacenter_edit_schema
)
488 r
= utils
.remove_extra_items(http_content
, datacenter_edit_schema
)
489 if r
is not None: print "http_edit_datacenter_id: Warning: remove extra items ", r
492 datacenter_id
= nfvo
.edit_datacenter(mydb
, datacenter_id_name
, http_content
['datacenter'])
493 return http_get_datacenter_id('any', datacenter_id
)
494 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
495 logger
.error("http_edit_datacenter_id error {}: {}".format(e
.http_code
, str(e
)))
496 bottle
.abort(e
.http_code
, str(e
))
498 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>/networks', method
='GET') #deprecated
499 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>/netmaps', method
='GET')
500 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>/netmaps/<netmap_id>', method
='GET')
501 def http_getnetmap_datacenter_id(tenant_id
, datacenter_id
, netmap_id
=None):
502 '''get datacenter networks, can use both uuid or name'''
503 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
506 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
, "datacenter")
507 where_
= {"datacenter_id":datacenter_dict
['uuid']}
509 if utils
.check_valid_uuid(netmap_id
):
510 where_
["uuid"] = netmap_id
512 where_
["name"] = netmap_id
513 netmaps
=mydb
.get_rows(FROM
='datacenter_nets',
514 SELECT
=('name','vim_net_id as vim_id', 'uuid', 'type','multipoint','shared','description', 'created_at'),
516 convert_datetime2str(netmaps
)
517 utils
.convert_str2boolean(netmaps
, ('shared', 'multipoint') )
518 if netmap_id
and len(netmaps
)==1:
519 data
={'netmap' : netmaps
[0]}
520 elif netmap_id
and len(netmaps
)==0:
521 bottle
.abort(HTTP_Not_Found
, "No netmap found with " + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), where_
.iteritems())) )
524 data
={'netmaps' : netmaps
}
525 return format_out(data
)
526 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
527 logger
.error("http_getnetwork_datacenter_id error {}: {}".format(e
.http_code
, str(e
)))
528 bottle
.abort(e
.http_code
, str(e
))
530 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>/netmaps', method
='DELETE')
531 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>/netmaps/<netmap_id>', method
='DELETE')
532 def http_delnetmap_datacenter_id(tenant_id
, datacenter_id
, netmap_id
=None):
533 '''get datacenter networks, can use both uuid or name'''
534 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
537 datacenter_dict
= mydb
.get_table_by_uuid_name('datacenters', datacenter_id
, "datacenter")
538 where_
= {"datacenter_id":datacenter_dict
['uuid']}
540 if utils
.check_valid_uuid(netmap_id
):
541 where_
["uuid"] = netmap_id
543 where_
["name"] = netmap_id
544 #change_keys_http2db(content, http2db_tenant, reverse=True)
545 deleted
= mydb
.delete_row(FROM
='datacenter_nets', WHERE
= where_
)
546 if deleted
== 0 and netmap_id
:
547 bottle
.abort(HTTP_Not_Found
, "No netmap found with " + " and ".join(map(lambda x
: str(x
[0])+": "+str(x
[1]), where_
.iteritems())) )
549 return format_out({"result": "netmap %s deleted" % netmap_id
})
551 return format_out({"result": "%d netmap deleted" % deleted
})
552 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
553 logger
.error("http_delnetmap_datacenter_id error {}: {}".format(e
.http_code
, str(e
)))
554 bottle
.abort(e
.http_code
, str(e
))
557 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>/netmaps/upload', method
='POST')
558 def http_uploadnetmap_datacenter_id(tenant_id
, datacenter_id
):
559 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
561 netmaps
= nfvo
.datacenter_new_netmap(mydb
, tenant_id
, datacenter_id
, None)
562 convert_datetime2str(netmaps
)
563 utils
.convert_str2boolean(netmaps
, ('shared', 'multipoint') )
564 data
={'netmaps' : netmaps
}
565 return format_out(data
)
566 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
567 logger
.error("http_uploadnetmap_datacenter_id error {}: {}".format(e
.http_code
, str(e
)))
568 bottle
.abort(e
.http_code
, str(e
))
570 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>/netmaps', method
='POST')
571 def http_postnetmap_datacenter_id(tenant_id
, datacenter_id
):
572 '''creates a new netmap'''
573 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
575 http_content
,_
= format_in( netmap_new_schema
)
576 r
= utils
.remove_extra_items(http_content
, netmap_new_schema
)
577 if r
is not None: print "http_postnetmap_datacenter_id: Warning: remove extra items ", r
580 #obtain data, check that only one exist
581 netmaps
= nfvo
.datacenter_new_netmap(mydb
, tenant_id
, datacenter_id
, http_content
)
582 convert_datetime2str(netmaps
)
583 utils
.convert_str2boolean(netmaps
, ('shared', 'multipoint') )
584 data
={'netmaps' : netmaps
}
585 return format_out(data
)
586 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
587 logger
.error("http_postnetmap_datacenter_id error {}: {}".format(e
.http_code
, str(e
)))
588 bottle
.abort(e
.http_code
, str(e
))
590 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>/netmaps/<netmap_id>', method
='PUT')
591 def http_putnettmap_datacenter_id(tenant_id
, datacenter_id
, netmap_id
):
593 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
595 http_content
,_
= format_in( netmap_edit_schema
)
596 r
= utils
.remove_extra_items(http_content
, netmap_edit_schema
)
597 if r
is not None: print "http_putnettmap_datacenter_id: Warning: remove extra items ", r
599 #obtain data, check that only one exist
601 nfvo
.datacenter_edit_netmap(mydb
, tenant_id
, datacenter_id
, netmap_id
, http_content
)
602 return http_getnetmap_datacenter_id(tenant_id
, datacenter_id
, netmap_id
)
603 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
604 logger
.error("http_putnettmap_datacenter_id error {}: {}".format(e
.http_code
, str(e
)))
605 bottle
.abort(e
.http_code
, str(e
))
608 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>/action', method
='POST')
609 def http_action_datacenter_id(tenant_id
, datacenter_id
):
610 '''perform an action over datacenter, can use both uuid or name'''
611 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
613 http_content
,_
= format_in( datacenter_action_schema
)
614 r
= utils
.remove_extra_items(http_content
, datacenter_action_schema
)
615 if r
is not None: print "http_action_datacenter_id: Warning: remove extra items ", r
618 #obtain data, check that only one exist
619 result
= nfvo
.datacenter_action(mydb
, tenant_id
, datacenter_id
, http_content
)
620 if 'net-update' in http_content
:
621 return http_getnetmap_datacenter_id(datacenter_id
)
623 return format_out(result
)
624 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
625 logger
.error("http_action_datacenter_id error {}: {}".format(e
.http_code
, str(e
)))
626 bottle
.abort(e
.http_code
, str(e
))
629 @bottle.route(url_base
+ '/datacenters/<datacenter_id>', method
='DELETE')
630 def http_delete_datacenter_id( datacenter_id
):
631 '''delete a tenant from database, can use both uuid or name'''
633 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
635 data
= nfvo
.delete_datacenter(mydb
, datacenter_id
)
636 return format_out({"result":"datacenter '" + data
+ "' deleted"})
637 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
638 logger
.error("http_delete_datacenter_id error {}: {}".format(e
.http_code
, str(e
)))
639 bottle
.abort(e
.http_code
, str(e
))
641 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>', method
='POST')
642 def http_associate_datacenters(tenant_id
, datacenter_id
):
643 '''associate an existing datacenter to a this tenant. '''
644 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
646 http_content
,_
= format_in( datacenter_associate_schema
)
647 r
= utils
.remove_extra_items(http_content
, datacenter_associate_schema
)
648 if r
!= None: print "http_associate_datacenters: Warning: remove extra items ", r
650 id_
= nfvo
.associate_datacenter_to_tenant(mydb
, tenant_id
, datacenter_id
,
651 http_content
['datacenter'].get('vim_tenant'),
652 http_content
['datacenter'].get('vim_tenant_name'),
653 http_content
['datacenter'].get('vim_username'),
654 http_content
['datacenter'].get('vim_password')
656 return http_get_datacenter_id(tenant_id
, id_
)
657 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
658 logger
.error("http_associate_datacenters error {}: {}".format(e
.http_code
, str(e
)))
659 bottle
.abort(e
.http_code
, str(e
))
661 @bottle.route(url_base
+ '/<tenant_id>/datacenters/<datacenter_id>', method
='DELETE')
662 def http_deassociate_datacenters(tenant_id
, datacenter_id
):
663 '''deassociate an existing datacenter to a this tenant. '''
664 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
666 data
= nfvo
.deassociate_datacenter_to_tenant(mydb
, tenant_id
, datacenter_id
)
667 return format_out({"result": data
})
668 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
669 logger
.error("http_deassociate_datacenters error {}: {}".format(e
.http_code
, str(e
)))
670 bottle
.abort(e
.http_code
, str(e
))
672 @bottle.route(url_base
+ '/<tenant_id>/vim/<datacenter_id>/<item>', method
='GET')
673 @bottle.route(url_base
+ '/<tenant_id>/vim/<datacenter_id>/<item>/<name>', method
='GET')
674 def http_get_vim_items(tenant_id
, datacenter_id
, item
, name
=None):
675 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
677 data
= nfvo
.vim_action_get(mydb
, tenant_id
, datacenter_id
, item
, name
)
678 return format_out(data
)
679 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
680 logger
.error("http_get_vim_items error {}: {}".format(e
.http_code
, str(e
)))
681 bottle
.abort(e
.http_code
, str(e
))
683 @bottle.route(url_base
+ '/<tenant_id>/vim/<datacenter_id>/<item>/<name>', method
='DELETE')
684 def http_del_vim_items(tenant_id
, datacenter_id
, item
, name
):
685 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
687 data
= nfvo
.vim_action_delete(mydb
, tenant_id
, datacenter_id
, item
, name
)
688 return format_out({"result":data
})
689 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
690 logger
.error("http_del_vim_items error {}: {}".format(e
.http_code
, str(e
)))
691 bottle
.abort(e
.http_code
, str(e
))
692 @bottle.route(url_base
+ '/<tenant_id>/vim/<datacenter_id>/<item>', method
='POST')
693 def http_post_vim_items(tenant_id
, datacenter_id
, item
):
694 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
695 http_content
,_
= format_in( object_schema
)
697 data
= nfvo
.vim_action_create(mydb
, tenant_id
, datacenter_id
, item
, http_content
)
698 return format_out(data
)
699 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
700 logger
.error("http_post_vim_items error {}: {}".format(e
.http_code
, str(e
)))
701 bottle
.abort(e
.http_code
, str(e
))
703 @bottle.route(url_base
+ '/<tenant_id>/vnfs', method
='GET')
704 def http_get_vnfs(tenant_id
):
705 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
707 if tenant_id
!= 'any':
708 #check valid tenant_id
709 nfvo
.check_tenant(mydb
, tenant_id
)
710 select_
,where_
,limit_
= filter_query_string(bottle
.request
.query
, None,
711 ('uuid','name','description','public', "tenant_id", "created_at") )
713 if tenant_id
!= "any":
714 where_or
["tenant_id"] = tenant_id
715 where_or
["public"] = True
716 vnfs
= mydb
.get_rows(FROM
='vnfs', SELECT
=select_
,WHERE
=where_
,WHERE_OR
=where_or
, WHERE_AND_OR
="AND",LIMIT
=limit_
)
717 #change_keys_http2db(content, http2db_vnf, reverse=True)
718 utils
.convert_str2boolean(vnfs
, ('public',))
719 convert_datetime2str(vnfs
)
721 return format_out(data
)
722 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
723 logger
.error("http_get_vnfs error {}: {}".format(e
.http_code
, str(e
)))
724 bottle
.abort(e
.http_code
, str(e
))
726 @bottle.route(url_base
+ '/<tenant_id>/vnfs/<vnf_id>', method
='GET')
727 def http_get_vnf_id(tenant_id
,vnf_id
):
728 '''get vnf details, can use both uuid or name'''
729 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
731 vnf
= nfvo
.get_vnf_id(mydb
,tenant_id
,vnf_id
)
732 utils
.convert_str2boolean(vnf
, ('public',))
733 convert_datetime2str(vnf
)
734 return format_out(vnf
)
735 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
736 logger
.error("http_get_vnf_id error {}: {}".format(e
.http_code
, str(e
)))
737 bottle
.abort(e
.http_code
, str(e
))
739 @bottle.route(url_base
+ '/<tenant_id>/vnfs', method
='POST')
740 def http_post_vnfs(tenant_id
):
741 '''insert a vnf into the catalogue. Creates the flavor and images in the VIM, and creates the VNF and its internal structure in the OPENMANO DB'''
742 #print "Parsing the YAML file of the VNF"
744 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
745 http_content
, used_schema
= format_in( vnfd_schema_v01
, ("schema_version",), {"0.2": vnfd_schema_v02
})
746 r
= utils
.remove_extra_items(http_content
, used_schema
)
747 if r
is not None: print "http_post_vnfs: Warning: remove extra items ", r
749 if used_schema
== vnfd_schema_v01
:
750 vnf_id
= nfvo
.new_vnf(mydb
,tenant_id
,http_content
)
751 elif used_schema
== vnfd_schema_v02
:
752 vnf_id
= nfvo
.new_vnf_v02(mydb
,tenant_id
,http_content
)
754 logger
.warning('Unexpected schema_version: %s', http_content
.get("schema_version"))
755 bottle
.abort(HTTP_Bad_Request
, "Invalid schema version")
756 return http_get_vnf_id(tenant_id
, vnf_id
)
757 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
758 logger
.error("http_post_vnfs error {}: {}".format(e
.http_code
, str(e
)))
759 bottle
.abort(e
.http_code
, str(e
))
761 @bottle.route(url_base
+ '/<tenant_id>/vnfs/<vnf_id>', method
='DELETE')
762 def http_delete_vnf_id(tenant_id
,vnf_id
):
763 '''delete a vnf from database, and images and flavors in VIM when appropriate, can use both uuid or name'''
764 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
765 #check valid tenant_id and deletes the vnf, including images,
767 data
= nfvo
.delete_vnf(mydb
,tenant_id
,vnf_id
)
768 #print json.dumps(data, indent=4)
769 return format_out({"result":"VNF " + data
+ " deleted"})
770 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
771 logger
.error("http_delete_vnf_id error {}: {}".format(e
.http_code
, str(e
)))
772 bottle
.abort(e
.http_code
, str(e
))
774 #@bottle.route(url_base + '/<tenant_id>/hosts/topology', method='GET')
775 #@bottle.route(url_base + '/<tenant_id>/physicalview/Madrid-Alcantara', method='GET')
776 @bottle.route(url_base
+ '/<tenant_id>/physicalview/<datacenter>', method
='GET')
777 def http_get_hosts(tenant_id
, datacenter
):
778 '''get the tidvim host hopology from the vim.'''
779 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
780 #print "http_get_hosts received by tenant " + tenant_id + ' datacenter ' + datacenter
782 if datacenter
== 'treeview':
783 data
= nfvo
.get_hosts(mydb
, tenant_id
)
785 #openmano-gui is using a hardcoded value for the datacenter
786 result
, data
= nfvo
.get_hosts_info(mydb
, tenant_id
) #, datacenter)
789 print "http_get_hosts error %d %s" % (-result
, data
)
790 bottle
.abort(-result
, data
)
792 convert_datetime2str(data
)
793 print json
.dumps(data
, indent
=4)
794 return format_out(data
)
795 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
796 logger
.error("http_get_hosts error {}: {}".format(e
.http_code
, str(e
)))
797 bottle
.abort(e
.http_code
, str(e
))
800 @bottle.route(url_base
+ '/<path:path>', method
='OPTIONS')
801 def http_options_deploy(path
):
802 '''For some reason GUI web ask for OPTIONS that must be responded'''
803 #TODO: check correct path, and correct headers request
804 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
805 bottle
.response
.set_header('Access-Control-Allow-Methods','POST, GET, PUT, DELETE, OPTIONS')
806 bottle
.response
.set_header('Accept','application/yaml,application/json')
807 bottle
.response
.set_header('Content-Type','application/yaml,application/json')
808 bottle
.response
.set_header('Access-Control-Allow-Headers','content-type')
809 bottle
.response
.set_header('Access-Control-Allow-Origin','*')
812 @bottle.route(url_base
+ '/<tenant_id>/topology/deploy', method
='POST')
813 def http_post_deploy(tenant_id
):
814 '''post topology deploy.'''
815 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
817 http_content
, used_schema
= format_in( nsd_schema_v01
, ("schema_version",), {2: nsd_schema_v02
})
818 #r = utils.remove_extra_items(http_content, used_schema)
819 #if r is not None: print "http_post_deploy: Warning: remove extra items ", r
820 #print "http_post_deploy input: ", http_content
823 scenario_id
= nfvo
.new_scenario(mydb
, tenant_id
, http_content
)
824 instance
= nfvo
.start_scenario(mydb
, tenant_id
, scenario_id
, http_content
['name'], http_content
['name'])
825 #print json.dumps(data, indent=4)
826 return format_out(instance
)
827 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
828 logger
.error("http_post_deploy error {}: {}".format(e
.http_code
, str(e
)))
829 bottle
.abort(e
.http_code
, str(e
))
831 @bottle.route(url_base
+ '/<tenant_id>/topology/verify', method
='POST')
832 def http_post_verify(tenant_id
):
834 # '''post topology verify'''
835 # print "http_post_verify by tenant " + tenant_id + ' datacenter ' + datacenter
836 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
843 @bottle.route(url_base
+ '/<tenant_id>/scenarios', method
='POST')
844 def http_post_scenarios(tenant_id
):
845 '''add a scenario into the catalogue. Creates the scenario and its internal structure in the OPENMANO DB'''
846 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
847 http_content
, used_schema
= format_in( nsd_schema_v01
, ("schema_version",), {2: nsd_schema_v02
, "0.3": nsd_schema_v03
})
848 #r = utils.remove_extra_items(http_content, used_schema)
849 #if r is not None: print "http_post_scenarios: Warning: remove extra items ", r
850 #print "http_post_scenarios input: ", http_content
852 if used_schema
== nsd_schema_v01
:
853 scenario_id
= nfvo
.new_scenario(mydb
, tenant_id
, http_content
)
854 elif used_schema
== nsd_schema_v02
:
855 scenario_id
= nfvo
.new_scenario_v02(mydb
, tenant_id
, http_content
)
856 elif used_schema
== nsd_schema_v03
:
857 scenario_id
= nfvo
.new_scenario_v03(mydb
, tenant_id
, http_content
)
859 logger
.warning('Unexpected schema_version: %s', http_content
.get("schema_version"))
860 bottle
.abort(HTTP_Bad_Request
, "Invalid schema version")
861 #print json.dumps(data, indent=4)
862 #return format_out(data)
863 return http_get_scenario_id(tenant_id
, scenario_id
)
864 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
865 logger
.error("http_post_scenarios error {}: {}".format(e
.http_code
, str(e
)))
866 bottle
.abort(e
.http_code
, str(e
))
868 @bottle.route(url_base
+ '/<tenant_id>/scenarios/<scenario_id>/action', method
='POST')
869 def http_post_scenario_action(tenant_id
, scenario_id
):
870 '''take an action over a scenario'''
871 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
872 #check valid tenant_id
874 nfvo
.check_tenant(mydb
, tenant_id
)
876 http_content
,_
= format_in( scenario_action_schema
)
877 r
= utils
.remove_extra_items(http_content
, scenario_action_schema
)
878 if r
is not None: print "http_post_scenario_action: Warning: remove extra items ", r
879 if "start" in http_content
:
880 data
= nfvo
.start_scenario(mydb
, tenant_id
, scenario_id
, http_content
['start']['instance_name'], \
881 http_content
['start'].get('description',http_content
['start']['instance_name']),
882 http_content
['start'].get('datacenter') )
883 return format_out(data
)
884 elif "deploy" in http_content
: #Equivalent to start
885 data
= nfvo
.start_scenario(mydb
, tenant_id
, scenario_id
, http_content
['deploy']['instance_name'],
886 http_content
['deploy'].get('description',http_content
['deploy']['instance_name']),
887 http_content
['deploy'].get('datacenter') )
888 return format_out(data
)
889 elif "reserve" in http_content
: #Reserve resources
890 data
= nfvo
.start_scenario(mydb
, tenant_id
, scenario_id
, http_content
['reserve']['instance_name'],
891 http_content
['reserve'].get('description',http_content
['reserve']['instance_name']),
892 http_content
['reserve'].get('datacenter'), startvms
=False )
893 return format_out(data
)
894 elif "verify" in http_content
: #Equivalent to start and then delete
895 data
= nfvo
.start_scenario(mydb
, tenant_id
, scenario_id
, http_content
['verify']['instance_name'],
896 http_content
['verify'].get('description',http_content
['verify']['instance_name']),
897 http_content
['verify'].get('datacenter'), startvms
=False )
898 instance_id
= data
['uuid']
899 nfvo
.delete_instance(mydb
, tenant_id
,instance_id
)
900 return format_out({"result":"Verify OK"})
901 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
902 logger
.error("http_post_scenario_action error {}: {}".format(e
.http_code
, str(e
)))
903 bottle
.abort(e
.http_code
, str(e
))
905 @bottle.route(url_base
+ '/<tenant_id>/scenarios', method
='GET')
906 def http_get_scenarios(tenant_id
):
907 '''get scenarios list'''
908 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
910 #check valid tenant_id
911 if tenant_id
!= "any":
912 nfvo
.check_tenant(mydb
, tenant_id
)
914 s
,w
,l
=filter_query_string(bottle
.request
.query
, None, ('uuid', 'name', 'description', 'tenant_id', 'created_at', 'public'))
916 if tenant_id
!= "any":
917 where_or
["tenant_id"] = tenant_id
918 where_or
["public"] = True
919 scenarios
= mydb
.get_rows(SELECT
=s
, WHERE
=w
, WHERE_OR
=where_or
, WHERE_AND_OR
="AND", LIMIT
=l
, FROM
='scenarios')
920 convert_datetime2str(scenarios
)
921 utils
.convert_str2boolean(scenarios
, ('public',) )
922 data
={'scenarios':scenarios
}
923 #print json.dumps(scenarios, indent=4)
924 return format_out(data
)
925 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
926 logger
.error("http_get_scenarios error {}: {}".format(e
.http_code
, str(e
)))
927 bottle
.abort(e
.http_code
, str(e
))
929 @bottle.route(url_base
+ '/<tenant_id>/scenarios/<scenario_id>', method
='GET')
930 def http_get_scenario_id(tenant_id
, scenario_id
):
931 '''get scenario details, can use both uuid or name'''
932 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
934 #check valid tenant_id
935 if tenant_id
!= "any":
936 nfvo
.check_tenant(mydb
, tenant_id
)
938 scenario
= mydb
.get_scenario(scenario_id
, tenant_id
)
939 convert_datetime2str(scenario
)
940 data
={'scenario' : scenario
}
941 return format_out(data
)
942 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
943 logger
.error("http_get_scenarios error {}: {}".format(e
.http_code
, str(e
)))
944 bottle
.abort(e
.http_code
, str(e
))
946 @bottle.route(url_base
+ '/<tenant_id>/scenarios/<scenario_id>', method
='DELETE')
947 def http_delete_scenario_id(tenant_id
, scenario_id
):
948 '''delete a scenario from database, can use both uuid or name'''
950 #check valid tenant_id
951 if tenant_id
!= "any":
952 nfvo
.check_tenant(mydb
, tenant_id
)
954 data
= mydb
.delete_scenario(scenario_id
, tenant_id
)
955 #print json.dumps(data, indent=4)
956 return format_out({"result":"scenario " + data
+ " deleted"})
957 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
958 logger
.error("http_delete_scenario_id error {}: {}".format(e
.http_code
, str(e
)))
959 bottle
.abort(e
.http_code
, str(e
))
962 @bottle.route(url_base
+ '/<tenant_id>/scenarios/<scenario_id>', method
='PUT')
963 def http_put_scenario_id(tenant_id
, scenario_id
):
964 '''edit an existing scenario id'''
965 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
966 http_content
,_
= format_in( scenario_edit_schema
)
967 #r = utils.remove_extra_items(http_content, scenario_edit_schema)
968 #if r is not None: print "http_put_scenario_id: Warning: remove extra items ", r
969 #print "http_put_scenario_id input: ", http_content
971 nfvo
.edit_scenario(mydb
, tenant_id
, scenario_id
, http_content
)
972 #print json.dumps(data, indent=4)
973 #return format_out(data)
974 return http_get_scenario_id(tenant_id
, scenario_id
)
975 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
976 logger
.error("http_put_scenario_id error {}: {}".format(e
.http_code
, str(e
)))
977 bottle
.abort(e
.http_code
, str(e
))
979 @bottle.route(url_base
+ '/<tenant_id>/instances', method
='POST')
980 def http_post_instances(tenant_id
):
981 '''take an action over a scenario'''
982 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
984 #check valid tenant_id
985 if tenant_id
!= "any":
986 nfvo
.check_tenant(mydb
, tenant_id
)
988 http_content
,used_schema
= format_in( instance_scenario_create_schema_v01
)
989 r
= utils
.remove_extra_items(http_content
, used_schema
)
991 logger
.warning("http_post_instances: Warning: remove extra items %s", str(r
))
992 data
= nfvo
.create_instance(mydb
, tenant_id
, http_content
["instance"])
993 return format_out(data
)
994 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
995 logger
.error("http_post_instances error {}: {}".format(e
.http_code
, str(e
)))
996 bottle
.abort(e
.http_code
, str(e
))
1001 @bottle.route(url_base
+ '/<tenant_id>/instances', method
='GET')
1002 def http_get_instances(tenant_id
):
1003 '''get instance list'''
1005 #check valid tenant_id
1006 if tenant_id
!= "any":
1007 nfvo
.check_tenant(mydb
, tenant_id
)
1009 s
,w
,l
=filter_query_string(bottle
.request
.query
, None, ('uuid', 'name', 'scenario_id', 'tenant_id', 'description', 'created_at'))
1010 if tenant_id
!= "any":
1011 w
['tenant_id'] = tenant_id
1012 instances
= mydb
.get_rows(SELECT
=s
, WHERE
=w
, LIMIT
=l
, FROM
='instance_scenarios')
1013 convert_datetime2str(instances
)
1014 utils
.convert_str2boolean(instances
, ('public',) )
1015 data
={'instances':instances
}
1016 return format_out(data
)
1017 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
1018 logger
.error("http_get_instances error {}: {}".format(e
.http_code
, str(e
)))
1019 bottle
.abort(e
.http_code
, str(e
))
1021 @bottle.route(url_base
+ '/<tenant_id>/instances/<instance_id>', method
='GET')
1022 def http_get_instance_id(tenant_id
, instance_id
):
1023 '''get instances details, can use both uuid or name'''
1024 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
1026 #check valid tenant_id
1027 if tenant_id
!= "any":
1028 nfvo
.check_tenant(mydb
, tenant_id
)
1029 if tenant_id
== "any":
1031 #obtain data (first time is only to check that the instance exists)
1032 instance_dict
= mydb
.get_instance_scenario(instance_id
, tenant_id
, verbose
=True)
1034 nfvo
.refresh_instance(mydb
, tenant_id
, instance_dict
)
1035 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
1036 logger
.warn("nfvo.refresh_instance couldn't refresh the status of the instance: %s" % str(e
))
1037 #obtain data with results upated
1038 instance
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
1039 convert_datetime2str(instance
)
1040 #print json.dumps(instance, indent=4)
1041 return format_out(instance
)
1042 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
1043 logger
.error("http_get_instance_id error {}: {}".format(e
.http_code
, str(e
)))
1044 bottle
.abort(e
.http_code
, str(e
))
1046 @bottle.route(url_base
+ '/<tenant_id>/instances/<instance_id>', method
='DELETE')
1047 def http_delete_instance_id(tenant_id
, instance_id
):
1048 '''delete instance from VIM and from database, can use both uuid or name'''
1049 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
1051 #check valid tenant_id
1052 if tenant_id
!= "any":
1053 nfvo
.check_tenant(mydb
, tenant_id
)
1054 if tenant_id
== "any":
1057 message
= nfvo
.delete_instance(mydb
, tenant_id
,instance_id
)
1058 return format_out({"result":message
})
1059 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
1060 logger
.error("http_delete_instance_id error {}: {}".format(e
.http_code
, str(e
)))
1061 bottle
.abort(e
.http_code
, str(e
))
1063 @bottle.route(url_base
+ '/<tenant_id>/instances/<instance_id>/action', method
='POST')
1064 def http_post_instance_scenario_action(tenant_id
, instance_id
):
1065 '''take an action over a scenario instance'''
1066 logger
.debug('FROM %s %s %s', bottle
.request
.remote_addr
, bottle
.request
.method
, bottle
.request
.url
)
1068 #check valid tenant_id
1069 if tenant_id
!= "any":
1070 nfvo
.check_tenant(mydb
, tenant_id
)
1073 http_content
,_
= format_in( instance_scenario_action_schema
)
1074 r
= utils
.remove_extra_items(http_content
, instance_scenario_action_schema
)
1075 if r
is not None: print "http_post_instance_scenario_action: Warning: remove extra items ", r
1076 #print "http_post_instance_scenario_action input: ", http_content
1078 instance
= mydb
.get_instance_scenario(instance_id
, tenant_id
)
1079 instance_id
= instance
["uuid"]
1081 data
= nfvo
.instance_action(mydb
, tenant_id
, instance_id
, http_content
)
1082 return format_out(data
)
1083 except (nfvo
.NfvoException
, db_base_Exception
) as e
:
1084 logger
.error("http_post_instance_scenario_action error {}: {}".format(e
.http_code
, str(e
)))
1085 bottle
.abort(e
.http_code
, str(e
))
1097 def error400(error
):
1098 e
={"error":{"code":error
.status_code
, "type":error
.status
, "description":error
.body
}}
1099 bottle
.response
.headers
['Access-Control-Allow-Origin'] = '*'
1100 return format_out(e
)