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
'''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:
'''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
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']
'''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:
'''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
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
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:
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
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)
'''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
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)
'''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
'''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
'''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'),
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)
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)
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:
'''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
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:
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'], \
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)
'''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
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:
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)
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
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
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
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
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:
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)
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)
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)
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
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)
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)
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)
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
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)
import MySQLdb as mdb
import uuid as myUuid
-from utils import auxiliary_functions as af
+import utils
import json
import yaml
import time
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() ))
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']
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)
# #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 +"'" )
# 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:
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
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])
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
#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 +"'" )
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])
#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 +"'" )
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
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
--- /dev/null
+# -*- 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,<its content as a string>) in case of success or (False, <error message>) 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, <error message>) 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
+
+++ /dev/null
-# -*- 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,<its content as a string>) in case of success or (False, <error message>) 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, <error message>) 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
-