minor changes at database connection exception management. Change openmanoconfig... 30/530/1
authortierno <alfonso.tiernosepulveda@telefonica.com>
Tue, 11 Oct 2016 12:06:25 +0000 (12:06 +0000)
committertierno <alfonso.tiernosepulveda@telefonica.com>
Tue, 11 Oct 2016 12:06:25 +0000 (12:06 +0000)
Change-Id: I4dbff4e3b5e21ff362f1487a63a213852ed18f78
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
db_base.py
nfvo_db.py
openmanoconfig.py [new file with mode: 0755]
openmanoconfig.sh [deleted file]
openmanod.py

index f6a5c59..807c73d 100644 (file)
@@ -75,7 +75,7 @@ def _convert_datetime2str(var):
         for v in var:
             _convert_datetime2str(v)
 
-def _convert_bandwidth(data, reverse=False):
+def _convert_bandwidth(data, reverse=False, logger=None):
     '''Check the field bandwidth recursivelly and when found, it removes units and convert to number 
     It assumes that bandwidth is well formed
     Attributes:
@@ -87,7 +87,7 @@ def _convert_bandwidth(data, reverse=False):
     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)
+                _convert_bandwidth(data[k], reverse, logger)
         if "bandwidth" in data:
             try:
                 value=str(data["bandwidth"])
@@ -102,12 +102,13 @@ def _convert_bandwidth(data, reverse=False):
                     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"]
+                if logger:
+                    logger.error("convert_bandwidth exception for type '%s' data '%s'", type(data["bandwidth"]), 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)
+                _convert_bandwidth(k, reverse, logger)
 
 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 
@@ -164,10 +165,11 @@ class db_base():
             if database:    self.database = database
 
             self.con = mdb.connect(self.host, self.user, self.passwd, self.database)
-            print "DB: connected to %s@%s -> %s" % (self.user, self.host, self.database)
-        except mdb.Error, e:
-            raise db_base_Exception("Cannot connect to DB {}@{} -> {} Error {}: {}".format(self.user, self.host, self.database, e.args[0], e.args[1]),
-                                    code = HTTP_Internal_Server_Error )
+            self.logger.debug("DB: connected to '%s' at '%s@%s'", self.database, self.user, self.host)
+        except mdb.Error as e:
+            raise db_base_Exception("Cannot connect to DataBase '{}' at '{}@{}' Error {}: {}".format(
+                                    self.database, self.user, self.host, e.args[0], e.args[1]),
+                                    http_code = HTTP_Unauthorized )
         
     def get_db_version(self):
         ''' Obtain the database schema version.
index 1fa5d72..6ab73e9 100644 (file)
@@ -81,7 +81,7 @@ class nfvo_db(db_base.db_base):
                         dataifacesDict[vm['name']] = {}
                         for numa in vm.get('numas', []):
                             for dataiface in numa.get('interfaces',[]):
-                                db_base._convert_bandwidth(dataiface)
+                                db_base._convert_bandwidth(dataiface, logger=self.logger)
                                 dataifacesDict[vm['name']][dataiface['name']] = {}
                                 dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface['vpci']
                                 dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth']
@@ -93,7 +93,7 @@ class nfvo_db(db_base.db_base):
                         if 'bridge-ifaces' in  vm:
                             bridgeInterfacesDict[vm['name']] = {}
                             for bridgeiface in vm['bridge-ifaces']:
-                                db_base._convert_bandwidth(bridgeiface)
+                                db_base._convert_bandwidth(bridgeiface, logger=self.logger)
                                 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)
@@ -209,7 +209,7 @@ class nfvo_db(db_base.db_base):
                         dataifacesDict[vm['name']] = {}
                         for numa in vm.get('numas', []):
                             for dataiface in numa.get('interfaces',[]):
-                                db_base._convert_bandwidth(dataiface)
+                                db_base._convert_bandwidth(dataiface, logger=self.logger)
                                 dataifacesDict[vm['name']][dataiface['name']] = {}
                                 dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface['vpci']
                                 dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth']
@@ -221,7 +221,7 @@ class nfvo_db(db_base.db_base):
                         if 'bridge-ifaces' in  vm:
                             bridgeInterfacesDict[vm['name']] = {}
                             for bridgeiface in vm['bridge-ifaces']:
-                                db_base._convert_bandwidth(bridgeiface)
+                                db_base._convert_bandwidth(bridgeiface, logger=self.logger)
                                 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)
diff --git a/openmanoconfig.py b/openmanoconfig.py
new file mode 100755 (executable)
index 0000000..07c5e24
--- /dev/null
@@ -0,0 +1,124 @@
+#!/usr/bin/env python
+# -*- 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
+##
+
+
+"""
+Read openmanod.cfg file and creates envioronment variables for openmano client
+Call it wusing execution quotes, or copy paste the output to set your shell envioronment
+It read database to look for a ninc tenant / datacenter
+"""
+
+from __future__ import print_function
+from  os import environ
+from openmanod import load_configuration
+#from socket import gethostname
+from db_base import db_base_Exception
+import nfvo_db
+import getopt
+import sys
+
+
+__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
+__date__ ="$26-aug-2014 11:09:29$"
+__version__="0.0.1-r509"
+version_date="Oct 2016"
+database_version="0.16"      #expected database schema version
+
+
+def usage():
+    print("Usage: ", sys.argv[0], "[options]")
+    print("      -v|--version: prints current version")
+    print("      -c|--config [configuration_file]: loads the configuration file (default: openmanod.cfg)")
+    print("      -h|--help: shows this help")
+    return
+
+
+if __name__ == "__main__":
+    # Read parameters and configuration file
+    try:
+        # load parameters and configuration
+        opts, args = getopt.getopt(sys.argv[1:], "vhc:",
+                                   ["config=", "help", "version"])
+        config_file = 'openmanod.cfg'
+
+        for o, a in opts:
+            if o in ("-v", "--version"):
+                print("openmanoconfig.py version " + __version__ + ' ' + version_date)
+                print("(c) Copyright Telefonica")
+                exit()
+            elif o in ("-h", "--help"):
+                usage()
+                exit()
+            elif o in ("-c", "--config"):
+                config_file = a
+            else:
+                assert False, "Unhandled option"
+        global_config = load_configuration(config_file)
+        if global_config["http_host"] == "0.0.0.0":
+            global_config["http_host"] = "localhost" #gethostname()
+        environ["OPENMANO_HOST"]=global_config["http_host"]
+        print("export OPENMANO_HOST='{}'".format(global_config["http_host"]))
+        environ["OPENMANO_PORT"] = str(global_config["http_port"])
+        print("export OPENMANO_PORT={}".format(global_config["http_port"]))
+
+        mydb = nfvo_db.nfvo_db();
+        mydb.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name'])
+        try:
+            tenants = mydb.get_rows(FROM="nfvo_tenants")
+            if not tenants:
+                print("#No tenant found", file=sys.stderr)
+            elif len(tenants) > 1:
+                print("#Found several tenants export OPENMANO_TENANT=", file=sys.stderr, end="")
+                for tenant in tenants:
+                    print(" '{}'".format(tenant["name"]), file=sys.stderr, end="")
+                print("")
+            else:
+                environ["OPENMANO_TENANT"] = tenants[0]["name"]
+                print("export OPENMANO_TENANT='{}'".format(tenants[0]["name"]))
+
+            dcs = mydb.get_rows(FROM="datacenters")
+            if not dcs:
+                print("#No datacenter found", file=sys.stderr)
+            elif len(dcs) > 1:
+                print("#Found several datacenters export OPENMANO_DATACENTER=", file=sys.stderr, end="")
+                for dc in dcs:
+                    print(" '{}'".format(dc["name"]), file=sys.stderr, end="")
+                print("")
+            else:
+                environ["OPENMANO_DATACENTER"] = dcs[0]["name"]
+                print("export OPENMANO_DATACENTER='{}'".format(dcs[0]["name"]))
+
+        except db_base_Exception as e:
+            print("#DATABASE is not a MANO one or it is a '0.0' version. Try to upgrade to version '{}' with \
+                            './database_utils/migrate_mano_db.sh'".format(database_version), file=sys.stderr)
+            exit(-1)
+
+
+
+    except db_base_Exception as e:
+        print("#"+str(e), file=sys.stderr)
+        exit(-1)
+
+    except SystemExit:
+        pass
diff --git a/openmanoconfig.sh b/openmanoconfig.sh
deleted file mode 100755 (executable)
index 696ad93..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-#!/bin/bash
-
-##
-# 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
-##
-
-export OPENMANO_HOST=localhost
-export OPENMANO_PORT=9090
-export OPENMANO_TENANT=bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb     #Use here the appropriate tenant id provided by openmano after creating the tenant
-#Uncomment the following line in case that there are several datacenters and use the appropriate id
-#export OPENMANO_DATACENTER=dddddddd-dddd-dddd-dddd-dddddddddddd
-
index fcfd11c..a76ed70 100755 (executable)
@@ -45,6 +45,7 @@ import yaml
 import nfvo_db
 from jsonschema import validate as js_v, exceptions as js_e
 from openmano_schemas import config_schema
+from db_base import db_base_Exception
 import nfvo
 import logging
 import logging.handlers as log_handlers
@@ -257,17 +258,19 @@ if __name__=="__main__":
         
         # Initialize DB connection
         mydb = nfvo_db.nfvo_db();
-        if mydb.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name']) == -1:
-            logger.critical("Cannot connect to database %s at %s@%s", global_config['db_name'], global_config['db_user'], global_config['db_host'])
+        mydb.connect(global_config['db_host'], global_config['db_user'], global_config['db_passwd'], global_config['db_name'])
+        try:
+            r = mydb.get_db_version()
+            if r[1] != database_version:
+                logger.critical("DATABASE wrong version '%s'. \
+                                Try to upgrade/downgrade to version '%s' with './database_utils/migrate_mano_db.sh'",
+                                r[1], database_version)
+                exit(-1)
+        except db_base_Exception as e:
+            logger.critical("DATABASE is not a MANO one or it is a '0.0' version. Try to upgrade to version '%s' with \
+                            './database_utils/migrate_mano_db.sh'", database_version)
             exit(-1)
-        r = mydb.get_db_version()
-        if r[0]<0:
-            logger.critical("DATABASE is not a MANO one or it is a '0.0' version. Try to upgrade to version '%s' with './database_utils/migrate_mano_db.sh'", database_version)
-            exit(-1)
-        elif r[1]!=database_version:
-            logger.critical("DATABASE wrong version '%s'. Try to upgrade/downgrade to version '%s' with './database_utils/migrate_mano_db.sh'", r[1], database_version)
-            exit(-1)
-        
+
         nfvo.global_config=global_config
         
         httpthread = httpserver.httpserver(mydb, False, global_config['http_host'], global_config['http_port'])
@@ -305,4 +308,7 @@ if __name__=="__main__":
     except LoadConfigurationException as e:
         logger.critical(str(e))
         exit(-1)
+    except db_base_Exception as e:
+        logger.critical(str(e))
+        exit(-1)