v0.4.38 new openmanoclient.py library; new version2 for scenario descriptor
authortierno <alfonso.tiernosepulveda@telefonica.com>
Tue, 17 May 2016 12:50:46 +0000 (14:50 +0200)
committertierno <alfonso.tiernosepulveda@telefonica.com>
Tue, 17 May 2016 12:50:46 +0000 (14:50 +0200)
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
1  2 
httpserver.py
nfvo.py
nfvo_db.py
utils.py

diff --cc httpserver.py
index 0e60b3d,2fa4200..7e4a82c
mode 100644,100755..100644
@@@ -783,8 -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, ("version",), {"v0.2": nsd_schema_v02})
 +    http_content, used_schema = format_in( nsd_schema_v01, ("schema_version",), {2: nsd_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_deploy: Warning: remove extra items ", r
      print "http_post_deploy input: ",  http_content
      
@@@ -816,8 -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",), {"0.2": nsd_schema_v02})
 +    http_content, used_schema = format_in( nsd_schema_v01, ("schema_version",), {2: nsd_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_scenarios: Warning: remove extra items ", r
      print "http_post_scenarios input: ",  http_content
      if http_content.get("schema_version") == None:
diff --cc nfvo.py
+++ b/nfvo.py
@@@ -2367,10 -2367,7 +2367,10 @@@ def vim_action_get(mydb, tenant_id, dat
      
  def vim_action_delete(mydb, tenant_id, datacenter, item, name):
      #get datacenter info
-     if af.check_valid_uuid(datacenter): 
 +    if tenant_id == "any":
 +        tenant_id=None
 +
+     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_create(mydb, tenant_id, datacenter, item, descriptor):
      #get datacenter info
      print "vim_action_create descriptor", descriptor
-     if af.check_valid_uuid(datacenter): 
 +    if tenant_id == "any":
 +        tenant_id=None
 +
+     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 --cc nfvo_db.py
Simple merge
diff --cc utils.py
index 0000000,81a0158..8102773
mode 000000,100755..100644
--- /dev/null
+++ b/utils.py
@@@ -1,0 -1,175 +1,175 @@@
 -auxiliary_functions is a module that implements functions that are used by all openmano modules,
+ # -*- 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
+ ##
+ '''
++utils 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
+