From: tierno Date: Wed, 6 Jul 2016 15:20:40 +0000 (+0200) Subject: Moved auxiliary_functions.py from utils package to the main package X-Git-Tag: v1.0.0~77 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=42fcc3be3eab33e0d934b8774a33629f07d47674;p=osm%2FRO.git Moved auxiliary_functions.py from utils package to the main package Signed-off-by: tierno --- diff --git a/httpserver.py b/httpserver.py index 0e60b3d2..2fa4200d 100644 --- a/httpserver.py +++ b/httpserver.py @@ -43,7 +43,7 @@ from openmano_schemas import vnfd_schema_v01, vnfd_schema_v02, \ datacenter_schema, datacenter_edit_schema, datacenter_action_schema, datacenter_associate_schema,\ object_schema, netmap_new_schema, netmap_edit_schema import nfvo -from utils import auxiliary_functions as af +import utils global mydb global url_base @@ -316,7 +316,7 @@ def http_post_tenants(): '''insert a tenant into the catalogue. ''' #parse input data http_content,_ = format_in( tenant_schema ) - r = af.remove_extra_items(http_content, tenant_schema) + r = utils.remove_extra_items(http_content, tenant_schema) if r is not None: print "http_post_tenants: Warning: remove extra items ", r result, data = nfvo.new_tenant(mydb, http_content['tenant']) if result < 0: @@ -330,7 +330,7 @@ def http_edit_tenant_id(tenant_id): '''edit tenant details, can use both uuid or name''' #parse input data http_content,_ = format_in( tenant_edit_schema ) - r = af.remove_extra_items(http_content, tenant_edit_schema) + r = utils.remove_extra_items(http_content, tenant_edit_schema) if r is not None: print "http_edit_tenant_id: Warning: remove extra items ", r #obtain data, check that only one exist @@ -402,7 +402,7 @@ def http_get_datacenter_id(tenant_id, datacenter_id): bottle.abort(HTTP_Not_Found, 'Tenant %s not found' % tenant_id) return #obtain data - what = 'uuid' if af.check_valid_uuid(datacenter_id) else 'name' + what = 'uuid' if utils.check_valid_uuid(datacenter_id) else 'name' where_={} where_[what] = datacenter_id select_=['uuid', 'name','vim_url', 'vim_url_admin', 'type', 'config', 'description', 'd.created_at as created_at'] @@ -455,7 +455,7 @@ def http_post_datacenters(): '''insert a tenant into the catalogue. ''' #parse input data http_content,_ = format_in( datacenter_schema ) - r = af.remove_extra_items(http_content, datacenter_schema) + r = utils.remove_extra_items(http_content, datacenter_schema) if r is not None: print "http_post_tenants: Warning: remove extra items ", r result, data = nfvo.new_datacenter(mydb, http_content['datacenter']) if result < 0: @@ -469,7 +469,7 @@ def http_edit_datacenter_id(datacenter_id_name): '''edit datacenter details, can use both uuid or name''' #parse input data http_content,_ = format_in( datacenter_edit_schema ) - r = af.remove_extra_items(http_content, datacenter_edit_schema) + r = utils.remove_extra_items(http_content, datacenter_edit_schema) if r is not None: print "http_edit_datacenter_id: Warning: remove extra items ", r @@ -492,7 +492,7 @@ def http_getnetmap_datacenter_id(tenant_id, datacenter_id, netmap_id=None): bottle.abort(-result, datacenter_dict) where_= {"datacenter_id":datacenter_dict['uuid']} if netmap_id: - if af.check_valid_uuid(netmap_id): + if utils.check_valid_uuid(netmap_id): where_["uuid"] = netmap_id else: where_["name"] = netmap_id @@ -504,7 +504,7 @@ def http_getnetmap_datacenter_id(tenant_id, datacenter_id, netmap_id=None): bottle.abort(-result, content) convert_datetime2str(content) - af.convert_str2boolean(content, ('shared', 'multipoint') ) + utils.convert_str2boolean(content, ('shared', 'multipoint') ) if netmap_id and len(content)==1: data={'netmap' : content[0]} elif netmap_id and len(content)==0: @@ -525,7 +525,7 @@ def http_delnetmap_datacenter_id(tenant_id, datacenter_id, netmap_id=None): bottle.abort(-result, datacenter_dict) where_= {"datacenter_id":datacenter_dict['uuid']} if netmap_id: - if af.check_valid_uuid(netmap_id): + if utils.check_valid_uuid(netmap_id): where_["uuid"] = netmap_id else: where_["name"] = netmap_id @@ -549,7 +549,7 @@ def http_uploadnetmap_datacenter_id(tenant_id, datacenter_id): print "http_postnetmap_datacenter_id error %d %s" % (result, content) bottle.abort(-result, content) convert_datetime2str(content) - af.convert_str2boolean(content, ('shared', 'multipoint') ) + utils.convert_str2boolean(content, ('shared', 'multipoint') ) print content data={'netmaps' : content} return format_out(data) @@ -559,7 +559,7 @@ def http_postnetmap_datacenter_id(tenant_id, datacenter_id): '''creates a new netmap''' #parse input data http_content,_ = format_in( netmap_new_schema ) - r = af.remove_extra_items(http_content, netmap_new_schema) + r = utils.remove_extra_items(http_content, netmap_new_schema) if r is not None: print "http_action_datacenter_id: Warning: remove extra items ", r #obtain data, check that only one exist @@ -568,7 +568,7 @@ def http_postnetmap_datacenter_id(tenant_id, datacenter_id): print "http_postnetmap_datacenter_id error %d %s" % (result, content) bottle.abort(-result, content) convert_datetime2str(content) - af.convert_str2boolean(content, ('shared', 'multipoint') ) + utils.convert_str2boolean(content, ('shared', 'multipoint') ) print content data={'netmaps' : content} return format_out(data) @@ -578,7 +578,7 @@ def http_putnettmap_datacenter_id(tenant_id, datacenter_id, netmap_id): '''edit a netmap''' #parse input data http_content,_ = format_in( netmap_edit_schema ) - r = af.remove_extra_items(http_content, netmap_edit_schema) + r = utils.remove_extra_items(http_content, netmap_edit_schema) if r is not None: print "http_putnettmap_datacenter_id: Warning: remove extra items ", r #obtain data, check that only one exist @@ -595,7 +595,7 @@ def http_action_datacenter_id(tenant_id, datacenter_id): '''perform an action over datacenter, can use both uuid or name''' #parse input data http_content,_ = format_in( datacenter_action_schema ) - r = af.remove_extra_items(http_content, datacenter_action_schema) + r = utils.remove_extra_items(http_content, datacenter_action_schema) if r is not None: print "http_action_datacenter_id: Warning: remove extra items ", r #obtain data, check that only one exist @@ -626,7 +626,7 @@ def http_associate_datacenters(tenant_id, datacenter_id): '''associate an existing datacenter to a this tenant. ''' #parse input data http_content,_ = format_in( datacenter_associate_schema ) - r = af.remove_extra_items(http_content, datacenter_associate_schema) + r = utils.remove_extra_items(http_content, datacenter_associate_schema) if r != None: print "http_associate_datacenters: Warning: remove extra items ", r result, data = nfvo.associate_datacenter_to_tenant(mydb, tenant_id, datacenter_id, http_content['datacenter'].get('vim_tenant'), @@ -701,7 +701,7 @@ def http_get_vnfs(tenant_id): bottle.abort(-result, content) else: #change_keys_http2db(content, http2db_vnf, reverse=True) - af.convert_str2boolean(content, ('public',)) + utils.convert_str2boolean(content, ('public',)) convert_datetime2str(content) data={'vnfs' : content} return format_out(data) @@ -714,7 +714,7 @@ def http_get_vnf_id(tenant_id,vnf_id): print "http_post_vnfs error %d %s" % (-result, data) bottle.abort(-result, data) - af.convert_str2boolean(data, ('public',)) + utils.convert_str2boolean(data, ('public',)) convert_datetime2str(data) return format_out(data) @@ -724,7 +724,7 @@ def http_post_vnfs(tenant_id): print "Parsing the YAML file of the VNF" #parse input data http_content, used_schema = format_in( vnfd_schema_v01, ("version",), {"v0.2": vnfd_schema_v02}) - r = af.remove_extra_items(http_content, used_schema) + r = utils.remove_extra_items(http_content, used_schema) if r is not None: print "http_post_vnfs: Warning: remove extra items ", r result, data = nfvo.new_vnf(mydb,tenant_id,http_content) if result < 0: @@ -783,8 +783,8 @@ def http_post_deploy(tenant_id): '''post topology deploy.''' print "http_post_deploy by tenant " + tenant_id - http_content, used_schema = format_in( nsd_schema_v01, ("schema_version",), {2: nsd_schema_v02}) - #r = af.remove_extra_items(http_content, used_schema) + http_content, used_schema = format_in( nsd_schema_v01, ("version",), {"v0.2": nsd_schema_v02}) + #r = utils.remove_extra_items(http_content, used_schema) #if r is not None: print "http_post_deploy: Warning: remove extra items ", r print "http_post_deploy input: ", http_content @@ -816,8 +816,8 @@ def http_post_verify(tenant_id): def http_post_scenarios(tenant_id): '''add a scenario into the catalogue. Creates the scenario and its internal structure in the OPENMANO DB''' print "http_post_scenarios by tenant " + tenant_id - http_content, used_schema = format_in( nsd_schema_v01, ("schema_version",), {2: nsd_schema_v02}) - #r = af.remove_extra_items(http_content, used_schema) + http_content, used_schema = format_in( nsd_schema_v01, ("schema_version",), {"0.2": nsd_schema_v02}) + #r = utils.remove_extra_items(http_content, used_schema) #if r is not None: print "http_post_scenarios: Warning: remove extra items ", r print "http_post_scenarios input: ", http_content if http_content.get("schema_version") == None: @@ -842,7 +842,7 @@ def http_post_scenario_action(tenant_id, scenario_id): return #parse input data http_content,_ = format_in( scenario_action_schema ) - r = af.remove_extra_items(http_content, scenario_action_schema) + r = utils.remove_extra_items(http_content, scenario_action_schema) if r is not None: print "http_post_scenario_action: Warning: remove extra items ", r if "start" in http_content: result, data = nfvo.start_scenario(mydb, tenant_id, scenario_id, http_content['start']['instance_name'], \ @@ -907,7 +907,7 @@ def http_get_scenarios(tenant_id): bottle.abort(-result, data) else: convert_datetime2str(data) - af.convert_str2boolean(data, ('public',) ) + utils.convert_str2boolean(data, ('public',) ) scenarios={'scenarios':data} #print json.dumps(scenarios, indent=4) return format_out(scenarios) @@ -954,7 +954,7 @@ def http_put_scenario_id(tenant_id, scenario_id): '''edit an existing scenario id''' print "http_put_scenarios by tenant " + tenant_id http_content,_ = format_in( scenario_edit_schema ) - #r = af.remove_extra_items(http_content, scenario_edit_schema) + #r = utils.remove_extra_items(http_content, scenario_edit_schema) #if r is not None: print "http_put_scenario_id: Warning: remove extra items ", r print "http_put_scenario_id input: ", http_content @@ -977,7 +977,7 @@ def http_post_instances(tenant_id): return #parse input data http_content,used_schema = format_in( instance_scenario_create_schema) - r = af.remove_extra_items(http_content, used_schema) + r = utils.remove_extra_items(http_content, used_schema) if r is not None: print "http_post_instances: Warning: remove extra items ", r result, data = nfvo.create_instance(mydb, tenant_id, http_content["instance"]) if result < 0: @@ -1008,7 +1008,7 @@ def http_get_instances(tenant_id): bottle.abort(-result, data) else: convert_datetime2str(data) - af.convert_str2boolean(data, ('public',) ) + utils.convert_str2boolean(data, ('public',) ) instances={'instances':data} print json.dumps(instances, indent=4) return format_out(instances) @@ -1073,7 +1073,7 @@ def http_post_instance_scenario_action(tenant_id, instance_id): return #parse input data http_content,_ = format_in( instance_scenario_action_schema ) - r = af.remove_extra_items(http_content, instance_scenario_action_schema) + r = utils.remove_extra_items(http_content, instance_scenario_action_schema) if r is not None: print "http_post_instance_scenario_action: Warning: remove extra items ", r print "http_post_instance_scenario_action input: ", http_content #obtain data diff --git a/nfvo.py b/nfvo.py index 1a97fa2d..c7932262 100644 --- a/nfvo.py +++ b/nfvo.py @@ -30,7 +30,7 @@ __date__ ="$16-sep-2014 22:05:01$" import imp #import json import yaml -from utils import auxiliary_functions as af +import utils from nfvo_db import HTTP_Unauthorized, HTTP_Bad_Request, HTTP_Internal_Server_Error, HTTP_Not_Found,\ HTTP_Conflict, HTTP_Method_Not_Allowed import console_proxy_thread as cli @@ -1201,7 +1201,7 @@ def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instanc datacenter_id = None datacenter_name=None if datacenter != None: - if af.check_valid_uuid(datacenter): + if utils.check_valid_uuid(datacenter): datacenter_id = datacenter else: datacenter_name = datacenter @@ -1446,7 +1446,7 @@ def create_instance(mydb, tenant_id, instance_dict): datacenter_name=None datacenter = instance_dict.get("datacenter") if datacenter: - if af.check_valid_uuid(datacenter): + if utils.check_valid_uuid(datacenter): datacenter_id = datacenter else: datacenter_name = datacenter @@ -1517,7 +1517,7 @@ def create_instance(mydb, tenant_id, instance_dict): lookfor_network = False if "netmap-use" in descriptor_net: lookfor_network = True - if af.check_valid_uuid(descriptor_net["netmap-use"]): + if utils.check_valid_uuid(descriptor_net["netmap-use"]): filter_text = "scenario id '%s'" % descriptor_net["netmap-use"] lookfor_filter["id"] = descriptor_net["netmap-use"] else: @@ -2051,7 +2051,7 @@ def delete_datacenter(mydb, datacenter): def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None): #get datacenter info - if af.check_valid_uuid(datacenter): + if utils.check_valid_uuid(datacenter): result, vims = get_vim(mydb, datacenter_id=datacenter) else: result, vims = get_vim(mydb, datacenter_name=datacenter) @@ -2132,7 +2132,7 @@ def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id= def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None): #get datacenter info - if af.check_valid_uuid(datacenter): + if utils.check_valid_uuid(datacenter): result, vims = get_vim(mydb, datacenter_id=datacenter) else: result, vims = get_vim(mydb, datacenter_name=datacenter) @@ -2192,7 +2192,7 @@ def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id= def datacenter_action(mydb, tenant_id, datacenter, action_dict): #DEPRECATED #get datacenter info - if af.check_valid_uuid(datacenter): + if utils.check_valid_uuid(datacenter): result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_id=datacenter) else: result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_name=datacenter) @@ -2232,13 +2232,13 @@ def datacenter_action(mydb, tenant_id, datacenter, action_dict): return 200, result elif 'net-edit' in action_dict: net = action_dict['net-edit'].pop('net') - what = 'vim_net_id' if af.check_valid_uuid(net) else 'name' + what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' result, content = mydb.update_rows('datacenter_nets', action_dict['net-edit'], WHERE={'datacenter_id':datacenter_id, what: net}) return result, content elif 'net-delete' in action_dict: net = action_dict['net-deelte'].get('net') - what = 'vim_net_id' if af.check_valid_uuid(net) else 'name' + what = 'vim_net_id' if utils.check_valid_uuid(net) else 'name' result, content = mydb.delete_row_by_dict(FROM='datacenter_nets', WHERE={'datacenter_id':datacenter_id, what: net}) return result, content @@ -2248,7 +2248,7 @@ def datacenter_action(mydb, tenant_id, datacenter, action_dict): def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict): #get datacenter info - if af.check_valid_uuid(datacenter): + if utils.check_valid_uuid(datacenter): result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_id=datacenter) else: result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_name=datacenter) @@ -2262,14 +2262,14 @@ def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict): return -HTTP_Conflict, "More than one datacenters found, try to identify with uuid" datacenter_id=vims.keys()[0] - what = 'uuid' if af.check_valid_uuid(netmap) else 'name' + what = 'uuid' if utils.check_valid_uuid(netmap) else 'name' result, content = mydb.update_rows('datacenter_nets', action_dict['netmap'], WHERE={'datacenter_id':datacenter_id, what: netmap}) return result, content def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None): #get datacenter info - if af.check_valid_uuid(datacenter): + if utils.check_valid_uuid(datacenter): result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_id=datacenter) else: result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_name=datacenter) @@ -2327,7 +2327,7 @@ def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None): def vim_action_get(mydb, tenant_id, datacenter, item, name): #get datacenter info - if af.check_valid_uuid(datacenter): + if utils.check_valid_uuid(datacenter): result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_id=datacenter) else: result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_name=datacenter) @@ -2343,7 +2343,7 @@ def vim_action_get(mydb, tenant_id, datacenter, item, name): myvim=vims[datacenter_id] filter_dict={} if name: - if af.check_valid_uuid(name): + if utils.check_valid_uuid(name): filter_dict["id"] = name else: filter_dict["name"] = name @@ -2415,7 +2415,7 @@ def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): if tenant_id == "any": tenant_id=None - if af.check_valid_uuid(datacenter): + if utils.check_valid_uuid(datacenter): result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_id=datacenter) else: result, vims = get_vim(mydb, nfvo_tenant=tenant_id, datacenter_name=datacenter) diff --git a/nfvo_db.py b/nfvo_db.py index 1bf3e4bc..28317ec3 100644 --- a/nfvo_db.py +++ b/nfvo_db.py @@ -29,7 +29,7 @@ __date__ ="$28-aug-2014 10:05:01$" import MySQLdb as mdb import uuid as myUuid -from utils import auxiliary_functions as af +import utils import json import yaml import time @@ -515,7 +515,7 @@ class nfvo_db(): if error_item_text==None: error_item_text = table - what = 'uuid' if af.check_valid_uuid(uuid_name) else 'name' + what = 'uuid' if utils.check_valid_uuid(uuid_name) else 'name' cmd = " SELECT * FROM %s WHERE %s='%s'" % (table, what, uuid_name) if WHERE_OR: where_or = " OR ".join(map(self.__tuple2db_format_where, WHERE_OR.iteritems() )) @@ -604,7 +604,7 @@ class nfvo_db(): dataifacesDict[vm['name']] = {} for numa in vm.get('numas', []): for dataiface in numa.get('interfaces',[]): - af.convert_bandwidth(dataiface) + utils.convert_bandwidth(dataiface) dataifacesDict[vm['name']][dataiface['name']] = {} dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface['vpci'] dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth'] @@ -616,7 +616,7 @@ class nfvo_db(): if 'bridge-ifaces' in vm: bridgeInterfacesDict[vm['name']] = {} for bridgeiface in vm['bridge-ifaces']: - af.convert_bandwidth(bridgeiface) + utils.convert_bandwidth(bridgeiface) bridgeInterfacesDict[vm['name']][bridgeiface['name']] = {} bridgeInterfacesDict[vm['name']][bridgeiface['name']]['vpci'] = bridgeiface.get('vpci',None) bridgeInterfacesDict[vm['name']][bridgeiface['name']]['mac'] = bridgeiface.get('mac_address',None) @@ -862,7 +862,7 @@ class nfvo_db(): # #scenario table # where_list=[] # if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" ) -# if af.check_valid_uuid(instance_scenario_id): +# if utils.check_valid_uuid(instance_scenario_id): # where_list.append( "uuid='" + instance_scenario_id +"'" ) # else: # where_list.append( "name='" + instance_scenario_id +"'" ) @@ -893,8 +893,8 @@ class nfvo_db(): # self.cur.execute("SELECT uuid, vim_interface_id, instance_vm_id, instance_net_id FROM instance_interfaces WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'") # instance_scenario_dict['instance_interfaces'] = self.cur.fetchall() # -# af.convert_datetime2str(instance_scenario_dict) -# af.convert_str2boolean(instance_scenario_dict, ('public','shared','external') ) +# utils.convert_datetime2str(instance_scenario_dict) +# utils.convert_str2boolean(instance_scenario_dict, ('public','shared','external') ) # print "2******************************************************************" # return 1, instance_scenario_dict # except (mdb.Error, AttributeError), e: @@ -912,7 +912,7 @@ class nfvo_db(): with self.con: self.cur = self.con.cursor(mdb.cursors.DictCursor) #scenario table - if af.check_valid_uuid(scenario_id): + if utils.check_valid_uuid(scenario_id): where_text = "uuid='%s'" % scenario_id else: where_text = "name='%s'" % scenario_id @@ -986,8 +986,8 @@ class nfvo_db(): else: net['vim_id']=d_net['vim_net_id'] - af.convert_datetime2str(scenario_dict) - af.convert_str2boolean(scenario_dict, ('public','shared','external') ) + utils.convert_datetime2str(scenario_dict) + utils.convert_str2boolean(scenario_dict, ('public','shared','external') ) return 1, scenario_dict except (mdb.Error, AttributeError), e: print "nfvo_db.get_scenario DB Exception %d: %s" % (e.args[0], e.args[1]) @@ -1025,7 +1025,7 @@ class nfvo_db(): self.cur = self.con.cursor(mdb.cursors.DictCursor) #scenario table - if af.check_valid_uuid(scenario_id): + if utils.check_valid_uuid(scenario_id): where_text = "uuid='%s'" % scenario_id else: where_text = "name='%s'" % scenario_id @@ -1168,7 +1168,7 @@ class nfvo_db(): #instance table where_list=[] if tenant_id is not None: where_list.append( "inst.tenant_id='" + tenant_id +"'" ) - if af.check_valid_uuid(instance_id): + if utils.check_valid_uuid(instance_id): where_list.append( "inst.uuid='" + instance_id +"'" ) else: where_list.append( "inst.name='" + instance_id +"'" ) @@ -1229,8 +1229,8 @@ class nfvo_db(): self.cur.execute(cmd) instance_dict['nets'] = self.cur.fetchall() - af.convert_datetime2str(instance_dict) - af.convert_str2boolean(instance_dict, ('public','shared','external') ) + utils.convert_datetime2str(instance_dict) + utils.convert_str2boolean(instance_dict, ('public','shared','external') ) return 1, instance_dict except (mdb.Error, AttributeError), e: print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1]) @@ -1250,7 +1250,7 @@ class nfvo_db(): #instance table where_list=[] if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" ) - if af.check_valid_uuid(instance_id): + if utils.check_valid_uuid(instance_id): where_list.append( "uuid='" + instance_id +"'" ) else: where_list.append( "name='" + instance_id +"'" ) diff --git a/openmanod.py b/openmanod.py index 522a6576..0df8202d 100755 --- a/openmanod.py +++ b/openmanod.py @@ -45,7 +45,7 @@ import getopt import yaml import nfvo_db from jsonschema import validate as js_v, exceptions as js_e -from utils import auxiliary_functions as af +import utils from openmano_schemas import config_schema import nfvo @@ -59,7 +59,7 @@ def load_configuration(configuration_file): return (False, "Error: Configuration file '"+configuration_file+"' does not exists.") #Read file - (return_status, code) = af.read_file(configuration_file) + (return_status, code) = utils.read_file(configuration_file) if not return_status: return (return_status, "Error loading configuration file '"+configuration_file+"': "+code) #Parse configuration file diff --git a/utils.py b/utils.py new file mode 100755 index 00000000..81a01588 --- /dev/null +++ b/utils.py @@ -0,0 +1,175 @@ +# -*- coding: utf-8 -*- + +## +# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. +# This file is part of openmano +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact with: nfvlabs@tid.es +## + +''' +auxiliary_functions is a module that implements functions that are used by all openmano modules, +dealing with aspects such as reading/writing files, formatting inputs/outputs for quick translation +from dictionaries to appropriate database dictionaries, etc. +''' +__author__="Alfonso Tierno, Gerardo Garcia" +__date__ ="$08-sep-2014 12:21:22$" + +import datetime +from jsonschema import validate as js_v, exceptions as js_e +#from bs4 import BeautifulSoup + +def read_file(file_to_read): + """Reads a file specified by 'file_to_read' and returns (True,) in case of success or (False, ) in case of failure""" + try: + f = open(file_to_read, 'r') + read_data = f.read() + f.close() + except Exception,e: + return (False, str(e)) + + return (True, read_data) + +def write_file(file_to_write, text): + """Write a file specified by 'file_to_write' and returns (True,NOne) in case of success or (False, ) in case of failure""" + try: + f = open(file_to_write, 'w') + f.write(text) + f.close() + except Exception,e: + return (False, str(e)) + + return (True, None) + +def format_in(http_response, schema): + try: + client_data = http_response.json() + js_v(client_data, schema) + #print "Input data: ", str(client_data) + return True, client_data + except js_e.ValidationError, exc: + print "validate_in error, jsonschema exception ", exc.message, "at", exc.path + return False, ("validate_in error, jsonschema exception ", exc.message, "at", exc.path) + +def remove_extra_items(data, schema): + deleted=[] + if type(data) is tuple or type(data) is list: + for d in data: + a= remove_extra_items(d, schema['items']) + if a is not None: deleted.append(a) + elif type(data) is dict: + #TODO deal with patternProperties + if 'properties' not in schema: + return None + for k in data.keys(): + if k not in schema['properties'].keys(): + del data[k] + deleted.append(k) + else: + a = remove_extra_items(data[k], schema['properties'][k]) + if a is not None: deleted.append({k:a}) + if len(deleted) == 0: return None + elif len(deleted) == 1: return deleted[0] + else: return deleted + +#def format_html2text(http_content): +# soup=BeautifulSoup(http_content) +# text = soup.p.get_text() + " " + soup.pre.get_text() +# return text + + +def convert_bandwidth(data, reverse=False): + '''Check the field bandwidth recursivelly and when found, it removes units and convert to number + It assumes that bandwidth is well formed + Attributes: + 'data': dictionary bottle.FormsDict variable to be checked. None or empty is consideted valid + 'reverse': by default convert form str to int (Mbps), if True it convert from number to units + Return: + None + ''' + if type(data) is dict: + for k in data.keys(): + if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list: + convert_bandwidth(data[k], reverse) + if "bandwidth" in data: + try: + value=str(data["bandwidth"]) + if not reverse: + pos = value.find("bps") + if pos>0: + if value[pos-1]=="G": data["bandwidth"] = int(data["bandwidth"][:pos-1]) * 1000 + elif value[pos-1]=="k": data["bandwidth"]= int(data["bandwidth"][:pos-1]) / 1000 + else: data["bandwidth"]= int(data["bandwidth"][:pos-1]) + else: + value = int(data["bandwidth"]) + if value % 1000 == 0: data["bandwidth"]=str(value/1000) + " Gbps" + else: data["bandwidth"]=str(value) + " Mbps" + except: + print "convert_bandwidth exception for type", type(data["bandwidth"]), " data", data["bandwidth"] + return + if type(data) is tuple or type(data) is list: + for k in data: + if type(k) is dict or type(k) is tuple or type(k) is list: + convert_bandwidth(k, reverse) + + + +def convert_datetime2str(var): + '''Converts a datetime variable to a string with the format '%Y-%m-%dT%H:%i:%s' + It enters recursively in the dict var finding this kind of variables + ''' + if type(var) is dict: + for k,v in var.items(): + if type(v) is datetime.datetime: + var[k]= v.strftime('%Y-%m-%dT%H:%M:%S') + elif type(v) is dict or type(v) is list or type(v) is tuple: + convert_datetime2str(v) + if len(var) == 0: return True + elif type(var) is list or type(var) is tuple: + for v in var: + convert_datetime2str(v) + +def convert_str2boolean(data, items): + '''Check recursively the content of data, and if there is an key contained in items, convert value from string to boolean + Done recursively + Attributes: + 'data': dictionary variable to be checked. None or empty is considered valid + 'items': tuple of keys to convert + Return: + None + ''' + if type(data) is dict: + for k in data.keys(): + if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list: + convert_str2boolean(data[k], items) + if k in items: + if type(data[k]) is str: + if data[k]=="false" or data[k]=="False": data[k]=False + elif data[k]=="true" or data[k]=="True": data[k]=True + if type(data) is tuple or type(data) is list: + for k in data: + if type(k) is dict or type(k) is tuple or type(k) is list: + convert_str2boolean(k, items) + +def check_valid_uuid(uuid): + id_schema = {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"} + try: + js_v(uuid, id_schema) + return True + except js_e.ValidationError: + return False + diff --git a/utils/auxiliary_functions.py b/utils/auxiliary_functions.py deleted file mode 100755 index 81a01588..00000000 --- a/utils/auxiliary_functions.py +++ /dev/null @@ -1,175 +0,0 @@ -# -*- coding: utf-8 -*- - -## -# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. -# This file is part of openmano -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -# -# For those usages not covered by the Apache License, Version 2.0 please -# contact with: nfvlabs@tid.es -## - -''' -auxiliary_functions is a module that implements functions that are used by all openmano modules, -dealing with aspects such as reading/writing files, formatting inputs/outputs for quick translation -from dictionaries to appropriate database dictionaries, etc. -''' -__author__="Alfonso Tierno, Gerardo Garcia" -__date__ ="$08-sep-2014 12:21:22$" - -import datetime -from jsonschema import validate as js_v, exceptions as js_e -#from bs4 import BeautifulSoup - -def read_file(file_to_read): - """Reads a file specified by 'file_to_read' and returns (True,) in case of success or (False, ) in case of failure""" - try: - f = open(file_to_read, 'r') - read_data = f.read() - f.close() - except Exception,e: - return (False, str(e)) - - return (True, read_data) - -def write_file(file_to_write, text): - """Write a file specified by 'file_to_write' and returns (True,NOne) in case of success or (False, ) in case of failure""" - try: - f = open(file_to_write, 'w') - f.write(text) - f.close() - except Exception,e: - return (False, str(e)) - - return (True, None) - -def format_in(http_response, schema): - try: - client_data = http_response.json() - js_v(client_data, schema) - #print "Input data: ", str(client_data) - return True, client_data - except js_e.ValidationError, exc: - print "validate_in error, jsonschema exception ", exc.message, "at", exc.path - return False, ("validate_in error, jsonschema exception ", exc.message, "at", exc.path) - -def remove_extra_items(data, schema): - deleted=[] - if type(data) is tuple or type(data) is list: - for d in data: - a= remove_extra_items(d, schema['items']) - if a is not None: deleted.append(a) - elif type(data) is dict: - #TODO deal with patternProperties - if 'properties' not in schema: - return None - for k in data.keys(): - if k not in schema['properties'].keys(): - del data[k] - deleted.append(k) - else: - a = remove_extra_items(data[k], schema['properties'][k]) - if a is not None: deleted.append({k:a}) - if len(deleted) == 0: return None - elif len(deleted) == 1: return deleted[0] - else: return deleted - -#def format_html2text(http_content): -# soup=BeautifulSoup(http_content) -# text = soup.p.get_text() + " " + soup.pre.get_text() -# return text - - -def convert_bandwidth(data, reverse=False): - '''Check the field bandwidth recursivelly and when found, it removes units and convert to number - It assumes that bandwidth is well formed - Attributes: - 'data': dictionary bottle.FormsDict variable to be checked. None or empty is consideted valid - 'reverse': by default convert form str to int (Mbps), if True it convert from number to units - Return: - None - ''' - if type(data) is dict: - for k in data.keys(): - if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list: - convert_bandwidth(data[k], reverse) - if "bandwidth" in data: - try: - value=str(data["bandwidth"]) - if not reverse: - pos = value.find("bps") - if pos>0: - if value[pos-1]=="G": data["bandwidth"] = int(data["bandwidth"][:pos-1]) * 1000 - elif value[pos-1]=="k": data["bandwidth"]= int(data["bandwidth"][:pos-1]) / 1000 - else: data["bandwidth"]= int(data["bandwidth"][:pos-1]) - else: - value = int(data["bandwidth"]) - if value % 1000 == 0: data["bandwidth"]=str(value/1000) + " Gbps" - else: data["bandwidth"]=str(value) + " Mbps" - except: - print "convert_bandwidth exception for type", type(data["bandwidth"]), " data", data["bandwidth"] - return - if type(data) is tuple or type(data) is list: - for k in data: - if type(k) is dict or type(k) is tuple or type(k) is list: - convert_bandwidth(k, reverse) - - - -def convert_datetime2str(var): - '''Converts a datetime variable to a string with the format '%Y-%m-%dT%H:%i:%s' - It enters recursively in the dict var finding this kind of variables - ''' - if type(var) is dict: - for k,v in var.items(): - if type(v) is datetime.datetime: - var[k]= v.strftime('%Y-%m-%dT%H:%M:%S') - elif type(v) is dict or type(v) is list or type(v) is tuple: - convert_datetime2str(v) - if len(var) == 0: return True - elif type(var) is list or type(var) is tuple: - for v in var: - convert_datetime2str(v) - -def convert_str2boolean(data, items): - '''Check recursively the content of data, and if there is an key contained in items, convert value from string to boolean - Done recursively - Attributes: - 'data': dictionary variable to be checked. None or empty is considered valid - 'items': tuple of keys to convert - Return: - None - ''' - if type(data) is dict: - for k in data.keys(): - if type(data[k]) is dict or type(data[k]) is tuple or type(data[k]) is list: - convert_str2boolean(data[k], items) - if k in items: - if type(data[k]) is str: - if data[k]=="false" or data[k]=="False": data[k]=False - elif data[k]=="true" or data[k]=="True": data[k]=True - if type(data) is tuple or type(data) is list: - for k in data: - if type(k) is dict or type(k) is tuple or type(k) is list: - convert_str2boolean(k, items) - -def check_valid_uuid(uuid): - id_schema = {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"} - try: - js_v(uuid, id_schema) - return True - except js_e.ValidationError: - return False -