openmano first code upload 06/206/1
authortierno <alfonso.tiernosepulveda@telefonica.com>
Mon, 21 Mar 2016 16:37:52 +0000 (17:37 +0100)
committertierno <alfonso.tiernosepulveda@telefonica.com>
Mon, 21 Mar 2016 16:37:52 +0000 (17:37 +0100)
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
36 files changed:
LICENSE [new file with mode: 0644]
auxiliary_functions.py [new file with mode: 0755]
console_proxy_thread.py [new file with mode: 0755]
database_utils/dump_db.sh [new file with mode: 0755]
database_utils/init_mano_db.sh [new file with mode: 0755]
database_utils/mano_db_structure.sql [new file with mode: 0644]
database_utils/migrate_mano_db.sh [new file with mode: 0755]
httpserver.py [new file with mode: 0755]
nfvo.py [new file with mode: 0644]
nfvo_db.py [new file with mode: 0644]
openmano [new file with mode: 0755]
openmano_schemas.py [new file with mode: 0644]
openmanoconfig.sh [new file with mode: 0755]
openmanod.cfg [new file with mode: 0644]
openmanod.py [new file with mode: 0755]
scenarios/examples/complex.yaml [new file with mode: 0755]
scenarios/examples/complex2.yaml [new file with mode: 0755]
scenarios/examples/simple.yaml [new file with mode: 0755]
scenarios/scenario-template.yaml [new file with mode: 0755]
scripts/openmano-report.sh [new file with mode: 0755]
scripts/service-openmano.sh [new file with mode: 0755]
test/test-multivim.sh [new file with mode: 0755]
test/test_os.sh [new file with mode: 0755]
test/test_osconnector.py [new file with mode: 0755]
utils/__init__.py [new file with mode: 0644]
utils/auxiliary_functions.py [new file with mode: 0755]
vimconn.py [new file with mode: 0755]
vimconn_openstack.py [new file with mode: 0644]
vimconn_openvim.py [new file with mode: 0755]
vnfs/examples/dataplaneVNF1.yaml [new file with mode: 0644]
vnfs/examples/dataplaneVNF2.yaml [new file with mode: 0644]
vnfs/examples/dataplaneVNF3.yaml [new file with mode: 0644]
vnfs/examples/dataplaneVNF_2VMs.yaml [new file with mode: 0644]
vnfs/examples/linux.yaml [new file with mode: 0644]
vnfs/vnf-template-2vm.yaml [new file with mode: 0644]
vnfs/vnf-template.yaml [new file with mode: 0644]

diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..e06d208
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,202 @@
+Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "{}"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright {yyyy} {name of copyright owner}
+
+   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.
+
diff --git a/auxiliary_functions.py b/auxiliary_functions.py
new file mode 100755 (executable)
index 0000000..81a0158
--- /dev/null
@@ -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,<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
+    
diff --git a/console_proxy_thread.py b/console_proxy_thread.py
new file mode 100755 (executable)
index 0000000..67da12c
--- /dev/null
@@ -0,0 +1,183 @@
+# -*- 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
+##
+
+'''
+Implement like a proxy for TCP/IP in a separated thread.
+It creates two sockets to bypass the TCP/IP packets among the fix console 
+server specified at class construction (console_host, console_port)
+and a client that connect against the (host, port) specified also at construction
+
+                ---------------------           -------------------------------
+                |       OPENMANO     |          |         VIM                  |
+client 1  ----> | ConsoleProxyThread | ------>  |  Console server              |
+client 2  ----> |  (host, port)      | ------>  |(console_host, console_server)|
+   ...           --------------------            ------------------------------
+'''
+__author__="Alfonso Tierno"
+__date__ ="$19-nov-2015 09:07:15$"
+
+import socket
+import select
+import threading
+
+
+class ConsoleProxyException(Exception):
+    '''raise when an exception has found''' 
+class ConsoleProxyExceptionPortUsed(ConsoleProxyException):
+    '''raise when the port is used''' 
+
+class ConsoleProxyThread(threading.Thread):
+    buffer_size = 4096
+    check_finish = 1 #frequency to check if requested to end in seconds
+
+    def __init__(self, host, port, console_host, console_port):
+        try:
+            threading.Thread.__init__(self)
+            self.console_host = console_host
+            self.console_port = console_port
+            self.host = host
+            self.port = port
+            self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+            self.server.bind((host, port))
+            self.server.listen(200)
+            #TODO timeout in a lock section can be used to autoterminate the thread
+            #when inactivity and timeout<time : set timeout=0 and terminate
+            #from outside, close class when timeout==0; set timeout=time+120 when adding a new console on this thread
+            #set self.timeout = time.time() + 120 at init
+            self.name = "ConsoleProxy " + console_host + ":" + str(console_port)
+            self.input_list = [self.server]
+            self.channel = {}
+            self.terminate = False #put at True from outside to force termination
+        except (socket.error, socket.herror, socket.gaierror, socket.timeout) as e:
+            if e is socket.error and e.errno==98:
+                raise ConsoleProxyExceptionPortUsed("socket.error " + str(e))
+            raise ConsoleProxyException(type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0])) )
+        
+    def run(self):
+        while 1:
+            try:
+                inputready, _, _ = select.select(self.input_list, [], [], self.check_finish)
+            except select.error as e:
+                print self.name, ": Exception on select %s: %s" % (type(e).__name__, str(e) )
+                self.on_terminate()
+
+            if self.terminate:
+                self.on_terminate()
+                print self.name, ": Terminate because commanded"
+                break
+            
+            for sock in inputready:
+                if sock == self.server:
+                    self.on_accept()
+                else:
+                    self.on_recv(sock)
+                    
+    def on_terminate(self):
+        while self.input_list:
+            if self.input_list[0] is self.server:
+                self.server.close()
+                del self.input_list[0]
+            else:
+                self.on_close(self.input_list[0], "Terminating thread")
+
+    def on_accept(self):
+        #accept
+        try:
+            clientsock, clientaddr = self.server.accept()
+        except (socket.error, socket.herror, socket.gaierror, socket.timeout) as e:
+            print self.name, ": Exception on_accept %s: %s" % (type(e).__name__, str(e) )
+            return False
+        #print self.name, ": Accept new client ", clientaddr
+
+        #connect
+        try:
+            forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            forward.connect((self.console_host, self.console_port))
+            name = "%s:%d => (%s:%d => %s:%d) => %s:%d" %\
+                (clientsock.getpeername() + clientsock.getsockname()  + forward.getsockname() + forward.getpeername() )
+            print self.name, ": new connection " + name
+                
+            self.input_list.append(clientsock)
+            self.input_list.append(forward)
+            info = { "name": name,
+                    "clientsock" : clientsock,
+                    "serversock" : forward
+                    }
+            self.channel[clientsock] = info
+            self.channel[forward] = info
+            return True
+        except (socket.error, socket.herror, socket.gaierror, socket.timeout) as e:
+            print self.name, ": Exception on_connect to server %s:%d; %s: %s" % (self.console_host, self.console_port, type(e).__name__, str(e) )
+            print self.name, ": Close client side ", clientaddr
+            clientsock.close()
+            return False
+
+    def on_close(self, sock, cause):
+        if sock not in self.channel:
+            return  #can happen if there is data ready to received at both sides and the channel has been deleted. QUITE IMPROBABLE but just in case
+        info = self.channel[sock]
+        #debug info
+        sockname = "client" if sock is info["clientsock"] else "server"
+        print self.name, ": del connection %s %s at %s side" % (info["name"], cause, sockname)
+        #close sockets
+        try:
+            # close the connection with client
+            info["clientsock"].close()  # equivalent to do self.s.close()
+        except (socket.error, socket.herror, socket.gaierror, socket.timeout) as e:
+            print self.name, ": Exception on_close client socket %s: %s" % (type(e).__name__, str(e) )
+        try:
+            # close the connection with remote server
+            info["serversock"].close()
+        except (socket.error, socket.herror, socket.gaierror, socket.timeout) as e:
+            print self.name, ": Exception on_close server socket %s: %s" % (type(e).__name__, str(e) )
+        
+        #remove objects from input_list
+        self.input_list.remove(info["clientsock"])
+        self.input_list.remove(info["serversock"])
+        # delete both objects from channel dict
+        del self.channel[info["clientsock"]]
+        del self.channel[info["serversock"]]
+
+    def on_recv(self, sock):
+        if sock not in self.channel:
+            return  #can happen if there is data ready to received at both sides and the channel has been deleted. QUITE IMPROBABLE but just in case
+        info = self.channel[sock]
+        peersock = info["serversock"] if sock is info["clientsock"] else info["clientsock"]
+        try:
+            data = sock.recv(self.buffer_size)
+            if len(data) == 0:
+                self.on_close(sock, "peer closed")
+            else:
+                #print self.data
+                sock = peersock
+                peersock.send(data)
+        except (socket.error, socket.herror, socket.gaierror, socket.timeout) as e:
+            #print self.name, ": Exception %s: %s" % (type(e).__name__, str(e) )
+            self.on_close(sock, "Exception %s: %s" % (type(e).__name__, str(e) ))
+
+        
+
+    #def start_timeout(self):
+    #    self.timeout = time.time() + 120
+        
diff --git a/database_utils/dump_db.sh b/database_utils/dump_db.sh
new file mode 100755 (executable)
index 0000000..dfd5bbf
--- /dev/null
@@ -0,0 +1,122 @@
+#!/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
+##
+
+
+DBUSER="mano"
+DBPASS=""
+DBHOST="localhost"
+DBPORT="3306"
+DBNAME="mano_db"
+# Detect paths
+MYSQL=$(which mysql)
+AWK=$(which awk)
+GREP=$(which grep)
+DIRNAME=`dirname $0`
+function usage(){
+    echo -e "Usage: $0 OPTIONS"
+    echo -e "  Dumps openmano database content"
+    echo -e "  OPTIONS"
+    echo -e "     -u USER  database user. '$DBUSER' by default. Prompts if DB access fails"
+    echo -e "     -p PASS  database password. 'No password' by default. Prompts if DB access fails"
+    echo -e "     -P PORT  database port. '$DBPORT' by default"
+    echo -e "     -h HOST  database host. '$DBHOST' by default"
+    echo -e "     -d NAME  database name. '$DBNAME' by default.  Prompts if DB access fails"
+    echo -e "     --help   shows this help"
+}
+
+while getopts ":u:p:P:h:-:" o; do
+    case "${o}" in
+        u)
+            DBUSER="$OPTARG"
+            ;;
+        p)
+            DBPASS="$OPTARG"
+            ;;
+        P)
+            DBPORT="$OPTARG"
+            ;;
+        d)
+            DBNAME="$OPTARG"
+            ;;
+        h)
+            DBHOST="$OPTARG"
+            ;;
+        -)
+            [ "${OPTARG}" == "help" ] && usage && exit 0
+            echo "Invalid option: --$OPTARG" >&2 && usage  >&2
+            exit 1
+            ;;
+        \?)
+            echo "Invalid option: -$OPTARG" >&2 && usage  >&2
+            exit 1
+            ;;
+        :)
+            echo "Option -$OPTARG requires an argument." >&2 && usage  >&2
+            exit 1
+            ;;
+        *)
+            usage >&2
+            exit -1
+            ;;
+    esac
+done
+shift $((OPTIND-1))
+
+#check and ask for database user password
+DBUSER_="-u$DBUSER"
+DBPASS_=""
+[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
+DBHOST_="-h$DBHOST"
+DBPORT_="-P$DBPORT"
+while !  echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME >/dev/null 2>&1
+do
+        [ -n "$logintry" ] &&  echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
+        [ -z "$logintry" ] &&  echo -e "\nProvide database name and credentials"
+        read -e -p "mysql database name($DBNAME): " KK
+        [ -n "$KK" ] && DBNAME="$KK"
+        read -e -p "mysql user($DBUSER): " KK
+        [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER"
+        read -e -s -p "mysql password: " DBPASS
+        [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
+        [ -z "$DBPASS" ] && DBPASS_=""
+        logintry="yes"
+        echo
+done
+
+#echo structure, including the content of schema_version
+mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-data --add-drop-table --add-drop-database --routines --databases $DBNAME > ${DIRNAME}/${DBNAME}_structure.sql
+echo -e "\n\n\n\n" >> ${DIRNAME}/${DBNAME}_structure.sql
+mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-create-info $DBNAME --tables schema_version 2>/dev/null  >> ${DIRNAME}/${DBNAME}_structure.sql
+echo "    ${DIRNAME}/${DBNAME}_structure.sql"
+
+#echo only data
+mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --no-create-info $DBNAME > ${DIRNAME}/${DBNAME}_data.sql
+echo "    ${DIRNAME}/${DBNAME}_data.sql"
+
+#echo all
+mysqldump $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ --add-drop-table --add-drop-database --routines --databases $DBNAME > ${DIRNAME}/${DBNAME}_all.sql
+echo "    ${DIRNAME}/${DBNAME}_all.sql"
+
diff --git a/database_utils/init_mano_db.sh b/database_utils/init_mano_db.sh
new file mode 100755 (executable)
index 0000000..c876ea4
--- /dev/null
@@ -0,0 +1,113 @@
+#!/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
+##
+
+DBUSER="mano"
+DBPASS=""
+DBHOST="localhost"
+DBPORT="3306"
+DBNAME="mano_db"
+
+# Detect paths
+MYSQL=$(which mysql)
+AWK=$(which awk)
+GREP=$(which grep)
+DIRNAME=`dirname $0`
+
+function usage(){
+    echo -e "Usage: $0 OPTIONS"
+    echo -e "  Inits openmano database; deletes previous one and loads from ${DBNAME}_structure.sql"
+    echo -e "  OPTIONS"
+    echo -e "     -u USER  database user. '$DBUSER' by default. Prompts if DB access fails"
+    echo -e "     -p PASS  database password. 'No password' by default. Prompts if DB access fails"
+    echo -e "     -P PORT  database port. '$DBPORT' by default"
+    echo -e "     -h HOST  database host. '$DBHOST' by default"
+#    echo -e "     -d NAME  database name. '$DBNAME' by default.  Prompts if DB access fails"
+    echo -e "     --help   shows this help"
+}
+
+while getopts ":u:p:P:h:-:" o; do
+    case "${o}" in
+        u)
+            DBUSER="$OPTARG"
+            ;;
+        p)
+            DBPASS="$OPTARG"
+            ;;
+        P)
+            DBPORT="$OPTARG"
+            ;;
+#        d)
+#            DBNAME="$OPTARG"
+#            ;;
+        h)
+            DBHOST="$OPTARG"
+            ;;
+        -)
+            [ "${OPTARG}" == "help" ] && usage && exit 0
+            echo "Invalid option: --$OPTARG" >&2 && usage  >&2
+            exit 1
+            ;;
+        \?)
+            echo "Invalid option: -$OPTARG" >&2 && usage  >&2
+            exit 1
+            ;;
+        :)
+            echo "Option -$OPTARG requires an argument." >&2 && usage  >&2
+            exit 1
+            ;;
+        *)
+            usage >&2
+            exit -1
+            ;;
+    esac
+done
+shift $((OPTIND-1))
+
+#check and ask for database user password
+DBUSER_="-u$DBUSER"
+DBPASS_=""
+[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
+DBHOST_="-h$DBHOST"
+DBPORT_="-P$DBPORT"
+while !  echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ >/dev/null 2>&1
+do
+        [ -n "$logintry" ] &&  echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
+        [ -z "$logintry" ] &&  echo -e "\nProvide database credentials"
+#        read -e -p "mysql database name($DBNAME): " KK
+#        [ -n "$KK" ] && DBNAME="$KK"
+        read -e -p "mysql user($DBUSER): " KK
+        [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER"
+        read -e -s -p "mysql password: " DBPASS
+        [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
+        [ -z "$DBPASS" ] && DBPASS_=""
+        logintry="yes"
+        echo
+done
+
+#${DIRNAME}/quick_delete_db.sh $MUSER $MPASS $MDB $HOST $PORT
+echo "    loading ${DIRNAME}/${DBNAME}_structure.sql"
+mysql  $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ < ${DIRNAME}/${DBNAME}_structure.sql
+
+echo "    migrage database version"
+${DIRNAME}/migrate_mano_db.sh $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ -d$DBNAME
+
diff --git a/database_utils/mano_db_structure.sql b/database_utils/mano_db_structure.sql
new file mode 100644 (file)
index 0000000..cea38f4
--- /dev/null
@@ -0,0 +1,519 @@
+/**
+* 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
+**/
+-- MySQL dump 10.13  Distrib 5.5.35, for debian-linux-gnu (x86_64)
+--
+-- Host: localhost    Database: mano_db
+-- ------------------------------------------------------
+-- Server version      5.5.35-1ubuntu1
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8 */;
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
+/*!40103 SET TIME_ZONE='+00:00' */;
+/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
+/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
+/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
+
+--
+-- Current Database: `mano_db`
+--
+
+/*!40000 DROP DATABASE IF EXISTS `mano_db`*/;
+
+CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mano_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
+
+USE `mano_db`;
+
+--
+-- Table structure for table `datacenter_nets`
+--
+
+DROP TABLE IF EXISTS `datacenter_nets`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `datacenter_nets` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL COMMENT 'To match with sce_nets',
+  `vim_net_id` varchar(36) NOT NULL,
+  `datacenter_id` varchar(36) NOT NULL,
+  `type` enum('bridge','data','ptp') NOT NULL DEFAULT 'data' COMMENT 'Type of network',
+  `multipoint` enum('true','false') NOT NULL DEFAULT 'true',
+  `shared` enum('true','false') NOT NULL DEFAULT 'false' COMMENT 'If can be shared with serveral scenarios',
+  `description` varchar(100) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `name_datacenter_id` (`name`,`datacenter_id`),
+  KEY `FK_datacenter_nets_datacenters` (`datacenter_id`),
+  CONSTRAINT `FK_datacenter_nets_datacenters` FOREIGN KEY (`datacenter_id`) REFERENCES `datacenters` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Contain the external nets of a datacenter';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `datacenters`
+--
+
+DROP TABLE IF EXISTS `datacenters`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `datacenters` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL,
+  `description` varchar(100) DEFAULT NULL,
+  `type` enum('openvim','openstack') NOT NULL DEFAULT 'openvim',
+  `vim_url` varchar(150) NOT NULL COMMENT 'URL of the VIM for the REST API',
+  `vim_url_admin` varchar(150) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `name` (`name`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Datacenters managed by the NFVO.';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `instance_interfaces`
+--
+
+DROP TABLE IF EXISTS `instance_interfaces`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `instance_interfaces` (
+  `uuid` varchar(36) NOT NULL,
+  `instance_vm_id` varchar(36) NOT NULL,
+  `instance_net_id` varchar(36) NOT NULL,
+  `interface_id` varchar(36) NOT NULL,
+  `vim_interface_id` varchar(36) NOT NULL COMMENT 'vim identity for that interface',
+  `type` enum('internal','external') NOT NULL COMMENT 'Indicates if this interface is external to a vnf, or internal',
+  PRIMARY KEY (`uuid`),
+  KEY `FK_instance_vms` (`instance_vm_id`),
+  KEY `FK_instance_nets` (`instance_net_id`),
+  KEY `FK_instance_ids` (`interface_id`),
+  CONSTRAINT `FK_instance_ids` FOREIGN KEY (`interface_id`) REFERENCES `interfaces` (`uuid`),
+  CONSTRAINT `FK_instance_nets` FOREIGN KEY (`instance_net_id`) REFERENCES `instance_nets` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
+  CONSTRAINT `FK_instance_vms` FOREIGN KEY (`instance_vm_id`) REFERENCES `instance_vms` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Table with all running associattion among VM instances and net instances';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `instance_nets`
+--
+
+DROP TABLE IF EXISTS `instance_nets`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `instance_nets` (
+  `uuid` varchar(36) NOT NULL,
+  `vim_net_id` varchar(36) NOT NULL COMMENT 'Network ID in the VIM DB',
+  `instance_scenario_id` varchar(36) NOT NULL,
+  `status` enum('ACTIVE','DOWN','BUILD','ERROR') NOT NULL DEFAULT 'BUILD',
+  `multipoint` enum('true','false') NOT NULL DEFAULT 'true',
+  `external` enum('true','false') NOT NULL DEFAULT 'false' COMMENT 'If external, means that it already exists at VIM',
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `vim_net_id_instance_scenario_id` (`vim_net_id`,`instance_scenario_id`),
+  KEY `FK_instance_nets_instance_scenarios` (`instance_scenario_id`),
+  CONSTRAINT `FK_instance_nets_instance_scenarios` FOREIGN KEY (`instance_scenario_id`) REFERENCES `instance_scenarios` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Instances of networks';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `instance_scenarios`
+--
+
+DROP TABLE IF EXISTS `instance_scenarios`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `instance_scenarios` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL,
+  `scenario_id` varchar(36) NOT NULL,
+  `nfvo_tenant_id` varchar(36) NOT NULL,
+  `vim_tenant_id` varchar(36) NOT NULL,
+  `datacenter_id` varchar(36) NOT NULL,
+  `description` varchar(100) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `name` (`name`),
+  KEY `FK_scenarios_nfvo_tenants` (`nfvo_tenant_id`),
+  KEY `FK_instance_scenarios_vim_tenants` (`vim_tenant_id`),
+  KEY `FK_instance_scenarios_datacenters` (`datacenter_id`),
+  KEY `FK_instance_scenarios_scenarios` (`scenario_id`),
+  CONSTRAINT `FK_instance_scenarios_datacenters` FOREIGN KEY (`datacenter_id`) REFERENCES `datacenters` (`uuid`),
+  CONSTRAINT `FK_instance_scenarios_nfvo_tenants` FOREIGN KEY (`nfvo_tenant_id`) REFERENCES `nfvo_tenants` (`uuid`),
+  CONSTRAINT `FK_instance_scenarios_scenarios` FOREIGN KEY (`scenario_id`) REFERENCES `scenarios` (`uuid`),
+  CONSTRAINT `FK_instance_scenarios_vim_tenants` FOREIGN KEY (`vim_tenant_id`) REFERENCES `vim_tenants` (`uuid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Instances of scenarios defined by the user';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `instance_vms`
+--
+
+DROP TABLE IF EXISTS `instance_vms`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `instance_vms` (
+  `uuid` varchar(36) NOT NULL,
+  `instance_vnf_id` varchar(36) NOT NULL,
+  `vm_id` varchar(36) NOT NULL,
+  `vim_vm_id` varchar(36) NOT NULL COMMENT 'VM ID in the VIM DB',
+  `status` enum('ACTIVE','PAUSED','INACTIVE','CREATING','ERROR','DELETING') NOT NULL DEFAULT 'CREATING',
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `vim_vm_id` (`vim_vm_id`),
+  KEY `FK_instance_vms_vms` (`vm_id`),
+  KEY `FK_instance_vms_instance_vnfs` (`instance_vnf_id`),
+  CONSTRAINT `FK_instance_vms_instance_vnfs` FOREIGN KEY (`instance_vnf_id`) REFERENCES `instance_vnfs` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
+  CONSTRAINT `FK_instance_vms_vms` FOREIGN KEY (`vm_id`) REFERENCES `vms` (`uuid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Instances of VMs as part of VNF instances';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `instance_vnfs`
+--
+
+DROP TABLE IF EXISTS `instance_vnfs`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `instance_vnfs` (
+  `uuid` varchar(36) NOT NULL,
+  `instance_scenario_id` varchar(36) NOT NULL,
+  `vnf_id` varchar(36) NOT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  KEY `FK_instance_vnfs_vnfs` (`vnf_id`),
+  KEY `FK_instance_vnfs_instance_scenarios` (`instance_scenario_id`),
+  CONSTRAINT `FK_instance_vnfs_instance_scenarios` FOREIGN KEY (`instance_scenario_id`) REFERENCES `instance_scenarios` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
+  CONSTRAINT `FK_instance_vnfs_vnfs` FOREIGN KEY (`vnf_id`) REFERENCES `vnfs` (`uuid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Instances of VNFs as part of a scenario';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `interfaces`
+--
+
+DROP TABLE IF EXISTS `interfaces`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `interfaces` (
+  `uuid` varchar(36) NOT NULL,
+  `internal_name` varchar(25) NOT NULL,
+  `external_name` varchar(25) DEFAULT NULL COMMENT 'NULL if the interface is not an external interface',
+  `vm_id` varchar(36) NOT NULL,
+  `net_id` varchar(36) DEFAULT NULL,
+  `type` enum('mgmt','bridge','data') NOT NULL DEFAULT 'data' COMMENT 'Type of network',
+  `vpci` char(12) DEFAULT NULL,
+  `bw` mediumint(8) unsigned DEFAULT NULL COMMENT 'BW expressed in Mbits/s. Maybe this field is not necessary.',
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  `model` varchar(12) DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `internal_name_vm_id` (`internal_name`,`vm_id`),
+  KEY `FK_interfaces_vms` (`vm_id`),
+  KEY `FK_interfaces_nets` (`net_id`),
+  CONSTRAINT `FK_interfaces_nets` FOREIGN KEY (`net_id`) REFERENCES `nets` (`uuid`) ON DELETE CASCADE,
+  CONSTRAINT `FK_interfaces_vms` FOREIGN KEY (`vm_id`) REFERENCES `vms` (`uuid`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='VM interfaces';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `logs`
+--
+
+DROP TABLE IF EXISTS `logs`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `logs` (
+  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `nfvo_tenant_id` varchar(36) DEFAULT NULL,
+  `related` enum('nfvo_tenants','datacenters','vim_tenants','tenants_datacenters','vnfs','vms','interfaces','nets','scenarios','sce_vnfs','sce_interfaces','sce_nets','instance_scenarios','instance_vnfs','instance_vms','instance_nets','instance_interfaces') NOT NULL COMMENT 'Relevant element for the log',
+  `uuid` varchar(36) DEFAULT NULL COMMENT 'Uuid of vnf, scenario, etc. that log relates to',
+  `level` enum('panic','error','info','debug','verbose') NOT NULL,
+  `description` varchar(200) NOT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=3286 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `nets`
+--
+
+DROP TABLE IF EXISTS `nets`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `nets` (
+  `uuid` varchar(36) NOT NULL,
+  `vnf_id` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL,
+  `type` enum('bridge','data','ptp') NOT NULL DEFAULT 'data' COMMENT 'Type of network',
+  `multipoint` enum('true','false') NOT NULL DEFAULT 'false',
+  `description` varchar(100) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `vnf_id_name` (`vnf_id`,`name`),
+  CONSTRAINT `FK_nets_vnfs` FOREIGN KEY (`vnf_id`) REFERENCES `vnfs` (`uuid`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Networks in a VNF definition. These are only the internal networks among VMs of the same VNF.';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `nfvo_tenants`
+--
+
+DROP TABLE IF EXISTS `nfvo_tenants`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `nfvo_tenants` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL,
+  `description` varchar(100) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `name` (`name`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Scenarios defined by the user';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `sce_interfaces`
+--
+
+DROP TABLE IF EXISTS `sce_interfaces`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `sce_interfaces` (
+  `uuid` varchar(36) NOT NULL,
+  `sce_vnf_id` varchar(36) NOT NULL,
+  `sce_net_id` varchar(36) DEFAULT NULL,
+  `interface_id` varchar(36) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  KEY `FK_sce_interfaces_sce_vnfs` (`sce_vnf_id`),
+  KEY `FK_sce_interfaces_sce_nets` (`sce_net_id`),
+  KEY `FK_sce_interfaces_interfaces` (`interface_id`),
+  CONSTRAINT `FK_sce_interfaces_interfaces` FOREIGN KEY (`interface_id`) REFERENCES `interfaces` (`uuid`),
+  CONSTRAINT `FK_sce_interfaces_sce_nets` FOREIGN KEY (`sce_net_id`) REFERENCES `sce_nets` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
+  CONSTRAINT `FK_sce_interfaces_sce_vnfs` FOREIGN KEY (`sce_vnf_id`) REFERENCES `sce_vnfs` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='VNF interfaces in a scenario definition.';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `sce_nets`
+--
+
+DROP TABLE IF EXISTS `sce_nets`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `sce_nets` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) DEFAULT NULL,
+  `scenario_id` varchar(36) DEFAULT NULL COMMENT 'NULL if net is matched to several scenarios',
+  `type` enum('bridge','data','ptp') NOT NULL DEFAULT 'data' COMMENT 'Type of network',
+  `multipoint` enum('true','false') NOT NULL DEFAULT 'true',
+  `external` enum('true','false') NOT NULL DEFAULT 'false' COMMENT 'If external, net is already present at VIM',
+  `description` varchar(100) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  `graph` varchar(200) DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  KEY `FK_sce_nets_scenarios` (`scenario_id`),
+  CONSTRAINT `FK_sce_nets_scenarios` FOREIGN KEY (`scenario_id`) REFERENCES `scenarios` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Networks in a scenario definition. It only considers networks among VNFs. Networks among internal VMs are only considered in tble ''nets''.';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `sce_vnfs`
+--
+
+DROP TABLE IF EXISTS `sce_vnfs`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `sce_vnfs` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL,
+  `scenario_id` varchar(36) NOT NULL,
+  `vnf_id` varchar(36) NOT NULL,
+  `description` varchar(100) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  `graph` varchar(200) DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `name_scenario_id` (`name`,`scenario_id`),
+  KEY `FK_sce_vnfs_scenarios` (`scenario_id`),
+  KEY `FK_sce_vnfs_vnfs` (`vnf_id`),
+  CONSTRAINT `FK_sce_vnfs_scenarios` FOREIGN KEY (`scenario_id`) REFERENCES `scenarios` (`uuid`) ON DELETE CASCADE ON UPDATE CASCADE,
+  CONSTRAINT `FK_sce_vnfs_vnfs` FOREIGN KEY (`vnf_id`) REFERENCES `vnfs` (`uuid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='VNFs in scenario definitions. This table also contains the Physical Network Functions and the external elements such as MAN, Core, etc.\r\n';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `scenarios`
+--
+
+DROP TABLE IF EXISTS `scenarios`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `scenarios` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL,
+  `nfvo_tenant_id` varchar(36) DEFAULT NULL,
+  `description` varchar(100) DEFAULT NULL,
+  `public` enum('true','false') NOT NULL DEFAULT 'false',
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `name` (`name`),
+  KEY `FK_scenarios_nfvo_tenants` (`nfvo_tenant_id`),
+  CONSTRAINT `FK_scenarios_nfvo_tenants` FOREIGN KEY (`nfvo_tenant_id`) REFERENCES `nfvo_tenants` (`uuid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Scenarios defined by the user';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `tenants_datacenters`
+--
+
+DROP TABLE IF EXISTS `tenants_datacenters`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `tenants_datacenters` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `nfvo_tenant_id` varchar(36) NOT NULL,
+  `datacenter_id` varchar(36) NOT NULL,
+  `vim_tenant_id` varchar(36) NOT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`id`),
+  UNIQUE KEY `datacenter_nfvo_tenant` (`datacenter_id`,`nfvo_tenant_id`),
+  KEY `FK_nfvo_tenants_datacenters` (`datacenter_id`),
+  KEY `FK_nfvo_tenants_vim_tenants` (`vim_tenant_id`),
+  KEY `FK_tenants_datacenters_nfvo_tenants` (`nfvo_tenant_id`),
+  CONSTRAINT `FK_tenants_datacenters_datacenters` FOREIGN KEY (`datacenter_id`) REFERENCES `datacenters` (`uuid`),
+  CONSTRAINT `FK_tenants_datacenters_nfvo_tenants` FOREIGN KEY (`nfvo_tenant_id`) REFERENCES `nfvo_tenants` (`uuid`),
+  CONSTRAINT `FK_tenants_datacenters_vim_tenants` FOREIGN KEY (`vim_tenant_id`) REFERENCES `vim_tenants` (`uuid`)
+) ENGINE=InnoDB AUTO_INCREMENT=85 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Scenarios defined by the user';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `uuids`
+--
+
+DROP TABLE IF EXISTS `uuids`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `uuids` (
+  `uuid` varchar(36) NOT NULL,
+  `root_uuid` varchar(36) DEFAULT NULL COMMENT 'Some related UUIDs can be grouped by this field, so that they can be deleted at once',
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `used_at` enum('nfvo_tenants','datacenters','vim_tenants','vnfs','vms','interfaces','nets','scenarios','sce_vnfs','sce_interfaces','sce_nets','instance_scenarios','instance_vnfs','instance_vms','instance_nets','instance_interfaces') DEFAULT NULL COMMENT 'Table that uses this UUID',
+  PRIMARY KEY (`uuid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Table with all unique IDs used to avoid UUID repetitions among different elements';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `vim_tenants`
+--
+
+DROP TABLE IF EXISTS `vim_tenants`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `vim_tenants` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL,
+  `vim_tenant_id` varchar(36) NOT NULL COMMENT 'Tenant ID in the VIM DB',
+  `created` enum('true','false') NOT NULL DEFAULT 'false' COMMENT 'Indicates if this tenant has been created by openmano, or it existed on VIM',
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `name` (`name`),
+  UNIQUE KEY `vim_tenant_id` (`vim_tenant_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='Scenarios defined by the user';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `vms`
+--
+
+DROP TABLE IF EXISTS `vms`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `vms` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL,
+  `vnf_id` varchar(36) NOT NULL,
+  `vim_flavor_id` varchar(36) NOT NULL COMMENT 'Flavor ID in the VIM DB',
+  `vim_image_id` varchar(36) NOT NULL COMMENT 'Image ID in the VIM DB',
+  `image_path` varchar(100) NOT NULL COMMENT 'Path where the image of the VM is located',
+  `description` varchar(100) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `name_vnf_id` (`name`,`vnf_id`),
+  KEY `FK_vms_vnfs` (`vnf_id`),
+  CONSTRAINT `FK_vms_vnfs` FOREIGN KEY (`vnf_id`) REFERENCES `vnfs` (`uuid`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='VM definitions. It contains the set of VMs used by the VNF definitions.';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Table structure for table `vnfs`
+--
+
+DROP TABLE IF EXISTS `vnfs`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `vnfs` (
+  `uuid` varchar(36) NOT NULL,
+  `name` varchar(36) NOT NULL,
+  `path` varchar(100) DEFAULT NULL COMMENT 'Path where the YAML descriptor of the VNF can be found. NULL if it is a physical network function.',
+  `physical` enum('true','false') NOT NULL DEFAULT 'false',
+  `public` enum('true','false') NOT NULL DEFAULT 'true',
+  `description` varchar(100) DEFAULT NULL,
+  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+  `modified_at` timestamp NULL DEFAULT NULL,
+  `class` varchar(36) DEFAULT 'MISC',
+  PRIMARY KEY (`uuid`),
+  UNIQUE KEY `name` (`name`),
+  UNIQUE KEY `path` (`path`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='VNF definitions. This is the catalogue of VNFs. It also includes Physical Network Functions or Physical Elements.\r\n';
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping routines for database 'mano_db'
+--
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+
+-- Dump completed on 2015-03-11 17:34:28
diff --git a/database_utils/migrate_mano_db.sh b/database_utils/migrate_mano_db.sh
new file mode 100755 (executable)
index 0000000..3364d4f
--- /dev/null
@@ -0,0 +1,579 @@
+#!/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
+##
+
+#
+#Upgrade/Downgrade openmano database preserving the content
+#
+
+DBUSER="mano"
+DBPASS=""
+DBHOST="localhost"
+DBPORT="3306"
+DBNAME="mano_db"
+# Detect paths
+MYSQL=$(which mysql)
+AWK=$(which awk)
+GREP=$(which grep)
+DIRNAME=`dirname $0`
+
+function usage(){
+    echo -e "Usage: $0 OPTIONS  [{openmano_version}]"
+    echo -e "  Upgrades/Downgrades openmano database preserving the content"
+    echo -e "   if openmano_version is not provided it tries to get from openmanod.py using relative path"
+    echo -e "  OPTIONS"
+    echo -e "     -u USER  database user. '$DBUSER' by default. Prompts if DB access fails"
+    echo -e "     -p PASS  database password. 'No password' by default. Prompts if DB access fails"
+    echo -e "     -P PORT  database port. '$DBPORT' by default"
+    echo -e "     -h HOST  database host. '$DBHOST' by default"
+    echo -e "     -d NAME  database name. '$DBNAME' by default.  Prompts if DB access fails"
+    echo -e "     --help   shows this help"
+}
+
+while getopts ":u:p:P:h:d:-:" o; do
+    case "${o}" in
+        u)
+            DBUSER="$OPTARG"
+            ;;
+        p)
+            DBPASS="$OPTARG"
+            ;;
+        P)
+            DBPORT="$OPTARG"
+            ;;
+        d)
+            DBNAME="$OPTARG"
+            ;;
+        h)
+            DBHOST="$OPTARG"
+            ;;
+        -)
+            [ "${OPTARG}" == "help" ] && usage && exit 0
+            echo "Invalid option: --$OPTARG" >&2 && usage  >&2
+            exit 1
+            ;; 
+        \?)
+            echo "Invalid option: -$OPTARG" >&2 && usage  >&2
+            exit 1
+            ;;
+        :)
+            echo "Option -$OPTARG requires an argument." >&2 && usage  >&2
+            exit 1
+            ;;
+        *)
+            usage >&2
+            exit -1
+            ;;
+    esac
+done
+shift $((OPTIND-1))
+
+
+#GET OPENMANO VERSION
+OPENMANO_VER="$1"
+if [ -z "$OPENMANO_VER" ]
+then 
+    OPENMANO_VER=`${DIRNAME}/../openmanod.py -v`
+    OPENMANO_VER=${OPENMANO_VER%%-r*}
+    OPENMANO_VER=${OPENMANO_VER##*version }
+    echo "    Detected openmano version $OPENMANO_VER"
+fi
+VERSION_1=`echo $OPENMANO_VER | cut -f 1 -d"."`
+VERSION_2=`echo $OPENMANO_VER | cut -f 2 -d"."`
+VERSION_3=`echo $OPENMANO_VER | cut -f 3 -d"."`
+if ! [ "$VERSION_1" -ge 0 -a "$VERSION_2" -ge 0 -a "$VERSION_3" -ge 0 ] 2>/dev/null
+then 
+    [ -n "$1" ] && echo "Invalid openmano version '$1', expected 'X.X.X'" >&2
+    [ -z "$1" ] && echo "Can not get openmano version" >&2
+    exit -1
+fi
+OPENMANO_VER_NUM=`printf "%d%03d%03d" ${VERSION_1} ${VERSION_2} ${VERSION_3}`
+
+#check and ask for database user password
+DBUSER_="-u$DBUSER"
+[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
+DBHOST_="-h$DBHOST"
+DBPORT_="-P$DBPORT"
+while !  echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME >/dev/null 2>&1
+do
+        [ -n "$logintry" ] &&  echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
+        [ -z "$logintry" ] &&  echo -e "\nProvide database name and credentials"
+        read -e -p "mysql database name($DBNAME): " KK
+        [ -n "$KK" ] && DBNAME="$KK"
+        read -e -p "mysql user($DBUSER): " KK
+        [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER"
+        read -e -s -p "mysql password: " DBPASS
+        [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
+        [ -z "$DBPASS" ] && DBPASS_=""
+        logintry="yes"
+        echo
+done
+
+DBCMD="mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ $DBNAME"
+#echo DBCMD $DBCMD
+
+#GET DATABASE VERSION
+#check that the database seems a openmano database
+if ! echo -e "show create table vnfs;\nshow create table scenarios" | $DBCMD >/dev/null 2>&1
+then
+    echo "    database $DBNAME does not seem to be an openmano database" >&2
+    exit -1;
+fi
+
+if ! echo 'show create table schema_version;' | $DBCMD >/dev/null 2>&1
+then
+    DATABASE_VER="0.0"
+    DATABASE_VER_NUM=0
+else 
+    DATABASE_VER_NUM=`echo "select max(version_int) from schema_version;" | $DBCMD | tail -n+2` 
+    DATABASE_VER=`echo "select version from schema_version where version_int='$DATABASE_VER_NUM';" | $DBCMD | tail -n+2` 
+    [ "$DATABASE_VER_NUM" -lt 0 -o "$DATABASE_VER_NUM" -gt 100 ] && echo "    Error can not get database version ($DATABASE_VER?)" >&2 && exit -1
+    #echo "_${DATABASE_VER_NUM}_${DATABASE_VER}"
+fi
+
+
+#GET DATABASE TARGET VERSION
+DATABASE_TARGET_VER_NUM=0
+[ $OPENMANO_VER_NUM -ge 2002 ] && DATABASE_TARGET_VER_NUM=1   #0.2.2 =>  1
+[ $OPENMANO_VER_NUM -ge 2005 ] && DATABASE_TARGET_VER_NUM=2   #0.2.5 =>  2
+[ $OPENMANO_VER_NUM -ge 3003 ] && DATABASE_TARGET_VER_NUM=3   #0.3.3 =>  3
+[ $OPENMANO_VER_NUM -ge 3005 ] && DATABASE_TARGET_VER_NUM=4   #0.3.5 =>  4
+[ $OPENMANO_VER_NUM -ge 4001 ] && DATABASE_TARGET_VER_NUM=5   #0.4.1 =>  5
+[ $OPENMANO_VER_NUM -ge 4002 ] && DATABASE_TARGET_VER_NUM=6   #0.4.2 =>  6
+[ $OPENMANO_VER_NUM -ge 4003 ] && DATABASE_TARGET_VER_NUM=7   #0.4.3 =>  7
+[ $OPENMANO_VER_NUM -ge 4032 ] && DATABASE_TARGET_VER_NUM=8   #0.4.32=>  8
+[ $OPENMANO_VER_NUM -ge 4033 ] && DATABASE_TARGET_VER_NUM=9   #0.4.33=>  9
+[ $OPENMANO_VER_NUM -ge 4036 ] && DATABASE_TARGET_VER_NUM=10  #0.4.36=>  10
+#TODO ... put next versions here
+
+
+function upgrade_to_1(){
+    echo "    upgrade database from version 0.0 to version 0.1"
+    echo "      CREATE TABLE \`schema_version\`"
+    echo "CREATE TABLE \`schema_version\` (
+       \`version_int\` INT NOT NULL COMMENT 'version as a number. Must not contain gaps',
+       \`version\` VARCHAR(20) NOT NULL COMMENT 'version as a text',
+       \`openmano_ver\` VARCHAR(20) NOT NULL COMMENT 'openmano version',
+       \`comments\` VARCHAR(2000) NULL COMMENT 'changes to database',
+       \`date\` DATE NULL,
+       PRIMARY KEY (\`version_int\`)
+       )
+       COMMENT='database schema control version'
+       COLLATE='utf8_general_ci'
+       ENGINE=InnoDB;" | $DBCMD  || ! echo "ERROR. Aborted!" || exit -1
+    echo "INSERT INTO \`schema_version\` (\`version_int\`, \`version\`, \`openmano_ver\`, \`comments\`, \`date\`)
+        VALUES (1, '0.1', '0.2.2', 'insert schema_version', '2015-05-08');" | $DBCMD
+}
+function downgrade_from_1(){
+    echo "    downgrade database from version 0.1 to version 0.0"
+    echo "      DROP TABLE \`schema_version\`"
+    echo "DROP TABLE \`schema_version\`;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+function upgrade_to_2(){
+    echo "    upgrade database from version 0.1 to version 0.2"
+    echo "      Add columns user/passwd to table 'vim_tenants'"
+    echo "ALTER TABLE vim_tenants ADD COLUMN user VARCHAR(36) NULL COMMENT 'Credentials for vim' AFTER created,
+       ADD COLUMN passwd VARCHAR(50) NULL COMMENT 'Credentials for vim' AFTER user;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Add table 'images' and 'datacenters_images'"
+    echo "CREATE TABLE images (
+       uuid VARCHAR(36) NOT NULL,
+       name VARCHAR(50) NOT NULL,
+       location VARCHAR(200) NOT NULL,
+       description VARCHAR(100) NULL,
+       metadata VARCHAR(400) NULL,
+       PRIMARY KEY (uuid),
+       UNIQUE INDEX location (location)  )
+        COLLATE='utf8_general_ci'
+        ENGINE=InnoDB;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "CREATE TABLE datacenters_images (
+       id INT NOT NULL AUTO_INCREMENT,
+       image_id VARCHAR(36) NOT NULL,
+       datacenter_id VARCHAR(36) NOT NULL,
+       vim_id VARCHAR(36) NOT NULL,
+       PRIMARY KEY (id),
+       CONSTRAINT FK__images FOREIGN KEY (image_id) REFERENCES images (uuid) ON UPDATE CASCADE ON DELETE CASCADE,
+       CONSTRAINT FK__datacenters_i FOREIGN KEY (datacenter_id) REFERENCES datacenters (uuid) ON UPDATE CASCADE ON DELETE CASCADE  )
+        COLLATE='utf8_general_ci'
+        ENGINE=InnoDB;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      migrate data from table 'vms' into 'images'"
+    echo "INSERT INTO images (uuid, name, location) SELECT DISTINCT vim_image_id, vim_image_id, image_path FROM vms;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "INSERT INTO datacenters_images (image_id, datacenter_id, vim_id)
+          SELECT DISTINCT vim_image_id, datacenters.uuid, vim_image_id FROM vms JOIN datacenters;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Add table 'flavors' and 'datacenter_flavors'"
+    echo "CREATE TABLE flavors (
+       uuid VARCHAR(36) NOT NULL,
+       name VARCHAR(50) NOT NULL,
+       description VARCHAR(100) NULL,
+       disk SMALLINT(5) UNSIGNED NULL DEFAULT NULL,
+       ram SMALLINT(5) UNSIGNED NULL DEFAULT NULL,
+       vcpus SMALLINT(5) UNSIGNED NULL DEFAULT NULL,
+       extended VARCHAR(2000) NULL DEFAULT NULL COMMENT 'Extra description json format of needed resources and pining, orginized in sets per numa',
+       PRIMARY KEY (uuid)  )
+        COLLATE='utf8_general_ci'
+        ENGINE=InnoDB;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "CREATE TABLE datacenters_flavors (
+       id INT NOT NULL AUTO_INCREMENT,
+       flavor_id VARCHAR(36) NOT NULL,
+       datacenter_id VARCHAR(36) NOT NULL,
+       vim_id VARCHAR(36) NOT NULL,
+       PRIMARY KEY (id),
+       CONSTRAINT FK__flavors FOREIGN KEY (flavor_id) REFERENCES flavors (uuid) ON UPDATE CASCADE ON DELETE CASCADE,
+       CONSTRAINT FK__datacenters_f FOREIGN KEY (datacenter_id) REFERENCES datacenters (uuid) ON UPDATE CASCADE ON DELETE CASCADE  )
+        COLLATE='utf8_general_ci'
+        ENGINE=InnoDB;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      migrate data from table 'vms' into 'flavors'"
+    echo "INSERT INTO flavors (uuid, name) SELECT DISTINCT vim_flavor_id, vim_flavor_id FROM vms;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "INSERT INTO datacenters_flavors (flavor_id, datacenter_id, vim_id)
+          SELECT DISTINCT vim_flavor_id, datacenters.uuid, vim_flavor_id FROM vms JOIN datacenters;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vms ALTER vim_flavor_id DROP DEFAULT, ALTER vim_image_id DROP DEFAULT;
+          ALTER TABLE vms CHANGE COLUMN vim_flavor_id flavor_id VARCHAR(36) NOT NULL COMMENT 'Link to flavor table' AFTER vnf_id,
+          CHANGE COLUMN vim_image_id image_id VARCHAR(36) NOT NULL COMMENT 'Link to image table' AFTER flavor_id, 
+          ADD CONSTRAINT FK_vms_images  FOREIGN KEY (image_id) REFERENCES  images (uuid),
+          ADD CONSTRAINT FK_vms_flavors FOREIGN KEY (flavor_id) REFERENCES flavors (uuid);
+         " | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "INSERT INTO schema_version (version_int, version, openmano_ver, comments, date) VALUES (2, '0.2', '0.2.5', 'new tables images,flavors', '2015-07-13');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+
+}   
+     
+function downgrade_from_2(){
+    echo "    downgrade database from version 0.2 to version 0.1"
+    echo "       migrate back data from 'datacenters_images' 'datacenters_flavors' into 'vms'"
+    echo "ALTER TABLE vms ALTER image_id DROP DEFAULT, ALTER flavor_id DROP DEFAULT;
+          ALTER TABLE vms CHANGE COLUMN flavor_id vim_flavor_id VARCHAR(36) NOT NULL COMMENT 'Flavor ID in the VIM DB' AFTER vnf_id,
+          CHANGE COLUMN image_id vim_image_id VARCHAR(36) NOT NULL COMMENT 'Image ID in the VIM DB' AFTER vim_flavor_id,
+          DROP FOREIGN KEY FK_vms_flavors, DROP INDEX FK_vms_flavors,
+          DROP FOREIGN KEY FK_vms_images, DROP INDEX FK_vms_images;
+         " | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+#    echo "UPDATE v SET v.vim_image_id=di.vim_id
+#          FROM  vms as v INNER JOIN images as i ON v.vim_image_id=i.uuid 
+#          INNER JOIN datacenters_images as di ON i.uuid=di.image_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Delete columns 'user/passwd' from 'vim_tenants'"
+    echo "ALTER TABLE vim_tenants DROP COLUMN user, DROP COLUMN passwd; " | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "        delete tables 'datacenter_images', 'images'"
+    echo "DROP TABLE \`datacenters_images\`;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "DROP TABLE \`images\`;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "        delete tables 'datacenter_flavors', 'flavors'"
+    echo "DROP TABLE \`datacenters_flavors\`;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "DROP TABLE \`flavors\`;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "DELETE FROM schema_version WHERE version_int='2';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+function upgrade_to_3(){
+    echo "    upgrade database from version 0.2 to version 0.3"
+    echo "      Change table 'logs', 'uuids"
+    echo "ALTER TABLE logs CHANGE COLUMN related related VARCHAR(36) NOT NULL COMMENT 'Relevant element for the log' AFTER nfvo_tenant_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE uuids CHANGE COLUMN used_at used_at VARCHAR(36) NULL DEFAULT NULL COMMENT 'Table that uses this UUID' AFTER created_at;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Add column created to table 'datacenters_images' and 'datacenters_flavors'"
+    for table in datacenters_images datacenters_flavors
+    do
+        echo "ALTER TABLE $table ADD COLUMN created ENUM('true','false') NOT NULL DEFAULT 'false' 
+            COMMENT 'Indicates if it has been created by openmano, or already existed' AFTER vim_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done
+    echo "ALTER TABLE images CHANGE COLUMN metadata metadata VARCHAR(2000) NULL DEFAULT NULL AFTER description;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Allow null to column 'vim_interface_id' in 'instance_interfaces'"
+    echo "ALTER TABLE instance_interfaces CHANGE COLUMN vim_interface_id vim_interface_id VARCHAR(36) NULL DEFAULT NULL COMMENT 'vim identity for that interface' AFTER interface_id; " | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Add column config to table 'datacenters'"
+    echo "ALTER TABLE datacenters ADD COLUMN config VARCHAR(4000) NULL DEFAULT NULL COMMENT 'extra config information in json' AFTER vim_url_admin;
+       " | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Add column datacenter_id to table 'vim_tenants'"
+    echo "ALTER TABLE vim_tenants ADD COLUMN datacenter_id VARCHAR(36) NULL COMMENT 'Datacenter of this tenant' AFTER uuid,
+       DROP INDEX name, DROP INDEX vim_tenant_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vim_tenants CHANGE COLUMN name vim_tenant_name VARCHAR(36) NULL DEFAULT NULL COMMENT 'tenant name at VIM' AFTER datacenter_id,
+       CHANGE COLUMN vim_tenant_id vim_tenant_id VARCHAR(36) NULL DEFAULT NULL COMMENT 'Tenant ID at VIM' AFTER vim_tenant_name;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "UPDATE vim_tenants as vt LEFT JOIN tenants_datacenters as td ON vt.uuid=td.vim_tenant_id
+       SET vt.datacenter_id=td.datacenter_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "DELETE FROM vim_tenants WHERE datacenter_id is NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vim_tenants ALTER datacenter_id DROP DEFAULT;
+       ALTER TABLE vim_tenants
+       CHANGE COLUMN datacenter_id datacenter_id VARCHAR(36) NOT NULL COMMENT 'Datacenter of this tenant' AFTER uuid;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vim_tenants ADD CONSTRAINT FK_vim_tenants_datacenters FOREIGN KEY (datacenter_id) REFERENCES datacenters (uuid)
+       ON UPDATE CASCADE ON DELETE CASCADE;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+
+    echo "INSERT INTO schema_version (version_int, version, openmano_ver, comments, date) VALUES (3, '0.3', '0.3.3', 'alter vim_tenant tables', '2015-07-28');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+
+function downgrade_from_3(){
+    echo "    downgrade database from version 0.3 to version 0.2"
+    echo "      Change back table 'logs', 'uuids'"
+    echo "ALTER TABLE logs CHANGE COLUMN related related ENUM('nfvo_tenants','datacenters','vim_tenants','tenants_datacenters','vnfs','vms','interfaces','nets','scenarios','sce_vnfs','sce_interfaces','sce_nets','instance_scenarios','instance_vnfs','instance_vms','instance_nets','instance_interfaces') NOT NULL COMMENT 'Relevant element for the log' AFTER nfvo_tenant_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE uuids CHANGE COLUMN used_at used_at ENUM('nfvo_tenants','datacenters','vim_tenants','vnfs','vms','interfaces','nets','scenarios','sce_vnfs','sce_interfaces','sce_nets','instance_scenarios','instance_vnfs','instance_vms','instance_nets','instance_interfaces') NULL DEFAULT NULL COMMENT 'Table that uses this UUID' AFTER created_at;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Delete column created from table 'datacenters_images' and 'datacenters_flavors'"
+    for table in datacenters_images datacenters_flavors
+    do
+        echo "ALTER TABLE $table DROP COLUMN created;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done
+    echo "ALTER TABLE images CHANGE COLUMN metadata metadata VARCHAR(400) NULL DEFAULT NULL AFTER description;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Deny back null to column 'vim_interface_id' in 'instance_interfaces'"
+    echo "ALTER TABLE instance_interfaces CHANGE COLUMN vim_interface_id vim_interface_id VARCHAR(36) NOT NULL COMMENT 'vim identity for that interface' AFTER interface_id; " | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "       Delete column config to table 'datacenters'"
+    echo "ALTER TABLE datacenters DROP COLUMN config;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "       Delete column datacenter_id to table 'vim_tenants'"
+    echo "ALTER TABLE vim_tenants DROP COLUMN datacenter_id, DROP FOREIGN KEY FK_vim_tenants_datacenters;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vim_tenants CHANGE COLUMN vim_tenant_name name VARCHAR(36) NULL DEFAULT NULL COMMENT '' AFTER uuid"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vim_tenants ALTER name DROP DEFAULT;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vim_tenants CHANGE COLUMN name name VARCHAR(36) NOT NULL AFTER uuid"| $DBCMD || ! echo "Warning changing column name at vim_tenants!"
+    echo "ALTER TABLE vim_tenants ADD UNIQUE INDEX name (name);" | $DBCMD || ! echo "Warning add unique index name at vim_tenants!"
+    echo "ALTER TABLE vim_tenants ALTER vim_tenant_id DROP DEFAULT;"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vim_tenants CHANGE COLUMN vim_tenant_id vim_tenant_id VARCHAR(36) NOT NULL COMMENT 'Tenant ID in the VIM DB' AFTER name;"| $DBCMD || ! echo "Warning changing column vim_tenant_id at vim_tenants!"
+    echo "ALTER TABLE vim_tenants ADD UNIQUE INDEX vim_tenant_id (vim_tenant_id);" | $DBCMD || ! echo "Warning add unique index vim_tenant_id at vim_tenants!"
+    echo "DELETE FROM schema_version WHERE version_int='3';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+function upgrade_to_4(){
+    echo "    upgrade database from version 0.3 to version 0.4"
+    echo "      Enlarge graph field at tables 'sce_vnfs', 'sce_nets'"
+    for table in sce_vnfs sce_nets
+    do
+        echo "ALTER TABLE $table CHANGE COLUMN graph graph VARCHAR(2000) NULL DEFAULT NULL AFTER modified_at;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done
+    echo "ALTER TABLE datacenters CHANGE COLUMN type type VARCHAR(36) NOT NULL DEFAULT 'openvim' AFTER description;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "INSERT INTO schema_version (version_int, version, openmano_ver, comments, date) VALUES (4, '0.4', '0.3.5', 'enlarge graph field at sce_vnfs/nets', '2015-10-20');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+function downgrade_from_4(){
+    echo "    downgrade database from version 0.4 to version 0.3"
+    echo "      Shorten back graph field at tables 'sce_vnfs', 'sce_nets'"
+    for table in sce_vnfs sce_nets
+    do
+        echo "ALTER TABLE $table CHANGE COLUMN graph graph VARCHAR(2000) NULL DEFAULT NULL AFTER modified_at;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done
+    echo "ALTER TABLE datacenters CHANGE COLUMN type type ENUM('openvim','openstack') NOT NULL DEFAULT 'openvim' AFTER description;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "DELETE FROM schema_version WHERE version_int='4';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+function upgrade_to_5(){
+    echo "    upgrade database from version 0.4 to version 0.5"
+    echo "      Add 'mac' field for bridge interfaces in table 'interfaces'"
+    echo "ALTER TABLE interfaces ADD COLUMN mac CHAR(18) NULL DEFAULT NULL AFTER model;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "INSERT INTO schema_version (version_int, version, openmano_ver, comments, date) VALUES (5, '0.5', '0.4.1', 'Add mac address for bridge interfaces', '2015-12-14');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+function downgrade_from_5(){
+    echo "    downgrade database from version 0.5 to version 0.4"
+    echo "      Remove 'mac' field for bridge interfaces in table 'interfaces'"
+    echo "ALTER TABLE interfaces DROP COLUMN mac;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "DELETE FROM schema_version WHERE version_int='5';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+function upgrade_to_6(){
+    echo "    upgrade database from version 0.5 to version 0.6"
+    echo "      Add 'descriptor' field text to 'vnfd', 'scenarios'"
+    echo "ALTER TABLE vnfs ADD COLUMN descriptor TEXT NULL DEFAULT NULL COMMENT 'Original text descriptor used for create the VNF' AFTER class;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE scenarios ADD COLUMN descriptor TEXT NULL DEFAULT NULL COMMENT 'Original text descriptor used for create the scenario' AFTER modified_at;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Add 'last_error', 'vim_info' to 'instance_vms', 'instance_nets'"
+    echo "ALTER TABLE instance_vms  ADD COLUMN error_msg VARCHAR(1024) NULL DEFAULT NULL AFTER status;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_vms  ADD COLUMN vim_info TEXT NULL DEFAULT NULL AFTER error_msg;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_vms  CHANGE COLUMN status status ENUM('ACTIVE','INACTIVE','BUILD','ERROR','VIM_ERROR','PAUSED','SUSPENDED','DELETED') NOT NULL DEFAULT 'BUILD' AFTER vim_vm_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets ADD COLUMN error_msg VARCHAR(1024) NULL DEFAULT NULL AFTER status;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets ADD COLUMN vim_info TEXT NULL DEFAULT NULL AFTER error_msg;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets CHANGE COLUMN status status ENUM('ACTIVE','DOWN','BUILD','ERROR','VIM_ERROR','INACTIVE','DELETED') NOT NULL DEFAULT 'BUILD' AFTER instance_scenario_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Add 'mac_address', 'ip_address', 'vim_info' to 'instance_interfaces'"
+    echo "ALTER TABLE instance_interfaces ADD COLUMN mac_address VARCHAR(32) NULL DEFAULT NULL AFTER vim_interface_id, ADD COLUMN ip_address VARCHAR(64) NULL DEFAULT NULL AFTER mac_address, ADD COLUMN vim_info TEXT NULL DEFAULT NULL AFTER ip_address;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Add 'sce_vnf_id','datacenter_id','vim_tenant_id' field to 'instance_vnfs'"
+    echo "ALTER TABLE instance_vnfs ADD COLUMN sce_vnf_id VARCHAR(36) NULL DEFAULT NULL AFTER vnf_id, ADD CONSTRAINT FK_instance_vnfs_sce_vnfs FOREIGN KEY (sce_vnf_id) REFERENCES sce_vnfs (uuid) ON UPDATE CASCADE ON DELETE SET NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_vnfs ADD COLUMN vim_tenant_id VARCHAR(36) NULL DEFAULT NULL AFTER sce_vnf_id, ADD CONSTRAINT FK_instance_vnfs_vim_tenants FOREIGN KEY (vim_tenant_id) REFERENCES vim_tenants (uuid) ON UPDATE RESTRICT ON DELETE RESTRICT;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_vnfs ADD COLUMN datacenter_id VARCHAR(36) NULL DEFAULT NULL AFTER vim_tenant_id, ADD CONSTRAINT FK_instance_vnfs_datacenters FOREIGN KEY (datacenter_id) REFERENCES datacenters (uuid) ON UPDATE RESTRICT ON DELETE RESTRICT;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Add 'sce_net_id','net_id','datacenter_id','vim_tenant_id' field to 'instance_nets'"
+    echo "ALTER TABLE instance_nets ADD COLUMN sce_net_id VARCHAR(36) NULL DEFAULT NULL AFTER instance_scenario_id, ADD CONSTRAINT FK_instance_nets_sce_nets FOREIGN KEY (sce_net_id) REFERENCES sce_nets (uuid) ON UPDATE CASCADE ON DELETE SET NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets ADD COLUMN net_id VARCHAR(36) NULL DEFAULT NULL AFTER sce_net_id, ADD CONSTRAINT FK_instance_nets_nets FOREIGN KEY (net_id) REFERENCES nets (uuid) ON UPDATE CASCADE ON DELETE SET NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets ADD COLUMN vim_tenant_id VARCHAR(36) NULL DEFAULT NULL AFTER net_id, ADD CONSTRAINT FK_instance_nets_vim_tenants FOREIGN KEY (vim_tenant_id) REFERENCES vim_tenants (uuid) ON UPDATE RESTRICT ON DELETE RESTRICT;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets ADD COLUMN datacenter_id VARCHAR(36) NULL DEFAULT NULL AFTER vim_tenant_id, ADD CONSTRAINT FK_instance_nets_datacenters FOREIGN KEY (datacenter_id) REFERENCES datacenters (uuid) ON UPDATE RESTRICT ON DELETE RESTRICT;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "INSERT INTO schema_version (version_int, version, openmano_ver, comments, date) VALUES (6, '0.6', '0.4.2', 'Adding VIM status info', '2015-12-22');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+function downgrade_from_6(){
+    echo "    downgrade database from version 0.6 to version 0.5"
+    echo "      Remove 'descriptor' field from 'vnfd', 'scenarios' tables"
+    echo "ALTER TABLE vnfs      DROP COLUMN descriptor;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE scenarios DROP COLUMN descriptor;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Remove 'last_error', 'vim_info' from 'instance_vms', 'instance_nets'"
+    echo "ALTER TABLE instance_vms  DROP COLUMN error_msg, DROP COLUMN vim_info;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_vms  CHANGE COLUMN status status ENUM('ACTIVE','PAUSED','INACTIVE','CREATING','ERROR','DELETING') NOT NULL DEFAULT 'CREATING' AFTER vim_vm_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets DROP COLUMN error_msg, DROP COLUMN vim_info;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets CHANGE COLUMN status status ENUM('ACTIVE','DOWN','BUILD','ERROR') NOT NULL DEFAULT 'BUILD' AFTER instance_scenario_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Remove 'mac_address', 'ip_address', 'vim_info' from 'instance_interfaces'"
+    echo "ALTER TABLE instance_interfaces DROP COLUMN mac_address, DROP COLUMN ip_address, DROP COLUMN vim_info;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Remove 'sce_vnf_id','datacenter_id','vim_tenant_id' field from 'instance_vnfs'"
+    echo "ALTER TABLE instance_vnfs DROP COLUMN sce_vnf_id, DROP FOREIGN KEY FK_instance_vnfs_sce_vnfs;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_vnfs DROP COLUMN vim_tenant_id, DROP FOREIGN KEY FK_instance_vnfs_vim_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_vnfs DROP COLUMN datacenter_id, DROP FOREIGN KEY FK_instance_vnfs_datacenters;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      Remove 'sce_net_id','net_id','datacenter_id','vim_tenant_id' field from 'instance_nets'"
+    echo "ALTER TABLE instance_nets DROP COLUMN sce_net_id, DROP FOREIGN KEY FK_instance_nets_sce_nets;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets DROP COLUMN net_id, DROP FOREIGN KEY FK_instance_nets_nets;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets DROP COLUMN vim_tenant_id, DROP FOREIGN KEY FK_instance_nets_vim_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_nets DROP COLUMN datacenter_id, DROP FOREIGN KEY FK_instance_nets_datacenters;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "DELETE FROM schema_version WHERE version_int='6';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+function upgrade_to_7(){
+    echo "    upgrade database from version 0.6 to version 0.7"
+    echo "      Change created_at, modified_at from timestamp to unix float at all database"
+    for table in datacenters datacenter_nets instance_nets instance_scenarios instance_vms instance_vnfs interfaces nets nfvo_tenants scenarios sce_interfaces sce_nets sce_vnfs tenants_datacenters vim_tenants vms vnfs uuids
+    do
+         echo -en "        $table               \r"
+         echo "ALTER TABLE $table ADD COLUMN created_at_ DOUBLE NOT NULL after created_at;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+         echo "UPDATE $table SET created_at_=unix_timestamp(created_at);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+         echo "ALTER TABLE $table DROP COLUMN created_at, CHANGE COLUMN created_at_ created_at DOUBLE NOT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+         [[ $table == uuids ]] || echo "ALTER TABLE $table CHANGE COLUMN modified_at modified_at DOUBLE NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done
+    
+    echo "      Add 'descriptor' field text to 'vnfd', 'scenarios'"
+    echo "INSERT INTO schema_version (version_int, version, openmano_ver, comments, date) VALUES (7, '0.7', '0.4.3', 'Changing created_at time at database', '2016-01-25');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+function downgrade_from_7(){
+    echo "    downgrade database from version 0.7 to version 0.6"
+    echo "      Change back created_at, modified_at from unix float to timestamp at all database"
+    for table in datacenters datacenter_nets instance_nets instance_scenarios instance_vms instance_vnfs interfaces nets nfvo_tenants scenarios sce_interfaces sce_nets sce_vnfs tenants_datacenters vim_tenants vms vnfs uuids
+    do
+         echo -en "        $table               \r"
+         echo "ALTER TABLE $table ADD COLUMN created_at_ DOUBLE NOT NULL after created_at;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+         echo "ALTER TABLE $table ADD COLUMN created_at_ TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP after created_at;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+         echo "UPDATE $table SET created_at_=from_unixtime(created_at);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+         echo "ALTER TABLE $table DROP COLUMN created_at, CHANGE COLUMN created_at_ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+         [[ $table == uuids ]] || echo "ALTER TABLE $table CHANGE COLUMN modified_at modified_at TIMESTAMP NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done
+    echo "      Remove 'descriptor' field from 'vnfd', 'scenarios' tables"
+    echo "DELETE FROM schema_version WHERE version_int='7';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+function upgrade_to_8(){
+    echo "    upgrade database from version 0.7 to version 0.8"
+    echo "      Change enalarge name, description to 255 at all database"
+    for table in datacenters datacenter_nets flavors images instance_scenarios nets nfvo_tenants scenarios sce_nets sce_vnfs vms vnfs
+    do
+         echo -en "        $table               \r"
+         echo "ALTER TABLE $table CHANGE COLUMN name name VARCHAR(255) NOT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+         echo "ALTER TABLE $table CHANGE COLUMN description description VARCHAR(255) NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done
+    echo -en "        interfaces           \r"
+    echo "ALTER TABLE interfaces CHANGE COLUMN internal_name internal_name VARCHAR(255) NOT NULL, CHANGE COLUMN external_name external_name VARCHAR(255) NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vim_tenants CHANGE COLUMN vim_tenant_name vim_tenant_name VARCHAR(64) NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo -en "        vim_tenants          \r"
+    echo "ALTER TABLE vim_tenants CHANGE COLUMN user user VARCHAR(64) NULL DEFAULT NULL, CHANGE COLUMN passwd passwd VARCHAR(64) NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "INSERT INTO schema_version (version_int, version, openmano_ver, comments, date) VALUES (8, '0.8', '0.4.32', 'Enlarging name at database', '2016-02-01');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+function downgrade_from_8(){
+    echo "    downgrade database from version 0.8 to version 0.7"
+    echo "      Change back name,description to shorter length at all database"
+    for table in datacenters datacenter_nets flavors images instance_scenarios nets nfvo_tenants scenarios sce_nets sce_vnfs vms vnfs
+    do
+         name_length=50
+         [[ $table == flavors ]] || [[ $table == images ]] || name_length=36 
+         echo -en "        $table               \r"
+         echo "ALTER TABLE $table CHANGE COLUMN name name VARCHAR($name_length) NOT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+         echo "ALTER TABLE $table CHANGE COLUMN description description VARCHAR(100) NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done
+    echo -en "        interfaces           \r"
+    echo "ALTER TABLE interfaces CHANGE COLUMN internal_name internal_name VARCHAR(25) NOT NULL, CHANGE COLUMN external_name external_name VARCHAR(25) NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo -en "        vim_tenants          \r"
+    echo "ALTER TABLE vim_tenants CHANGE COLUMN vim_tenant_name vim_tenant_name VARCHAR(36) NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE vim_tenants CHANGE COLUMN user user VARCHAR(36) NULL DEFAULT NULL, CHANGE COLUMN passwd passwd VARCHAR(50) NULL DEFAULT NULL;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "DELETE FROM schema_version WHERE version_int='8';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+function upgrade_to_9(){
+    echo "    upgrade database from version 0.8 to version 0.9"
+    echo "      Add more status to 'instance_vms'"
+    echo "ALTER TABLE instance_vms CHANGE COLUMN status status ENUM('ACTIVE:NoMgmtIP','ACTIVE','INACTIVE','BUILD','ERROR','VIM_ERROR','PAUSED','SUSPENDED','DELETED') NOT NULL DEFAULT 'BUILD';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "INSERT INTO schema_version (version_int, version, openmano_ver, comments, date) VALUES (9, '0.9', '0.4.33', 'Add ACTIVE:NoMgmtIP to instance_vms table', '2016-02-05');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+function downgrade_from_9(){
+    echo "    downgrade database from version 0.9 to version 0.8"
+    echo "      Add more status to 'instance_vms'"
+    echo "ALTER TABLE instance_vms CHANGE COLUMN status status ENUM('ACTIVE','INACTIVE','BUILD','ERROR','VIM_ERROR','PAUSED','SUSPENDED','DELETED') NOT NULL DEFAULT 'BUILD';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "DELETE FROM schema_version WHERE version_int='9';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+function upgrade_to_10(){
+    echo "    upgrade database from version 0.9 to version 0.10"
+    echo "      add tenant to 'vnfs'"
+    echo "ALTER TABLE vnfs ADD COLUMN tenant_id VARCHAR(36) NULL DEFAULT NULL AFTER name, ADD CONSTRAINT FK_vnfs_nfvo_tenants FOREIGN KEY (tenant_id) REFERENCES nfvo_tenants (uuid) ON UPDATE CASCADE ON DELETE SET NULL, CHANGE COLUMN public public ENUM('true','false') NOT NULL DEFAULT 'false' AFTER physical, DROP INDEX name, DROP INDEX path, DROP COLUMN path;"  | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE scenarios DROP FOREIGN KEY FK_scenarios_nfvo_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE scenarios CHANGE COLUMN nfvo_tenant_id tenant_id VARCHAR(36) NULL DEFAULT NULL after name, ADD CONSTRAINT FK_scenarios_nfvo_tenants FOREIGN KEY (tenant_id) REFERENCES nfvo_tenants (uuid);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_scenarios DROP FOREIGN KEY FK_instance_scenarios_nfvo_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_scenarios CHANGE COLUMN nfvo_tenant_id tenant_id VARCHAR(36) NULL DEFAULT NULL after name, ADD CONSTRAINT FK_instance_scenarios_nfvo_tenants FOREIGN KEY (tenant_id) REFERENCES nfvo_tenants (uuid);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      rename 'vim_tenants' table to 'datacenter_tenants'"
+    echo "RENAME TABLE vim_tenants TO datacenter_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    for table in tenants_datacenters instance_scenarios instance_vnfs instance_nets
+    do
+        NULL="NOT NULL"
+        [[ $table == instance_vnfs ]] && NULL="NULL DEFAULT NULL"
+        echo "ALTER TABLE ${table} DROP FOREIGN KEY FK_${table}_vim_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+        echo "ALTER TABLE ${table} ALTER vim_tenant_id DROP DEFAULT;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+        echo "ALTER TABLE ${table} CHANGE COLUMN vim_tenant_id datacenter_tenant_id VARCHAR(36)  ${NULL} AFTER datacenter_id, ADD CONSTRAINT FK_${table}_datacenter_tenants FOREIGN KEY (datacenter_tenant_id) REFERENCES datacenter_tenants (uuid); " | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done    
+    echo "INSERT INTO schema_version (version_int, version, openmano_ver, comments, date) VALUES (10, '0.10', '0.4.36', 'tenant management of vnfs,scenarios', '2016-03-08');" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+function downgrade_from_10(){
+    echo "    downgrade database from version 0.10 to version 0.9"
+    echo "      remove tenant from 'vnfs'"
+    echo "ALTER TABLE vnfs DROP COLUMN tenant_id, DROP FOREIGN KEY FK_vnfs_nfvo_tenants, ADD UNIQUE INDEX name (name), ADD COLUMN path VARCHAR(100) NULL DEFAULT NULL COMMENT 'Path where the YAML descriptor of the VNF can be found. NULL if it is a physical network function.' AFTER name, ADD UNIQUE INDEX path (path), CHANGE COLUMN public public ENUM('true','false') NOT NULL DEFAULT 'true' AFTER physical;"  | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE scenarios DROP FOREIGN KEY FK_scenarios_nfvo_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE scenarios CHANGE COLUMN tenant_id nfvo_tenant_id VARCHAR(36) NULL DEFAULT NULL after name, ADD CONSTRAINT FK_scenarios_nfvo_tenants FOREIGN KEY (nfvo_tenant_id) REFERENCES nfvo_tenants (uuid);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_scenarios DROP FOREIGN KEY FK_instance_scenarios_nfvo_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "ALTER TABLE instance_scenarios CHANGE COLUMN tenant_id nfvo_tenant_id VARCHAR(36) NULL DEFAULT NULL after name, ADD CONSTRAINT FK_instance_scenarios_nfvo_tenants FOREIGN KEY (nfvo_tenant_id) REFERENCES nfvo_tenants (uuid);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    echo "      rename back 'datacenter_tenants' table to 'vim_tenants'"
+    echo "RENAME TABLE datacenter_tenants TO vim_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    for table in tenants_datacenters instance_scenarios instance_vnfs instance_nets
+    do
+        echo "ALTER TABLE ${table} DROP FOREIGN KEY FK_${table}_datacenter_tenants;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+        NULL="NOT NULL"
+        [[ $table == instance_vnfs ]] && NULL="NULL DEFAULT NULL"
+        echo "ALTER TABLE ${table} ALTER datacenter_tenant_id DROP DEFAULT;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+        echo "ALTER TABLE ${table} CHANGE COLUMN datacenter_tenant_id vim_tenant_id VARCHAR(36) $NULL AFTER datacenter_id, ADD CONSTRAINT FK_${table}_vim_tenants FOREIGN KEY (vim_tenant_id) REFERENCES vim_tenants (uuid); " | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+    done    
+    echo "DELETE FROM schema_version WHERE version_int='10';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+
+function upgrade_to_X(){
+    echo "      change 'datacenter_nets'"
+    echo "ALTER TABLE datacenter_nets ADD COLUMN vim_tenant_id VARCHAR(36) NOT NULL AFTER datacenter_id, DROP INDEX name_datacenter_id, ADD UNIQUE INDEX name_datacenter_id (name, datacenter_id, vim_tenant_id);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+function downgrade_from_X(){
+    echo "      Change back 'datacenter_nets'"
+    echo "ALTER TABLE datacenter_nets DROP COLUMN vim_tenant_id, DROP INDEX name_datacenter_id, ADD UNIQUE INDEX name_datacenter_id (name, datacenter_id);" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1
+}
+#TODO ... put funtions here
+
+
+[ $DATABASE_TARGET_VER_NUM -eq $DATABASE_VER_NUM ] && echo "    current database version $DATABASE_VER is ok"
+#UPGRADE DATABASE step by step
+while [ $DATABASE_TARGET_VER_NUM -gt $DATABASE_VER_NUM ]
+do
+    DATABASE_VER_NUM=$((DATABASE_VER_NUM+1))
+    upgrade_to_${DATABASE_VER_NUM}
+    #FILE_="${DIRNAME}/upgrade_to_${DATABASE_VER_NUM}.sh"
+    #[ ! -x "$FILE_" ] && echo "Error, can not find script '$FILE_' to upgrade" >&2 && exit -1
+    #$FILE_ || exit -1  # if fail return
+done
+
+#DOWNGRADE DATABASE step by step
+while [ $DATABASE_TARGET_VER_NUM -lt $DATABASE_VER_NUM ]
+do
+    #FILE_="${DIRNAME}/downgrade_from_${DATABASE_VER_NUM}.sh"
+    #[ ! -x "$FILE_" ] && echo "Error, can not find script '$FILE_' to downgrade" >&2 && exit -1
+    #$FILE_ || exit -1  # if fail return
+    downgrade_from_${DATABASE_VER_NUM}
+    DATABASE_VER_NUM=$((DATABASE_VER_NUM-1))
+done
+
+#echo done
+
diff --git a/httpserver.py b/httpserver.py
new file mode 100755 (executable)
index 0000000..29af42d
--- /dev/null
@@ -0,0 +1,1107 @@
+# -*- 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
+##
+
+'''
+HTTP server implementing the openmano API. It will answer to POST, PUT, GET methods in the appropriate URLs
+and will use the nfvo.py module to run the appropriate method.
+Every YAML/JSON file is checked against a schema in openmano_schemas.py module.  
+'''
+__author__="Alfonso Tierno, Gerardo Garcia"
+__date__ ="$17-sep-2014 09:07:15$"
+
+import bottle
+import yaml
+import json
+import threading
+import time
+
+from jsonschema import validate as js_v, exceptions as js_e
+from openmano_schemas import vnfd_schema_v01, vnfd_schema_v02, \
+                            nsd_schema_v01, nsd_schema_v02, scenario_edit_schema, \
+                            scenario_action_schema, instance_scenario_action_schema, instance_scenario_create_schema, \
+                            tenant_schema, tenant_edit_schema,\
+                            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
+
+global mydb
+global url_base
+url_base="/openmano"
+
+HTTP_Bad_Request =          400
+HTTP_Unauthorized =         401 
+HTTP_Not_Found =            404 
+HTTP_Forbidden =            403
+HTTP_Method_Not_Allowed =   405 
+HTTP_Not_Acceptable =       406
+HTTP_Service_Unavailable =  503 
+HTTP_Internal_Server_Error= 500 
+
+def delete_nulls(var):
+    if type(var) is dict:
+        for k in var.keys():
+            if var[k] is None: del var[k]
+            elif type(var[k]) is dict or type(var[k]) is list or type(var[k]) is tuple: 
+                if delete_nulls(var[k]): del var[k]
+        if len(var) == 0: return True
+    elif type(var) is list or type(var) is tuple:
+        for k in var:
+            if type(k) is dict: delete_nulls(k)
+        if len(var) == 0: return True
+    return False
+
+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 float and k in ("created_at", "modified_at"):
+                var[k] = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(v) )
+            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)
+
+
+class httpserver(threading.Thread):
+    def __init__(self, db, admin=False, host='localhost', port=9090):
+        #global url_base
+        global mydb
+        #initialization
+        threading.Thread.__init__(self)
+        self.host = host
+        self.port = port   #Port where the listen service must be started
+        if admin==True:
+            self.name = "http_admin"
+        else:
+            self.name = "http"
+            #self.url_preffix = 'http://' + host + ':' + str(port) + url_base
+            mydb = db
+        #self.first_usable_connection_index = 10
+        #self.next_connection_index = self.first_usable_connection_index #The next connection index to be used 
+        #Ensure that when the main program exits the thread will also exit
+        self.daemon = True
+        self.setDaemon(True)
+         
+    def run(self):
+        bottle.run(host=self.host, port=self.port, debug=True) #quiet=True
+           
+def run_bottle(db, host_='localhost', port_=9090):
+    '''used for launching in main thread, so that it can be debugged'''
+    global mydb
+    mydb = db
+    bottle.run(host=host_, port=port_, debug=True) #quiet=True
+    
+
+@bottle.route(url_base + '/', method='GET')
+def http_get():
+    print 
+    return 'works' #TODO: to be completed
+
+#
+# Util functions
+#
+
+def change_keys_http2db(data, http_db, reverse=False):
+    '''Change keys of dictionary data acording to the key_dict values
+    This allow change from http interface names to database names.
+    When reverse is True, the change is otherwise
+    Attributes:
+        data: can be a dictionary or a list
+        http_db: is a dictionary with hhtp names as keys and database names as value
+        reverse: by default change is done from http api to database. If True change is done otherwise
+    Return: None, but data is modified'''
+    if type(data) is tuple or type(data) is list:
+        for d in data:
+            change_keys_http2db(d, http_db, reverse)
+    elif type(data) is dict or type(data) is bottle.FormsDict:
+        if reverse:
+            for k,v in http_db.items():
+                if v in data: data[k]=data.pop(v)
+        else:
+            for k,v in http_db.items():
+                if k in data: data[v]=data.pop(k)
+
+def format_out(data):
+    '''return string of dictionary data according to requested json, yaml, xml. By default json'''
+    if 'application/yaml' in bottle.request.headers.get('Accept'):
+        bottle.response.content_type='application/yaml'
+        print yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False, tags=False, encoding='utf-8', allow_unicode=True) 
+        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='"'
+    else: #by default json
+        bottle.response.content_type='application/json'
+        #return data #json no style
+        return json.dumps(data, indent=4) + "\n"
+
+def format_in(default_schema, version_fields=None, version_dict_schema=None):
+    ''' Parse the content of HTTP request against a json_schema
+        Parameters
+            default_schema: The schema to be parsed by default if no version field is found in the client data
+            version_fields: If provided it contains a tuple or list with the fields to iterate across the client data to obtain the version
+            version_dict_schema: It contains a dictionary with the version as key, and json schema to apply as value
+                It can contain a None as key, and this is apply if the client data version does not match any key 
+        Return:
+            user_data, used_schema: if the data is successfully decoded and matches the schema
+            launch a bottle abort if fails
+    '''
+    #print "HEADERS :" + str(bottle.request.headers.items())
+    try:
+        error_text = "Invalid header format "
+        format_type = bottle.request.headers.get('Content-Type', 'application/json')
+        if 'application/json' in format_type:
+            error_text = "Invalid json format "
+            #Use the json decoder instead of bottle decoder because it informs about the location of error formats with a ValueError exception
+            client_data = json.load(bottle.request.body)
+            #client_data = bottle.request.json()
+        elif 'application/yaml' in format_type:
+            error_text = "Invalid yaml format "
+            client_data = yaml.load(bottle.request.body)
+        elif 'application/xml' in format_type:
+            bottle.abort(501, "Content-Type: application/xml not supported yet.")
+        else:
+            print 'Content-Type ' + str(format_type) + ' not supported.'
+            bottle.abort(HTTP_Not_Acceptable, 'Content-Type ' + str(format_type) + ' not supported.')
+            return
+        #if client_data == None:
+        #    bottle.abort(HTTP_Bad_Request, "Content error, empty")
+        #    return
+
+        #look for the client provider version
+        error_text = "Invalid content "
+        client_version = None
+        used_schema = None
+        if version_fields != None:
+            client_version = client_data
+            for field in version_fields:
+                if field in client_version:
+                    client_version = client_version[field]
+                else:
+                    client_version=None
+                    break
+        if client_version==None:
+            used_schema=default_schema
+        elif version_dict_schema!=None:
+            if client_version in version_dict_schema:
+                used_schema = version_dict_schema[client_version]
+            elif None in version_dict_schema:
+                used_schema = version_dict_schema[None]
+        if used_schema==None:
+            bottle.abort(HTTP_Bad_Request, "Invalid schema version or missing version field")
+            
+        js_v(client_data, used_schema)
+        return client_data, used_schema
+    except (ValueError, yaml.YAMLError) as exc:
+        error_text += str(exc)
+        print error_text 
+        bottle.abort(HTTP_Bad_Request, error_text)
+    except js_e.ValidationError as exc:
+        print "validate_in error, jsonschema exception ", exc.message, "at", exc.path
+        error_pos = ""
+        if len(exc.path)>0: error_pos=" at " + ":".join(map(json.dumps, exc.path))
+        bottle.abort(HTTP_Bad_Request, error_text + exc.message + error_pos)
+    #except:
+    #    bottle.abort(HTTP_Bad_Request, "Content error: Failed to parse Content-Type",  error_pos)
+    #    raise
+
+def filter_query_string(qs, http2db, allowed):
+    '''Process query string (qs) checking that contains only valid tokens for avoiding SQL injection
+    Attributes:
+        'qs': bottle.FormsDict variable to be processed. None or empty is considered valid
+        'http2db': dictionary with change from http API naming (dictionary key) to database naming(dictionary value)
+        'allowed': list of allowed string tokens (API http naming). All the keys of 'qs' must be one of 'allowed'
+    Return: A tuple with the (select,where,limit) to be use in a database query. All of then transformed to the database naming
+        select: list of items to retrieve, filtered by query string 'field=token'. If no 'field' is present, allowed list is returned
+        where: dictionary with key, value, taken from the query string token=value. Empty if nothing is provided
+        limit: limit dictated by user with the query string 'limit'. 100 by default
+    abort if not permited, using bottel.abort
+    '''
+    where={}
+    limit=100
+    select=[]
+    if type(qs) is not bottle.FormsDict:
+        print '!!!!!!!!!!!!!!invalid query string not a dictionary'
+        #bottle.abort(HTTP_Internal_Server_Error, "call programmer")
+    else:
+        for k in qs:
+            if k=='field':
+                select += qs.getall(k)
+                for v in select:
+                    if v not in allowed:
+                        bottle.abort(HTTP_Bad_Request, "Invalid query string at 'field="+v+"'")
+            elif k=='limit':
+                try:
+                    limit=int(qs[k])
+                except:
+                    bottle.abort(HTTP_Bad_Request, "Invalid query string at 'limit="+qs[k]+"'")
+            else:
+                if k not in allowed:
+                    bottle.abort(HTTP_Bad_Request, "Invalid query string at '"+k+"="+qs[k]+"'")
+                if qs[k]!="null":  where[k]=qs[k]
+                else: where[k]=None 
+    if len(select)==0: select += allowed
+    #change from http api to database naming
+    for i in range(0,len(select)):
+        k=select[i]
+        if http2db and k in http2db: 
+            select[i] = http2db[k]
+    if http2db:
+        change_keys_http2db(where, http2db)
+    print "filter_query_string", select,where,limit
+    
+    return select,where,limit
+
+@bottle.hook('after_request')
+def enable_cors():
+    '''Don't know yet if really needed. Keep it just in case'''
+    bottle.response.headers['Access-Control-Allow-Origin'] = '*'
+
+#
+# VNFs
+#
+
+@bottle.route(url_base + '/tenants', method='GET')
+def http_get_tenants():
+    select_,where_,limit_ = filter_query_string(bottle.request.query, None,
+            ('uuid','name','description','created_at') )
+    result, content = mydb.get_table(FROM='nfvo_tenants', SELECT=select_,WHERE=where_,LIMIT=limit_)
+    if result < 0:
+        print "http_get_tenants Error", content
+        bottle.abort(-result, content)
+    else:
+        #change_keys_http2db(content, http2db_tenant, reverse=True)
+        convert_datetime2str(content)
+        data={'tenants' : content}
+        return format_out(data)
+
+@bottle.route(url_base + '/tenants/<tenant_id>', method='GET')
+def http_get_tenant_id(tenant_id):
+    '''get tenant details, can use both uuid or name'''
+    #obtain data
+    result, content = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id, "tenant") 
+    if result < 0:
+        print "http_get_tenant_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+    #change_keys_http2db(content, http2db_tenant, reverse=True)
+    convert_datetime2str(content)
+    print content
+    data={'tenant' : content}
+    return format_out(data)
+
+@bottle.route(url_base + '/tenants', method='POST')
+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)
+    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:
+        print "http_post_tenants error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        return http_get_tenant_id(data)
+
+@bottle.route(url_base + '/tenants/<tenant_id>', method='PUT')
+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)
+    if r is not None: print "http_edit_tenant_id: Warning: remove extra items ", r
+    
+    #obtain data, check that only one exist
+    result, content = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
+    if result < 0:
+        print "http_edit_tenant_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+    
+    #edit data 
+    tenant_id = content['uuid']
+    where={'uuid': content['uuid']}
+    result, content = mydb.update_rows('nfvo_tenants', http_content['tenant'], where)
+    if result < 0:
+        print "http_edit_tenant_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+
+    return http_get_tenant_id(tenant_id)
+
+@bottle.route(url_base + '/tenants/<tenant_id>', method='DELETE')
+def http_delete_tenant_id(tenant_id):
+    '''delete a tenant from database, can use both uuid or name'''
+    
+    result, data = nfvo.delete_tenant(mydb, tenant_id)
+    if result < 0:
+        print "http_delete_tenant_id error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        #print json.dumps(data, indent=4)
+        return format_out({"result":"tenant " + data + " deleted"})
+    
+
+@bottle.route(url_base + '/<tenant_id>/datacenters', method='GET')
+def http_get_datacenters(tenant_id):
+    #check valid tenant_id
+    if tenant_id != 'any':
+        if not nfvo.check_tenant(mydb, tenant_id): 
+            print 'httpserver.http_get_datacenters () tenant %s not found' % tenant_id
+            bottle.abort(HTTP_Not_Found, 'Tenant %s not found' % tenant_id)
+            return
+    select_,where_,limit_ = filter_query_string(bottle.request.query, None,
+            ('uuid','name','vim_url','type','created_at') )
+    if tenant_id != 'any':
+        where_['nfvo_tenant_id'] = tenant_id
+        if 'created_at' in select_:
+            select_[ select_.index('created_at') ] = 'd.created_at as created_at'
+        if 'created_at' in where_:
+            where_['d.created_at'] = where_.pop('created_at')
+        result, content = mydb.get_table(FROM='datacenters as d join tenants_datacenters as td on d.uuid=td.datacenter_id',
+                                      SELECT=select_,WHERE=where_,LIMIT=limit_)
+    else:
+        result, content = mydb.get_table(FROM='datacenters',
+                                      SELECT=select_,WHERE=where_,LIMIT=limit_)
+    if result < 0:
+        print "http_get_datacenters Error", content
+        bottle.abort(-result, content)
+    else:
+        #change_keys_http2db(content, http2db_tenant, reverse=True)
+        convert_datetime2str(content)
+        data={'datacenters' : content}
+        return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>', method='GET')
+def http_get_datacenter_id(tenant_id, datacenter_id):
+    '''get datacenter details, can use both uuid or name'''
+    #check valid tenant_id
+    if tenant_id != 'any':
+        if not nfvo.check_tenant(mydb, tenant_id): 
+            print 'httpserver.http_get_datacenter_id () tenant %s not found' % tenant_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'
+    where_={}
+    where_[what] = datacenter_id
+    select_=['uuid', 'name','vim_url', 'vim_url_admin', 'type', 'config', 'description', 'd.created_at as created_at']
+    if tenant_id != 'any':
+        select_.append("datacenter_tenant_id")
+        where_['td.nfvo_tenant_id']= tenant_id
+        from_='datacenters as d join tenants_datacenters as td on d.uuid=td.datacenter_id'
+    else:
+        from_='datacenters as d'
+    result, content = mydb.get_table(
+                SELECT=select_,
+                FROM=from_,
+                WHERE=where_)
+
+    if result < 0:
+        print "http_get_datacenter_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+    elif result==0:
+        bottle.abort( HTTP_Not_Found, "No datacenter found for tenant with %s '%s'" %(what, datacenter_id) )
+    elif result>1: 
+        bottle.abort( HTTP_Bad_Request, "More than one datacenter found for tenant with %s '%s'" %(what, datacenter_id) )
+
+    if tenant_id != 'any':
+        #get vim tenant info
+        result, content2 = mydb.get_table(
+                SELECT=("vim_tenant_name", "vim_tenant_id", "user"),
+                FROM="datacenter_tenants",
+                WHERE={"uuid": content[0]["datacenter_tenant_id"]},
+                ORDER_BY=("created", ) )
+        del content[0]["datacenter_tenant_id"]
+        if result < 0:
+            print "http_get_datacenter_id vim_tenant_info error %d %s" % (result, content2)
+            bottle.abort(-result, content2)
+        content[0]["vim_tenants"] = content2
+
+    print content
+    if content[0]['config'] != None:
+        try:
+            config_dict = yaml.load(content[0]['config'])
+            content[0]['config'] = config_dict
+        except Exception, e:
+            print "Exception '%s' while trying to load config information" % str(e)
+    #change_keys_http2db(content, http2db_datacenter, reverse=True)
+    convert_datetime2str(content[0])
+    data={'datacenter' : content[0]}
+    return format_out(data)
+
+@bottle.route(url_base + '/datacenters', method='POST')
+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)
+    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:
+        print "http_post_datacenters error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        return http_get_datacenter_id('any', data)
+
+@bottle.route(url_base + '/datacenters/<datacenter_id_name>', method='PUT')
+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)
+    if r is not None: print "http_edit_datacenter_id: Warning: remove extra items ", r
+    
+    
+    result, datacenter_id = nfvo.edit_datacenter(mydb, datacenter_id_name, http_content['datacenter'])
+    if result < 0:
+        print "http_edit_datacenter_id error %d %s" % (-result, datacenter_id)
+        bottle.abort(-result, datacenter_id)
+    else:
+        return http_get_datacenter_id('any', datacenter_id)
+
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>/networks', method='GET')  #deprecated
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>/netmaps', method='GET')
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>/netmaps/<netmap_id>', method='GET')
+def http_getnetmap_datacenter_id(tenant_id, datacenter_id, netmap_id=None):
+    '''get datacenter networks, can use both uuid or name'''
+    #obtain data
+    result, datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter_id, "datacenter") 
+    if result < 0:
+        print "http_getnetwork_datacenter_id error %d %s" % (result, datacenter_dict)
+        bottle.abort(-result, datacenter_dict)
+    where_= {"datacenter_id":datacenter_dict['uuid']}
+    if netmap_id:
+        if af.check_valid_uuid(netmap_id):
+            where_["uuid"] = netmap_id
+        else:
+            where_["name"] = netmap_id
+    result, content =mydb.get_table(FROM='datacenter_nets',
+                                    SELECT=('name','vim_net_id as vim_id', 'uuid', 'type','multipoint','shared','description', 'created_at'),
+                                    WHERE=where_ ) 
+    if result < 0:
+        print "http_getnetwork_datacenter_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+
+    convert_datetime2str(content)
+    af.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(HTTP_Not_Found, "No netmap found with " + " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), where_.iteritems())) )
+        return 
+    else:
+        data={'netmaps' : content}
+    return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>/netmaps', method='DELETE')
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>/netmaps/<netmap_id>', method='DELETE')
+def http_delnetmap_datacenter_id(tenant_id, datacenter_id, netmap_id=None):
+    '''get datacenter networks, can use both uuid or name'''
+    #obtain data
+    result, datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter_id, "datacenter") 
+    if result < 0:
+        print "http_delnetmap_datacenter_id error %d %s" % (result, datacenter_dict)
+        bottle.abort(-result, datacenter_dict)
+    where_= {"datacenter_id":datacenter_dict['uuid']}
+    if netmap_id:
+        if af.check_valid_uuid(netmap_id):
+            where_["uuid"] = netmap_id
+        else:
+            where_["name"] = netmap_id
+    #change_keys_http2db(content, http2db_tenant, reverse=True)
+    result, content =mydb.delete_row_by_dict(FROM='datacenter_nets', WHERE= where_) 
+    if result < 0:
+        print "http_delnetmap_datacenter_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+    elif result == 0 and netmap_id :
+        bottle.abort(HTTP_Not_Found, "No netmap found with " + " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), where_.iteritems())) )
+    if netmap_id:
+        return format_out({"result": "netmap %s deleted" % netmap_id})
+    else:
+        return format_out({"result": "%d netmap deleted" % result})
+
+
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>/netmaps/upload', method='POST')
+def http_uploadnetmap_datacenter_id(tenant_id, datacenter_id):
+    result, content = nfvo.datacenter_new_netmap(mydb, tenant_id, datacenter_id, None)
+    if result < 0:
+        print "http_postnetmap_datacenter_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+    convert_datetime2str(content)
+    af.convert_str2boolean(content, ('shared', 'multipoint') )
+    print content
+    data={'netmaps' : content}
+    return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>/netmaps', method='POST')
+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)
+    if r is not None: print "http_action_datacenter_id: Warning: remove extra items ", r
+    
+    #obtain data, check that only one exist
+    result, content = nfvo.datacenter_new_netmap(mydb, tenant_id, datacenter_id, http_content)
+    if result < 0:
+        print "http_postnetmap_datacenter_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+    convert_datetime2str(content)
+    af.convert_str2boolean(content, ('shared', 'multipoint') )
+    print content
+    data={'netmaps' : content}
+    return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>/netmaps/<netmap_id>', method='PUT')
+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)
+    if r is not None: print "http_putnettmap_datacenter_id: Warning: remove extra items ", r
+    
+    #obtain data, check that only one exist
+    result, content = nfvo.datacenter_edit_netmap(mydb, tenant_id, datacenter_id, netmap_id, http_content)
+    if result < 0:
+        print "http_putnettmap_datacenter_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+    else:
+        return http_getnetmap_datacenter_id(tenant_id, datacenter_id, netmap_id)
+    
+
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>/action', method='POST')
+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)
+    if r is not None: print "http_action_datacenter_id: Warning: remove extra items ", r
+    
+    #obtain data, check that only one exist
+    result, content = nfvo.datacenter_action(mydb, tenant_id, datacenter_id, http_content)
+    if result < 0:
+        print "http_action_datacenter_id error %d %s" % (result, content)
+        bottle.abort(-result, content)
+    if 'net-update' in http_content:
+        return http_getnetmap_datacenter_id(datacenter_id)
+    else:
+        return format_out(content)
+
+
+@bottle.route(url_base + '/datacenters/<datacenter_id>', method='DELETE')
+def http_delete_datacenter_id( datacenter_id):
+    '''delete a tenant from database, can use both uuid or name'''
+    
+    result, data = nfvo.delete_datacenter(mydb, datacenter_id)
+    if result < 0:
+        print "http_delete_datacenter_id error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        #print json.dumps(data, indent=4)
+        return format_out({"result":"datacenter " + data + " deleted"})
+
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>', method='POST')
+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)
+    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'),
+                                http_content['datacenter'].get('vim_tenant_name'),
+                                http_content['datacenter'].get('vim_username'),
+                                http_content['datacenter'].get('vim_password')
+                     )
+    if result < 0:
+        print "http_associate_datacenters error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        print "http_associate_datacenters data" , data 
+        return http_get_datacenter_id(tenant_id, data)
+
+@bottle.route(url_base + '/<tenant_id>/datacenters/<datacenter_id>', method='DELETE')
+def http_deassociate_datacenters(tenant_id, datacenter_id):
+    '''deassociate an existing datacenter to a this tenant. '''
+    result, data = nfvo.deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter_id)
+    if result < 0:
+        print "http_deassociate_datacenters error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        return format_out({"result":data})
+       
+
+
+@bottle.route(url_base + '/<tenant_id>/vim/<datacenter_id>/<item>', method='GET')
+@bottle.route(url_base + '/<tenant_id>/vim/<datacenter_id>/<item>/<name>', method='GET')
+def http_get_vim_items(tenant_id, datacenter_id, item, name=None):
+    result, data = nfvo.vim_action_get(mydb, tenant_id, datacenter_id, item, name)
+    if result < 0:
+        print "http_get_vim_items error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/vim/<datacenter_id>/<item>/<name>', method='DELETE')
+def http_del_vim_items(tenant_id, datacenter_id, item, name):
+    result, data = nfvo.vim_action_delete(mydb, tenant_id, datacenter_id, item, name)
+    if result < 0:
+        print "http_get_vim_items error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        return format_out({"result":data})
+
+@bottle.route(url_base + '/<tenant_id>/vim/<datacenter_id>/<item>', method='POST')
+def http_post_vim_items(tenant_id, datacenter_id, item):
+    http_content,_ = format_in( object_schema )
+    result, data = nfvo.vim_action_create(mydb, tenant_id, datacenter_id, item, http_content)
+    if result < 0:
+        print "http_post_vim_items error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/vnfs', method='GET')
+def http_get_vnfs(tenant_id):
+    #check valid tenant_id
+    if tenant_id != "any" and not nfvo.check_tenant(mydb, tenant_id): 
+        print 'httpserver.http_get_vnf_id() tenant %s not found' % tenant_id
+        bottle.abort(HTTP_Not_Found, 'Tenant %s not found' % tenant_id)
+        return
+    select_,where_,limit_ = filter_query_string(bottle.request.query, None,
+            ('uuid','name','description','public', "tenant_id", "created_at") )
+    where_or = {}
+    if tenant_id != "any":
+        where_or["tenant_id"] = tenant_id
+        where_or["public"] = True
+    result, content = mydb.get_table(FROM='vnfs', SELECT=select_,WHERE=where_,WHERE_OR=where_or, WHERE_AND_OR="AND",LIMIT=limit_)
+    if result < 0:
+        print "http_get_vnfs Error", content
+        bottle.abort(-result, content)
+    else:
+        #change_keys_http2db(content, http2db_vnf, reverse=True)
+        af.convert_str2boolean(content, ('public',))
+        convert_datetime2str(content)
+        data={'vnfs' : content}
+        return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/vnfs/<vnf_id>', method='GET')
+def http_get_vnf_id(tenant_id,vnf_id):
+    '''get vnf details, can use both uuid or name'''
+    result, data = nfvo.get_vnf_id(mydb,tenant_id,vnf_id)
+    if result < 0:
+        print "http_post_vnfs error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+
+    af.convert_str2boolean(data, ('public',))
+    convert_datetime2str(data)
+    return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/vnfs', method='POST')
+def http_post_vnfs(tenant_id):
+    '''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'''
+    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)
+    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:
+        print "http_post_vnfs error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        return http_get_vnf_id(tenant_id,data)
+            
+@bottle.route(url_base + '/<tenant_id>/vnfs/<vnf_id>', method='DELETE')
+def http_delete_vnf_id(tenant_id,vnf_id):
+    '''delete a vnf from database, and images and flavors in VIM when appropriate, can use both uuid or name'''
+    #check valid tenant_id and deletes the vnf, including images, 
+    result, data = nfvo.delete_vnf(mydb,tenant_id,vnf_id)
+    if result < 0:
+        print "http_delete_vnf_id error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        #print json.dumps(data, indent=4)
+        return format_out({"result":"VNF " + data + " deleted"})
+
+#@bottle.route(url_base + '/<tenant_id>/hosts/topology', method='GET')
+#@bottle.route(url_base + '/<tenant_id>/physicalview/Madrid-Alcantara', method='GET')
+@bottle.route(url_base + '/<tenant_id>/physicalview/<datacenter>', method='GET')
+def http_get_hosts(tenant_id, datacenter):
+    '''get the tidvim host hopology from the vim.'''
+    global mydb
+    print "http_get_hosts received by tenant " + tenant_id + ' datacenter ' + datacenter
+    if datacenter == 'treeview':
+        result, data = nfvo.get_hosts(mydb, tenant_id)
+    else:
+        #openmano-gui is using a hardcoded value for the datacenter
+        result, data = nfvo.get_hosts_info(mydb, tenant_id) #, datacenter)
+    
+    if result < 0:
+        print "http_post_vnfs error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        convert_datetime2str(data)
+        print json.dumps(data, indent=4)
+        return format_out(data)
+
+
+@bottle.route(url_base + '/<path:path>', method='OPTIONS')
+def http_options_deploy(path):
+    '''For some reason GUI web ask for OPTIONS that must be responded'''
+    #TODO: check correct path, and correct headers request
+    bottle.response.set_header('Access-Control-Allow-Methods','POST, GET, PUT, DELETE, OPTIONS')
+    bottle.response.set_header('Accept','application/yaml,application/json')
+    bottle.response.set_header('Content-Type','application/yaml,application/json')
+    bottle.response.set_header('Access-Control-Allow-Headers','content-type')
+    bottle.response.set_header('Access-Control-Allow-Origin','*')
+    return
+
+@bottle.route(url_base + '/<tenant_id>/topology/deploy', method='POST')
+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})
+    #r = af.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
+    
+    result, scenario_uuid = nfvo.new_scenario(mydb, tenant_id, http_content)
+    if result < 0:
+        print "http_post_deploy error creating the scenario %d %s" % (-result, scenario_uuid)
+        bottle.abort(-result, scenario_uuid)
+
+    result, data = nfvo.start_scenario(mydb, tenant_id, scenario_uuid, http_content['name'], http_content['name'])
+    if result < 0:
+        print "http_post_deploy error launching the scenario %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        print json.dumps(data, indent=4)
+        return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/topology/verify', method='POST')
+def http_post_verify(tenant_id):
+    #TODO:
+#    '''post topology verify'''
+#    print "http_post_verify by tenant " + tenant_id + ' datacenter ' + datacenter
+    return 
+
+#
+# SCENARIOS
+#
+
+@bottle.route(url_base + '/<tenant_id>/scenarios', method='POST')
+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})
+    #r = af.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:
+        result, data = nfvo.new_scenario(mydb, tenant_id, http_content)
+    else:
+        result, data = nfvo.new_scenario_v02(mydb, tenant_id, http_content)
+    if result < 0:
+        print "http_post_scenarios error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        #print json.dumps(data, indent=4)
+        #return format_out(data)
+        return http_get_scenario_id(tenant_id,data)
+
+@bottle.route(url_base + '/<tenant_id>/scenarios/<scenario_id>/action', method='POST')
+def http_post_scenario_action(tenant_id, scenario_id):
+    '''take an action over a scenario'''
+    #check valid tenant_id
+    if not nfvo.check_tenant(mydb, tenant_id): 
+        print 'httpserver.http_post_scenario_action() tenant %s not found' % tenant_id
+        bottle.abort(HTTP_Not_Found, 'Tenant %s not found' % tenant_id)
+        return
+    #parse input data
+    http_content,_ = format_in( scenario_action_schema )
+    r = af.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'], \
+                    http_content['start'].get('description',http_content['start']['instance_name']),
+                    http_content['start'].get('datacenter') )
+        if result < 0:
+            print "http_post_scenario_action start error %d: %s" % (-result, data)
+            bottle.abort(-result, data)
+        else:
+            return format_out(data)
+    elif "deploy" in http_content:   #Equivalent to start
+        result, data = nfvo.start_scenario(mydb, tenant_id, scenario_id, http_content['deploy']['instance_name'],
+                    http_content['deploy'].get('description',http_content['deploy']['instance_name']),
+                    http_content['deploy'].get('datacenter') )
+        if result < 0:
+            print "http_post_scenario_action deploy error %d: %s" % (-result, data)
+            bottle.abort(-result, data)
+        else:
+            return format_out(data)
+    elif "reserve" in http_content:   #Reserve resources
+        result, data = nfvo.start_scenario(mydb, tenant_id, scenario_id, http_content['reserve']['instance_name'],
+                    http_content['reserve'].get('description',http_content['reserve']['instance_name']),
+                    http_content['reserve'].get('datacenter'),  startvms=False )
+        if result < 0:
+            print "http_post_scenario_action reserve error %d: %s" % (-result, data)
+            bottle.abort(-result, data)
+        else:
+            return format_out(data)
+    elif "verify" in http_content:   #Equivalent to start and then delete
+        result, data = nfvo.start_scenario(mydb, tenant_id, scenario_id, http_content['verify']['instance_name'],
+                    http_content['verify'].get('description',http_content['verify']['instance_name']),
+                    http_content['verify'].get('datacenter'), startvms=False )
+        if result < 0 or result!=1:
+            print "http_post_scenario_action verify error during start %d: %s" % (-result, data)
+            bottle.abort(-result, data)
+        instance_id = data['uuid']
+        result, message = nfvo.delete_instance(mydb, tenant_id,instance_id)
+        if result < 0:
+            print "http_post_scenario_action verify error during start delete_instance_id %d %s" % (-result, message)
+            bottle.abort(-result, message)
+        else:
+            #print json.dumps(data, indent=4)
+            return format_out({"result":"Verify OK"})
+
+@bottle.route(url_base + '/<tenant_id>/scenarios', method='GET')
+def http_get_scenarios(tenant_id):
+    '''get scenarios list'''
+    #check valid tenant_id
+    if tenant_id != "any" and not nfvo.check_tenant(mydb, tenant_id): 
+        print "httpserver.http_get_scenarios() tenant '%s' not found" % tenant_id
+        bottle.abort(HTTP_Not_Found, "Tenant '%s' not found" % tenant_id)
+        return
+    #obtain data
+    s,w,l=filter_query_string(bottle.request.query, None, ('uuid', 'name', 'description', 'tenant_id', 'created_at', 'public'))
+    where_or={}
+    if tenant_id != "any":
+        where_or["tenant_id"] = tenant_id
+        where_or["public"] = True
+    result, data = mydb.get_table(SELECT=s, WHERE=w, WHERE_OR=where_or, WHERE_AND_OR="AND", LIMIT=l, FROM='scenarios')
+    if result < 0:
+        print "http_get_scenarios error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        convert_datetime2str(data)
+        af.convert_str2boolean(data, ('public',) )
+        scenarios={'scenarios':data}
+        #print json.dumps(scenarios, indent=4)
+        return format_out(scenarios)
+
+@bottle.route(url_base + '/<tenant_id>/scenarios/<scenario_id>', method='GET')
+def http_get_scenario_id(tenant_id, scenario_id):
+    '''get scenario details, can use both uuid or name'''
+    #check valid tenant_id
+    if tenant_id != "any" and not nfvo.check_tenant(mydb, tenant_id): 
+        print "httpserver.http_get_scenario_id() tenant '%s' not found" % tenant_id
+        bottle.abort(HTTP_Not_Found, "Tenant '%s' not found" % tenant_id)
+        return
+    #obtain data
+    result, content = mydb.get_scenario(scenario_id, tenant_id)
+    if result < 0:
+        print "http_get_scenario_id error %d %s" % (-result, content)
+        bottle.abort(-result, content)
+    else:
+        #print json.dumps(content, indent=4)
+        convert_datetime2str(content)
+        data={'scenario' : content}
+        return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/scenarios/<scenario_id>', method='DELETE')
+def http_delete_scenario_id(tenant_id, scenario_id):
+    '''delete a scenario from database, can use both uuid or name'''
+    #check valid tenant_id
+    if tenant_id != "any" and not nfvo.check_tenant(mydb, tenant_id): 
+        print "httpserver.http_delete_scenario_id() tenant '%s' not found" % tenant_id
+        bottle.abort(HTTP_Not_Found, "Tenant '%s' not found" % tenant_id)
+        return
+    #obtain data
+    result, data = mydb.delete_scenario(scenario_id, tenant_id)
+    if result < 0:
+        print "http_delete_scenario_id error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        #print json.dumps(data, indent=4)
+        return format_out({"result":"scenario " + data + " deleted"})
+
+
+@bottle.route(url_base + '/<tenant_id>/scenarios/<scenario_id>', method='PUT')
+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)
+    #if r is not None: print "http_put_scenario_id: Warning: remove extra items ", r
+    print "http_put_scenario_id input: ",  http_content
+    
+    result, data = nfvo.edit_scenario(mydb, tenant_id, scenario_id, http_content)
+    if result < 0:
+        print "http_put_scenarios error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        #print json.dumps(data, indent=4)
+        #return format_out(data)
+        return http_get_scenario_id(tenant_id,data)
+
+@bottle.route(url_base + '/<tenant_id>/instances', method='POST')
+def http_post_instances(tenant_id):
+    '''take an action over a scenario'''
+    #check valid tenant_id
+    if not nfvo.check_tenant(mydb, tenant_id): 
+        print 'httpserver.http_post_scenario_action() tenant %s not found' % tenant_id
+        bottle.abort(HTTP_Not_Found, 'Tenant %s not found' % 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)
+    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:
+        print "http_post_instances start error %d: %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        return format_out(data)
+
+#
+# INSTANCES
+#
+@bottle.route(url_base + '/<tenant_id>/instances', method='GET')
+def http_get_instances(tenant_id):
+    '''get instance list'''
+    #check valid tenant_id
+    if tenant_id != "any" and not nfvo.check_tenant(mydb, tenant_id): 
+        print 'httpserver.http_get_instances() tenant %s not found' % tenant_id
+        bottle.abort(HTTP_Not_Found, 'Tenant %s not found' % tenant_id)
+        return
+    #obtain data
+    s,w,l=filter_query_string(bottle.request.query, None, ('uuid', 'name', 'scenario_id', 'tenant_id', 'description', 'created_at'))
+    where_or={}
+    if tenant_id != "any":
+        w['tenant_id'] = tenant_id
+    result, data = mydb.get_table(SELECT=s, WHERE=w, LIMIT=l, FROM='instance_scenarios')
+    if result < 0:
+        print "http_get_instances error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        convert_datetime2str(data)
+        af.convert_str2boolean(data, ('public',) )
+        instances={'instances':data}
+        print json.dumps(instances, indent=4)
+        return format_out(instances)
+
+@bottle.route(url_base + '/<tenant_id>/instances/<instance_id>', method='GET')
+def http_get_instance_id(tenant_id, instance_id):
+    '''get instances details, can use both uuid or name'''
+    #check valid tenant_id
+    if tenant_id != "any" and not nfvo.check_tenant(mydb, tenant_id): 
+        print 'httpserver.http_get_instance_id() tenant %s not found' % tenant_id
+        bottle.abort(HTTP_Not_Found, 'Tenant %s not found' % tenant_id)
+        return
+    if tenant_id == "any":
+        tenant_id = None
+  
+    #obtain data (first time is only to check that the instance exists)
+    result, data = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True)
+    if result < 0:
+        print "http_get_instance_id error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+        return
+    
+    r,c = nfvo.refresh_instance(mydb, tenant_id, data)
+    if r<0:
+        print "WARNING: nfvo.refresh_instance couldn't refresh the status of the instance: %s" %c
+    #obtain data with results upated
+    result, data = mydb.get_instance_scenario(instance_id, tenant_id)
+    if result < 0:
+        print "http_get_instance_id error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+        return
+    convert_datetime2str(data)
+    print json.dumps(data, indent=4)
+    return format_out(data)
+
+@bottle.route(url_base + '/<tenant_id>/instances/<instance_id>', method='DELETE')
+def http_delete_instance_id(tenant_id, instance_id):
+    '''delete instance from VIM and from database, can use both uuid or name'''
+    #check valid tenant_id
+    if tenant_id != "any" and not nfvo.check_tenant(mydb, tenant_id): 
+        print 'httpserver.http_delete_instance_id() tenant %s not found' % tenant_id
+        bottle.abort(HTTP_Not_Found, 'Tenant %s not found' % tenant_id)
+        return
+    if tenant_id == "any":
+        tenant_id = None
+    #obtain data
+    result, message = nfvo.delete_instance(mydb, tenant_id,instance_id)
+    if result < 0:
+        print "http_delete_instance_id error %d %s" % (-result, message)
+        bottle.abort(-result, message)
+    else:
+        #print json.dumps(data, indent=4)
+        return format_out({"result":message})
+
+@bottle.route(url_base + '/<tenant_id>/instances/<instance_id>/action', method='POST')
+def http_post_instance_scenario_action(tenant_id, instance_id):
+    '''take an action over a scenario instance'''
+    #check valid tenant_id
+    if not nfvo.check_tenant(mydb, tenant_id): 
+        print 'httpserver.http_post_instance_scenario_action() tenant %s not found' % tenant_id
+        bottle.abort(HTTP_Not_Found, 'Tenant %s not found' % tenant_id)
+        return
+    #parse input data
+    http_content,_ = format_in( instance_scenario_action_schema )
+    r = af.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
+    result, data = mydb.get_instance_scenario(instance_id, tenant_id)
+    if result < 0:
+        print "http_get_instance_id error %d %s" % (-result, data)
+        bottle.abort(-result, data)
+    instance_id = data["uuid"]
+    
+    result, data = nfvo.instance_action(mydb, tenant_id, instance_id, http_content)
+    if result < 0:
+        print "http_post_scenario_action error %d: %s" % (-result, data)
+        bottle.abort(-result, data)
+    else:
+        return format_out(data)
+
+
+@bottle.error(400)
+@bottle.error(401) 
+@bottle.error(404) 
+@bottle.error(403)
+@bottle.error(405) 
+@bottle.error(406)
+@bottle.error(409)
+@bottle.error(503) 
+@bottle.error(500)
+def error400(error):
+    e={"error":{"code":error.status_code, "type":error.status, "description":error.body}}
+    bottle.response.headers['Access-Control-Allow-Origin'] = '*'
+    return format_out(e)
+
diff --git a/nfvo.py b/nfvo.py
new file mode 100644 (file)
index 0000000..1c76984
--- /dev/null
+++ b/nfvo.py
@@ -0,0 +1,2444 @@
+# -*- 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
+##
+
+'''
+NFVO engine, implementing all the methods for the creation, deletion and management of vnfs, scenarios and instances
+'''
+__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
+__date__ ="$16-sep-2014 22:05:01$"
+
+import imp
+#import json
+import yaml
+from utils import auxiliary_functions as af
+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
+
+global global_config
+global vimconn_imported
+
+vimconn_imported={} #dictionary with VIM type as key, loaded module as value
+
+class NfvoException(Exception):
+    def __init__(self, error_code, text_message):
+        self.error_code = error_code
+        Exception.__init__(self, text_message)
+
+
+def get_flavorlist(mydb, vnf_id, nfvo_tenant=None):
+    '''Obtain flavorList
+    return result, content:
+        <0, error_text upon error
+        nb_records, flavor_list on success
+    '''
+    WHERE_dict={}
+    WHERE_dict['vnf_id'] = vnf_id
+    if nfvo_tenant is not None:
+        WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
+    
+    #result, content = mydb.get_table(FROM='vms join vnfs on vms.vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
+    #result, content = mydb.get_table(FROM='vms',SELECT=('vim_flavor_id',),WHERE=WHERE_dict )
+    result, content = mydb.get_table(FROM='vms join flavors on vms.flavor_id=flavors.uuid',SELECT=('flavor_id',),WHERE=WHERE_dict )
+    if result < 0:
+        print "nfvo.get_flavorlist error %d %s" % (result, content)
+        return -result, content
+    print "get_flavor_list result:", result
+    print "get_flavor_list content:", content
+    flavorList=[]
+    for flavor in content:
+        flavorList.append(flavor['flavor_id'])
+    return result, flavorList
+
+def get_imagelist(mydb, vnf_id, nfvo_tenant=None):
+    '''Obtain imageList
+    return result, content:
+        <0, error_text upon error
+        nb_records, flavor_list on success
+    '''
+    WHERE_dict={}
+    WHERE_dict['vnf_id'] = vnf_id
+    if nfvo_tenant is not None:
+        WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
+    
+    #result, content = mydb.get_table(FROM='vms join vnfs on vms-vnf_id = vnfs.uuid',SELECT=('uuid'),WHERE=WHERE_dict )
+    result, content = mydb.get_table(FROM='vms join images on vms.image_id=images.uuid',SELECT=('image_id',),WHERE=WHERE_dict )
+    if result < 0:
+        print "nfvo.get_imagelist error %d %s" % (result, content)
+        return -result, content
+    print "get_image_list result:", result
+    print "get_image_list content:", content
+    imageList=[]
+    for image in content:
+        imageList.append(image['image_id'])
+    return result, imageList
+
+def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, vim_tenant=None):
+    '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters
+    return result, content:
+        <0, error_text upon error
+        NUMBER, dictionary with datacenter_id: vim_class with these keys: 
+            'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd'
+    '''
+    WHERE_dict={}
+    if nfvo_tenant     is not None:  WHERE_dict['nfvo_tenant_id'] = nfvo_tenant
+    if datacenter_id   is not None:  WHERE_dict['d.uuid']  = datacenter_id
+    if datacenter_name is not None:  WHERE_dict['d.name']  = datacenter_name
+    if vim_tenant      is not None:  WHERE_dict['dt.vim_tenant_id']  = vim_tenant
+    if nfvo_tenant or vim_tenant:
+        from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid'
+        select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name',
+                   'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id',
+                   'user','passwd')
+    else:
+        from_ = 'datacenters as d'
+        select_ = ('type','config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name')
+    result, content = mydb.get_table(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
+    if result < 0:
+        print "nfvo.get_vim error %d %s" % (result, content)
+        return result, content
+    #elif result==0:
+    #    print "nfvo.get_vim not found a valid VIM with the input params " + str(WHERE_dict)
+    #    return -HTTP_Not_Found, "datacenter not found for " +  str(WHERE_dict)
+    #print content
+    vim_dict={}
+    for vim in content:
+        extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id')}
+        if vim["config"] != None:
+            extra.update(yaml.load(vim["config"]))
+        if vim["type"] not in vimconn_imported:
+            module_info=None
+            try:
+                module = "vimconn_" + vim["type"]
+                module_info = imp.find_module(module)
+                vim_conn = imp.load_module(vim["type"], *module_info)
+                vimconn_imported[vim["type"]] = vim_conn
+            except (IOError, ImportError) as e:
+                if module_info and module_info[0]:
+                    file.close(module_info[0])
+                print "Cannot open VIM module '%s.py'; %s: %s" % ( module, type(e).__name__, str(e))
+                return -HTTP_Bad_Request, "Unknown vim type %s" % vim["type"]
+
+        try:
+            tenant=vim.get('vim_tenant_id')
+            if not tenant:
+                tenant=vim.get('vim_tenant_name')
+            #if not tenant:
+            #    return -HTTP_Bad_Request, "You must provide a valid tenant name or uuid for VIM  %s" % ( vim["type"])
+            vim_dict[ vim['datacenter_id'] ] = vimconn_imported[ vim["type"] ].vimconnector(
+                            uuid=vim['datacenter_id'], name=vim['datacenter_name'],
+                            tenant=tenant, 
+                            url=vim['vim_url'], url_admin=vim['vim_url_admin'], 
+                            user=vim.get('user'), passwd=vim.get('passwd'),
+                            config=extra
+                    )
+        except Exception as e:
+            return -HTTP_Internal_Server_Error, "Error at VIM  %s; %s: %s" % ( vim["type"], type(e).__name__, str(e))
+    return len(vim_dict), vim_dict
+
+def rollback(mydb,  vims, rollback_list):
+    undeleted_items=[]
+    #delete things by reverse order 
+    for i in range(len(rollback_list)-1, -1, -1):
+        item = rollback_list[i]
+        if item["where"]=="vim":
+            if item["vim_id"] not in vims:
+                continue
+            vim=vims[ item["vim_id"] ]
+            if item["what"]=="image":
+                result, message = vim.delete_tenant_image(item["uuid"])
+                if result < 0:
+                    print "Error in rollback. Not possible to delete VIM image '%s'. Message: %s" % (item["uuid"],message)
+                    undeleted_items.append("image %s from VIM %s" % (item["uuid"],vim["name"]))
+                else:
+                    result, message = mydb.delete_row_by_dict(FROM="datacenters_images", WEHRE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
+                    if result < 0:
+                        print "Error in rollback. Not possible to delete image '%s' from DB.dacenters_images. Message: %s" % (item["uuid"],message)
+            elif item["what"]=="flavor":
+                result, message = vim.delete_tenant_flavor(item["uuid"])
+                if result < 0:
+                    print "Error in rollback. Not possible to delete VIM flavor '%s'. Message: %s" % (item["uuid"],message)
+                    undeleted_items.append("flavor %s from VIM %s" % (item["uuid"],vim["name"]))
+                else:
+                    result, message = mydb.delete_row_by_dict(FROM="datacenters_flavos", WEHRE={"datacenter_id": vim["id"], "vim_id":item["uuid"]})
+                    if result < 0:
+                        print "Error in rollback. Not possible to delete flavor '%s' from DB.dacenters_flavors. Message: %s" % (item["uuid"],message)
+            elif item["what"]=="network":
+                result, message = vim.delete_tenant_network(item["uuid"])
+                if result < 0:
+                    print "Error in rollback. Not possible to delete VIM network  '%s'. Message: %s" % (item["uuid"],message)
+                    undeleted_items.append("network %s from VIM %s" % (item["uuid"],vim["name"]))
+            elif item["what"]=="vm":
+                result, message = vim.delete_tenant_vminstance(item["uuid"])
+                if result < 0:
+                    print "Error in rollback. Not possible to delete VIM VM  '%s'. Message: %s" % (item["uuid"],message)
+                    undeleted_items.append("VM %s from VIM %s" % (item["uuid"],vim["name"]))
+        else: # where==mano
+            if item["what"]=="image":
+                result, message = mydb.delete_row_by_dict(FROM="images", WEHRE={"uuid": item["uuid"]})
+                if result < 0:
+                    print "Error in rollback. Not possible to delete image '%s' from DB.images. Message: %s" % (item["uuid"],message)
+                    undeleted_items.append("image %s" % (item["uuid"]))
+            elif item["what"]=="flavor":
+                result, message = mydb.delete_row_by_dict(FROM="flavors", WEHRE={"uuid": item["uuid"]})
+                if result < 0:
+                    print "Error in rollback. Not possible to delete flavor '%s' from DB.flavors. Message: %s" % (item["uuid"],message)
+                    undeleted_items.append("flavor %s" % (item["uuid"]))
+    if len(undeleted_items)==0: 
+        return True," Rollback successful."
+    else:
+        return False," Rollback fails to delete: " + str(undeleted_items)
+  
+def check_vnf_descriptor(vnf_descriptor):
+    global global_config
+    #create a dictionary with vnfc-name: vnfc:interface-list  key:values pairs 
+    vnfc_interfaces={}
+    for vnfc in vnf_descriptor["vnf"]["VNFC"]:
+        name_list = []
+        #dataplane interfaces
+        for numa in vnfc.get("numas",() ):
+            for interface in numa.get("interfaces",()):
+                if interface["name"] in name_list:
+                    return -HTTP_Bad_Request, "Error at vnf:VNFC[name:'%s']:numas:interfaces:name, interface name '%s' already used in this VNFC" %(vnfc["name"], interface["name"])
+                name_list.append( interface["name"] ) 
+        #bridge interfaces
+        for interface in vnfc.get("bridge-ifaces",() ):
+            if interface["name"] in name_list:
+                return -HTTP_Bad_Request, "Error at vnf:VNFC[name:'%s']:bridge-ifaces:name, interface name '%s' already used in this VNFC" %(vnfc["name"], interface["name"])
+            name_list.append( interface["name"] ) 
+        vnfc_interfaces[ vnfc["name"] ] = name_list
+    
+    #check if the info in external_connections matches with the one in the vnfcs
+    name_list=[]
+    for external_connection in vnf_descriptor["vnf"].get("external-connections",() ):
+        if external_connection["name"] in name_list:
+            return -HTTP_Bad_Request, "Error at vnf:external-connections:name, value '%s' already used as an external-connection" %(external_connection["name"])
+        name_list.append(external_connection["name"])
+        if external_connection["VNFC"] not in vnfc_interfaces:
+            return -HTTP_Bad_Request, "Error at vnf:external-connections[name:'%s']:VNFC, value '%s' does not match any VNFC" %(external_connection["name"], external_connection["VNFC"])
+        if external_connection["local_iface_name"] not in vnfc_interfaces[ external_connection["VNFC"] ]:
+            return -HTTP_Bad_Request, "Error at vnf:external-connections[name:'%s']:local_iface_name, value '%s' does not match any interface of this VNFC" %(external_connection["name"], external_connection["local_iface_name"])
+    
+    #check if the info in internal_connections matches with the one in the vnfcs
+    name_list=[]
+    for internal_connection in vnf_descriptor["vnf"].get("internal-connections",() ):
+        if internal_connection["name"] in name_list:
+            return -HTTP_Bad_Request, "Error at vnf:internal-connections:name, value '%s' already used as an internal-connection" %(internal_connection["name"])
+        name_list.append(internal_connection["name"])
+        #We should check that internal-connections of type "ptp" have only 2 elements
+        if len(internal_connection["elements"])>2 and internal_connection["type"] == "ptp":
+            return -HTTP_Bad_Request, "Error at vnf:internal-connections[name:'%s']:elements, size must be 2 for a type:'ptp'" %(internal_connection["name"])
+        for port in internal_connection["elements"]:
+            if port["VNFC"] not in vnfc_interfaces:
+                return -HTTP_Bad_Request, "Error at vnf:internal-connections[name:'%s']:elements[]:VNFC, value '%s' does not match any VNFC" %(internal_connection["name"], port["VNFC"])
+            if port["local_iface_name"] not in vnfc_interfaces[ port["VNFC"] ]:
+                return -HTTP_Bad_Request, "Error at vnf:internal-connections[name:'%s']:elements[]:local_iface_name, value '%s' does not match any interface of this VNFC" %(internal_connection["name"], port["local_iface_name"])
+
+    
+    return 200, None
+
+def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error = False):
+    #look if image exist
+    if only_create_at_vim:
+        image_mano_id = image_dict['uuid']
+    else:
+        res,content = mydb.get_table(FROM="images", WHERE={'location':image_dict['location'], 'metadata':image_dict['metadata']})
+        if res>=1:
+            image_mano_id = content[0]['uuid']
+        elif res<0:
+            return res, content
+        else:
+            #create image
+            temp_image_dict={'name':image_dict['name'],         'description':image_dict.get('description',None),
+                            'location':image_dict['location'],  'metadata':image_dict.get('metadata',None)
+                            }
+            res,content = mydb.new_row('images', temp_image_dict, tenant_id=None, add_uuid=True)
+            if res>0:
+                image_mano_id= content
+                rollback_list.append({"where":"mano", "what":"image","uuid":image_mano_id})
+            else:
+                return res if res<0 else -1, content
+    #create image at every vim
+    for vim_id,vim in vims.iteritems():
+        image_created="false"
+        #look at database
+        res_db,image_db = mydb.get_table(FROM="datacenters_images", WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
+        if res_db<0:
+            return res_db, image_db
+        #look at VIM if this image exist
+        res_vim, image_vim_id = vim.get_image_id_from_path(image_dict['location'])
+        if res_vim < 0:
+            print "Error contacting VIM to know if the image %s existed previously." %image_vim_id
+            continue
+        elif res_vim==0:
+            #Create the image in VIM
+            result, image_vim_id = vim.new_tenant_image(image_dict)
+            if result < 0:
+                print "Error creating image at VIM: %s." %image_vim_id
+                if return_on_error:
+                    return result, image_vim_id
+                continue
+            else:
+                rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"image","uuid":image_vim_id})
+                image_created="true"
+            
+        #if reach here the image has been create or exist
+        if res_db==0:
+            #add new vim_id at datacenters_images
+            mydb.new_row('datacenters_images', {'datacenter_id':vim_id, 'image_id':image_mano_id, 'vim_id': image_vim_id, 'created':image_created})
+        elif image_db[0]["vim_id"]!=image_vim_id:
+            #modify existing vim_id at datacenters_images
+            mydb.update_rows('datacenters_images', UPDATE={'vim_id':image_vim_id}, WHERE={'datacenter_id':vim_id, 'image_id':image_mano_id})
+            
+    return 1, image_vim_id if only_create_at_vim else image_mano_id
+
+def create_or_use_flavor(mydb, vims, flavor_dict, rollback_list, only_create_at_vim=False, return_on_error = False):
+    temp_flavor_dict= {'disk':flavor_dict.get('disk',1),
+            'ram':flavor_dict.get('ram'),
+            'vcpus':flavor_dict.get('vcpus'),
+        }
+    if 'extended' in flavor_dict and flavor_dict['extended']==None:
+        del flavor_dict['extended']
+    if 'extended' in flavor_dict:
+        temp_flavor_dict['extended']=yaml.safe_dump(flavor_dict['extended'],default_flow_style=True,width=256)
+
+    #look if flavor exist
+    if only_create_at_vim:
+        flavor_mano_id = flavor_dict['uuid']
+    else:
+        res,content = mydb.get_table(FROM="flavors", WHERE=temp_flavor_dict)
+        if res>=1:
+            flavor_mano_id = content[0]['uuid']
+        elif res<0:
+            return res, content
+        else:
+            #create flavor
+            #create one by one the images of aditional disks
+            dev_image_list=[] #list of images
+            if 'extended' in flavor_dict and flavor_dict['extended']!=None:
+                dev_nb=0
+                for device in flavor_dict['extended'].get('devices',[]):
+                    if "image" not in device:
+                        continue
+                    image_dict={'location':device['image'], 'name':flavor_dict['name']+str(dev_nb)+"-img", 'description':flavor_dict.get('description')}
+                    image_metadata_dict = device.get('image metadata', None)
+                    image_metadata_str = None
+                    if image_metadata_dict != None: 
+                        image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
+                    image_dict['metadata']=image_metadata_str
+                    res, image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
+                    if res < 0:
+                        return res, image_id + rollback(mydb, vims, rollback_list)[1]
+                    print "Additional disk image id for VNFC %s: %s" % (flavor_dict['name']+str(dev_nb)+"-img", image_id)
+                    dev_image_list.append(image_id)
+                    dev_nb += 1                
+            temp_flavor_dict['name'] = flavor_dict['name']
+            temp_flavor_dict['description'] = flavor_dict.get('description',None)
+            res,content = mydb.new_row('flavors', temp_flavor_dict, tenant_id=None, add_uuid=True)
+            if res>0:
+                flavor_mano_id= content
+                rollback_list.append({"where":"mano", "what":"flavor","uuid":flavor_mano_id})
+            else:
+                return res if res<0 else -1, content
+    #create flavor at every vim
+    if 'uuid' in flavor_dict:
+        del flavor_dict['uuid']
+    flavor_vim_id=None
+    for vim_id,vim in vims.items():
+        flavor_created="false"
+        #look at database
+        res_db,flavor_db = mydb.get_table(FROM="datacenters_flavors", WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
+        if res_db<0:
+            return res_db, flavor_db
+        #look at VIM if this flavor exist  SKIPPED
+        #res_vim, flavor_vim_id = vim.get_flavor_id_from_path(flavor_dict['location'])
+        #if res_vim < 0:
+        #    print "Error contacting VIM to know if the flavor %s existed previously." %flavor_vim_id
+        #    continue
+        #elif res_vim==0:
+    
+        #Create the flavor in VIM
+        #Translate images at devices from MANO id to VIM id
+        error=False
+        if 'extended' in flavor_dict and flavor_dict['extended']!=None and "devices" in flavor_dict['extended']:
+            #make a copy of original devices
+            devices_original=[]
+            for device in flavor_dict["extended"].get("devices",[]):
+                dev={}
+                dev.update(device)
+                devices_original.append(dev)
+                if 'image' in device:
+                    del device['image']
+                if 'image metadata' in device:
+                    del device['image metadata']
+            dev_nb=0
+            for index in range(0,len(devices_original)) :
+                device=devices_original[index]
+                if "image" not in device:
+                    continue
+                image_dict={'location':device['image'], 'name':flavor_dict['name']+str(dev_nb)+"-img", 'description':flavor_dict.get('description')}
+                image_metadata_dict = device.get('image metadata', None)
+                image_metadata_str = None
+                if image_metadata_dict != None: 
+                    image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
+                image_dict['metadata']=image_metadata_str
+                r,image_mano_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False)
+                if r<0:
+                    print "Error creating device image for flavor: %s." %image_mano_id
+                    error_text = image_mano_id
+                    error=True
+                    break
+                image_dict["uuid"]=image_mano_id
+                r,image_vim_id=create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=True)
+                if r<0:
+                    print "Error creating device image for flavor at VIM: %s." %image_vim_id
+                    error_text = image_vim_id
+                    error=True
+                    break
+                flavor_dict["extended"]["devices"][index]['imageRef']=image_vim_id
+                dev_nb += 1
+        if error:
+            if return_on_error:
+                return r, error_text
+            continue
+        if res_db>0:
+            #check that this vim_id exist in VIM, if not create
+            flavor_vim_id=flavor_db[0]["vim_id"]
+            result, _ = vim.get_tenant_flavor(flavor_vim_id)
+            if result>=0: #flavor exist
+                continue
+        #create flavor at vim
+        print "nfvo.create_or_use_flavor() adding flavor to VIM %s" % vim["name"]
+        result, flavor_vim_id = vim.new_tenant_flavor(flavor_dict)
+        
+        if result < 0:
+            print "Error creating flavor at VIM %s: %s." %(vim["name"], flavor_vim_id)
+            if return_on_error:
+                return result, flavor_vim_id
+            continue
+        else:
+            rollback_list.append({"where":"vim", "vim_id": vim_id, "what":"flavor","uuid":flavor_vim_id})
+            flavor_created="true"
+        
+        #if reach here the flavor has been create or exist
+        if res_db==0:
+            #add new vim_id at datacenters_flavors
+            mydb.new_row('datacenters_flavors', {'datacenter_id':vim_id, 'flavor_id':flavor_mano_id, 'vim_id': flavor_vim_id, 'created':flavor_created})
+        elif flavor_db[0]["vim_id"]!=flavor_vim_id:
+            #modify existing vim_id at datacenters_flavors
+            mydb.update_rows('datacenters_flavors', UPDATE={'vim_id':flavor_vim_id}, WHERE={'datacenter_id':vim_id, 'flavor_id':flavor_mano_id})
+            
+    return 1, flavor_vim_id if only_create_at_vim else flavor_mano_id
+
+def new_vnf(mydb, tenant_id, vnf_descriptor):
+    global global_config
+    
+    # Step 1. Check the VNF descriptor
+    result, message = check_vnf_descriptor(vnf_descriptor)
+    if result < 0:
+        print "new_vnf error: %s" %message
+        return result, message
+
+    # Step 2. Check tenant exist
+    if tenant_id != "any":
+        if not check_tenant(mydb, tenant_id): 
+            print 'nfvo.new_vnf() tenant %s not found' % tenant_id
+            return -HTTP_Not_Found, 'Tenant %s not found' % tenant_id
+        if "tenant_id" in vnf_descriptor["vnf"]:
+            if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
+                print "nfvo.new_vnf() tenant '%s' not found" % tenant_id
+                return -HTTP_Unauthorized, "VNF can not have a different tenant owner '%s', must be '%s'" %(vnf_descriptor["vnf"]["tenant_id"], tenant_id)
+        else:
+            vnf_descriptor['vnf']['tenant_id'] = tenant_id
+        # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
+        result, vims = get_vim(mydb, tenant_id)
+        if result < 0:
+            print "nfvo.new_vnf() error. Datacenter not found"
+            return result, vims
+    else:
+        vims={}
+
+    # Step 4. Review the descriptor and add missing  fields
+    #print vnf_descriptor
+    print "Refactoring VNF descriptor with fields: description, public (default: true)"
+    vnf_name = vnf_descriptor['vnf']['name']
+    vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
+    if "physical" in vnf_descriptor['vnf']:
+        del vnf_descriptor['vnf']['physical']
+    #print vnf_descriptor
+    # Step 5. Check internal connections
+    # TODO: to be moved to step 1????
+    internal_connections=vnf_descriptor['vnf'].get('internal_connections',[])
+    for ic in internal_connections:
+        if len(ic['elements'])>2 and ic['type']=='ptp':
+            return -HTTP_Bad_Request, "Mismatch 'type':'ptp' with %d elements at 'vnf':'internal-conections'['name':'%s']. Change 'type' to 'data'" %(len(ic), ic['name'])
+        elif len(ic['elements'])==2 and ic['type']=='data':
+            return -HTTP_Bad_Request, "Mismatch 'type':'data' with 2 elements at 'vnf':'internal-conections'['name':'%s']. Change 'type' to 'ptp'" %(ic['name'])
+    
+    # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM 
+    print 'BEGIN creation of VNF "%s"' % vnf_name
+    print "VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC']))
+    
+    #For each VNFC, we add it to the VNFCDict and we  create a flavor.
+    VNFCDict = {}     # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
+    rollback_list = []    # It will contain the new images created in mano. It is used for rollback
+
+    try:
+        print "Creating additional disk images and new flavors in the VIM for each VNFC"
+        for vnfc in vnf_descriptor['vnf']['VNFC']:
+            VNFCitem={}
+            VNFCitem["name"] = vnfc['name']
+            VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
+            
+            print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
+            
+            myflavorDict = {}
+            myflavorDict["name"] = vnfc['name']+"-flv"
+            myflavorDict["description"] = VNFCitem["description"]
+            myflavorDict["ram"] = vnfc.get("ram", 0)
+            myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
+            myflavorDict["disk"] = vnfc.get("disk", 1)
+            myflavorDict["extended"] = {}
+            
+            devices = vnfc.get("devices")
+            if devices != None:
+                myflavorDict["extended"]["devices"] = devices
+            
+            # TODO:
+            # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table
+            # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host 
+            
+            # Previous code has been commented
+            #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
+            #    myflavorDict["flavor"]['extended']['processor_ranking'] = 200
+            #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
+            #    myflavorDict["flavor"]['extended']['processor_ranking'] = 300
+            #else:
+            #    result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
+            #    if result2:
+            #        print "Error creating flavor: unknown processor model. Rollback successful."
+            #        return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
+            #    else:
+            #        return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
+            myflavorDict['extended']['processor_ranking'] = 100  #Hardcoded value, while we decide when the mapping is done
+     
+            if 'numas' in vnfc and len(vnfc['numas'])>0:
+                myflavorDict['extended']['numas'] = vnfc['numas']
+
+            #print myflavorDict
+    
+            # Step 6.2 New flavors are created in the VIM
+            res, flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
+            if res < 0:
+                return res, flavor_id + rollback(mydb, vims, rollback_list)[1]
+
+            print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
+            VNFCitem["flavor_id"] = flavor_id
+            VNFCDict[vnfc['name']] = VNFCitem
+            
+        print "Creating new images in the VIM for each VNFC"
+        # Step 6.3 New images are created in the VIM
+        #For each VNFC, we must create the appropriate image.
+        #This "for" loop might be integrated with the previous one 
+        #In case this integration is made, the VNFCDict might become a VNFClist.
+        for vnfc in vnf_descriptor['vnf']['VNFC']:
+            print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
+            image_dict={'location':vnfc['VNFC image'], 'name':vnfc['name']+"-img", 'description':VNFCDict[vnfc['name']]['description']}
+            image_metadata_dict = vnfc.get('image metadata', None)
+            image_metadata_str = None
+            if image_metadata_dict is not None: 
+                image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
+            image_dict['metadata']=image_metadata_str
+            #print "create_or_use_image", mydb, vims, image_dict, rollback_list
+            res, image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
+            if res < 0:
+                return res, image_id + rollback(mydb, vims, rollback_list)[1]
+            print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
+            VNFCDict[vnfc['name']]["image_id"] = image_id
+            VNFCDict[vnfc['name']]["image_path"] = vnfc['VNFC image']
+
+    except KeyError as e:
+        print "Error while creating a VNF. KeyError: " + str(e)
+        _, message = rollback(mydb, vims, rollback_list)
+        return -HTTP_Internal_Server_Error, "Error while creating a VNF. KeyError " + str(e) + "." + message
+        
+    # Step 7. Storing the VNF descriptor in the repository
+    if "descriptor" not in vnf_descriptor["vnf"]:
+        vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
+
+    # Step 8. Adding the VNF to the NFVO DB
+    try:
+        result, vnf_id = mydb.new_vnf_as_a_whole(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
+    except KeyError as e:
+        print "Error while creating a VNF. KeyError: " + str(e)
+        _, message = rollback(mydb, vims, rollback_list)
+        return -HTTP_Internal_Server_Error, "Error while creating a VNF. KeyError " + str(e) + "." + message
+    
+    if result < 0:
+        _, message = rollback(mydb, vims, rollback_list)
+        return result, vnf_id + "." + message
+
+    return 200,vnf_id
+
+def get_vnf_id(mydb, tenant_id, vnf_id):
+    #check valid tenant_id
+    if tenant_id != "any" and not check_tenant(mydb, tenant_id): 
+        print 'nfvo.get_vnf_id() tenant %s not found' % tenant_id
+        return -HTTP_Not_Found, "Tenant '%s' not found" % tenant_id
+    #obtain data
+    where_or = {}
+    if tenant_id != "any":
+        where_or["tenant_id"] = tenant_id
+        where_or["public"] = True
+    result, content = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND") 
+    if result < 0:
+        print "nfvo.get_vnf_id() error %d %s" % (result, content)
+        return (result, content)
+
+    
+    vnf_id=content["uuid"]
+    filter_keys = ('uuid','name','description','public', "tenant_id", "created_at")
+    filtered_content = dict( (k,v) for k,v in content.iteritems() if k in filter_keys )
+    #change_keys_http2db(filtered_content, http2db_vnf, reverse=True)
+    data={'vnf' : filtered_content}
+    #GET VM
+    result,content = mydb.get_table(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id',
+            SELECT=('vms.uuid as uuid','vms.name as name', 'vms.description as description'), 
+            WHERE={'vnfs.uuid': vnf_id} )
+    if result < 0:
+        print "nfvo.get_vnf_id error %d %s" % (result, content)
+        return (result, content)
+    elif result==0:
+        print "nfvo.get_vnf_id vnf '%s' not found" % vnf_id
+        return (-HTTP_Not_Found, "vnf %s not found" % vnf_id)
+
+    data['vnf']['VNFC'] = content
+    #GET NET
+    result,content = mydb.get_table(FROM='vnfs join nets on vnfs.uuid=nets.vnf_id', 
+                                    SELECT=('nets.uuid as uuid','nets.name as name','nets.description as description', 'nets.type as type', 'nets.multipoint as multipoint'),
+                                    WHERE={'vnfs.uuid': vnf_id} )
+    if result < 0:
+        print "nfvo.get_vnf_id error %d %s" % (result, content)
+        return (result, content)
+    else:
+        if result==0:
+            data['vnf']['nets'] = []
+        else:
+            data['vnf']['nets'] = content
+    #GET Interfaces
+    result,content = mydb.get_table(FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces on vms.uuid=interfaces.vm_id',\
+                                    SELECT=('interfaces.uuid as uuid','interfaces.external_name as external_name', 'vms.name as vm_name', 'interfaces.vm_id as vm_id', \
+                                            'interfaces.internal_name as internal_name', 'interfaces.type as type', 'interfaces.vpci as vpci','interfaces.bw as bw'),\
+                                    WHERE={'vnfs.uuid': vnf_id}, 
+                                    WHERE_NOT={'interfaces.external_name': None} )
+    #print content
+    if result < 0:
+        print "nfvo.get_vnf_id error %d %s" % (result, content)
+        return (result, content)
+    else:
+        if result==0:
+            data['vnf']['external-connections'] = []
+        else:
+            data['vnf']['external-connections'] = content
+    return 0, data
+
+
+def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None):
+    # Check tenant exist
+    if tenant_id != "any":
+        if not check_tenant(mydb, tenant_id): 
+            print 'nfvo.delete_vnf() tenant %s not found' % tenant_id
+            return -HTTP_Not_Found, 'Tenant %s not found' % tenant_id
+        # Get the URL of the VIM from the nfvo_tenant and the datacenter
+        result, vims = get_vim(mydb, tenant_id)
+        if result < 0:
+            print "nfvo.delete_vnf() error. Datacenter not found"
+            return result, vims
+    else:
+        vims={}
+
+    # Checking if it is a valid uuid and, if not, getting the uuid assuming that the name was provided"
+    where_or = {}
+    if tenant_id != "any":
+        where_or["tenant_id"] = tenant_id
+        where_or["public"] = True
+    result, content = mydb.get_table_by_uuid_name('vnfs', vnf_id, "VNF", WHERE_OR=where_or, WHERE_AND_OR="AND") 
+    if result < 0: 
+        return result, content
+    vnf_id = content["uuid"]
+    
+    # "Getting the list of flavors and tenants of the VNF"
+    result,flavorList = get_flavorlist(mydb, vnf_id) 
+    if result < 0:
+        print flavorList
+    elif result==0:
+        print "delete_vnf error. No flavors found for the VNF id '%s'" % vnf_id
+    
+    result,imageList = get_imagelist(mydb, vnf_id)
+    print "imageList", imageList
+    if result < 0:
+        print imageList
+    elif result==0:
+        print "delete_vnf error. No images found for the VNF id '%s'" % vnf_id
+    
+    result, content = mydb.delete_row('vnfs', vnf_id, tenant_id)
+    if result == 0:
+        return -HTTP_Not_Found, content
+    elif result >0:
+        print content
+    else:
+        print "delete_vnf error",result, content
+        return result, content
+    
+    undeletedItems = []
+    for flavor in flavorList:
+        #check if flavor is used by other vnf
+        r,c = mydb.get_table(FROM='vms', WHERE={'flavor_id':flavor} )
+        if r < 0:
+            print 'delete_vnf_error. Not possible to delete VIM flavor "%s". %s' % (flavor,c)
+            undeletedItems.append("flavor "+ flavor["flavor_id"])
+        elif r > 0:
+            print 'Flavor %s not deleted because it is being used by another VNF %s' %(flavor,str(c))
+            continue
+        #flavor not used, must be deleted
+        #delelte at VIM
+        r,c = mydb.get_table(FROM='datacenters_flavors', WHERE={'flavor_id':flavor})
+        if r>0:
+            for flavor_vim in c:
+                if flavor_vim["datacenter_id"] not in vims:
+                    continue
+                if flavor_vim['created']=='false': #skip this flavor because not created by openmano
+                    continue
+                myvim=vims[ flavor_vim["datacenter_id"] ]
+                result, message = myvim.delete_tenant_flavor(flavor_vim["vim_id"])
+                if result < 0:
+                    print 'delete_vnf_error. Not possible to delete VIM flavor "%s". Message: %s' % (flavor,message)
+                    if result != -HTTP_Not_Found:
+                        undeletedItems.append("flavor %s from VIM %s" % (flavor_vim["vim_id"], flavor_vim["datacenter_id"] ))
+        #delete flavor from Database, using table flavors and with cascade foreign key also at datacenters_flavors
+        result, content = mydb.delete_row('flavors', flavor)
+        if result <0:
+            undeletedItems.append("flavor %s" % flavor)
+        
+    for image in imageList:
+        #check if image is used by other vnf
+        r,c = mydb.get_table(FROM='vms', WHERE={'image_id':image} )
+        if r < 0:
+            print 'delete_vnf_error. Not possible to delete VIM image "%s". %s' % (image,c)
+            undeletedItems.append("image "+ image["image_id"])
+        elif r > 0:
+            print 'Image %s not deleted because it is being used by another VNF %s' %(image,str(c))
+            continue
+        #image not used, must be deleted
+        #delelte at VIM
+        r,c = mydb.get_table(FROM='datacenters_images', WHERE={'image_id':image})
+        if r>0:
+            for image_vim in c:
+                if image_vim["datacenter_id"] not in vims:
+                    continue
+                if image_vim['created']=='false': #skip this image because not created by openmano
+                    continue
+                myvim=vims[ image_vim["datacenter_id"] ]
+                result, message = myvim.delete_tenant_image(image_vim["vim_id"])
+                if result < 0:
+                    print 'delete_vnf_error. Not possible to delete VIM image "%s". Message: %s' % (image,message)
+                    if result != -HTTP_Not_Found:
+                        undeletedItems.append("image %s from VIM %s" % (image_vim["vim_id"], image_vim["datacenter_id"] ))
+        #delete image from Database, using table images and with cascade foreign key also at datacenters_images
+        result, content = mydb.delete_row('images', image)
+        if result <0:
+            undeletedItems.append("image %s" % image)
+
+    if undeletedItems: 
+        return 200, "delete_vnf error. Undeleted: %s" %(undeletedItems)
+    
+    return 200,vnf_id
+
+def get_hosts_info(mydb, nfvo_tenant_id, datacenter_name=None):
+    result, vims = get_vim(mydb, nfvo_tenant_id, None, datacenter_name)
+    if result < 0:
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % datacenter_name
+    myvim = vims.values()[0]
+    result,servers =  myvim.get_hosts_info()
+    if result < 0:
+        return result, servers
+    topology = {'name':myvim['name'] , 'servers': servers}
+    return result, topology
+
+def get_hosts(mydb, nfvo_tenant_id):
+    result, vims = get_vim(mydb, nfvo_tenant_id)
+    if result < 0:
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "No datacenter found"
+    myvim = vims.values()[0]
+    result,hosts =  myvim.get_hosts()
+    if result < 0:
+        return result, hosts
+    print '==================='
+    print 'hosts '+ yaml.safe_dump(hosts, indent=4, default_flow_style=False)
+
+    datacenter = {'Datacenters': [ {'name':myvim['name'],'servers':[]} ] }
+    for host in hosts:
+        server={'name':host['name'], 'vms':[]}
+        for vm in host['instances']:
+            #get internal name and model
+            r,c = mydb.get_table(SELECT=('name',), FROM='instance_vms as iv join vms on iv.vm_id=vms.uuid',\
+                WHERE={'vim_vm_id':vm['id']} )
+            if r==0:
+                print "nfvo.get_hosts virtual machine at VIM (%s) not found at tidnfvo" % vm['id']
+                continue
+            if r<0:
+                print "nfvo.get_hosts virtual machine at VIM (%s) error %d %s" % (vm['id'], r, c)
+                continue
+            server['vms'].append( {'name':vm['name'] , 'model':c[0]['name']} )
+        datacenter['Datacenters'][0]['servers'].append(server)
+    #return -400, "en construccion"
+    
+    #print 'datacenters '+ json.dumps(datacenter, indent=4)
+    return result, datacenter
+
+def new_scenario(mydb, tenant_id, topo):
+
+#    result, vims = get_vim(mydb, tenant_id)
+#    if result < 0:
+#        return result, vims
+#1: parse input
+    if tenant_id != "any":
+        if not check_tenant(mydb, tenant_id): 
+            print 'nfvo.new_scenario() tenant %s not found' % tenant_id
+            return -HTTP_Not_Found, 'Tenant %s not found' % tenant_id
+        if "tenant_id" in topo:
+            if topo["tenant_id"] != tenant_id:
+                print "nfvo.new_scenario() tenant '%s' not found" % tenant_id
+                return -HTTP_Unauthorized, "VNF can not have a different tenant owner '%s', must be '%s'" %(topo["tenant_id"], tenant_id)
+    else:
+        tenant_id=None
+
+#1.1: get VNFs and external_networks (other_nets). 
+    vnfs={}
+    other_nets={}  #external_networks, bridge_networks and data_networkds
+    nodes = topo['topology']['nodes']
+    for k in nodes.keys():
+        if nodes[k]['type'] == 'VNF':
+            vnfs[k] = nodes[k]
+            vnfs[k]['ifaces'] = {}
+        elif nodes[k]['type'] == 'other_network' or nodes[k]['type'] == 'external_network': 
+            other_nets[k] = nodes[k]
+            other_nets[k]['external']=True
+        elif nodes[k]['type'] == 'network': 
+            other_nets[k] = nodes[k]
+            other_nets[k]['external']=False
+        
+
+#1.2: Check that VNF are present at database table vnfs. Insert uuid, description and external interfaces
+    for name,vnf in vnfs.items():
+        WHERE_={}
+        error_text = ""
+        error_pos = "'topology':'nodes':'" + name + "'"
+        if 'vnf_id' in vnf:
+            error_text += " 'vnf_id' " +  vnf['vnf_id']
+            WHERE_['uuid'] = vnf['vnf_id']
+        if 'VNF model' in vnf:
+            error_text += " 'VNF model' " +  vnf['VNF model']
+            WHERE_['name'] = vnf['VNF model']
+        if len(WHERE_) == 0:
+            return -HTTP_Bad_Request, "needed a 'vnf_id' or 'VNF model' at " + error_pos
+        r,vnf_db = mydb.get_table(SELECT=('uuid','name','description'), FROM='vnfs', WHERE=WHERE_)
+        if r<0:
+            print "nfvo.new_scenario Error getting vnfs",r,vnf_db
+        elif r==0:
+            print "nfvo.new_scenario Error" + error_text + " is not present at database"
+            return -HTTP_Bad_Request, "unknown" + error_text + " at " + error_pos
+        elif r>1:
+            print "nfvo.new_scenario Error more than one" + error_text + " are present at database"
+            return -HTTP_Bad_Request, "more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'"
+        vnf['uuid']=vnf_db[0]['uuid']
+        vnf['description']=vnf_db[0]['description']
+        #get external interfaces
+        r,ext_ifaces = mydb.get_table(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'), 
+            FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id', 
+            WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
+        if r<0:
+            print "nfvo.new_scenario Error getting external interfaces of vnfs",r,ext_ifaces
+            return -HTTP_Internal_Server_Error, "Error getting external interfaces of vnfs: " + ext_ifaces
+        for ext_iface in ext_ifaces:
+            vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
+
+#1.4 get list of connections
+    conections = topo['topology']['connections']
+    conections_list = []
+    for k in conections.keys():
+        if type(conections[k]['nodes'])==dict: #dict with node:iface pairs
+            ifaces_list = conections[k]['nodes'].items()
+        elif type(conections[k]['nodes'])==list: #list with dictionary
+            ifaces_list=[]
+            conection_pair_list = map(lambda x: x.items(), conections[k]['nodes'] )
+            for k2 in conection_pair_list:
+                ifaces_list += k2
+
+        con_type = conections[k].get("type", "link")
+        if con_type != "link":
+            if k in other_nets:
+                return -HTTP_Bad_Request, "format error. Reapeted network name at 'topology':'connections':'%s'" % (str(k))
+            other_nets[k] = {'external': False}
+            if conections[k].get("graph"):
+                other_nets[k]["graph"] =   conections[k]["graph"]
+            ifaces_list.append( (k, None) )
+
+        
+        if con_type == "external_network":
+            other_nets[k]['external'] = True
+            if conections[k].get("model"):
+                other_nets[k]["model"] =   conections[k]["model"]
+            else:
+                other_nets[k]["model"] =   k
+        if con_type == "dataplane_net" or con_type == "bridge_net": 
+            other_nets[k]["model"] = con_type
+        
+        
+        conections_list.append(set(ifaces_list)) #from list to set to operate as a set (this conversion removes elements that are repeated in a list)
+        #print set(ifaces_list)
+    #check valid VNF and iface names
+        for iface in ifaces_list:
+            if iface[0] not in vnfs and iface[0] not in other_nets :
+                return -HTTP_Bad_Request, "format error. Invalid VNF name at 'topology':'connections':'%s':'nodes':'%s'" % (str(k), iface[0])
+            if iface[0] in vnfs and iface[1] not in vnfs[ iface[0] ]['ifaces']:
+                return -HTTP_Bad_Request, "format error. Invalid interface name at 'topology':'connections':'%s':'nodes':'%s':'%s'" % (str(k), iface[0], iface[1])
+
+#1.5 unify connections from the pair list to a consolidated list
+    index=0
+    while index < len(conections_list):
+        index2 = index+1
+        while index2 < len(conections_list):
+            if len(conections_list[index] & conections_list[index2])>0: #common interface, join nets
+                conections_list[index] |= conections_list[index2]
+                del conections_list[index2]
+            else:
+                index2 += 1
+        conections_list[index] = list(conections_list[index])  # from set to list again
+        index += 1
+    #for k in conections_list:
+    #    print k
+    
+
+
+#1.6 Delete non external nets
+#    for k in other_nets.keys():
+#        if other_nets[k]['model']=='bridge' or other_nets[k]['model']=='dataplane_net' or other_nets[k]['model']=='bridge_net':
+#            for con in conections_list:
+#                delete_indexes=[]
+#                for index in range(0,len(con)):
+#                    if con[index][0] == k: delete_indexes.insert(0,index) #order from higher to lower
+#                for index in delete_indexes:
+#                    del con[index]
+#            del other_nets[k]
+#1.7: Check external_ports are present at database table datacenter_nets
+    for k,net in other_nets.items():
+        error_pos = "'topology':'nodes':'" + k + "'"
+        if net['external']==False:
+            if 'name' not in net:
+                net['name']=k
+            if 'model' not in net:
+                return -HTTP_Bad_Request, "needed a 'model' at " + error_pos
+            if net['model']=='bridge_net':
+                net['type']='bridge';
+            elif net['model']=='dataplane_net':
+                net['type']='data';
+            else:
+                return -HTTP_Bad_Request, "unknown 'model' '"+ net['model'] +"' at " + error_pos
+        else: #external
+#IF we do not want to check that external network exist at datacenter
+            pass
+#ELSE    
+#             error_text = ""
+#             WHERE_={}
+#             if 'net_id' in net:
+#                 error_text += " 'net_id' " +  net['net_id']
+#                 WHERE_['uuid'] = net['net_id']
+#             if 'model' in net:
+#                 error_text += " 'model' " +  net['model']
+#                 WHERE_['name'] = net['model']
+#             if len(WHERE_) == 0:
+#                 return -HTTP_Bad_Request, "needed a 'net_id' or 'model' at " + error_pos
+#             r,net_db = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
+#                 FROM='datacenter_nets', WHERE=WHERE_ )
+#             if r<0:
+#                 print "nfvo.new_scenario Error getting datacenter_nets",r,net_db
+#             elif r==0:
+#                 print "nfvo.new_scenario Error" +error_text+ " is not present at database"
+#                 return -HTTP_Bad_Request, "unknown " +error_text+ " at " + error_pos
+#             elif r>1:
+#                 print "nfvo.new_scenario Error more than one external_network for " +error_text+ " is present at database" 
+#                 return -HTTP_Bad_Request, "more than one external_network for " +error_text+ "at "+ error_pos + " Concrete with 'net_id'" 
+#             other_nets[k].update(net_db[0])
+#ENDIF    
+    net_list={}
+    net_nb=0  #Number of nets
+    for con in conections_list:
+        #check if this is connected to a external net
+        other_net_index=-1
+        #print
+        #print "con", con
+        for index in range(0,len(con)):
+            #check if this is connected to a external net
+            for net_key in other_nets.keys():
+                if con[index][0]==net_key:
+                    if other_net_index>=0:
+                        error_text="There is some interface connected both to net '%s' and net '%s'" % (con[other_net_index][0], net_key) 
+                        print "nfvo.new_scenario " + error_text
+                        return -HTTP_Bad_Request, error_text
+                    else:
+                        other_net_index = index
+                        net_target = net_key
+                    break
+        #print "other_net_index", other_net_index
+        try:
+            if other_net_index>=0:
+                del con[other_net_index]
+#IF we do not want to check that external network exist at datacenter
+                if other_nets[net_target]['external'] :
+                    if "name" not in other_nets[net_target]:
+                        other_nets[net_target]['name'] =  other_nets[net_target]['model']
+                    if other_nets[net_target]["type"] == "external_network":
+                        if vnfs[ con[0][0] ]['ifaces'][ con[0][1] ]["type"] == "data":
+                            other_nets[net_target]["type"] =  "data"
+                        else:
+                            other_nets[net_target]["type"] =  "bridge"
+#ELSE    
+#                 if other_nets[net_target]['external'] :
+#                     type_='data' if len(con)>1 else 'ptp'  #an external net is connected to a external port, so it is ptp if only one connection is done to this net
+#                     if type_=='data' and other_nets[net_target]['type']=="ptp":
+#                         error_text = "Error connecting %d nodes on a not multipoint net %s" % (len(con), net_target)
+#                         print "nfvo.new_scenario " + error_text
+#                         return -HTTP_Bad_Request, error_text
+#ENDIF    
+                for iface in con:
+                    vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
+            else:
+                #create a net
+                net_type_bridge=False
+                net_type_data=False
+                net_target = "__-__net"+str(net_nb)
+                net_list[net_target] = {'name': "net-"+str(net_nb), 'description':"net-%s in scenario %s" %(net_nb,topo['name']),
+                    'external':False} 
+                for iface in con:
+                    vnfs[ iface[0] ]['ifaces'][ iface[1] ]['net_key'] = net_target
+                    iface_type = vnfs[ iface[0] ]['ifaces'][ iface[1] ]['type']
+                    if iface_type=='mgmt' or iface_type=='bridge':
+                        net_type_bridge = True
+                    else:
+                        net_type_data = True
+                if net_type_bridge and net_type_data:
+                    error_text = "Error connection interfaces of bridge type with data type. Firs node %s, iface %s" % (iface[0], iface[1])
+                    print "nfvo.new_scenario " + error_text
+                    return -HTTP_Bad_Request, error_text
+                elif net_type_bridge:
+                    type_='bridge'
+                else:
+                    type_='data' if len(con)>2 else 'ptp'
+                net_list[net_target]['type'] = type_
+                net_nb+=1
+        except Exception:
+            error_text = "Error connection node %s : %s does not match any VNF or interface" % (iface[0], iface[1])
+            print "nfvo.new_scenario " + error_text
+            #raise e
+            return -HTTP_Bad_Request, error_text
+
+#1.8: Connect to management net all not already connected interfaces of type 'mgmt'
+    #1.8.1 obtain management net 
+    r,mgmt_net = mydb.get_table(SELECT=('uuid','name','description','type','shared'),
+        FROM='datacenter_nets', WHERE={'name':'mgmt'} )
+    #1.8.2 check all interfaces from all vnfs 
+    if r>0:
+        add_mgmt_net = False
+        for vnf in vnfs.values():
+            for iface in vnf['ifaces'].values():
+                if iface['type']=='mgmt' and 'net_key' not in iface:
+                    #iface not connected
+                    iface['net_key'] = 'mgmt'
+                    add_mgmt_net = True
+        if add_mgmt_net and 'mgmt' not in net_list:
+            net_list['mgmt']=mgmt_net[0]
+            net_list['mgmt']['external']=True
+            net_list['mgmt']['graph']={'visible':False}
+
+    net_list.update(other_nets)
+    print
+    print 'net_list', net_list
+    print
+    print 'vnfs', vnfs
+    print
+
+#2: insert scenario. filling tables scenarios,sce_vnfs,sce_interfaces,sce_nets
+    r,c = mydb.new_scenario( { 'vnfs':vnfs, 'nets':net_list,
+        'tenant_id':tenant_id, 'name':topo['name'], 'description':topo.get('description',topo['name']) } )
+    
+    return r,c
+
+def new_scenario_v02(mydb, tenant_id, scenario):
+
+    if tenant_id != "any":
+        if not check_tenant(mydb, tenant_id): 
+            print 'nfvo.new_scenario_v02() tenant %s not found' % tenant_id
+            return -HTTP_Not_Found, 'Tenant %s not found' % tenant_id
+        if "tenant_id" in scenario:
+            if scenario["tenant_id"] != tenant_id:
+                print "nfvo.new_scenario_v02() tenant '%s' not found" % tenant_id
+                return -HTTP_Unauthorized, "VNF can not have a different tenant owner '%s', must be '%s'" %(scenario["tenant_id"], tenant_id)
+    else:
+        tenant_id=None
+
+#1: Check that VNF are present at database table vnfs and update content into scenario dict
+    for name,vnf in scenario["vnfs"].iteritems():
+        WHERE_={}
+        error_text = ""
+        error_pos = "'topology':'nodes':'" + name + "'"
+        if 'vnf_id' in vnf:
+            error_text += " 'vnf_id' " +  vnf['vnf_id']
+            WHERE_['uuid'] = vnf['vnf_id']
+        if 'vnf_model' in vnf:
+            error_text += " 'vnf_model' " +  vnf['vnf_model']
+            WHERE_['name'] = vnf['vnf_model']
+        if len(WHERE_) == 0:
+            return -HTTP_Bad_Request, "needed a 'vnf_id' or 'VNF model' at " + error_pos
+        r,vnf_db = mydb.get_table(SELECT=('uuid','name','description'), FROM='vnfs', WHERE=WHERE_)
+        if r<0:
+            print "nfvo.new_scenario Error getting vnfs",r,vnf_db
+        elif r==0:
+            print "nfvo.new_scenario Error" + error_text + " is not present at database"
+            return -HTTP_Bad_Request, "unknown" + error_text + " at " + error_pos
+        elif r>1:
+            print "nfvo.new_scenario Error more than one" + error_text + " are present at database"
+            return -HTTP_Bad_Request, "more than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'"
+        vnf['uuid']=vnf_db[0]['uuid']
+        vnf['description']=vnf_db[0]['description']
+        vnf['ifaces'] = {}
+        #get external interfaces
+        r,ext_ifaces = mydb.get_table(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'), 
+            FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id', 
+            WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
+        if r<0:
+            print "nfvo.new_scenario Error getting external interfaces of vnfs",r,ext_ifaces
+            return -HTTP_Internal_Server_Error, "Error getting external interfaces of vnfs: " + ext_ifaces
+        for ext_iface in ext_ifaces:
+            vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
+
+#2: Insert net_key at every vnf interface
+    for net_name,net in scenario["networks"].iteritems():
+        net_type_bridge=False
+        net_type_data=False
+        for iface_dict in net["interfaces"]:
+            for vnf,iface in iface_dict.iteritems():
+                if vnf not in scenario["vnfs"]:
+                    error_text = "Error at 'networks':'%s':'interfaces' VNF '%s' not match any VNF at 'vnfs'" % (net_name, vnf)
+                    print "nfvo.new_scenario_v02 " + error_text
+                    return -HTTP_Bad_Request, error_text
+                if iface not in scenario["vnfs"][vnf]['ifaces']:
+                    error_text = "Error at 'networks':'%s':'interfaces':'%s' interface not match any VNF interface" % (net_name, iface)
+                    print "nfvo.new_scenario_v02 " + error_text
+                    return -HTTP_Bad_Request, error_text
+                if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
+                    error_text = "Error at 'networks':'%s':'interfaces':'%s' interface already connected at network '%s'" \
+                                    % (net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
+                    print "nfvo.new_scenario_v02 " + error_text
+                    return -HTTP_Bad_Request, error_text
+                scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
+                iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
+                if iface_type=='mgmt' or iface_type=='bridge':
+                    net_type_bridge = True
+                else:
+                    net_type_data = True
+        if net_type_bridge and net_type_data:
+            error_text = "Error connection interfaces of bridge type and data type at 'networks':'%s':'interfaces'" % (net_name)
+            print "nfvo.new_scenario " + error_text
+            return -HTTP_Bad_Request, error_text
+        elif net_type_bridge:
+            type_='bridge'
+        else:
+            type_='data' if len(net["interfaces"])>2 else 'ptp'
+        net['type'] = type_
+        net['name'] = net_name
+        net['external'] = net.get('external', False)
+
+#3: insert at database
+    scenario["nets"] = scenario["networks"]
+    scenario['tenant_id'] = tenant_id
+    r,c = mydb.new_scenario( scenario)
+    
+    return r,c
+
+def edit_scenario(mydb, tenant_id, scenario_id, data):
+    data["uuid"] = scenario_id
+    data["tenant_id"] = tenant_id
+    r,c = mydb.edit_scenario( data )
+    return r,c
+
+def start_scenario(mydb, tenant_id, scenario_id, instance_scenario_name, instance_scenario_description, datacenter=None,vim_tenant=None, startvms=True):
+    print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
+    datacenter_id = None
+    datacenter_name=None
+    if datacenter != None:
+        if af.check_valid_uuid(datacenter): 
+            datacenter_id = datacenter
+        else:
+            datacenter_name = datacenter
+    result, vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, vim_tenant)
+    if result < 0:
+        print "start_scenario error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result > 1:
+        print "start_scenario error. Several datacenters available, you must concrete"
+        return -HTTP_Bad_Request, "Several datacenters available, you must concrete"
+    myvim = vims.values()[0]
+    myvim_tenant = myvim['tenant']
+    datacenter_id = myvim['id']
+    datacenter_name = myvim['name']
+    datacenter_tenant_id = myvim['config']['datacenter_tenant_id']
+    rollbackList=[]
+
+    print "Checking that the scenario_id exists and getting the scenario dictionary"
+    result, scenarioDict = mydb.get_scenario(scenario_id, tenant_id, datacenter_id)
+    if result < 0:
+        print "start_scenario error. Error interacting with NFVO DB"
+        return result, scenarioDict
+    elif result == 0:
+        print "start_scenario error. Scenario not found"
+        return result, scenarioDict
+
+    scenarioDict['datacenter_tenant_id'] = datacenter_tenant_id
+    scenarioDict['datacenter_id'] = datacenter_id
+    print '================scenarioDict======================='
+    #print json.dumps(scenarioDict, indent=4)
+    print 'BEGIN launching instance scenario "%s" based on "%s"' % (instance_scenario_name,scenarioDict['name'])
+
+    print "Scenario %s: consisting of %d VNF(s)" % (scenarioDict['name'],len(scenarioDict['vnfs']))
+    print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
+    
+    auxNetDict = {}   #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
+    auxNetDict['scenario'] = {}
+    
+    print "1. Creating new nets (sce_nets) in the VIM"
+    for sce_net in scenarioDict['nets']:
+        print "Net name: %s. Description: %s" % (sce_net["name"], sce_net["description"])
+        
+        myNetName = "%s.%s" % (instance_scenario_name, sce_net['name'])
+        myNetName = myNetName[0:255] #limit length
+        myNetType = sce_net['type']
+        myNetDict = {}
+        myNetDict["name"] = myNetName
+        myNetDict["type"] = myNetType
+        myNetDict["tenant_id"] = myvim_tenant
+        #TODO:
+        #We should use the dictionary as input parameter for new_tenant_network
+        print myNetDict
+        if not sce_net["external"]:
+            result, network_id = myvim.new_tenant_network(myNetName, myNetType)
+            if result < 0:
+                print "Error creating network: %s." %network_id
+                _, message = rollback(mydb, vims, rollbackList)
+                return result, "Error creating network: "+ network_id + "."+message
+
+            print "New VIM network created for scenario %s. Network id:  %s" % (scenarioDict['name'],network_id)
+            sce_net['vim_id'] = network_id
+            auxNetDict['scenario'][sce_net['uuid']] = network_id
+            rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
+        else:
+            if sce_net['vim_id'] == None:
+                error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
+                _, message = rollback(mydb, vims, rollbackList)
+                print "nfvo.start_scenario: " + error_text
+                return -HTTP_Bad_Request, error_text
+            print "Using existent VIM network for scenario %s. Network id %s" % (scenarioDict['name'],sce_net['vim_id'])
+            auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
+    
+    print "2. Creating new nets (vnf internal nets) in the VIM"
+    #For each vnf net, we create it and we add it to instanceNetlist.
+    for sce_vnf in scenarioDict['vnfs']:
+        for net in sce_vnf['nets']:
+            print "Net name: %s. Description: %s" % (net["name"], net["description"])
+            
+            myNetName = "%s.%s" % (instance_scenario_name,net['name'])
+            myNetName = myNetName[0:255] #limit length
+            myNetType = net['type']
+            myNetDict = {}
+            myNetDict["name"] = myNetName
+            myNetDict["type"] = myNetType
+            myNetDict["tenant_id"] = myvim_tenant
+            print myNetDict
+            #TODO:
+            #We should use the dictionary as input parameter for new_tenant_network
+            result, network_id = myvim.new_tenant_network(myNetName, myNetType)
+            if result < 0:
+                error_text="Error creating network: %s." % network_id
+                _, message = rollback(mydb, vims, rollbackList)
+                error_text += message
+                print "start_scenario: " + error_text
+                return result, error_text
+            print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
+            net['vim_id'] = network_id
+            if sce_vnf['uuid'] not in auxNetDict:
+                auxNetDict[sce_vnf['uuid']] = {}
+            auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
+            rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
+
+    print "auxNetDict:"
+    print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
+    
+    print "3. Creating new vm instances in the VIM"
+    #myvim.new_tenant_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
+    i = 0
+    for sce_vnf in scenarioDict['vnfs']:
+        for vm in sce_vnf['vms']:
+            i += 1
+            myVMDict = {}
+            #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
+            myVMDict['name'] = "%s.%s.%d" % (instance_scenario_name,sce_vnf['name'],i)
+            #myVMDict['description'] = vm['description']
+            myVMDict['description'] = myVMDict['name'][0:99]
+            if not startvms:
+                myVMDict['start'] = "no"
+            myVMDict['name'] = myVMDict['name'][0:255] #limit name length
+            print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
+            
+            #create image at vim in case it not exist
+            res, image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
+            if res<0:
+                print "start_scenario error getting image", image_dict
+                return res, image_dict
+            res, image_id = create_or_use_image(mydb, vims, image_dict, [], True)                
+            if res < 0:
+                print "start_scenario error adding image to VIM", image_dict
+                return res, image_id
+            vm['vim_image_id'] = image_id
+                
+            #create flavor at vim in case it not exist
+            res, flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
+            if res<0:
+                print "start_scenario error getting flavor", flavor_dict
+                return res, flavor_dict
+            if flavor_dict['extended']!=None:
+                flavor_dict['extended']= yaml.load(flavor_dict['extended'])
+            res, flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)                
+            if res < 0:
+                print "start_scenario error adding flavor to VIM", flavor_dict
+                return res, flavor_id
+            vm['vim_flavor_id'] = flavor_id
+
+            
+            myVMDict['imageRef'] = vm['vim_image_id']
+            myVMDict['flavorRef'] = vm['vim_flavor_id']
+            myVMDict['networks'] = []
+            for iface in vm['interfaces']:
+                netDict = {}
+                if iface['type']=="data":
+                    netDict['type'] = iface['model']
+                elif "model" in iface and iface["model"]!=None:
+                    netDict['model']=iface['model']
+                #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
+                #discover type of interface looking at flavor
+                for numa in flavor_dict.get('extended',{}).get('numas',[]):
+                    for flavor_iface in numa.get('interfaces',[]):
+                        if flavor_iface.get('name') == iface['internal_name']:
+                            if flavor_iface['dedicated'] == 'yes':
+                                netDict['type']="PF"    #passthrough
+                            elif flavor_iface['dedicated'] == 'no':
+                                netDict['type']="VF"    #siov
+                            elif flavor_iface['dedicated'] == 'yes:sriov':
+                                netDict['type']="VFnotShared"   #sriov but only one sriov on the PF
+                            netDict["mac_address"] = flavor_iface.get("mac_address")
+                            break;
+                netDict["use"]=iface['type']
+                if netDict["use"]=="data" and not netDict.get("type"):
+                    #print "netDict", netDict
+                    #print "iface", iface
+                    e_text = "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf['name'], vm['name'], iface['internal_name'])
+                    if flavor_dict.get('extended')==None:
+                        return -HTTP_Conflict, e_text + "After database migration some information is not available. \
+                                Try to delete and create the scenarios and VNFs again"
+                    else:
+                        return -HTTP_Internal_Server_Error, e_text
+                if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
+                    netDict["type"]="virtual"
+                if "vpci" in iface and iface["vpci"] is not None:
+                    netDict['vpci'] = iface['vpci']
+                if "mac" in iface and iface["mac"] is not None:
+                    netDict['mac_address'] = iface['mac']
+                netDict['name'] = iface['internal_name']
+                if iface['net_id'] is None:
+                    for vnf_iface in sce_vnf["interfaces"]:
+                        print iface
+                        print vnf_iface
+                        if vnf_iface['interface_id']==iface['uuid']:
+                            netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
+                            break
+                else:
+                    netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
+                #skip bridge ifaces not connected to any net
+                #if 'net_id' not in netDict or netDict['net_id']==None:
+                #    continue
+                myVMDict['networks'].append(netDict)
+            print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
+            print myVMDict['name']
+            print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
+            print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
+            print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
+            result, vm_id = myvim.new_tenant_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
+                    myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
+            if result < 0:
+                error_text = "Error creating vm instance: %s." % vm_id
+                _, message = rollback(mydb, vims, rollbackList)
+                error_text += message
+                print "start_scenario: " + error_text
+                return result, error_text
+            print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
+            vm['vim_id'] = vm_id
+            rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
+            #put interface uuid back to scenario[vnfs][vms[[interfaces]
+            for net in myVMDict['networks']:
+                if "vim_id" in net:
+                    for iface in vm['interfaces']:
+                        if net["name"]==iface["internal_name"]:
+                            iface["vim_id"]=net["vim_id"]
+                            break
+    
+    print "==================Deployment done=========="
+    print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
+    #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
+    result,c = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
+    if result <0:
+        error_text = c + "." 
+        _, message = rollback(mydb, vims, rollbackList)
+        error_text += message
+        print "start_scenario: " + error_text
+        return result, error_text
+        
+    return mydb.get_instance_scenario(c)
+
+def create_instance(mydb, tenant_id, instance_dict):
+    print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
+    scenario = instance_dict["scenario"]
+    datacenter_id = None
+    datacenter_name=None
+    datacenter = instance_dict.get("datacenter")
+    if datacenter:
+        if af.check_valid_uuid(datacenter): 
+            datacenter_id = datacenter
+        else:
+            datacenter_name = datacenter
+    result, vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, vim_tenant=None)
+    if result < 0:
+        print "start_scenario error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result > 1:
+        print "start_scenario error. Several datacenters available, you must concrete"
+        return -HTTP_Bad_Request, "Several datacenters available, you must concrete"
+    myvim = vims.values()[0]
+    #myvim_tenant = myvim['tenant']
+    datacenter_id = myvim['id']
+    datacenter_name = myvim['name']
+    datacenter_tenant_id = myvim['config']['datacenter_tenant_id']
+    rollbackList=[]
+
+    print "Checking that the scenario exists and getting the scenario dictionary"
+    result, scenarioDict = mydb.get_scenario(scenario, tenant_id, datacenter_id)
+    if result < 0:
+        print "start_scenario error. Error interacting with NFVO DB"
+        return result, scenarioDict
+    elif result == 0:
+        print "start_scenario error. Scenario not found"
+        return result, scenarioDict
+
+    scenarioDict['datacenter_tenant_id'] = datacenter_tenant_id
+    scenarioDict['datacenter_id'] = datacenter_id
+    
+    auxNetDict = {}   #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
+    auxNetDict['scenario'] = {}
+    
+    print "scenario dict: ",yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) 
+    instance_name = instance_dict["name"]
+    instance_description = instance_dict.get("description")
+    try:
+    #0 check correct parameters
+        for descriptor_net in instance_dict.get("networks",{}).keys():
+            found=False
+            for scenario_net in scenarioDict['nets']:
+                if descriptor_net == scenario_net["name"]:
+                    found = True
+                    break
+            if not found:
+                raise NfvoException(-HTTP_Bad_Request, "Invalid scenario network name '%s' at instance:networks" % descriptor_net )
+        for descriptor_vnf in instance_dict.get("vnfs",{}).keys():
+            found=False
+            for scenario_vnf in scenarioDict['vnfs']:
+                if descriptor_vnf == scenario_vnf['name']:
+                    found = True
+                    break
+            if not found:
+                raise NfvoException(-HTTP_Bad_Request, "Invalid vnf name '%s' at instance:vnfs" % descriptor_vnf )
+        
+    #1. Creating new nets (sce_nets) in the VIM"
+        for sce_net in scenarioDict['nets']:
+            descriptor_net =  instance_dict.get("networks",{}).get(sce_net["name"],{})
+            net_name = descriptor_net.get("name")
+            net_type = sce_net['type']
+            lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True
+            if sce_net["external"]:
+                if not net_name:
+                    net_name = sce_net["name"] 
+                if "netmap-use" in descriptor_net or "netmap-create" in descriptor_net:
+                    create_network = False
+                    lookfor_network = False
+                    if "netmap-use" in descriptor_net:
+                        lookfor_network = True
+                        if af.check_valid_uuid(descriptor_net["netmap-use"]):
+                            filter_text = "scenario id '%s'" % descriptor_net["netmap-use"]
+                            lookfor_filter["id"] = descriptor_net["netmap-use"]
+                        else: 
+                            filter_text = "scenario name '%s'" % descriptor_net["netmap-use"]
+                            lookfor_filter["name"] = descriptor_net["netmap-use"]
+                    if "netmap-create" in descriptor_net:
+                        create_network = True
+                        net_vim_name = net_name
+                        if descriptor_net["netmap-create"]:
+                            net_vim_name= descriptor_net["netmap-create"]
+                        
+                elif sce_net['vim_id'] != None:
+                    #there is a netmap at datacenter_nets database
+                    create_network = False
+                    lookfor_network = True
+                    lookfor_filter["id"] = sce_net['vim_id']
+                    filter_text = "vim_id '%s' datacenter_netmap name '%s'. Try to reload vims with datacenter-net-update" % (sce_net['vim_id'], sce_net["name"])
+                    #look for network at datacenter and return error
+                else:
+                    #There is not a netmap, look at datacenter for a net with this name and create if not found
+                    create_network = True
+                    lookfor_network = True
+                    lookfor_filter["name"] = sce_net["name"]
+                    net_vim_name = sce_net["name"]
+                    filter_text = "scenario name '%s'" % sce_net["name"]
+            else:
+                if not net_name:
+                    net_name = "%s.%s" %(instance_name, sce_net["name"])
+                    net_name = net_name[:255]     #limit length
+                net_vim_name = net_name
+                create_network = True
+                lookfor_network = False
+                
+            if lookfor_network:
+                result, vim_nets = myvim.get_network_list(filter_dict=lookfor_filter)
+                if result < 0:
+                    raise NfvoException(result, "Not possible to get vim network list " + vim_nets)
+                elif len(vim_nets) > 1:
+                    raise NfvoException(-HTTP_Bad_Request, "More than one candidate VIM network found for " + filter_text )
+                elif len(vim_nets) == 0:
+                    if not create_network:
+                        raise NfvoException(-HTTP_Bad_Request, "No candidate VIM network found for " + filter_text )
+                else:
+                    sce_net['vim_id'] = vim_nets[0]['id']
+                    auxNetDict['scenario'][sce_net['uuid']] = vim_nets[0]['id']
+                    create_network = False
+            if create_network:
+                #if network is not external
+                result, network_id = myvim.new_tenant_network(net_vim_name, net_type)
+                if result < 0:
+                    raise NfvoException(result, "Error creating vim network " + network_id)
+                sce_net['vim_id'] = network_id
+                auxNetDict['scenario'][sce_net['uuid']] = network_id
+                rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
+        
+    #2. Creating new nets (vnf internal nets) in the VIM"
+        #For each vnf net, we create it and we add it to instanceNetlist.
+        for sce_vnf in scenarioDict['vnfs']:
+            for net in sce_vnf['nets']:
+                descriptor_net =  instance_dict.get("vnfs",{}).get(sce_vnf["name"],{})
+                net_name = descriptor_net.get("name")
+                if not net_name:
+                    net_name = "%s.%s" %(instance_name, net["name"])
+                    net_name = net_name[:255]     #limit length
+                net_type = net['type']
+                result, network_id = myvim.new_tenant_network(net_name, net_type)
+                if result < 0:
+                    raise NfvoException(result, "Error creating vim network " + network_id)
+                net['vim_id'] = network_id
+                if sce_vnf['uuid'] not in auxNetDict:
+                    auxNetDict[sce_vnf['uuid']] = {}
+                auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
+                rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
+    
+        print "auxNetDict:"
+        print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
+        
+    #3. Creating new vm instances in the VIM
+        #myvim.new_tenant_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
+        for sce_vnf in scenarioDict['vnfs']:
+            i = 0
+            for vm in sce_vnf['vms']:
+                i += 1
+                myVMDict = {}
+                myVMDict['name'] = "%s.%s.%d" % (instance_name,sce_vnf['name'],i)
+                myVMDict['description'] = myVMDict['name'][0:99]
+#                if not startvms:
+#                    myVMDict['start'] = "no"
+                myVMDict['name'] = myVMDict['name'][0:255] #limit name length
+                #create image at vim in case it not exist
+                res, image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
+                if res<0:
+                    raise NfvoException(result, "Error getting VIM image "+ image_dict)
+                res, image_id = create_or_use_image(mydb, vims, image_dict, [], True)                
+                if res < 0:
+                    raise NfvoException(result, "Error adding image to VIM " + image_dict)
+                vm['vim_image_id'] = image_id
+                    
+                #create flavor at vim in case it not exist
+                res, flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
+                if res<0:
+                    raise NfvoException(result, "Error getting VIM flavor "+ flavor_dict)
+                if flavor_dict['extended']!=None:
+                    flavor_dict['extended']= yaml.load(flavor_dict['extended'])
+                res, flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)                
+                if res < 0:
+                    raise NfvoException(result, "Error adding flavor to VIM" + flavor_dict)
+                vm['vim_flavor_id'] = flavor_id
+                
+                myVMDict['imageRef'] = vm['vim_image_id']
+                myVMDict['flavorRef'] = vm['vim_flavor_id']
+                myVMDict['networks'] = []
+#TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
+                for iface in vm['interfaces']:
+                    netDict = {}
+                    if iface['type']=="data":
+                        netDict['type'] = iface['model']
+                    elif "model" in iface and iface["model"]!=None:
+                        netDict['model']=iface['model']
+                    #TODO in future, remove this because mac_address will not be set, and the type of PV,VF is obtained from iterface table model
+                    #discover type of interface looking at flavor
+                    for numa in flavor_dict.get('extended',{}).get('numas',[]):
+                        for flavor_iface in numa.get('interfaces',[]):
+                            if flavor_iface.get('name') == iface['internal_name']:
+                                if flavor_iface['dedicated'] == 'yes':
+                                    netDict['type']="PF"    #passthrough
+                                elif flavor_iface['dedicated'] == 'no':
+                                    netDict['type']="VF"    #siov
+                                elif flavor_iface['dedicated'] == 'yes:sriov':
+                                    netDict['type']="VFnotShared"   #sriov but only one sriov on the PF
+                                netDict["mac_address"] = flavor_iface.get("mac_address")
+                                break;
+                    netDict["use"]=iface['type']
+                    if netDict["use"]=="data" and not netDict.get("type"):
+                        #print "netDict", netDict
+                        #print "iface", iface
+                        e_text = "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf['name'], vm['name'], iface['internal_name'])
+                        if flavor_dict.get('extended')==None:
+                            raise NfvoException(-HTTP_Conflict, e_text + "After database migration some information is not available. \
+                                    Try to delete and create the scenarios and VNFs again")
+                        else:
+                            raise NfvoException(-HTTP_Internal_Server_Error, e_text)
+                    if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
+                        netDict["type"]="virtual"
+                    if "vpci" in iface and iface["vpci"] is not None:
+                        netDict['vpci'] = iface['vpci']
+                    if "mac" in iface and iface["mac"] is not None:
+                        netDict['mac_address'] = iface['mac']
+                    netDict['name'] = iface['internal_name']
+                    if iface['net_id'] is None:
+                        for vnf_iface in sce_vnf["interfaces"]:
+                            print iface
+                            print vnf_iface
+                            if vnf_iface['interface_id']==iface['uuid']:
+                                netDict['net_id'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
+                                break
+                    else:
+                        netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
+                    #skip bridge ifaces not connected to any net
+                    #if 'net_id' not in netDict or netDict['net_id']==None:
+                    #    continue
+                    myVMDict['networks'].append(netDict)
+                print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
+                print myVMDict['name']
+                print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
+                print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
+                print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
+                result, vm_id = myvim.new_tenant_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
+                        myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
+                if result < 0:
+                    raise NfvoException(result, "Error creating VIM instance" + vm_id)
+                vm['vim_id'] = vm_id
+                rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
+                #put interface uuid back to scenario[vnfs][vms[[interfaces]
+                for net in myVMDict['networks']:
+                    if "vim_id" in net:
+                        for iface in vm['interfaces']:
+                            if net["name"]==iface["internal_name"]:
+                                iface["vim_id"]=net["vim_id"]
+                                break
+        print "==================Deployment done=========="
+        print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
+        #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
+        result,c = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_name, instance_description, scenarioDict)
+        if result <0:
+            raise NfvoException(result, c)
+            
+        return mydb.get_instance_scenario(c)
+    except NfvoException as e:
+        error_text = str(e) + ". " 
+        _, message = rollback(mydb, vims, rollbackList)
+        error_text += message
+        print "create_instance: " + error_text
+        return e.error_code, error_text
+
+def delete_instance(mydb, tenant_id, instance_id):
+    print "Checking that the instance_id exists and getting the instance dictionary"
+    result, instanceDict = mydb.get_instance_scenario(instance_id, tenant_id)
+    if result < 0:
+        print "nfvo.delete_instance() error. Error getting info from database"
+        return result, instanceDict
+    elif result == 0:
+        print "delete_instance error. Instance not found"
+        return result, instanceDict
+    print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
+    tenant_id = instanceDict["tenant_id"]
+    print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
+    result, vims = get_vim(mydb, tenant_id, instanceDict['datacenter_id'])
+    if result < 0:
+        print "nfvo.delete_instance() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        print "!!!!!! nfvo.delete_instance() datacenter not found!!!!" 
+        myvim = None
+    else:
+        myvim = vims.values()[0]
+    
+    
+    #1. Delete from Database
+
+    #result,c = mydb.delete_row('instance_scenarios', instance_id, nfvo_tenant)
+    result,c = mydb.delete_instance_scenario(instance_id, tenant_id)
+    if result<0:
+        return result, c
+
+    #2. delete from VIM
+    if not myvim:
+        error_msg = "Not possible to delete VIM VMs and networks. Datacenter not found at database!!!"
+    else:
+        error_msg = ""
+
+    #2.1 deleting VMs
+    #vm_fail_list=[]
+    for sce_vnf in instanceDict['vnfs']:
+        if not myvim:
+            continue
+        for vm in sce_vnf['vms']:
+            result, vm_id = myvim.delete_tenant_vminstance(vm['vim_vm_id'])
+            if result < 0:
+                error_msg+="\n    Error: " + str(-result) + " VM id=" + vm['vim_vm_id']
+                #if result != -HTTP_Not_Found: vm_fail_list.append(vm)
+                print "Error " + str(-result) + " deleting VM instance '" + vm['name'] + "', uuid '" + vm['uuid'] + "', VIM id '" + vm['vim_vm_id'] + "', from VNF_id '" + sce_vnf['vnf_id'] + "':"  + vm_id
+    
+    #2.2 deleting NETS
+    #net_fail_list=[]
+    for net in instanceDict['nets']:
+        if net['external']:
+            continue #skip not created nets
+        if not myvim:
+            continue
+        result, net_id = myvim.delete_tenant_network(net['vim_net_id'])
+        if result < 0:
+            error_msg += "\n    Error: " + str(-result) + " NET id=" + net['vim_net_id']
+            #if result == -HTTP_Not_Found: net_fail_list.append(net)
+            print "Error " + str(-result) + " deleting NET uuid '" + net['uuid'] + "', VIM id '" + net['vim_net_id'] + "':"  + net_id
+
+    if len(error_msg)>0:
+        return 1, 'instance ' + instance_id + ' deleted but some elements could not be deleted, or already deleted (error: 404) from VIM: ' + error_msg
+    else:
+        return 1, 'instance ' + instance_id + ' deleted'
+
+def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None):
+    '''Refreshes a scenario instance. It modifies instanceDict'''
+    '''Returns:
+         - result: <0 if there is any unexpected error, n>=0 if no errors where n is the number of vms and nets that couldn't be updated in the database
+         - error_msg
+    '''
+    # Assumption: nfvo_tenant and instance_id were checked before entering into this function
+    print "nfvo.refresh_instance begins"
+    #print json.dumps(instanceDict, indent=4)
+    
+    print "Getting the VIM URL and the VIM tenant_id"
+    result, vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
+    if result < 0:
+        print "nfvo.refresh_instance() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(instanceDict['datacenter_id'])
+    myvim = vims.values()[0]
+     
+    # 1. Getting the status of all VMs
+    vmDict = {}
+    netDict = {}
+    for sce_vnf in instanceDict['vnfs']:
+        for vm in sce_vnf['vms']:
+            vmDict[vm['vim_vm_id']]=None
+            print "VACA vm", vm
+    
+    # 2. Getting the status of all nets
+    # TODO: update nets inside a vnf
+    for net in instanceDict['nets']:
+        #if net['external']:
+        #    continue #skip not created nets
+        netDict[net['vim_net_id']]=None
+    # 3. Refresh the status of VMs and nets from VIM. IT updates vmDict and netDict
+    result, refresh_message = myvim.refresh_tenant_vms_and_nets(vmDict, netDict)
+    if result < 0:
+        return result, refresh_message
+    
+    # 4. Update the status of VMs in the instanceDict, while collects the VMs whose status changed     
+    vms_updated = [] #List of VM instance uuids in openmano that were updated
+    vms_notupdated=[]
+    for sce_vnf in instanceDict['vnfs']:
+        for vm in sce_vnf['vms']:
+            vm_id = vm['vim_vm_id']
+            interfaces = vmDict[vm_id].pop('interfaces', [])
+            #4.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
+            has_mgmt_iface = False
+            for iface in vm["interfaces"]:
+                if iface["type"]=="mgmt":
+                    has_mgmt_iface = True
+            if vmDict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
+                    vmDict[vm_id]['status'] = "ACTIVE"
+            if vm['status'] != vmDict[vm_id]['status'] or vm.get('error_msg')!=vmDict[vm_id].get('error_msg') or vm.get('vim_info')!=vmDict[vm_id].get('vim_info'):
+                vm['status']    = vmDict[vm_id]['status']
+                vm['error_msg'] = vmDict[vm_id].get('error_msg')
+                vm['vim_info']  = vmDict[vm_id].get('vim_info')
+                # 4.1. Update in openmano DB the VMs whose status changed
+                result2, _ = mydb.update_rows('instance_vms', UPDATE=vmDict[vm_id], WHERE={'uuid':vm["uuid"]})
+                if result2<0:
+                    vms_notupdated.append(vm["uuid"])
+                elif result2>0:
+                    vms_updated.append(vm["uuid"])  
+            # 4.2. Update in openmano DB the interface VMs
+            for interface in interfaces:
+                #translate from vim_net_id to instance_net_id
+                network_id=None
+                for net in instanceDict['nets']:
+                    if net["vim_net_id"] == interface["vim_net_id"]:
+                        network_id = net["uuid"]
+                        break
+                if not network_id:
+                    continue
+                del interface["vim_net_id"]
+                result2, _ = mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
+                if result2<0:
+                    print "nfvo.refresh_instance error with vm=%s, interface_net_id=%s" % (vm["uuid"], network_id)
+    # 5. Update the status of nets in the instanceDict, while collects the nets whose status changed     
+    nets_updated = [] #List of net instance uuids in openmano that were updated
+    nets_notupdated=[]
+    # TODO: update nets inside a vnf  
+    for net in instanceDict['nets']:
+        net_id = net['vim_net_id']
+        if net['status'] != netDict[net_id]['status'] or net.get('error_msg')!=netDict[net_id].get('error_msg') or net.get('vim_info')!=netDict[net_id].get('vim_info'):
+            net['status']    = netDict[net_id]['status']
+            net['error_msg'] = netDict[net_id].get('error_msg')
+            net['vim_info']  = netDict[net_id].get('vim_info')
+            # 5.1. Update in openmano DB the nets whose status changed
+            result2, _ = mydb.update_rows('instance_nets', UPDATE=netDict[net_id], WHERE={'uuid':net["uuid"]})
+            if result2<0:
+                nets_notupdated.append(net["uuid"])
+            elif result2>0:
+                nets_updated.append(net["uuid"])  
+
+    # Returns appropriate output
+    print "nfvo.refresh_instance finishes"
+    print "VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s" \
+                % (str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated)) 
+    instance_id = instanceDict['uuid']
+    error_msg=refresh_message
+    if len(vms_notupdated)+len(nets_notupdated)>0:
+        if len(refresh_message)>0:
+            error_msg += "; "
+        error_msg += "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
+        return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
+    
+    return 0, 'Scenario instance ' + instance_id + ' refreshed. ' + error_msg
+
+def instance_action(mydb,nfvo_tenant,instance_id, action_dict):
+    print "Checking that the instance_id exists and getting the instance dictionary"
+    result, instanceDict = mydb.get_instance_scenario(instance_id, nfvo_tenant)
+    if result < 0:
+        print "nfvo.instance_action() error. Error getting info from database"
+        return result, instanceDict
+    elif result == 0:
+        print "instance_action error. Instance not found"
+        return -HTTP_Not_Found, "instance %s not found" % instance_id
+    #print yaml.safe_dump(instanceDict, indent=4, default_flow_style=False)
+
+    print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
+    result, vims = get_vim(mydb, nfvo_tenant, instanceDict['datacenter_id'])
+    if result < 0:
+        print "nfvo.instance_action() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(instanceDict['datacenter_id'])
+    myvim = vims.values()[0]
+    
+
+    input_vnfs = action_dict.pop("vnfs", [])
+    input_vms = action_dict.pop("vms", [])
+    action_over_all = True if len(input_vnfs)==0 and len (input_vms)==0 else False
+    vm_result = {}
+    vm_error = 0
+    vm_ok = 0
+    for sce_vnf in instanceDict['vnfs']:
+        for vm in sce_vnf['vms']:
+            if not action_over_all:
+                if sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \
+                    vm['uuid'] not in input_vms and vm['name'] not in input_vms:
+                    continue
+            result, data = myvim.action_tenant_vminstance(vm['vim_vm_id'], action_dict)
+            if result < 0:
+                vm_result[ vm['uuid'] ] = {"vim_result": -result, "name":vm['name'], "description": data}
+                vm_error+=1
+            else:
+                if "console" in action_dict:
+                    if data["server"]=="127.0.0.1" or data["server"]=="localhost":
+                        vm_result[ vm['uuid'] ] = {"vim_result": -HTTP_Unauthorized,
+                                                   "description": "this console is only reachable by local interface",
+                                                   "name":vm['name']
+                                                }
+                        vm_error+=1
+                        continue
+                    #print "console data", data
+                    r2, console_thread = create_or_use_console_proxy_thread(data["server"], data["port"])
+                    if r2<0:
+                        vm_result[ vm['uuid'] ] = {"vim_result": -r2, "name":vm['name'], "description": console_thread}
+                    else:
+                        vm_result[ vm['uuid'] ] = {"vim_result": result,
+                                                   "description": "%s//%s:%d/%s" %(data["protocol"], console_thread.host, console_thread.port, data["suffix"]),
+                                                   "name":vm['name']
+                                                }
+                        vm_ok +=1
+                else:
+                    vm_result[ vm['uuid'] ] = {"vim_result": result, "description": "ok", "name":vm['name']}
+                    vm_ok +=1
+
+    if vm_ok==0: #all goes wrong
+        return 1, vm_result
+    else:
+        return 1, vm_result
+    
+def create_or_use_console_proxy_thread(console_server, console_port):
+    #look for a non-used port
+    console_thread_key = console_server + ":" + str(console_port)
+    if console_thread_key in global_config["console_thread"]:
+        #global_config["console_thread"][console_thread_key].start_timeout()
+        return 1, global_config["console_thread"][console_thread_key]
+    
+    for port in  global_config["console_port_iterator"]():
+        print "create_or_use_console_proxy_thread() port:", port
+        if port in global_config["console_ports"]:
+            continue
+        try:
+            clithread = cli.ConsoleProxyThread(global_config['http_host'], port, console_server, console_port)
+            clithread.start()
+            global_config["console_thread"][console_thread_key] = clithread
+            global_config["console_ports"][port] = console_thread_key
+            return 1, clithread
+        except cli.ConsoleProxyExceptionPortUsed as e:
+            #port used, try with onoher
+            continue
+        except cli.ConsoleProxyException as e:
+            return -1, str(e)
+    return -1, "Not found any free 'http_console_ports'"
+
+def check_tenant(mydb, tenant_id):
+    '''check that tenant exists at database'''
+    result, _ = mydb.get_table(FROM='nfvo_tenants', SELECT=('uuid',), WHERE={'uuid': tenant_id})
+    if result<=0: return False
+    return True
+
+def new_tenant(mydb, tenant_dict):
+    result, tenant_id = mydb.new_row("nfvo_tenants", tenant_dict, None, add_uuid=True, log=True)
+    if result < 0:
+        return result, tenant_id
+    return 200,tenant_id
+
+def delete_tenant(mydb, tenant):
+    #get nfvo_tenant info
+    result,tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant, 'tenant')
+    if result < 0:
+        return result, tenant_dict
+
+    result, tenant_id = mydb.delete_row("nfvo_tenants", tenant_dict['uuid'], None)
+    if result < 0:
+        return result, tenant_id
+    return 200, tenant_dict['uuid']
+
+def new_datacenter(mydb, datacenter_descriptor):
+    if "config" in datacenter_descriptor:
+        datacenter_descriptor["config"]=yaml.safe_dump(datacenter_descriptor["config"],default_flow_style=True,width=256)
+    result, datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, None, add_uuid=True, log=True)
+    if result < 0:
+        return result, datacenter_id
+    return 200,datacenter_id
+
+def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor):
+    #obtain data, check that only one exist
+    result, content = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name)
+    if result < 0:
+        return result, content
+    #edit data 
+    datacenter_id = content['uuid']
+    where={'uuid': content['uuid']}
+    if "config" in datacenter_descriptor:
+        if datacenter_descriptor['config']!=None:
+            try:
+                new_config_dict = datacenter_descriptor["config"]
+                #delete null fields
+                to_delete=[]
+                for k in new_config_dict:
+                    if new_config_dict[k]==None:
+                        to_delete.append(k)
+                
+                config_dict = yaml.load(content["config"])
+                config_dict.update(new_config_dict)
+                #delete null fields
+                for k in to_delete:
+                    del config_dict[k]
+            except Exception,e:
+                return -HTTP_Bad_Request, "Bad format at datacenter:config " + str(e)
+        datacenter_descriptor["config"]= yaml.safe_dump(config_dict,default_flow_style=True,width=256) if len(config_dict)>0 else None
+    result, content = mydb.update_rows('datacenters', datacenter_descriptor, where)
+    if result < 0:
+        return result, datacenter_id
+    return 200, datacenter_id
+
+def delete_datacenter(mydb, datacenter):
+    #get nfvo_tenant info
+    result,datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter')
+    if result < 0:
+        return result, datacenter_dict
+
+    result, datacenter_id = mydb.delete_row("datacenters", datacenter_dict['uuid'], None)
+    if result < 0:
+        return result, datacenter_id
+    return 200, datacenter_dict['uuid']
+
+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): 
+        result, vims = get_vim(mydb, datacenter_id=datacenter)
+    else:
+        result, vims = get_vim(mydb, datacenter_name=datacenter)
+    if result < 0:
+        print "nfvo.associate_datacenter_to_tenant() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result>1:
+        print "nfvo.associate_datacenter_to_tenant() error. Several datacenters found"
+        #print result, vims
+        return -HTTP_Conflict, "More than one datacenters found, try to identify with uuid"
+    datacenter_id=vims.keys()[0]
+    myvim=vims[datacenter_id]
+    datacenter_name=myvim["name"]
+    
+    create_vim_tenant=True if vim_tenant_id==None and vim_tenant_name==None else False
+
+    #get nfvo_tenant info
+    result,tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', nfvo_tenant)
+    if result < 0:
+        return result, tenant_dict
+    if vim_tenant_name==None:
+        vim_tenant_name=tenant_dict['name']
+        
+    #check that this association does not exist before
+    tenants_datacenter_dict={"nfvo_tenant_id":tenant_dict['uuid'], "datacenter_id":datacenter_id }
+    result,content = mydb.get_table(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
+    if result>0:
+        return -HTTP_Conflict, "datacenter %s and tenant %s are already attached" %(datacenter_id, tenant_dict['uuid'])
+    elif result<0:
+        return result, content
+
+    vim_tenant_id_exist_atdb=False
+    if not create_vim_tenant:
+        where_={"datacenter_id": datacenter_id}
+        if vim_tenant_id!=None:
+            where_["vim_tenant_id"] = vim_tenant_id
+        if vim_tenant_name!=None:
+            where_["vim_tenant_name"] = vim_tenant_name
+        #check if vim_tenant_id is already at database
+        result,datacenter_tenants_dict = mydb.get_table(FROM='datacenter_tenants', WHERE=where_)
+        if result < 0:
+            return result, datacenter_tenants_dict
+        elif result>=1:
+            datacenter_tenants_dict = datacenter_tenants_dict[0]
+            vim_tenant_id_exist_atdb=True
+            #TODO check if a field has changed and edit entry at datacenter_tenants at DB
+        else: #result=0
+            datacenter_tenants_dict = {}
+            #insert at table datacenter_tenants
+    else: #if vim_tenant_id==None:
+        #create tenant at VIM if not provided
+        res, vim_tenant_id = myvim.new_tenant(vim_tenant_name, "created by openmano for datacenter "+datacenter_name)
+        if res < 0:
+            return res, "Not possible to create vim_tenant in VIM " + vim_tenant_id
+        datacenter_tenants_dict = {}
+        datacenter_tenants_dict["created"]="true"
+    
+    #fill datacenter_tenants table
+    if not vim_tenant_id_exist_atdb:
+        datacenter_tenants_dict["vim_tenant_id"]   = vim_tenant_id
+        datacenter_tenants_dict["vim_tenant_name"] = vim_tenant_name
+        datacenter_tenants_dict["user"]            = vim_username
+        datacenter_tenants_dict["passwd"]          = vim_password
+        datacenter_tenants_dict["datacenter_id"]   = datacenter_id
+        res,id_ = mydb.new_row('datacenter_tenants', datacenter_tenants_dict, tenant_dict['uuid'], True, True)
+        if res<1:
+            return -HTTP_Bad_Request, "Not possible to add %s to database datacenter_tenants table %s " %(vim_tenant_id, id_)
+        datacenter_tenants_dict["uuid"] = id_
+    
+    #fill tenants_datacenters table
+    tenants_datacenter_dict["datacenter_tenant_id"]=datacenter_tenants_dict["uuid"]
+    res,id_ = mydb.new_row('tenants_datacenters', tenants_datacenter_dict, tenant_dict['uuid'], False, True)
+    if res<1:
+        return -HTTP_Bad_Request, "Not possible to create an entry at database table datacenter_tenants: " + id_
+    return 200, datacenter_id
+
+def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=None):
+    #get datacenter info
+    if af.check_valid_uuid(datacenter): 
+        result, vims = get_vim(mydb, datacenter_id=datacenter)
+    else:
+        result, vims = get_vim(mydb, datacenter_name=datacenter)
+    if result < 0:
+        print "nfvo.deassociate_datacenter_to_tenant() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result>1:
+        print "nfvo.deassociate_datacenter_to_tenant() error. Several datacenters found"
+        return -HTTP_Conflict, "More than one datacenters found, try to identify with uuid"
+    datacenter_id=vims.keys()[0]
+    myvim=vims[datacenter_id]
+
+    #get nfvo_tenant info
+    if not tenant_id or tenant_id=="any":
+        tenant_uuid = None
+    else:
+        result,tenant_dict = mydb.get_table_by_uuid_name('nfvo_tenants', tenant_id)
+        if result < 0:
+            return result, tenant_dict
+        tenant_uuid = tenant_dict['uuid']
+
+    #check that this association exist before
+    tenants_datacenter_dict={"datacenter_id":datacenter_id }
+    if tenant_uuid:
+        tenants_datacenter_dict["nfvo_tenant_id"] = tenant_uuid
+    result,tenant_datacenter_list = mydb.get_table(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
+    if result==0 and tenant_uuid:
+        return -HTTP_Not_Found, "datacenter %s and tenant %s are not  attached" %(datacenter_id, tenant_dict['uuid'])
+    elif result<0:
+        return result, tenant_datacenter_list
+
+    #delete this association
+    result,data = mydb.delete_row_by_dict(FROM='tenants_datacenters', WHERE=tenants_datacenter_dict)
+    if result<0:
+        return result,data
+
+    #get vim_tenant info and deletes
+    warning=''
+    for tenant_datacenter_item in tenant_datacenter_list:
+        result,vim_tenant_dict = mydb.get_table_by_uuid_name('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'])
+        if result > 0:
+            #try to delete vim:tenant
+            result,data = mydb.delete_row('datacenter_tenants', tenant_datacenter_item['datacenter_tenant_id'], tenant_uuid)
+            if result<0:
+                pass #the error will be caused because dependencies, vim_tenant can not be deleted
+            elif vim_tenant_dict['created']=='true':
+                #delete tenant at VIM if created by NFVO
+                res, vim_tenant_id = myvim.delete_tenant(vim_tenant_dict['vim_tenant_id'])
+                if res < 0:
+                    warning = " Not possible to delete vim_tenant id %s name %s from VIM: %s " % (vim_tenant_dict['vim_tenant_id'], vim_tenant_dict['vim_tenant_name'], vim_tenant_id)
+                    print res, warning
+
+    return 200, "datacenter %s detached.%s" %(datacenter_id, warning)
+
+def datacenter_action(mydb, tenant_id, datacenter, action_dict):
+    #DEPRECATED
+    #get datacenter info    
+    if af.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)
+    if result < 0:
+        print "nfvo.datacenter_action() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result>1:
+        print "nfvo.datacenter_action() error. Several datacenters found"
+        return -HTTP_Conflict, "More than one datacenters found, try to identify with uuid"
+    datacenter_id=vims.keys()[0]
+    myvim=vims[datacenter_id]
+
+    if 'net-update' in action_dict:
+        result, content = myvim.get_network_list(filter_dict={'shared': True, 'admin_state_up': True, 'status': 'ACTIVE'})
+        print content
+        if result < 0:
+            print " Not possible to get_network_list from VIM: %s " % (content)
+            return -HTTP_Internal_Server_Error, content
+        #update nets Change from VIM format to NFVO format
+        net_list=[]
+        for net in content:
+            net_nfvo={'datacenter_id': datacenter_id}
+            net_nfvo['name']       = net['name']
+            #net_nfvo['description']= net['name']
+            net_nfvo['vim_net_id'] = net['id']
+            net_nfvo['type']       = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man')  to ('bridge','data','ptp')
+            net_nfvo['shared']     = net['shared']
+            net_nfvo['multipoint'] = False if net['type']=='ptp' else True
+            net_list.append(net_nfvo)
+        result, content = mydb.update_datacenter_nets(datacenter_id, net_list)
+        if result < 0:
+            return -HTTP_Internal_Server_Error, content
+        print "Inserted %d nets, deleted %d old nets" % (result, content)
+                
+        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'
+        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'
+        result, content = mydb.delete_row_by_dict(FROM='datacenter_nets', 
+                                WHERE={'datacenter_id':datacenter_id, what: net})
+        return result, content
+
+    else:
+        return -HTTP_Bad_Request, "Unknown action " + str(action_dict)
+
+def datacenter_edit_netmap(mydb, tenant_id, datacenter, netmap, action_dict):
+    #get datacenter info
+    if af.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)
+    if result < 0:
+        print "nfvo.datacenter_action() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result>1:
+        print "nfvo.datacenter_action() error. Several datacenters found"
+        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'
+    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): 
+        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)
+    if result < 0:
+        print "nfvo.datacenter_new_netmap() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result>1:
+        print "nfvo.datacenter_new_netmap() error. Several datacenters found"
+        return -HTTP_Conflict, "More than one datacenters found, try to identify with uuid"
+    datacenter_id=vims.keys()[0]
+    myvim=vims[datacenter_id]
+    filter_dict={}
+    if action_dict:
+        action_dict = action_dict["netmap"]
+        if 'vim_id' in action_dict:
+            filter_dict["id"] = action_dict['vim_id']
+        if 'vim_name' in action_dict:
+            filter_dict["name"] = action_dict['vim_name']
+    else:
+        filter_dict["shared"] = True
+    
+    result, content = myvim.get_network_list(filter_dict=filter_dict)
+    print content
+    if result != 200:
+        print " Not possible to get_network_list from VIM: %s " % (content)
+        return -HTTP_Internal_Server_Error, content
+    elif len(content)>1 and action_dict:
+        return -HTTP_Conflict, "more than two networks found, specify with vim_id"
+    elif len(content)==0: # and action_dict:
+        return -HTTP_Not_Found, "Not found a network at VIM with " + str(filter_dict)
+    net_list=[]
+    for net in content:
+        net_nfvo={'datacenter_id': datacenter_id}
+        if action_dict and "name" in action_dict:
+            net_nfvo['name']       = action_dict['name']
+        else:
+            net_nfvo['name']       = net['name']
+        #net_nfvo['description']= net['name']
+        net_nfvo['vim_net_id'] = net['id']
+        net_nfvo['type']       = net['type'][0:6] #change from ('ptp','data','bridge_data','bridge_man')  to ('bridge','data','ptp')
+        net_nfvo['shared']     = net['shared']
+        net_nfvo['multipoint'] = False if net['type']=='ptp' else True
+        result, content = mydb.new_row("datacenter_nets", net_nfvo, add_uuid=True)
+        if action_dict and result < 0 :
+            return result, content
+        if result < 0:
+            net_nfvo["status"] = "FAIL: " + content
+        else:
+            net_nfvo["status"] = "OK"
+            net_nfvo["uuid"] = content
+        net_list.append(net_nfvo)                         
+    return 200, net_list        
+
+def vim_action_get(mydb, tenant_id, datacenter, item, name):
+    #get datacenter info
+    if af.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)
+    if result < 0:
+        print "nfvo.datacenter_action() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result>1:
+        print "nfvo.datacenter_action() error. Several datacenters found"
+        return -HTTP_Conflict, "More than one datacenters found, try to identify with uuid"
+    datacenter_id=vims.keys()[0]
+    myvim=vims[datacenter_id]
+    filter_dict={}
+    if name:
+        if af.check_valid_uuid(name):
+            filter_dict["id"] = name
+        else:
+            filter_dict["name"] = name
+    if item=="networks":
+        filter_dict['tenant_id'] = myvim["tenant"]
+        result, content = myvim.get_network_list(filter_dict=filter_dict)
+    elif item=="tenants":
+        result, content = myvim.get_tenant_list(filter_dict=filter_dict)
+    else:
+        return -HTTP_Method_Not_Allowed, item + "?" 
+    if result < 0:
+        print "vim_action Not possible to get_%s_list from VIM: %s " % (item, content)
+        return -HTTP_Internal_Server_Error, content
+    print "vim_action response ", content #update nets Change from VIM format to NFVO format
+    if name and len(content)==1:
+        return 200, {item[:-1]: content[0]}
+    elif name and len(content)==0:
+        return -HTTP_Not_Found, "No %s found with %s" % (item[:-1], " and ".join(map(lambda x: str(x[0])+": "+str(x[1]), filter_dict.iteritems())))
+    else:
+        return 200, {item: content}
+    
+def vim_action_delete(mydb, tenant_id, datacenter, item, name):
+    #get datacenter info
+    if af.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)
+    if result < 0:
+        print "nfvo.datacenter_action() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result>1:
+        print "nfvo.datacenter_action() error. Several datacenters found"
+        return -HTTP_Conflict, "More than one datacenters found, try to identify with uuid"
+    datacenter_id=vims.keys()[0]
+    myvim=vims[datacenter_id]
+    #get uuid
+    if not af.check_valid_uuid(name):
+        result, content = vim_action_get(mydb, tenant_id, datacenter, item, name)
+        print content
+        if result < 0:
+            return result, content
+        items = content.values()[0]
+        if type(items)==list and len(items)==0:
+            return -HTTP_Not_Found, "Not found " + item
+        elif type(items)==list and len(items)>1:
+            return -HTTP_Not_Found, "Found more than one %s with this name. Use uuid." % item
+        else: # it is a dict
+            item_id = items["id"]
+            item_name = str(items.get("name"))
+        
+    if item=="networks":
+        result, content = myvim.delete_tenant_network(item_id)
+    elif item=="tenants":
+        result, content = myvim.delete_tenant(item_id)
+    else:
+        return -HTTP_Method_Not_Allowed, item + "?"    
+    if result < 0:
+        print "vim_action Not possible to delete %s %s from VIM: %s " % (item, name, content)
+        return result, content
+    return 200, "%s %s %s deleted" %( item[:-1], item_id,item_name)
+    
+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): 
+        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)
+    if result < 0:
+        print "nfvo.datacenter_action() error. Datacenter not found"
+        return result, vims
+    elif result == 0:
+        return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
+    elif result>1:
+        print "nfvo.datacenter_action() error. Several datacenters found"
+        return -HTTP_Conflict, "More than one datacenters found, try to identify with uuid"
+    datacenter_id=vims.keys()[0]
+    myvim=vims[datacenter_id]
+    
+    if item=="networks":
+        net = descriptor["network"]
+        net_name = net.pop("name")
+        net_type = net.pop("type", "bridge")
+        net_public=net.pop("shared", False)
+        result, content = myvim.new_tenant_network(net_name, net_type, net_public, **net)
+    elif item=="tenants":
+        tenant = descriptor["tenant"]
+        result, content = myvim.new_tenant(tenant["name"], tenant.get("description"))
+    else:
+        return -HTTP_Method_Not_Allowed, item + "?"    
+    if result < 0:
+        print "vim_action Not possible to create %s at VIM: %s " % (item, content)
+        return result, content
+    return vim_action_get(mydb, tenant_id, datacenter, item, content)
+
+    
\ No newline at end of file
diff --git a/nfvo_db.py b/nfvo_db.py
new file mode 100644 (file)
index 0000000..7104ca8
--- /dev/null
@@ -0,0 +1,1353 @@
+# -*- 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
+##
+
+'''
+NFVO DB engine. It implements all the methods to interact with the Openmano Database
+'''
+__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
+__date__ ="$28-aug-2014 10:05:01$"
+
+import MySQLdb as mdb
+import uuid as myUuid
+from utils import auxiliary_functions as af
+import json
+import yaml
+import time
+
+HTTP_Bad_Request = 400
+HTTP_Unauthorized = 401 
+HTTP_Not_Found = 404 
+HTTP_Method_Not_Allowed = 405 
+HTTP_Request_Timeout = 408
+HTTP_Conflict = 409
+HTTP_Service_Unavailable = 503 
+HTTP_Internal_Server_Error = 500 
+
+tables_with_created_field=["datacenters","instance_nets","instance_scenarios","instance_vms","instance_vnfs",
+                           "interfaces","nets","nfvo_tenants","scenarios","sce_interfaces","sce_nets",
+                           "sce_vnfs","tenants_datacenters","datacenter_tenants","vms","vnfs"]
+
+class nfvo_db():
+    def __init__(self):
+        #initialization
+        return
+
+    def connect(self, host=None, user=None, passwd=None, database=None):
+        '''Connect to specific data base. 
+        The first time a valid host, user, passwd and database must be provided,
+        Following calls can skip this parameters
+        '''
+        try:
+            if host     is not None: self.host = host
+            if user     is not None: self.user = user
+            if passwd   is not None: self.passwd = passwd
+            if database is not None: 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)
+            return 0
+        except mdb.Error, e:
+            print "nfvo_db.connect Error connecting to DB %s@%s -> %s Error %d: %s" % (self.user, self.host, self.database, e.args[0], e.args[1])
+            return -1
+        
+    def get_db_version(self):
+        ''' Obtain the database schema version.
+        Return: (negative, text) if error or version 0.0 where schema_version table is missing
+                (version_int, version_text) if ok
+        '''
+        cmd = "SELECT version_int,version,openmano_ver FROM schema_version"
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor()
+                    #print cmd
+                    self.cur.execute(cmd)
+                    rows = self.cur.fetchall()
+                    highest_version_int=0
+                    highest_version=""
+                    #print rows
+                    for row in rows: #look for the latest version
+                        if row[0]>highest_version_int:
+                            highest_version_int, highest_version = row[0:2]
+                    return highest_version_int, highest_version
+            except (mdb.Error, AttributeError), e:
+                #print cmd
+                print "get_db_version DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def disconnect(self):
+        '''disconnect from specific data base'''
+        try:
+            self.con.close()
+            del self.con
+        except mdb.Error, e:
+            print "Error disconnecting from DB: Error %d: %s" % (e.args[0], e.args[1])
+            return -1
+        except AttributeError, e: #self.con not defined
+            if e[0][-5:] == "'con'": return -1, "Database internal error, no connection."
+            else: raise
+
+    def format_error(self, e, command=None, extra=None): 
+        if type(e[0]) is str:
+            if e[0][-5:] == "'con'": return -HTTP_Internal_Server_Error, "DB Exception, no connection."
+            else: raise
+        if e.args[0]==2006 or e.args[0]==2013 : #MySQL server has gone away (((or)))    Exception 2013: Lost connection to MySQL server during query
+            #reconnect
+            self.connect()
+            return -HTTP_Request_Timeout,"Database reconnection. Try Again"
+        
+        fk=e.args[1].find("foreign key constraint fails")
+        if fk>=0:
+            if command=="update": return -HTTP_Bad_Request, "tenant_id %s not found." % extra
+            elif command=="delete":  return -HTTP_Bad_Request, "Resource is not free. There are %s that prevent deleting it." % extra
+        de = e.args[1].find("Duplicate entry")
+        fk = e.args[1].find("for key")
+        uk = e.args[1].find("Unknown column")
+        wc = e.args[1].find("in 'where clause'")
+        fl = e.args[1].find("in 'field list'")
+        #print de, fk, uk, wc,fl
+        if de>=0:
+            if fk>=0: #error 1062
+                return -HTTP_Conflict, "Value %s already in use for %s" % (e.args[1][de+15:fk], e.args[1][fk+7:])
+        if uk>=0:
+            if wc>=0:
+                return -HTTP_Bad_Request, "Field %s can not be used for filtering" % e.args[1][uk+14:wc]
+            if fl>=0:
+                return -HTTP_Bad_Request, "Field %s does not exist" % e.args[1][uk+14:wc]
+        return -HTTP_Internal_Server_Error, "Database internal Error %d: %s" % (e.args[0], e.args[1])
+    
+    def __str2db_format(self, data):
+        '''Convert string data to database format. 
+        If data is None it returns the 'Null' text,
+        otherwise it returns the text surrounded by quotes ensuring internal quotes are escaped.
+        '''
+        if data==None:
+            return 'Null'
+        out=str(data)
+        if "'" not in out:
+            return "'" + out + "'"
+        elif '"' not in out:
+            return '"' + out + '"'
+        else:
+            return json.dumps(out)
+    
+    def __tuple2db_format_set(self, data):
+        '''Compose the needed text for a SQL SET, parameter 'data' is a pair tuple (A,B),
+        and it returns the text 'A="B"', where A is a field of a table and B is the value 
+        If B is None it returns the 'A=Null' text, without surrounding Null by quotes
+        If B is not None it returns the text "A='B'" or 'A="B"' where B is surrounded by quotes,
+        and it ensures internal quotes of B are escaped.
+        '''
+        if data[1]==None:
+            return str(data[0]) + "=Null"
+        out=str(data[1])
+        if "'" not in out:
+            return str(data[0]) + "='" + out + "'"
+        elif '"' not in out:
+            return str(data[0]) + '="' + out + '"'
+        else:
+            return str(data[0]) + '=' + json.dumps(out)
+    
+    def __tuple2db_format_where(self, data):
+        '''Compose the needed text for a SQL WHERE, parameter 'data' is a pair tuple (A,B),
+        and it returns the text 'A="B"', where A is a field of a table and B is the value 
+        If B is None it returns the 'A is Null' text, without surrounding Null by quotes
+        If B is not None it returns the text "A='B'" or 'A="B"' where B is surrounded by quotes,
+        and it ensures internal quotes of B are escaped.
+        '''
+        if data[1]==None:
+            return str(data[0]) + " is Null"
+        
+#         if type(data[1]) is tuple:  #this can only happen in a WHERE_OR clause
+#             text =[]
+#             for d in data[1]:
+#                 if d==None:
+#                     text.append(str(data[0]) + " is Null")
+#                     continue
+#                 out=str(d)
+#                 if "'" not in out:
+#                     text.append( str(data[0]) + "='" + out + "'" )
+#                 elif '"' not in out:
+#                     text.append( str(data[0]) + '="' + out + '"' )
+#                 else:
+#                     text.append( str(data[0]) + '=' + json.dumps(out) )
+#             return " OR ".join(text)
+
+        out=str(data[1])
+        if "'" not in out:
+            return str(data[0]) + "='" + out + "'"
+        elif '"' not in out:
+            return str(data[0]) + '="' + out + '"'
+        else:
+            return str(data[0]) + '=' + json.dumps(out)
+
+    def __tuple2db_format_where_not(self, data):
+        '''Compose the needed text for a SQL WHERE(not). parameter 'data' is a pair tuple (A,B),
+        and it returns the text 'A<>"B"', where A is a field of a table and B is the value 
+        If B is None it returns the 'A is not Null' text, without surrounding Null by quotes
+        If B is not None it returns the text "A<>'B'" or 'A<>"B"' where B is surrounded by quotes,
+        and it ensures internal quotes of B are escaped.
+        '''
+        if data[1]==None:
+            return str(data[0]) + " is not Null"
+        out=str(data[1])
+        if "'" not in out:
+            return str(data[0]) + "<>'" + out + "'"
+        elif '"' not in out:
+            return str(data[0]) + '<>"' + out + '"'
+        else:
+            return str(data[0]) + '<>' + json.dumps(out)
+    
+    def __remove_quotes(self, data):
+        '''remove single quotes ' of any string content of data dictionary'''
+        for k,v in data.items():
+            if type(v) == str:
+                if "'" in v: 
+                    data[k] = data[k].replace("'","_")
+    
+    def __update_rows(self, table, UPDATE, WHERE, log=False, modified_time=0):
+        ''' Update one or several rows into a table.
+        Atributes
+            UPDATE: dictionary with the key: value to change
+            table: table where to update
+            WHERE: dictionary of elements to update
+        Return: (result, descriptive text) where result indicates the number of updated files, negative if error
+        '''
+                #gettting uuid 
+        uuid = WHERE['uuid'] if 'uuid' in WHERE else None
+        values = ",".join(map(self.__tuple2db_format_set, UPDATE.iteritems() ))
+        if modified_time:
+            values += ",modified_at=%f" % modified_time
+        cmd= "UPDATE " + table +" SET " + values +\
+            " WHERE " + " and ".join(map(self.__tuple2db_format_where, WHERE.iteritems() ))
+        print cmd
+        self.cur.execute(cmd) 
+        nb_rows = self.cur.rowcount
+        if nb_rows > 0 and log:                
+            #inserting new log
+            if uuid is None: uuid_k = uuid_v = ""
+            else: uuid_k=",uuid"; uuid_v=",'" + str(uuid) + "'"
+            cmd = "INSERT INTO logs (related,level%s,description) VALUES ('%s','debug'%s,\"updating %d entry %s\")" \
+                % (uuid_k, table, uuid_v, nb_rows, (str(UPDATE)).replace('"','-')  )
+            print cmd
+            self.cur.execute(cmd)                    
+        return nb_rows, "%d updated from %s" % (nb_rows, table[:-1] )
+    
+    def _new_row_internal(self, table, INSERT, tenant_id=None, add_uuid=False, root_uuid=None, log=False, created_time=0):
+        ''' Add one row into a table. It DOES NOT begin or end the transaction, so self.con.cursor must be created
+        Attribute 
+            INSERT: dictionary with the key: value to insert
+            table: table where to insert
+            tenant_id: only useful for logs. If provided, logs will use this tenant_id
+            add_uuid: if True, it will create an uuid key entry at INSERT if not provided
+        It checks presence of uuid and add one automatically otherwise
+        Return: (result, uuid) where result can be 0 if error, or 1 if ok
+        '''
+
+        if add_uuid:
+            #create uuid if not provided
+            if 'uuid' not in INSERT:
+                uuid = INSERT['uuid'] = str(myUuid.uuid1()) # create_uuid
+            else: 
+                uuid = str(INSERT['uuid'])
+        else:
+            uuid=None
+        if add_uuid:
+            #defining root_uuid if not provided
+            if root_uuid is None:
+                root_uuid = uuid
+            if created_time:
+                created_at = created_time
+            else:
+                created_at=time.time()
+            #inserting new uuid
+            cmd = "INSERT INTO uuids (uuid, root_uuid, used_at, created_at) VALUES ('%s','%s','%s', %f)" % (uuid, root_uuid, table, created_at)
+            print cmd
+            self.cur.execute(cmd)
+        #insertion
+        cmd= "INSERT INTO " + table +" SET " + \
+            ",".join(map(self.__tuple2db_format_set, INSERT.iteritems() )) 
+        if created_time:
+            cmd += ",created_at=%f" % created_time
+        print cmd
+        self.cur.execute(cmd)
+        nb_rows = self.cur.rowcount
+        #inserting new log
+        if nb_rows > 0 and log:                
+            if add_uuid: del INSERT['uuid']
+            if uuid is None: uuid_k = uuid_v = ""
+            else: uuid_k=",uuid"; uuid_v=",'" + str(uuid) + "'"
+            if tenant_id is None: tenant_k = tenant_v = ""
+            else: tenant_k=",nfvo_tenant_id"; tenant_v=",'" + str(tenant_id) + "'"
+            cmd = "INSERT INTO logs (related,level%s%s,description) VALUES ('%s','debug'%s%s,\"new %s %s\")" \
+                % (uuid_k, tenant_k, table, uuid_v, tenant_v, table[:-1], str(INSERT).replace('"','-'))
+            print cmd
+            self.cur.execute(cmd)                    
+        return nb_rows, uuid
+
+    def __get_rows(self,table,uuid):
+        self.cur.execute("SELECT * FROM " + str(table) +" where uuid='" + str(uuid) + "'")
+        rows = self.cur.fetchall()
+        return self.cur.rowcount, rows
+    
+    def new_row(self, table, INSERT, tenant_id=None, add_uuid=False, log=False, created_time=0):
+        ''' Add one row into a table.
+        Attribute 
+            INSERT: dictionary with the key: value to insert
+            table: table where to insert
+            tenant_id: only useful for logs. If provided, logs will use this tenant_id
+            add_uuid: if True, it will create an uuid key entry at INSERT if not provided
+        It checks presence of uuid and add one automatically otherwise
+        Return: (result, uuid) where result can be 0 if error, or 1 if ok
+        '''
+        if table in tables_with_created_field and created_time==0:
+            created_time=time.time()
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor()
+                    return self._new_row_internal(table, INSERT, tenant_id, add_uuid, None, log, created_time)
+                    
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.new_row DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def update_rows(self, table, UPDATE, WHERE, log=False, modified_time=0):
+        ''' Update one or several rows into a table.
+        Atributes
+            UPDATE: dictionary with the key: value to change
+            table: table where to update
+            WHERE: dictionary of elements to update
+        Return: (result, descriptive text) where result indicates the number of updated files
+        '''
+        if table in tables_with_created_field and modified_time==0:
+            modified_time=time.time()
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor()
+                    return self.__update_rows(table, UPDATE, WHERE, log)
+                    
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.update_rows DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def delete_row(self, table, uuid, tenant_id=None, log=True):
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    #delete host
+                    self.cur = self.con.cursor()
+                    cmd = "DELETE FROM %s WHERE uuid = '%s'" % (table, uuid)
+                    print cmd
+                    self.cur.execute(cmd)
+                    deleted = self.cur.rowcount
+                    if deleted == 1:
+                        #delete uuid
+                        if table == 'tenants': tenant_str=uuid
+                        elif tenant_id:
+                            tenant_str = tenant_id
+                        else: 
+                            tenant_str = 'Null'
+                        self.cur = self.con.cursor()
+                        cmd = "DELETE FROM uuids WHERE root_uuid = '%s'" % uuid
+                        print cmd
+                        self.cur.execute(cmd)
+                        #inserting new log
+                        if log:
+                            cmd = "INSERT INTO logs (related,level,uuid,nfvo_tenant_id,description) VALUES ('%s','debug','%s','%s','delete %s')" % (table, uuid, tenant_str, table[:-1])
+                            print cmd
+                            self.cur.execute(cmd)                    
+                return deleted, table[:-1] + " '%s' %s" %(uuid, "deleted" if deleted==1 else "not found")
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.delete_row DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c =  self.format_error(e, "delete", 'instances' if table=='hosts' or table=='tenants' else 'dependencies')
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def delete_row_by_dict(self, **sql_dict):
+        ''' Deletes rows from a table.
+        Attribute sql_dir: dictionary with the following key: value
+            'FROM': string of table name (Mandatory)
+            'WHERE': dict of key:values, translated to key=value AND ... (Optional)
+            'WHERE_NOT': dict of key:values, translated to key<>value AND ... (Optional)
+            'LIMIT': limit of number of rows (Optional)
+        Return: the (number of items deleted, descriptive test) if ok; (negative, descriptive text) if error
+        '''
+        #print sql_dict
+        from_  = "FROM " + str(sql_dict['FROM'])
+        #print 'from_', from_
+        if 'WHERE' in sql_dict and len(sql_dict['WHERE']) > 0:
+            w=sql_dict['WHERE']
+            where_ = "WHERE " + " AND ".join(map(self.__tuple2db_format_where, w.iteritems())) 
+        else: where_ = ""
+        if 'WHERE_NOT' in sql_dict and len(sql_dict['WHERE_NOT']) > 0: 
+            w=sql_dict['WHERE_NOT']
+            where_2 = " AND ".join(map(self.__tuple2db_format_where_not, w.iteritems()))
+            if len(where_)==0:   where_ = "WHERE " + where_2
+            else:                where_ = where_ + " AND " + where_2
+        #print 'where_', where_
+        limit_ = "LIMIT " + str(sql_dict['LIMIT']) if 'LIMIT' in sql_dict else ""
+        #print 'limit_', limit_
+        cmd =  " ".join( ("DELETE", from_, where_, limit_) )
+        print cmd
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    #delete host
+                    self.cur = self.con.cursor()
+                    self.cur.execute(cmd)
+                    deleted = self.cur.rowcount
+                return deleted, "%d deleted from %s" % (deleted, sql_dict['FROM'][:-1] )
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.delete_row DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c =  self.format_error(e, "delete", 'dependencies')
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def get_rows(self,table,uuid):
+        '''get row from a table based on uuid'''
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor(mdb.cursors.DictCursor)
+                    self.cur.execute("SELECT * FROM " + str(table) +" where uuid='" + str(uuid) + "'")
+                    rows = self.cur.fetchall()
+                    return self.cur.rowcount, rows
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.get_rows DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+    
+    def get_table(self, **sql_dict):
+        ''' Obtain rows from a table.
+        Attribute sql_dir: dictionary with the following key: value
+            'SELECT':    list or tuple of fields to retrieve) (by default all)
+            'FROM':      string of table name (Mandatory)
+            'WHERE':     dict of key:values, translated to key=value (key is null) AND ... (Optional)
+            'WHERE_NOT': dict of key:values, translated to key<>value (key is not null) AND ... (Optional)
+            'WHERE_OR': dict of key:values, translated to key=value OR ... (Optional)
+            'WHERE_AND_OR: str 'AND' or 'OR'(by default) mark the priority to 'WHERE AND (WHERE_OR)' or (WHERE) OR WHERE_OR' (Optional  
+            'LIMIT':     limit of number of rows (Optional)
+            'ORDER_BY':  list or tuple of fields to order
+        Return: a list with dictionaries at each row
+        '''
+        #print sql_dict
+        select_= "SELECT " + ("*" if 'SELECT' not in sql_dict else ",".join(map(str,sql_dict['SELECT'])) )
+        #print 'select_', select_
+        from_  = "FROM " + str(sql_dict['FROM'])
+        #print 'from_', from_
+        where_and = ""
+        where_or = ""
+        w=sql_dict.get('WHERE')
+        if w:
+            where_and = " AND ".join(map(self.__tuple2db_format_where, w.iteritems() ))
+        w=sql_dict.get('WHERE_NOT')
+        if w: 
+            if where_and: where_and += " AND "
+            where_and += " AND ".join(map(self.__tuple2db_format_where_not, w.iteritems() ) )
+        w=sql_dict.get('WHERE_OR')
+        if w:
+            where_or =  " OR ".join(map(self.__tuple2db_format_where, w.iteritems() ))
+        if where_and and where_or:
+            if sql_dict.get("WHERE_AND_OR") == "AND":
+                where_ = "WHERE " + where_and + " AND (" + where_or + ")"
+            else:
+                where_ = "WHERE (" + where_and + ") OR " + where_or
+        elif where_and and not where_or:
+            where_ = "WHERE " + where_and
+        elif not where_and and where_or:
+            where_ = "WHERE " + where_or
+        else:
+            where_ = ""
+        #print 'where_', where_
+        limit_ = "LIMIT " + str(sql_dict['LIMIT']) if 'LIMIT' in sql_dict else ""
+        order_ = "ORDER BY " + ",".join(map(str,sql_dict['SELECT'])) if 'ORDER_BY' in sql_dict else ""
+        
+        #print 'limit_', limit_
+        cmd =  " ".join( (select_, from_, where_, limit_, order_) )
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor(mdb.cursors.DictCursor)
+                    print cmd
+                    self.cur.execute(cmd)
+                    rows = self.cur.fetchall()
+                    return self.cur.rowcount, rows
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.get_table DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def get_table_by_uuid_name(self, table, uuid_name, error_item_text=None, allow_serveral=False, WHERE_OR={}, WHERE_AND_OR="OR"):
+        ''' Obtain One row from a table based on name or uuid.
+        Attribute:
+            table: string of table name
+            uuid_name: name or uuid. If not uuid format is found, it is considered a name
+            allow_severeral: if False return ERROR if more than one row are founded 
+            error_item_text: in case of error it identifies the 'item' name for a proper output text 
+            'WHERE_OR': dict of key:values, translated to key=value OR ... (Optional)
+            'WHERE_AND_OR: str 'AND' or 'OR'(by default) mark the priority to 'WHERE AND (WHERE_OR)' or (WHERE) OR WHERE_OR' (Optional  
+        Return: if allow_several==False, a dictionary with this row, or error if no item is found or more than one is found
+                if allow_several==True, a list of dictionaries with the row or rows, error if no item is found
+        '''
+
+        if error_item_text==None:
+            error_item_text = table
+        what = 'uuid' if af.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() ))
+            if WHERE_AND_OR == "AND":
+                cmd += " AND (" + where_or + ")"
+            else:
+                cmd += " OR " + where_or
+
+        
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor(mdb.cursors.DictCursor)
+                    print cmd
+                    self.cur.execute(cmd)
+                    number = self.cur.rowcount
+                    if number==0:
+                        return -HTTP_Not_Found, "No %s found with %s '%s'" %(error_item_text, what, uuid_name)
+                    elif number>1 and not allow_serveral: 
+                        return -HTTP_Bad_Request, "More than one %s found with %s '%s'" %(error_item_text, what, uuid_name)
+                    if allow_serveral:
+                        rows = self.cur.fetchall()
+                    else:
+                        rows = self.cur.fetchone()
+                    return number, rows
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.get_table_by_uuid_name DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def get_uuid(self, uuid):
+        '''check in the database if this uuid is already present'''
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor(mdb.cursors.DictCursor)
+                    self.cur.execute("SELECT * FROM uuids where uuid='" + str(uuid) + "'")
+                    rows = self.cur.fetchall()
+                    return self.cur.rowcount, rows
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.get_uuid DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def new_vnf_as_a_whole(self,nfvo_tenant,vnf_name,vnf_descriptor,VNFCDict):
+        print "Adding new vnf to the NFVO database"
+        for retry_ in range(0,2):
+            created_time = time.time()
+            try:
+                with self.con:
+            
+                    myVNFDict = {}
+                    myVNFDict["name"] = vnf_name
+                    myVNFDict["descriptor"] = vnf_descriptor['vnf'].get('descriptor')
+                    myVNFDict["public"] = vnf_descriptor['vnf'].get('public', "false")
+                    myVNFDict["description"] = vnf_descriptor['vnf']['description']
+                    myVNFDict["class"] = vnf_descriptor['vnf'].get('class',"MISC")
+                    myVNFDict["tenant_id"] = vnf_descriptor['vnf'].get("tenant_id")
+                    
+                    result, vnf_id = self._new_vnf(myVNFDict,nfvo_tenant,created_time)
+                    if result < 0:
+                        return result, "Error creating vnf in NFVO database: %s" %vnf_id
+                        
+                    print "VNF %s added to NFVO DB. VNF id: %s" % (vnf_name,vnf_id)
+                    
+                    print "Adding new vms to the NFVO database"
+                    #For each vm, we must create the appropriate vm in the NFVO database.
+                    vmDict = {}
+                    for _,vm in VNFCDict.iteritems():
+                        #This code could make the name of the vms grow and grow.
+                        #If we agree to follow this convention, we should check with a regex that the vnfc name is not including yet the vnf name  
+                        #vm['name'] = "%s-%s" % (vnf_name,vm['name'])
+                        print "VM name: %s. Description: %s" % (vm['name'], vm['description'])
+                        vm["vnf_id"] = vnf_id
+                        created_time += 0.00001
+                        result, vm_id = self._new_vm(vm,nfvo_tenant,vnf_id,created_time)
+                        if result < 0:
+                            return result, "Error creating vm in NFVO database: %s" %vm_id
+                        
+                        print "Internal vm id in NFVO DB: %s" % vm_id
+                        vmDict[vm['name']] = vm_id
+                
+                    #Collect the data interfaces of each VM/VNFC under the 'numas' field
+                    dataifacesDict = {}
+                    for vm in vnf_descriptor['vnf']['VNFC']:
+                        dataifacesDict[vm['name']] = {}
+                        for numa in vm.get('numas', []):
+                            for dataiface in numa.get('interfaces',[]):
+                                af.convert_bandwidth(dataiface)
+                                dataifacesDict[vm['name']][dataiface['name']] = {}
+                                dataifacesDict[vm['name']][dataiface['name']]['vpci'] = dataiface['vpci']
+                                dataifacesDict[vm['name']][dataiface['name']]['bw'] = dataiface['bandwidth']
+                                dataifacesDict[vm['name']][dataiface['name']]['model'] = "PF" if dataiface['dedicated']=="yes" else ("VF"  if dataiface['dedicated']=="no" else "VFnotShared")
+    
+                    #Collect the bridge interfaces of each VM/VNFC under the 'bridge-ifaces' field
+                    bridgeInterfacesDict = {}
+                    for vm in vnf_descriptor['vnf']['VNFC']:
+                        if 'bridge-ifaces' in  vm:
+                            bridgeInterfacesDict[vm['name']] = {}
+                            for bridgeiface in vm['bridge-ifaces']:
+                                af.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)
+                                bridgeInterfacesDict[vm['name']][bridgeiface['name']]['bw'] = bridgeiface.get('bandwidth', None)
+                                bridgeInterfacesDict[vm['name']][bridgeiface['name']]['model'] = bridgeiface.get('model', None)
+                    
+                    #For each internal connection, we add it to the interfaceDict and we  create the appropriate net in the NFVO database.
+                    print "Adding new nets (VNF internal nets) to the NFVO database (if any)"
+                    internalconnList = []
+                    if 'internal-connections' in vnf_descriptor['vnf']:
+                        for net in vnf_descriptor['vnf']['internal-connections']:
+                            print "CODE TO BE CHECKED"
+                            print "Net name: %s. Description: %s" % (net['name'], net['description'])
+                            
+                            myNetDict = {}
+                            myNetDict["name"] = net['name']
+                            myNetDict["description"] = net['description']
+                            myNetDict["type"] = net['type']
+                            myNetDict["vnf_id"] = vnf_id
+                            
+                            created_time += 0.00001
+                            result, net_id = self._new_net(myNetDict,nfvo_tenant,vnf_id, created_time)
+                            if result < 0:
+                                return result, "Error creating net in NFVO database: %s" %net_id
+                                
+                            for element in net['elements']:
+                                ifaceItem = {}
+                                #ifaceItem["internal_name"] = "%s-%s-%s" % (net['name'],element['VNFC'], element['local_iface_name'])  
+                                ifaceItem["internal_name"] = element['local_iface_name']
+                                #ifaceItem["vm_id"] = vmDict["%s-%s" % (vnf_name,element['VNFC'])]
+                                ifaceItem["vm_id"] = vmDict[element['VNFC']]
+                                ifaceItem["net_id"] = net_id
+                                ifaceItem["type"] = net['type']
+                                if ifaceItem ["type"] == "data":
+                                    ifaceItem["vpci"] =  dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci'] 
+                                    ifaceItem["bw"] =    dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw']
+                                    ifaceItem["model"] = dataifacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model']
+                                else:
+                                    ifaceItem["vpci"] =  bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['vpci']
+                                    ifaceItem["mac"] =  bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['mac_address']
+                                    ifaceItem["bw"] =    bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['bw']
+                                    ifaceItem["model"] = bridgeInterfacesDict[ element['VNFC'] ][ element['local_iface_name'] ]['model']
+                                internalconnList.append(ifaceItem)
+                            
+                            print "Internal net id in NFVO DB: %s" % net_id
+                    
+                    print "Adding internal interfaces to the NFVO database (if any)"
+                    for iface in internalconnList:
+                        print "Iface name: %s" % iface['internal_name']
+                        created_time += 0.00001
+                        result, iface_id = self._new_interface(iface,nfvo_tenant,vnf_id,created_time)
+                        if result < 0:
+                            return result, "Error creating iface in NFVO database: %s" %iface_id
+                        print "Iface id in NFVO DB: %s" % iface_id
+                    
+                    print "Adding external interfaces to the NFVO database"
+                    for iface in vnf_descriptor['vnf']['external-connections']:
+                        myIfaceDict = {}
+                        #myIfaceDict["internal_name"] = "%s-%s-%s" % (vnf_name,iface['VNFC'], iface['local_iface_name'])  
+                        myIfaceDict["internal_name"] = iface['local_iface_name']
+                        #myIfaceDict["vm_id"] = vmDict["%s-%s" % (vnf_name,iface['VNFC'])]
+                        myIfaceDict["vm_id"] = vmDict[iface['VNFC']]
+                        myIfaceDict["external_name"] = iface['name']
+                        myIfaceDict["type"] = iface['type']
+                        if iface["type"] == "data":
+                            myIfaceDict["vpci"]  = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
+                            myIfaceDict["bw"]    = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
+                            myIfaceDict["model"] = dataifacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
+                        else:
+                            myIfaceDict["vpci"]  = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['vpci']
+                            myIfaceDict["bw"]    = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['bw']
+                            myIfaceDict["model"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['model']
+                            myIfaceDict["mac"] = bridgeInterfacesDict[ iface['VNFC'] ][ iface['local_iface_name'] ]['mac']
+                        print "Iface name: %s" % iface['name']
+                        created_time += 0.00001
+                        result, iface_id = self._new_interface(myIfaceDict,nfvo_tenant,vnf_id,created_time)
+                        if result < 0:
+                            return result, "Error creating iface in NFVO database: %s" %iface_id
+                        print "Iface id in NFVO DB: %s" % iface_id
+                    
+                    return 1,vnf_id
+                
+            except (mdb.Error, AttributeError), e:
+                print "new_vnf_as_a_whole DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+        
+    def _new_vnf(self, vnf_dict, tenant_id, created_time=0):
+        #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True)
+        return self._new_row_internal('vnfs', vnf_dict, tenant_id, add_uuid=True, root_uuid=None, log=True, created_time=created_time)
+
+    def _new_vm(self, vm_dict, tenant_id, vnf_id = None, created_time=0):
+        #return self.new_row('vms', vm_dict, tenant_id, True, True)
+        return self._new_row_internal('vms', vm_dict, tenant_id, add_uuid=True, root_uuid=vnf_id, log=True, created_time=created_time)
+
+
+    def _new_net(self, net_dict, tenant_id, vnf_id = None, created_time=0):
+        #return self.new_row('nets', net_dict, tenant_id, True, True)
+        return self._new_row_internal('nets', net_dict, tenant_id, add_uuid=True, root_uuid=vnf_id, log=True, created_time=created_time)
+    
+    def _new_interface(self, interface_dict, tenant_id, vnf_id = None, created_time=0):
+        #return self.new_row('interfaces', interface_dict, tenant_id, True, True)
+        return self._new_row_internal('interfaces', interface_dict, tenant_id, add_uuid=True, root_uuid=vnf_id, log=True, created_time=created_time)
+
+    def new_scenario(self, scenario_dict):
+        for retry_ in range(0,2):
+            created_time = time.time()
+            try:
+                with self.con:
+                    self.cur = self.con.cursor()
+                    tenant_id = scenario_dict.get('tenant_id')
+                    #scenario
+                    INSERT_={'tenant_id': tenant_id,
+                    'name': scenario_dict['name'],'description': scenario_dict['description']}
+                    
+                    r,scenario_uuid =  self._new_row_internal('scenarios', INSERT_, tenant_id, True, None, True,created_time)
+                    if r<0:
+                        print 'nfvo_db.new_scenario Error inserting at table scenarios: ' + scenario_uuid
+                        return r,scenario_uuid
+                    #sce_nets
+                    for net in scenario_dict['nets'].values():
+                        net_dict={'scenario_id': scenario_uuid}
+                        net_dict["name"] = net["name"]
+                        net_dict["type"] = net["type"]
+                        net_dict["description"] = net.get("description")
+                        net_dict["external"] = net.get("external", False)
+                        if "graph" in net:
+                            #net["graph"]=yaml.safe_dump(net["graph"],default_flow_style=True,width=256)
+                            #TODO, must be json because of the GUI, change to yaml
+                            net_dict["graph"]=json.dumps(net["graph"])
+                        created_time += 0.00001
+                        r,net_uuid =  self._new_row_internal('sce_nets', net_dict, tenant_id, True, None, True, created_time)
+                        if r<0:
+                            print 'nfvo_db.new_scenario Error inserting at table sce_vnfs: ' + net_uuid
+                            return r, net_uuid
+                        net['uuid']=net_uuid
+                    #sce_vnfs
+                    for k,vnf in scenario_dict['vnfs'].items():
+                        INSERT_={'scenario_id': scenario_uuid,
+                                'name': k,
+                                'vnf_id': vnf['uuid'],
+                                #'description': scenario_dict['name']
+                                'description': vnf['description']
+                            }
+                        if "graph" in vnf:
+                            #INSERT_["graph"]=yaml.safe_dump(vnf["graph"],default_flow_style=True,width=256)
+                            #TODO, must be json because of the GUI, change to yaml
+                            INSERT_["graph"]=json.dumps(vnf["graph"])
+                        created_time += 0.00001
+                        r,scn_vnf_uuid =  self._new_row_internal('sce_vnfs', INSERT_, tenant_id, True, scenario_uuid, True, created_time)
+                        if r<0:
+                            print 'nfvo_db.new_scenario Error inserting at table sce_vnfs: ' + scn_vnf_uuid
+                            return r, scn_vnf_uuid
+                        vnf['scn_vnf_uuid']=scn_vnf_uuid
+                        #sce_interfaces
+                        for iface in vnf['ifaces'].values():
+                            print 'iface', iface
+                            if 'net_key' not in iface:
+                                continue
+                            iface['net_id'] = scenario_dict['nets'][ iface['net_key'] ]['uuid']
+                            INSERT_={'sce_vnf_id': scn_vnf_uuid,
+                                'sce_net_id': iface['net_id'],
+                                'interface_id':  iface[ 'uuid' ]
+                            }
+                            created_time += 0.00001
+                            r,iface_uuid =  self._new_row_internal('sce_interfaces', INSERT_, tenant_id, True, scenario_uuid, True, created_time)
+                            if r<0:
+                                print 'nfvo_db.new_scenario Error inserting at table sce_vnfs: ' + iface_uuid
+                                return r, iface_uuid
+                            
+                    return 1, scenario_uuid
+                    
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.new_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def edit_scenario(self, scenario_dict):
+        for retry_ in range(0,2):
+            modified_time = time.time()
+            try:
+                with self.con:
+                    self.cur = self.con.cursor()
+                    #check that scenario exist
+                    tenant_id = scenario_dict.get('tenant_id')
+                    scenario_uuid = scenario_dict['uuid']
+                    
+                    where_text = "uuid='%s'" % scenario_uuid
+                    if not tenant_id and tenant_id != "any":
+                        where_text += " AND (tenant_id='%s' OR public='True')" % (tenant_id)
+                    self.cur.execute("SELECT * FROM scenarios WHERE "+ where_text)
+                    self.cur.fetchall()
+                    if self.cur.rowcount==0:
+                        return -HTTP_Bad_Request, "No scenario found with this criteria " + where_text
+                    elif self.cur.rowcount>1:
+                        return -HTTP_Bad_Request, "More than one scenario found with this criteria " + where_text
+
+                    #scenario
+                    nodes = {}
+                    topology = scenario_dict.pop("topology", None)
+                    if topology != None and "nodes" in topology:
+                        nodes = topology.get("nodes",{})
+                    UPDATE_ = {}
+                    if "name" in scenario_dict:        UPDATE_["name"] = scenario_dict["name"]
+                    if "description" in scenario_dict: UPDATE_["description"] = scenario_dict["description"]
+                    if len(UPDATE_)>0:
+                        WHERE_={'tenant_id': tenant_id, 'uuid': scenario_uuid}
+                        r,c =  self.__update_rows('scenarios', UPDATE_, WHERE_, modified_time=modified_time)
+                        if r<0:
+                            print 'nfvo_db.edit_scenario Error ' + c + ' updating table scenarios: ' + scenario_uuid
+                            return r,scenario_uuid
+                    #sce_nets
+                    for node_id, node in nodes.items():
+                        if "graph" in node:
+                            #node["graph"] = yaml.safe_dump(node["graph"],default_flow_style=True,width=256)
+                            #TODO, must be json because of the GUI, change to yaml
+                            node["graph"] = json.dumps(node["graph"])
+                        WHERE_={'scenario_id': scenario_uuid, 'uuid': node_id}
+                        r,c =  self.__update_rows('sce_nets', node, WHERE_)
+                        if r<=0:
+                            r,c =  self.__update_rows('sce_vnfs', node, WHERE_, modified_time=modified_time)
+                            if r<0:
+                                print 'nfvo_db.edit_scenario Error updating table sce_nets,sce_vnfs: ' + scenario_uuid
+                                return r, scenario_uuid
+                    return 1, scenario_uuid
+                    
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.new_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+#     def get_instance_scenario(self, instance_scenario_id, tenant_id=None):
+#         '''Obtain the scenario instance information, filtering by one or serveral of the tenant, uuid or name
+#         instance_scenario_id is the uuid or the name if it is not a valid uuid format
+#         Only one scenario isntance must mutch the filtering or an error is returned
+#         ''' 
+#         print "1******************************************************************"
+#         try:
+#             with self.con:
+#                 self.cur = self.con.cursor(mdb.cursors.DictCursor)
+#                 #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):
+#                     where_list.append( "uuid='" + instance_scenario_id +"'" )
+#                 else:
+#                     where_list.append( "name='" + instance_scenario_id +"'" )
+#                 where_text = " AND ".join(where_list)
+#                 self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text)
+#                 rows = self.cur.fetchall()
+#                 if self.cur.rowcount==0:
+#                     return -HTTP_Bad_Request, "No scenario instance found with this criteria " + where_text
+#                 elif self.cur.rowcount>1:
+#                     return -HTTP_Bad_Request, "More than one scenario instance found with this criteria " + where_text
+#                 instance_scenario_dict = rows[0]
+#                 
+#                 #instance_vnfs
+#                 self.cur.execute("SELECT uuid,vnf_id FROM instance_vnfs WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
+#                 instance_scenario_dict['instance_vnfs'] = self.cur.fetchall()
+#                 for vnf in instance_scenario_dict['instance_vnfs']:
+#                     #instance_vms
+#                     self.cur.execute("SELECT uuid, vim_vm_id "+
+#                                 "FROM instance_vms  "+
+#                                 "WHERE instance_vnf_id='" + vnf['uuid'] +"'"  
+#                                 )
+#                     vnf['instance_vms'] = self.cur.fetchall()
+#                 #instance_nets
+#                 self.cur.execute("SELECT uuid, vim_net_id FROM instance_nets WHERE instance_scenario_id='"+ instance_scenario_dict['uuid'] + "'")
+#                 instance_scenario_dict['instance_nets'] = self.cur.fetchall()
+#                 
+#                 #instance_interfaces
+#                 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') )
+#                 print "2******************************************************************"
+#                 return 1, instance_scenario_dict
+#         except (mdb.Error, AttributeError), e:
+#             print "nfvo_db.get_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
+#             return self.format_error(e)
+
+    def get_scenario(self, scenario_id, tenant_id=None, datacenter_id=None):
+        '''Obtain the scenario information, filtering by one or serveral of the tenant, uuid or name
+        scenario_id is the uuid or the name if it is not a valid uuid format
+        if datacenter_id is provided, it supply aditional vim_id fields with the matching vim uuid 
+        Only one scenario must mutch the filtering or an error is returned
+        ''' 
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor(mdb.cursors.DictCursor)
+                    #scenario table
+                    if af.check_valid_uuid(scenario_id):
+                        where_text = "uuid='%s'" % scenario_id
+                    else:
+                        where_text = "name='%s'" % scenario_id
+                    if not tenant_id and tenant_id != "any":
+                        where_text += " AND (tenant_id='%s' OR public='True')" % (tenant_id)
+                    cmd = "SELECT * FROM scenarios WHERE "+ where_text
+                    print cmd
+                    self.cur.execute(cmd)
+                    rows = self.cur.fetchall()
+                    if self.cur.rowcount==0:
+                        return -HTTP_Bad_Request, "No scenario found with this criteria " + where_text
+                    elif self.cur.rowcount>1:
+                        return -HTTP_Bad_Request, "More than one scenario found with this criteria " + where_text
+                    scenario_dict = rows[0]
+                    
+                    #sce_vnfs
+                    cmd = "SELECT uuid,name,vnf_id,description FROM sce_vnfs WHERE scenario_id='%s' ORDER BY created_at" % scenario_dict['uuid']
+                    self.cur.execute(cmd)
+                    scenario_dict['vnfs'] = self.cur.fetchall()
+                    for vnf in scenario_dict['vnfs']:
+                        #sce_interfaces
+                        cmd = "SELECT uuid,sce_net_id,interface_id FROM sce_interfaces WHERE sce_vnf_id='%s' ORDER BY created_at" %vnf['uuid']
+                        self.cur.execute(cmd)
+                        vnf['interfaces'] = self.cur.fetchall()
+                        #vms
+                        cmd = "SELECT vms.uuid as uuid, flavor_id, image_id, vms.name as name, vms.description as description " \
+                                " FROM vnfs join vms on vnfs.uuid=vms.vnf_id " \
+                                " WHERE vnfs.uuid='" + vnf['vnf_id'] +"'"  \
+                                " ORDER BY vms.created_at"
+                        self.cur.execute(cmd)
+                        vnf['vms'] = self.cur.fetchall()
+                        for vm in vnf['vms']:
+                            if datacenter_id!=None:
+                                self.cur.execute("SELECT vim_id FROM datacenters_images WHERE image_id='%s' AND datacenter_id='%s'" %(vm['image_id'],datacenter_id)) 
+                                if self.cur.rowcount==1:
+                                    vim_image_dict = self.cur.fetchone()
+                                    vm['vim_image_id']=vim_image_dict['vim_id']
+                                self.cur.execute("SELECT vim_id FROM datacenters_flavors WHERE flavor_id='%s' AND datacenter_id='%s'" %(vm['flavor_id'],datacenter_id)) 
+                                if self.cur.rowcount==1:
+                                    vim_flavor_dict = self.cur.fetchone()
+                                    vm['vim_flavor_id']=vim_flavor_dict['vim_id']
+                                
+                            #interfaces
+                            cmd = "SELECT uuid,internal_name,external_name,net_id,type,vpci,mac,bw,model" \
+                                    " FROM interfaces" \
+                                    " WHERE vm_id='%s'" \
+                                    " ORDER BY created_at" %   vm['uuid']
+                            self.cur.execute(cmd)
+                            vm['interfaces'] = self.cur.fetchall()
+                        #nets    every net of a vms
+                        self.cur.execute("SELECT uuid,name,type,description FROM nets WHERE vnf_id='" + vnf['vnf_id'] +"'"  )
+                        vnf['nets'] = self.cur.fetchall()
+                    #sce_nets
+                    cmd = "SELECT uuid,name,type,external,description" \
+                          " FROM sce_nets  WHERE scenario_id='%s'" \
+                          " ORDER BY created_at " % scenario_dict['uuid']
+                    self.cur.execute(cmd)
+                    scenario_dict['nets'] = self.cur.fetchall()
+                    #datacenter_nets
+                    for net in scenario_dict['nets']:
+                        if str(net['external']) == 'false':
+                            continue
+                        WHERE_=" WHERE name='%s'" % net['name']
+                        if datacenter_id!=None:
+                            WHERE_ += " AND datacenter_id='%s'" % datacenter_id
+                        self.cur.execute("SELECT vim_net_id FROM datacenter_nets" + WHERE_ ) 
+                        d_net = self.cur.fetchone()
+                        if d_net==None or datacenter_id==None:
+                            #print "nfvo_db.get_scenario() WARNING external net %s not found"  % net['name']
+                            net['vim_id']=None
+                        else:
+                            net['vim_id']=d_net['vim_net_id']
+                    
+                    af.convert_datetime2str(scenario_dict)
+                    af.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])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def get_uuid_from_name(self, table, name):
+        '''Searchs in table the name and returns the uuid
+        ''' 
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor(mdb.cursors.DictCursor)
+                    where_text = "name='" + name +"'"
+                    self.cur.execute("SELECT * FROM " + table + " WHERE "+ where_text)
+                    rows = self.cur.fetchall()
+                    if self.cur.rowcount==0:
+                        return 0, "Name %s not found in table %s" %(name, table)
+                    elif self.cur.rowcount>1:
+                        return self.cur.rowcount, "More than one VNF with name %s found in table %s" %(name, table)
+                    return self.cur.rowcount, rows[0]["uuid"]
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.get_uuid_from_name DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def delete_scenario(self, scenario_id, tenant_id=None):
+        '''Deletes a scenario, filtering by one or several of the tenant, uuid or name
+        scenario_id is the uuid or the name if it is not a valid uuid format
+        Only one scenario must mutch the filtering or an error is returned
+        ''' 
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor(mdb.cursors.DictCursor)
+    
+                    #scenario table
+                    if af.check_valid_uuid(scenario_id):
+                        where_text = "uuid='%s'" % scenario_id
+                    else:
+                        where_text = "name='%s'" % scenario_id
+                    if not tenant_id and tenant_id != "any":
+                        where_text += " AND (tenant_id='%s' OR public='True')" % tenant_id
+                    self.cur.execute("SELECT * FROM scenarios WHERE "+ where_text)
+                    rows = self.cur.fetchall()
+                    if self.cur.rowcount==0:
+                        return -HTTP_Bad_Request, "No scenario found with this criteria " + where_text
+                    elif self.cur.rowcount>1:
+                        return -HTTP_Bad_Request, "More than one scenario found with this criteria " + where_text
+                    scenario_uuid = rows[0]["uuid"]
+                    scenario_name = rows[0]["name"]
+                    
+                    #sce_vnfs
+                    self.cur.execute("DELETE FROM scenarios WHERE uuid='" + scenario_uuid + "'")
+    
+                    return 1, scenario_uuid + " " + scenario_name
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.delete_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c =  self.format_error(e, "delete", "instances running")
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def new_instance_scenario_as_a_whole(self,tenant_id,instance_scenario_name,instance_scenario_description,scenarioDict):
+        print "Adding new instance scenario to the NFVO database"
+        for retry_ in range(0,2):
+            created_time = time.time()
+            try:
+                with self.con:
+                    self.cur = self.con.cursor()
+                    #instance_scenarios
+                    datacenter_tenant_id = scenarioDict['datacenter_tenant_id']
+                    datacenter_id = scenarioDict['datacenter_id']
+                    INSERT_={'tenant_id': tenant_id,
+                        'datacenter_tenant_id': datacenter_tenant_id,
+                        'name': instance_scenario_name,
+                        'description': instance_scenario_description,
+                        'scenario_id' : scenarioDict['uuid'],
+                        'datacenter_id': datacenter_id
+                    }
+                    r,instance_uuid =  self._new_row_internal('instance_scenarios', INSERT_, tenant_id, True, None, True, created_time)
+                    if r<0:
+                        print 'nfvo_db.new_instance_scenario_as_a_whole() Error inserting at table instance_scenarios: ' + instance_uuid
+                        return r, instance_uuid                
+                    
+                    net_scene2instance={}
+                    #instance_nets   #nets interVNF
+                    for net in scenarioDict['nets']:
+                        INSERT_={'vim_net_id': net['vim_id'], 'external': net['external'], 'instance_scenario_id':instance_uuid } #,  'type': net['type']
+                        INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_id) 
+                        INSERT_['datacenter_tenant_id'] = net.get('datacenter_tenant_id', datacenter_tenant_id)
+                        if net.get("uuid"):
+                            INSERT_['sce_net_id'] = net['uuid']
+                        created_time += 0.00001
+                        r,instance_net_uuid =  self._new_row_internal('instance_nets', INSERT_, tenant_id, True, instance_uuid, True, created_time)
+                        if r<0:
+                            print 'nfvo_db.new_instance_scenario_as_a_whole() Error inserting at table instance_nets: ' + instance_net_uuid
+                            return r, instance_net_uuid                
+                        net_scene2instance[ net['uuid'] ] = instance_net_uuid
+                        net['uuid'] = instance_net_uuid  #overwrite scnario uuid by instance uuid
+                    
+                    #instance_vnfs
+                    for vnf in scenarioDict['vnfs']:
+                        INSERT_={'instance_scenario_id': instance_uuid,  'vnf_id': vnf['vnf_id']  }
+                        INSERT_['datacenter_id'] = vnf.get('datacenter_id', datacenter_id) 
+                        INSERT_['datacenter_tenant_id'] = vnf.get('datacenter_tenant_id', datacenter_tenant_id)
+                        if vnf.get("uuid"):
+                            INSERT_['sce_vnf_id'] = vnf['uuid']
+                        created_time += 0.00001
+                        r,instance_vnf_uuid =  self._new_row_internal('instance_vnfs', INSERT_, tenant_id, True, instance_uuid, True,created_time)
+                        if r<0:
+                            print 'nfvo_db.new_instance_scenario_as_a_whole() Error inserting at table instance_vnfs: ' + instance_vnf_uuid
+                            return r, instance_vnf_uuid                
+                        vnf['uuid'] = instance_vnf_uuid  #overwrite scnario uuid by instance uuid
+                        
+                        #instance_nets   #nets intraVNF
+                        for net in vnf['nets']:
+                            INSERT_={'vim_net_id': net['vim_id'], 'external': 'false', 'instance_scenario_id':instance_uuid  } #,  'type': net['type']
+                            INSERT_['datacenter_id'] = net.get('datacenter_id', datacenter_id) 
+                            INSERT_['datacenter_tenant_id'] = net.get('datacenter_tenant_id', datacenter_tenant_id)
+                            if net.get("uuid"):
+                                INSERT_['net_id'] = net['uuid']
+                            created_time += 0.00001
+                            r,instance_net_uuid =  self._new_row_internal('instance_nets', INSERT_, tenant_id, True, instance_uuid, True,created_time)
+                            if r<0:
+                                print 'nfvo_db.new_instance_scenario_as_a_whole() Error inserting at table instance_nets: ' + instance_net_uuid
+                                return r, instance_net_uuid                
+                            net_scene2instance[ net['uuid'] ] = instance_net_uuid
+                            net['uuid'] = instance_net_uuid  #overwrite scnario uuid by instance uuid
+                        
+                        #instance_vms
+                        for vm in vnf['vms']:
+                            INSERT_={'instance_vnf_id': instance_vnf_uuid,  'vm_id': vm['uuid'], 'vim_vm_id': vm['vim_id']  }
+                            created_time += 0.00001
+                            r,instance_vm_uuid =  self._new_row_internal('instance_vms', INSERT_, tenant_id, True, instance_uuid, True, created_time)
+                            if r<0:
+                                print 'nfvo_db.new_instance_scenario_as_a_whole() Error inserting at table instance_vms: ' + instance_vm_uuid
+                                return r, instance_vm_uuid                
+                            vm['uuid'] = instance_vm_uuid  #overwrite scnario uuid by instance uuid
+                            
+                            #instance_interfaces
+                            for interface in vm['interfaces']:
+                                net_id = interface.get('net_id', None)
+                                if net_id is None:
+                                    #check if is connected to a inter VNFs net
+                                    for iface in vnf['interfaces']:
+                                        if iface['interface_id'] == interface['uuid']:
+                                            net_id = iface.get('sce_net_id', None)
+                                            break
+                                if net_id is None:
+                                    continue
+                                interface_type='external' if interface['external_name'] is not None else 'internal'
+                                INSERT_={'instance_vm_id': instance_vm_uuid,  'instance_net_id': net_scene2instance[net_id],
+                                    'interface_id': interface['uuid'], 'vim_interface_id': interface.get('vim_id'), 'type':  interface_type  }
+                                #created_time += 0.00001
+                                r,interface_uuid =  self._new_row_internal('instance_interfaces', INSERT_, tenant_id, True, instance_uuid, True) #, created_time)
+                                if r<0:
+                                    print 'nfvo_db.new_instance_scenario_as_a_whole() Error inserting at table instance_interfaces: ' + interface_uuid
+                                    return r, interface_uuid                
+                                interface['uuid'] = interface_uuid  #overwrite scnario uuid by instance uuid
+                        
+    
+                
+                return 1, instance_uuid
+                
+            except (mdb.Error, AttributeError), e:
+                print "new_instance_scenario_as_a_whole DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+
+    def get_instance_scenario(self, instance_id, tenant_id=None, verbose=False):
+        '''Obtain the instance information, filtering by one or several of the tenant, uuid or name
+        instance_id is the uuid or the name if it is not a valid uuid format
+        Only one instance must mutch the filtering or an error is returned
+        ''' 
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor(mdb.cursors.DictCursor)
+                    #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):
+                        where_list.append( "inst.uuid='" + instance_id +"'" )
+                    else:
+                        where_list.append( "inst.name='" + instance_id +"'" )
+                    where_text = " AND ".join(where_list)
+                    command = "SELECT inst.uuid as uuid,inst.name as name,inst.scenario_id as scenario_id, datacenter_id" +\
+                                " ,datacenter_tenant_id, s.name as scenario_name,inst.tenant_id as tenant_id" + \
+                                " ,inst.description as description,inst.created_at as created_at" +\
+                            " FROM instance_scenarios as inst join scenarios as s on inst.scenario_id=s.uuid"+\
+                            " WHERE " + where_text
+                    self.cur.execute(command)
+                    rows = self.cur.fetchall()
+                    if self.cur.rowcount==0:
+                        return -HTTP_Bad_Request, "No instance found with this criteria " + where_text
+                    elif self.cur.rowcount>1:
+                        return -HTTP_Bad_Request, "More than one instance found with this criteria " + where_text
+                    instance_dict = rows[0]
+                    
+                    #instance_vnfs
+                    cmd = "SELECT iv.uuid as uuid,sv.vnf_id as vnf_id,sv.name as vnf_name, sce_vnf_id, datacenter_id, datacenter_tenant_id"\
+                            " FROM instance_vnfs as iv join sce_vnfs as sv on iv.sce_vnf_id=sv.uuid" \
+                            " WHERE iv.instance_scenario_id='%s'" \
+                            " ORDER BY iv.created_at " % instance_dict['uuid']
+                    self.cur.execute(cmd)
+                    instance_dict['vnfs'] = self.cur.fetchall()
+                    for vnf in instance_dict['vnfs']:
+                        vnf_manage_iface_list=[]
+                        #instance vms
+                        cmd = "SELECT iv.uuid as uuid, vim_vm_id, status, error_msg, vim_info, iv.created_at as created_at, name "\
+                                " FROM instance_vms as iv join vms on iv.vm_id=vms.uuid "\
+                                " WHERE instance_vnf_id='%s' ORDER BY iv.created_at" % vnf['uuid']
+                        self.cur.execute(cmd)
+                        vnf['vms'] = self.cur.fetchall()
+                        for vm in vnf['vms']:
+                            vm_manage_iface_list=[]
+                            #instance_interfaces
+                            cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address, ip_address, vim_info, i.type as type "\
+                                    " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid "\
+                                    " WHERE instance_vm_id='%s' ORDER BY created_at" % vm['uuid']
+                            self.cur.execute(cmd )
+                            vm['interfaces'] = self.cur.fetchall()
+                            for iface in vm['interfaces']:
+                                if iface["type"] == "mgmt" and iface["ip_address"]:
+                                    vnf_manage_iface_list.append(iface["ip_address"])
+                                    vm_manage_iface_list.append(iface["ip_address"])
+                                if not verbose:
+                                    del iface["type"]
+                            if vm_manage_iface_list: vm["ip_address"] = ",".join(vm_manage_iface_list)
+                        if vnf_manage_iface_list: vnf["ip_address"] = ",".join(vnf_manage_iface_list)
+                        
+                    #instance_nets
+                    #select_text = "instance_nets.uuid as uuid,sce_nets.name as net_name,instance_nets.vim_net_id as net_id,instance_nets.status as status,instance_nets.external as external" 
+                    #from_text = "instance_nets join instance_scenarios on instance_nets.instance_scenario_id=instance_scenarios.uuid " + \
+                    #            "join sce_nets on instance_scenarios.scenario_id=sce_nets.scenario_id"
+                    #where_text = "instance_nets.instance_scenario_id='"+ instance_dict['uuid'] + "'"
+                    cmd = "SELECT uuid,vim_net_id,status,error_msg,vim_info,external, sce_net_id, net_id as vnf_net_id, datacenter_id, datacenter_tenant_id"\
+                            " FROM instance_nets" \
+                            " WHERE instance_scenario_id='%s' ORDER BY created_at" % instance_dict['uuid']
+                    self.cur.execute(cmd)
+                    instance_dict['nets'] = self.cur.fetchall()
+                    
+                    af.convert_datetime2str(instance_dict)
+                    af.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])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+        
+    def delete_instance_scenario(self, instance_id, tenant_id=None):
+        '''Deletes a instance_Scenario, filtering by one or serveral of the tenant, uuid or name
+        instance_id is the uuid or the name if it is not a valid uuid format
+        Only one instance_scenario must mutch the filtering or an error is returned
+        ''' 
+        for retry_ in range(0,2):
+            try:
+                with self.con:
+                    self.cur = self.con.cursor(mdb.cursors.DictCursor)
+    
+                    #instance table
+                    where_list=[]
+                    if tenant_id is not None: where_list.append( "tenant_id='" + tenant_id +"'" )
+                    if af.check_valid_uuid(instance_id):
+                        where_list.append( "uuid='" + instance_id +"'" )
+                    else:
+                        where_list.append( "name='" + instance_id +"'" )
+                    where_text = " AND ".join(where_list)
+                    self.cur.execute("SELECT * FROM instance_scenarios WHERE "+ where_text)
+                    rows = self.cur.fetchall()
+                    if self.cur.rowcount==0:
+                        return -HTTP_Bad_Request, "No instance scenario found with this criteria " + where_text
+                    elif self.cur.rowcount>1:
+                        return -HTTP_Bad_Request, "More than one instance scenario found with this criteria " + where_text
+                    instance_uuid = rows[0]["uuid"]
+                    instance_name = rows[0]["name"]
+                    
+                    #sce_vnfs
+                    self.cur.execute("DELETE FROM instance_scenarios WHERE uuid='" + instance_uuid + "'")
+    
+                    return 1, instance_uuid + " " + instance_name
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.delete_instance_scenario DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e, "delete", "No dependences can avoid deleting!!!!")
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+    
+    def new_instance_scenario(self, instance_scenario_dict, tenant_id):
+        #return self.new_row('vnfs', vnf_dict, None, tenant_id, True, True)
+        return self._new_row_internal('instance_scenarios', instance_scenario_dict, tenant_id, add_uuid=True, root_uuid=None, log=True)
+
+    def update_instance_scenario(self, instance_scenario_dict):
+        #TODO:
+        return
+
+    def new_instance_vnf(self, instance_vnf_dict, tenant_id, instance_scenario_id = None):
+        #return self.new_row('vms', vm_dict, tenant_id, True, True)
+        return self._new_row_internal('instance_vnfs', instance_vnf_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
+
+    def update_instance_vnf(self, instance_vnf_dict):
+        #TODO:
+        return
+    
+    def delete_instance_vnf(self, instance_vnf_id):
+        #TODO:
+        return
+
+    def new_instance_vm(self, instance_vm_dict, tenant_id, instance_scenario_id = None):
+        #return self.new_row('vms', vm_dict, tenant_id, True, True)
+        return self._new_row_internal('instance_vms', instance_vm_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
+
+    def update_instance_vm(self, instance_vm_dict):
+        #TODO:
+        return
+    
+    def delete_instance_vm(self, instance_vm_id):
+        #TODO:
+        return
+
+    def new_instance_net(self, instance_net_dict, tenant_id, instance_scenario_id = None):
+        return self._new_row_internal('instance_nets', instance_net_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
+    
+    def update_instance_net(self, instance_net_dict):
+        #TODO:
+        return
+
+    def delete_instance_net(self, instance_net_id):
+        #TODO:
+        return
+    
+    def new_instance_interface(self, instance_interface_dict, tenant_id, instance_scenario_id = None):
+        return self._new_row_internal('instance_interfaces', instance_interface_dict, tenant_id, add_uuid=True, root_uuid=instance_scenario_id, log=True)
+
+    def update_instance_interface(self, instance_interface_dict):
+        #TODO:
+        return
+
+    def delete_instance_interface(self, instance_interface_dict):
+        #TODO:
+        return
+
+    def update_datacenter_nets(self, datacenter_id, new_net_list=[]):
+        ''' Removes the old and adds the new net list at datacenter list for one datacenter.
+        Attribute 
+            datacenter_id: uuid of the datacenter to act upon
+            table: table where to insert
+            new_net_list: the new values to be inserted. If empty it only deletes the existing nets
+        Return: (Inserted items, Deleted items) if OK, (-Error, text) if error
+        '''
+        for retry_ in range(0,2):
+            created_time = time.time()
+            try:
+                with self.con:
+                    self.cur = self.con.cursor()
+                    cmd="DELETE FROM datacenter_nets WHERE datacenter_id='%s'" % datacenter_id
+                    print cmd
+                    self.cur.execute(cmd)
+                    deleted = self.cur.rowcount
+                    for new_net in new_net_list:
+                        created_time += 0.00001
+                        self._new_row_internal('datacenter_nets', new_net, tenant_id=None, add_uuid=True, created_time=created_time)
+                    return len (new_net_list), deleted
+            except (mdb.Error, AttributeError), e:
+                print "nfvo_db.update_datacenter_nets DB Exception %d: %s" % (e.args[0], e.args[1])
+                r,c = self.format_error(e)
+                if r!=-HTTP_Request_Timeout or retry_==1: return r,c
+        
diff --git a/openmano b/openmano
new file mode 100755 (executable)
index 0000000..938d13d
--- /dev/null
+++ b/openmano
@@ -0,0 +1,1371 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# PYTHON_ARGCOMPLETE_OK
+
+##
+# 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
+##
+
+'''
+openmano client used to interact with openmano-server (openmanod) 
+'''
+__author__="Alfonso Tierno, Gerardo Garcia"
+__date__ ="$09-oct-2014 09:09:48$"
+__version__="0.4.3-r467"
+version_date="Mar 2016"
+
+from argcomplete.completers import FilesCompleter
+import os
+import argparse
+import argcomplete
+import requests
+import json
+import yaml
+import logging
+#from jsonschema import validate as js_v, exceptions as js_e
+
+class ArgumentParserError(Exception): pass
+
+class OpenmanoCLIError(Exception): pass
+
+class ThrowingArgumentParser(argparse.ArgumentParser):
+    def error(self, message):
+        print "Error: %s" %message
+        print
+        self.print_usage()
+        #self.print_help()
+        print
+        print "Type 'openmano -h' for help"
+        raise ArgumentParserError
+
+
+def config(args):
+    print "OPENMANO_HOST: %s" %mano_host
+    print "OPENMANO_PORT: %s" %mano_port
+    print "OPENMANO_TENANT: %s" %mano_tenant
+    print "OPENMANO_DATACENTER: %s" %str (mano_datacenter)
+
+def _print_verbose(mano_response, verbose_level=0):
+    content = mano_response.json()
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    if type(content)!=dict or len(content)!=1:
+        #print "Non expected format output"
+        print str(content)
+        return result
+    
+    val=content.values()[0]
+    if type(val)==str:
+        print val
+        return result
+    elif type(val) == list:
+        content_list = val
+    elif type(val)==dict:
+        content_list = [val]
+    else:
+        #print "Non expected dict/list format output"
+        print str(content)
+        return result
+    
+    #print content_list
+    if verbose_level==None:
+        verbose_level=0
+    if verbose_level >= 3:
+        print yaml.safe_dump(content, indent=4, default_flow_style=False)
+        return result
+
+    if mano_response.status_code == 200:
+        for content in content_list:
+            if "uuid" in content:
+                uuid = content['uuid']
+            elif "id" in content:
+                uuid = content['id']
+            elif "vim_id" in content:
+                uuid = content['vim_id']
+            myoutput = "%s %s" %(uuid.ljust(38),content['name'].ljust(20))
+            if "status" in content:
+                myoutput += " " + content['status'].ljust(20)
+            elif "enabled" in content and not content["enabled"]:
+                myoutput += " enabled=False".ljust(20)
+            if verbose_level >=1:
+                if 'created_at' in content:
+                    myoutput += " " + content['created_at'].ljust(20)
+                if verbose_level >=2:
+                    new_line='\n'
+                    if 'type' in content and content['type']!=None:
+                        myoutput += new_line + "  Type: " + content['type'].ljust(29)
+                        new_line=''
+                    if 'description' in content and content['description']!=None:
+                        myoutput += new_line + "  Description: " + content['description'].ljust(20)
+            print myoutput
+    else:
+        print content['error']['description']
+    return result
+
+def parser_json_yaml(file_name):
+    try:
+        f = file(file_name, "r")
+        text = f.read()
+        f.close()
+    except Exception as e:
+        return (False, str(e))
+           
+    #Read and parse file
+    if file_name[-5:]=='.yaml' or file_name[-4:]=='.yml' or (file_name[-5:]!='.json' and '\t' not in text):
+        try:
+            config = yaml.load(text)
+        except yaml.YAMLError as exc:
+            error_pos = ""
+            if hasattr(exc, 'problem_mark'):
+                mark = exc.problem_mark
+                error_pos = " at line:%s column:%s" % (mark.line+1, mark.column+1)
+            return (False, "Error loading file '"+file_name+"' yaml format error" + error_pos)
+    else: #json
+        try:
+            config = json.loads(text) 
+        except Exception as e:
+            return (False, "Error loading file '"+file_name+"' json format error " + str(e) )
+
+    return True, config
+
+def _load_file_or_yaml(content):
+    '''
+    'content' can be or a yaml/json file or a text containing a yaml/json text format
+    This function autodetect, trying to load and parse the file,
+    if fails trying to parse the 'content' text
+    Returns the dictionary once parsed, or print an error and finish the program
+    '''
+    #Check config file exists
+    if os.path.isfile(content):
+        r,payload = parser_json_yaml(content)
+        if not r:
+            print payload
+            exit(-1)
+    elif "{" in content or ":" in content:
+        try:
+            payload = yaml.load(content)
+        except yaml.YAMLError as exc:
+            error_pos = ""
+            if hasattr(exc, 'problem_mark'):
+                mark = exc.problem_mark
+                error_pos = " at position: (%s:%s)" % (mark.line+1, mark.column+1)
+            print "Error loading yaml/json text"+error_pos
+            exit (-1)
+    else:
+        print "'%s' is neither a valid file nor a yaml/json content" % content
+        exit(-1)
+    return payload
+
+def _get_item_uuid(item, item_name_id, tenant=None):
+    if tenant:
+        URLrequest = "http://%s:%s/openmano/%s/%s" %(mano_host, mano_port, tenant, item)
+    else:
+        URLrequest = "http://%s:%s/openmano/%s" %(mano_host, mano_port, item)
+    mano_response = requests.get(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    content = mano_response.json()
+    #print content
+    found = 0
+    for i in content[item]:
+        if i["uuid"] == item_name_id:
+            return item_name_id
+        if i["name"] == item_name_id:
+            uuid = i["uuid"]
+            found += 1
+    if found == 0:
+        raise OpenmanoCLIError("No %s found with name/uuid '%s'" %(item[:-1], item_name_id))
+    elif found > 1:
+        raise OpenmanoCLIError("%d %s found with name '%s'. uuid must be used" %(found, item, item_name_id))
+    return uuid
+# 
+# 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
+    
+def _get_tenant(tenant_name_id = None):
+    if not tenant_name_id:
+        tenant_name_id = mano_tenant
+        if not mano_tenant:
+            raise OpenmanoCLIError("'OPENMANO_TENANT' environment variable is not set")
+    return _get_item_uuid("tenants", tenant_name_id)
+
+def _get_datacenter(datacenter_name_id = None, tenant = "any"):
+    if not datacenter_name_id:
+        datacenter_name_id = mano_datacenter
+        if not datacenter_name_id:
+            raise OpenmanoCLIError("neither 'OPENMANO_DATACENTER' environment variable is set nor --datacenter option is used")
+    return _get_item_uuid("datacenters", datacenter_name_id, tenant)
+
+def vnf_create(args):
+    #print "vnf-create",args
+    headers_req = {'Accept': 'application/json', 'content-type': 'application/json'}
+    tenant = _get_tenant()
+    myvnf = _load_file_or_yaml(args.file)
+
+    if args.name or args.description or args.image_path:
+        #print args.name
+        try:
+            if args.name:
+                myvnf['vnf']['name'] = args.name
+            if args.description:
+                myvnf['vnf']['description'] = args.description
+            if args.image_path:
+                index=0
+                for image_path_ in args.image_path.split(","):
+                    #print "image-path", image_path_
+                    myvnf['vnf']['VNFC'][index]['VNFC image']=image_path_
+                    index=index+1
+        except (KeyError, TypeError), e:
+            if str(e)=='vnf':           error_pos= "missing field 'vnf'"
+            elif str(e)=='name':        error_pos= "missing field  'vnf':'name'"
+            elif str(e)=='description': error_pos= "missing field  'vnf':'description'"
+            elif str(e)=='VNFC':        error_pos= "missing field  'vnf':'VNFC'"
+            elif str(e)==str(index):    error_pos= "field  'vnf':'VNFC' must be an array"
+            elif str(e)=='VNFC image':  error_pos= "missing field 'vnf':'VNFC'['VNFC image']"
+            else:                       error_pos="wrong format"
+            print "Wrong VNF descriptor: " + error_pos
+            return -1 
+    payload_req = json.dumps(myvnf)
+        
+    #print payload_req
+        
+    URLrequest = "http://%s:%s/openmano/%s/vnfs" %(mano_host, mano_port, tenant)
+    logger.debug("openmano request: %s", payload_req)
+    mano_response = requests.post(URLrequest, headers = headers_req, data=payload_req)
+    logger.debug("openmano response: %s", mano_response.text )
+
+    return _print_verbose(mano_response, args.verbose)
+
+def vnf_list(args):
+    #print "vnf-list",args
+    if args.all:
+        tenant = "any"
+    else:
+        tenant = _get_tenant()
+    if args.name:
+        toshow = _get_item_uuid("vnfs", args.name, tenant)
+        URLrequest = "http://%s:%s/openmano/%s/vnfs/%s" %(mano_host, mano_port, tenant, toshow)
+    else:
+        URLrequest = "http://%s:%s/openmano/%s/vnfs" %(mano_host, mano_port, tenant)
+    mano_response = requests.get(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if args.verbose==None:
+        args.verbose=0
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    if mano_response.status_code == 200:
+        if not args.name:
+            if args.verbose >= 3:
+                print yaml.safe_dump(content, indent=4, default_flow_style=False)
+                return result
+            if len(content['vnfs']) == 0:
+                print "No VNFs were found."
+                return 404 #HTTP_Not_Found
+            for vnf in content['vnfs']:
+                myoutput = "%s %s" %(vnf['uuid'].ljust(38),vnf['name'].ljust(20))
+                if args.verbose >=1:
+                    myoutput = "%s %s" %(myoutput, vnf['created_at'].ljust(20))
+                print myoutput
+                if args.verbose >=2:
+                    print "  Description: %s" %vnf['description']
+                    print "  VNF descriptor file: %s" %vnf['path']
+        else:
+            if args.verbose:
+                print yaml.safe_dump(content, indent=4, default_flow_style=False)
+                return result
+            vnf = content['vnf']
+            print "%s %s %s" %(vnf['uuid'].ljust(38),vnf['name'].ljust(20), vnf['created_at'].ljust(20))
+            print "  Description: %s" %vnf['description']
+            #print "  VNF descriptor file: %s" %vnf['path']
+            print "    VMs:"
+            for vm in vnf['VNFC']:
+                #print "    %s %s %s" %(vm['name'].ljust(20), vm['uuid'].ljust(38), vm['description'].ljust(30))
+                print "        %s %s" %(vm['name'].ljust(20), vm['description'])
+            if len(vnf['nets'])>0:
+                print "    Internal nets:"
+                for net in vnf['nets']:
+                    print "        %s %s" %(net['name'].ljust(20), net['description'])
+            if len(vnf['external-connections'])>0:
+                print "    External interfaces:"
+                for interface in vnf['external-connections']:
+                    print "        %s %s %s %s" %(interface['external_name'].ljust(20), interface['vm_name'].ljust(20), interface['internal_name'].ljust(20), \
+                                                  interface['vpci'].ljust(14))
+    else:
+        print content['error']['description']
+        if args.verbose:
+            print yaml.safe_dump(content, indent=4, default_flow_style=False)
+    return result
+
+def vnf_delete(args):
+    #print "vnf-delete",args
+    if args.all:
+        tenant = "any"
+    else:
+        tenant = _get_tenant()
+    todelete = _get_item_uuid("vnfs", args.name, tenant=tenant)
+    if not args.force:
+        r = raw_input("Delete VNF %s (y/N)? " %(todelete))
+        if  not (len(r)>0  and r[0].lower()=="y"):
+            return 0
+    URLrequest = "http://%s:%s/openmano/%s/vnfs/%s" %(mano_host, mano_port, tenant, todelete)
+    mano_response = requests.delete(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if mano_response.status_code == 200:
+        print content['result']
+    else:
+        print content['error']['description']
+    return result
+
+def scenario_create(args):
+    #print "scenario-create",args
+    tenant = _get_tenant()
+    headers_req = {'content-type': 'application/yaml'}
+    myscenario = _load_file_or_yaml(args.file)
+
+    if args.name:
+        myscenario['name'] = args.name
+    if args.description:
+        myscenario['description'] = args.description
+    payload_req = yaml.safe_dump(myscenario, explicit_start=True, indent=4, default_flow_style=False, tags=False, encoding='utf-8', allow_unicode=True)
+    
+    #print payload_req
+        
+    URLrequest = "http://%s:%s/openmano/%s/scenarios" %(mano_host, mano_port, tenant)
+    logger.debug("openmano request: %s", payload_req)
+    mano_response = requests.post(URLrequest, headers = headers_req, data=payload_req)
+    logger.debug("openmano response: %s", mano_response.text )
+    return _print_verbose(mano_response, args.verbose)
+
+def scenario_list(args):
+    #print "scenario-list",args
+    if args.all:
+        tenant = "any"
+    else:
+        tenant = _get_tenant()
+    if args.name:
+        toshow = _get_item_uuid("scenarios", args.name, tenant)
+        URLrequest = "http://%s:%s/openmano/%s/scenarios/%s" %(mano_host, mano_port, tenant, toshow)
+    else:
+        URLrequest = "http://%s:%s/openmano/%s/scenarios" %(mano_host, mano_port, tenant)
+    mano_response = requests.get(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if args.verbose==None:
+        args.verbose=0
+
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    if mano_response.status_code == 200:
+        if not args.name:
+            if args.verbose >= 3:
+                print yaml.safe_dump(content, indent=4, default_flow_style=False)
+                return result
+            if len(content['scenarios']) == 0:
+                print "No scenarios were found."
+                return 404 #HTTP_Not_Found
+            for scenario in content['scenarios']:
+                myoutput = "%s %s" %(scenario['uuid'].ljust(38),scenario['name'].ljust(20))
+                if args.verbose >=1:
+                    myoutput = "%s %s" %(myoutput, scenario['created_at'].ljust(20))
+                print myoutput
+                if args.verbose >=2:
+                    print "  Description: %s" %scenario['description']
+        else:
+            if args.verbose:
+                print yaml.safe_dump(content, indent=4, default_flow_style=False)
+                return result
+            scenario = content['scenario']
+            myoutput = "%s %s %s" %(scenario['uuid'].ljust(38),scenario['name'].ljust(20), scenario['created_at'].ljust(20))
+            print myoutput
+            print "  Description: %s" %scenario['description']
+            print "    VNFs:"
+            for vnf in scenario['vnfs']:
+                print "        %s %s %s" %(vnf['name'].ljust(20), vnf['vnf_id'].ljust(38), vnf['description'])
+            if len(scenario['nets'])>0:
+                print "    Internal nets:"
+                for net in scenario['nets']:
+                    if net['description'] is None:   #if description does not exist, description is "-". Valid for external and internal nets.
+                        net['description'] = '-' 
+                    if not net['external']:
+                        print "        %s %s %s" %(net['name'].ljust(20), net['uuid'].ljust(38), net['description'].ljust(30))
+                print "    External nets:"
+                for net in scenario['nets']:
+                    if net['external']:
+                        print "        %s %s %s vim-id:%s" %(net['name'].ljust(20), net['uuid'].ljust(38), net['description'].ljust(30), net['vim_id'])
+    else:
+        print content['error']['description']
+        if args.verbose:
+            print yaml.safe_dump(content, indent=4, default_flow_style=False)
+    return result
+
+def scenario_delete(args):
+    #print "scenario-delete",args
+    if args.all:
+        tenant = "any"
+    else:
+        tenant = _get_tenant()
+    todelete = _get_item_uuid("scenarios", args.name, tenant=tenant)
+    if not args.force:
+        r = raw_input("Delete scenario %s (y/N)? " %(args.name))
+        if  not (len(r)>0  and r[0].lower()=="y"):
+            return 0
+    URLrequest = "http://%s:%s/openmano/%s/scenarios/%s" %(mano_host, mano_port, tenant, todelete)
+    mano_response = requests.delete(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if mano_response.status_code == 200:
+        print content['result']
+    else:
+        print content['error']['description']
+    return result
+
+def scenario_deploy(args):
+    print "This command is deprecated, use 'openmano instance-scenario-create --scenario %s --name %s' instead!!!" % (args.scenario, args.name)
+    print
+    args.file = None
+    args.netmap_use = None
+    args.netmap_create = None
+    return instance_create(args)
+
+#     #print "scenario-deploy",args
+#     headers_req = {'content-type': 'application/json'}
+#     action = {}
+#     actionCmd="start"
+#     if args.nostart:
+#         actionCmd="reserve"
+#     action[actionCmd] = {}
+#     action[actionCmd]["instance_name"] = args.name
+#     if args.datacenter != None:
+#         action[actionCmd]["datacenter"] = args.datacenter
+#     elif mano_datacenter != None:
+#         action[actionCmd]["datacenter"] = mano_datacenter
+#         
+#     if args.description:
+#         action[actionCmd]["description"] = args.description
+#     payload_req = json.dumps(action, indent=4)
+#     #print payload_req
+# 
+#     URLrequest = "http://%s:%s/openmano/%s/scenarios/%s/action" %(mano_host, mano_port, mano_tenant, args.scenario)
+#     logger.debug("openmano request: %s", payload_req)
+#     mano_response = requests.post(URLrequest, headers = headers_req, data=payload_req)
+#     logger.debug("openmano response: %s", mano_response.text )
+#     if args.verbose==None:
+#         args.verbose=0
+#     
+#     result = 0 if mano_response.status_code==200 else mano_response.status_code
+#     content = mano_response.json()
+#     #print json.dumps(content, indent=4)
+#     if args.verbose >= 3:
+#         print yaml.safe_dump(content, indent=4, default_flow_style=False)
+#         return result
+# 
+#     if mano_response.status_code == 200:
+#         myoutput = "%s %s" %(content['uuid'].ljust(38),content['name'].ljust(20))
+#         if args.verbose >=1:
+#             myoutput = "%s %s" %(myoutput, content['created_at'].ljust(20))
+#         if args.verbose >=2:
+#             myoutput = "%s %s %s" %(myoutput, content['description'].ljust(30))
+#         print myoutput
+#         print ""
+#         print "To check the status, run the following command:"
+#         print "openmano instance-scenario-list <instance_id>"
+#     else:
+#         print content['error']['description']
+#     return result
+
+def scenario_verify(args):
+    #print "scenario-verify",args
+    headers_req = {'content-type': 'application/json'}
+    action = {}
+    action["verify"] = {}
+    action["verify"]["instance_name"] = "scen-verify-return5"
+    payload_req = json.dumps(action, indent=4)
+    #print payload_req
+
+    URLrequest = "http://%s:%s/openmano/%s/scenarios/%s/action" %(mano_host, mano_port, mano_tenant, args.scenario)
+    logger.debug("openmano request: %s", payload_req)
+    mano_response = requests.post(URLrequest, headers = headers_req, data=payload_req)
+    logger.debug("openmano response: %s", mano_response.text )
+    
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if mano_response.status_code == 200:
+        print content['result']
+    else:
+        print content['error']['description']
+    return result
+
+def instance_create(args):
+    tenant = _get_tenant()
+    headers_req = {'content-type': 'application/yaml'}
+    myInstance={"instance": {}, "schema_version": "0.1"}
+    if args.file:
+        instance_dict = _load_file_or_yaml(args.file)
+        if "instance" not in instance_dict:
+            myInstance = {"instance": instance_dict, "schema_version": "0.1"}
+        else:
+            myInstance = instance_dict
+    if args.name:
+        myInstance["instance"]['name'] = args.name
+    if args.description:
+        myInstance["instance"]['description'] = args.description
+    if args.nostart:
+        myInstance["instance"]['action'] = "reserve"
+    #datacenter
+    datacenter = myInstance["instance"].get("datacenter")
+    if args.datacenter != None:
+        datacenter = args.datacenter
+    myInstance["instance"]["datacenter"] = _get_datacenter(datacenter, tenant)
+    #scenario
+    scenario = myInstance["instance"].get("scenario")
+    if args.scenario != None:
+        scenario = args.scenario
+    if not scenario:
+        print "you must provide an scenario in the file descriptor or with --scenario"
+        return -1
+    myInstance["instance"]["scenario"] = _get_item_uuid("scenarios", scenario, tenant)
+    if args.netmap_use:
+        if "networks" not in myInstance["instance"]:
+            myInstance["instance"]["networks"] = {}
+        for net in args.netmap_use:
+            net_comma_list = net.split(",")
+            for net_comma in net_comma_list:
+                net_tuple = net_comma.split("=")
+                if len(net_tuple) != 2:
+                    print "error at netmap-use. Expected net-scenario=net-datacenter. (%s)?" % net_comma
+                    return
+                net_scenario   = net_tuple[0].strip()
+                net_datacenter = net_tuple[1].strip()
+                if net_scenario not in myInstance["instance"]["networks"]:
+                    myInstance["instance"]["networks"][net_scenario] = {} 
+                myInstance["instance"]["networks"][net_scenario]["netmap-use"] = net_datacenter
+    if args.netmap_create:
+        if "networks" not in myInstance["instance"]:
+            myInstance["instance"]["networks"] = {}
+        for net in args.netmap_create:
+            net_comma_list = net.split(",")
+            for net_comma in net_comma_list:
+                net_tuple = net_comma.split("=")
+                if len(net_tuple) == 1:
+                    net_scenario   = net_tuple[0].strip()
+                    net_datacenter = None
+                elif len(net_tuple) == 2:
+                    net_scenario   = net_tuple[0].strip()
+                    net_datacenter = net_tuple[1].strip()
+                else:
+                    print "error at netmap-create. Expected net-scenario=net-datacenter or net-scenario. (%s)?" % net_comma
+                    return
+                if net_scenario not in myInstance["instance"]["networks"]:
+                    myInstance["instance"]["networks"][net_scenario] = {} 
+                myInstance["instance"]["networks"][net_scenario]["netmap-create"] = net_datacenter
+                        
+    payload_req = yaml.safe_dump(myInstance, explicit_start=True, indent=4, default_flow_style=False, tags=False, encoding='utf-8', allow_unicode=True)
+    logger.debug("openmano request: %s", payload_req)
+    URLrequest = "http://%s:%s/openmano/%s/instances" %(mano_host, mano_port, tenant)
+    mano_response = requests.post(URLrequest, headers = headers_req, data=payload_req)
+    logger.debug("openmano response: %s", mano_response.text )
+    if args.verbose==None:
+        args.verbose=0
+    
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if args.verbose >= 3:
+        print yaml.safe_dump(content, indent=4, default_flow_style=False)
+        return result
+
+    if mano_response.status_code == 200:
+        myoutput = "%s %s" %(content['uuid'].ljust(38),content['name'].ljust(20))
+        if args.verbose >=1:
+            myoutput = "%s %s" %(myoutput, content['created_at'].ljust(20))
+        if args.verbose >=2:
+            myoutput = "%s %s %s" %(myoutput, content['description'].ljust(30))
+        print myoutput
+    else:
+        print content['error']['description']
+    return result
+
+def instance_scenario_list(args):
+    #print "instance-scenario-list",args
+    if args.all:
+        tenant = "any"
+    else:
+        tenant = _get_tenant()
+    if args.name:
+        toshow = _get_item_uuid("instances", args.name, tenant)
+        URLrequest = "http://%s:%s/openmano/%s/instances/%s" %(mano_host, mano_port, tenant, toshow)
+    else:
+        URLrequest = "http://%s:%s/openmano/%s/instances" %(mano_host, mano_port, tenant)
+    mano_response = requests.get(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if args.verbose==None:
+        args.verbose=0
+
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    if mano_response.status_code == 200:
+        if not args.name:
+            if args.verbose >= 3:
+                print yaml.safe_dump(content, indent=4, default_flow_style=False)
+                return result
+            if len(content['instances']) == 0:
+                print "No scenario instances were found."
+                return result
+            for instance in content['instances']:
+                myoutput = "%s %s" %(instance['uuid'].ljust(38),instance['name'].ljust(20))
+                if args.verbose >=1:
+                    myoutput = "%s %s" %(myoutput, instance['created_at'].ljust(20))
+                print myoutput
+                if args.verbose >=2:
+                    print "Description: %s" %instance['description']
+        else:
+            if args.verbose:
+                print yaml.safe_dump(content, indent=4, default_flow_style=False)
+                return result
+            instance = content
+            print "%s %s %s" %(instance['uuid'].ljust(38),instance['name'].ljust(20),instance['created_at'].ljust(20))
+            print "Description: %s" %instance['description']
+            print "Template scenario id: %s" %instance['scenario_id']
+            print "Template scenario name: %s" %instance['scenario_name']
+            print "---------------------------------------"
+            print "VNF instances: %d" %len(instance['vnfs'])
+            for vnf in instance['vnfs']:
+                #print "    %s %s Template vnf name: %s Template vnf id: %s" %(vnf['uuid'].ljust(38), vnf['name'].ljust(20), vnf['vnf_name'].ljust(20), vnf['vnf_id'].ljust(38))
+                print "    %s %s Template vnf id: %s" %(vnf['uuid'].ljust(38), vnf['vnf_name'].ljust(20), vnf['vnf_id'].ljust(38))
+            if len(instance['nets'])>0:
+                print "---------------------------------------"
+                print "Internal nets:"
+                for net in instance['nets']:
+                    if not net['external']:
+                        print "    %s %s VIM ID: %s" %(net['uuid'].ljust(38), net['status'].ljust(12), net['vim_net_id'])
+                print "---------------------------------------"
+                print "External nets:"
+                for net in instance['nets']:
+                    if net['external']:
+                        print "    %s %s VIM ID: %s" %(net['uuid'].ljust(38), net['status'].ljust(12), net['vim_net_id'])
+            print "---------------------------------------"
+            print "VM instances:"
+            for vnf in instance['vnfs']:
+                for vm in vnf['vms']:
+                    print "    %s %s %s %s VIM ID: %s" %(vm['uuid'].ljust(38), vnf['vnf_name'].ljust(20), vm['name'].ljust(20), vm['status'].ljust(12), vm['vim_vm_id'])
+    else:
+        print content['error']['description']
+        if args.verbose:
+            print yaml.safe_dump(content, indent=4, default_flow_style=False)
+    return result
+
+def instance_scenario_status(args):
+    print "instance-scenario-status"
+    return 0
+
+def instance_scenario_delete(args):
+    if args.all:
+        tenant = "any"
+    else:
+        tenant = _get_tenant()
+    todelete = _get_item_uuid("instances", args.name, tenant=tenant)
+    #print "instance-scenario-delete",args
+    if not args.force:
+        r = raw_input("Delete scenario instance %s (y/N)? " %(args.name))
+        if  not (len(r)>0  and r[0].lower()=="y"):
+            return
+    URLrequest = "http://%s:%s/openmano/%s/instances/%s" %(mano_host, mano_port, tenant, todelete)
+    mano_response = requests.delete(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if mano_response.status_code == 200:
+        print content['result']
+    else:
+        print content['error']['description']
+    return result
+
+def instance_scenario_action(args):
+    #print "instance-scenario-action", args
+    tenant = _get_tenant()
+    toact = _get_item_uuid("instances", args.name, tenant=tenant)
+    action={}
+    action[ args.action ] = args.param
+    if args.vnf:
+        action["vnfs"] = args.vnf
+    if args.vm:
+        action["vms"] = args.vm
+    
+    headers_req = {'content-type': 'application/json'}
+    payload_req = json.dumps(action, indent=4)
+    URLrequest = "http://%s:%s/openmano/%s/instances/%s/action" %(mano_host, mano_port, tenant, toact)
+    logger.debug("openmano request: %s", payload_req)
+    mano_response = requests.post(URLrequest, headers = headers_req, data=payload_req)
+    logger.debug("openmano response: %s", mano_response.text )
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if mano_response.status_code == 200:
+        if args.verbose:
+            print yaml.safe_dump(content, indent=4, default_flow_style=False)
+            return result
+        for uuid,c in content.iteritems():
+            print "%s %s %s" %(uuid.ljust(38), c['name'].ljust(20),c['description'].ljust(20))
+    else:
+        print content['error']['description']
+    return result
+
+
+def instance_vnf_list(args):
+    print "instance-vnf-list"
+    return 0
+
+def instance_vnf_status(args):
+    print "instance-vnf-status"
+    return 0
+
+def tenant_create(args):
+    headers_req = {'Accept': 'application/json', 'content-type': 'application/json'}
+    tenant_dict={"name": args.name}
+    if args.description!=None:
+        tenant_dict["description"] = args.description 
+    payload_req = json.dumps( {"tenant": tenant_dict })
+    
+    #print payload_req
+        
+    URLrequest = "http://%s:%s/openmano/tenants" %(mano_host, mano_port)
+    logger.debug("openmano request: %s", payload_req)
+    mano_response = requests.post(URLrequest, headers=headers_req, data=payload_req)
+    logger.debug("openmano response: %s", mano_response.text )
+    return _print_verbose(mano_response, args.verbose)
+
+def tenant_list(args):
+    #print "tenant-list",args
+    if args.name:
+        toshow = _get_item_uuid("vnfs", args.name)
+        URLrequest = "http://%s:%s/openmano/tenants/%s" %(mano_host, mano_port, toshow)
+    else:
+        URLrequest = "http://%s:%s/openmano/tenants" %(mano_host, mano_port)
+    mano_response = requests.get(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    if args.verbose==None:
+        args.verbose=0
+    if args.name!=None:
+        args.verbose += 1
+    return _print_verbose(mano_response, args.verbose)
+
+def tenant_delete(args):
+    #print "tenant-delete",args
+    todelete = _get_item_uuid("tenants", args.name)
+    if not args.force:
+        r = raw_input("Delete tenant %s (y/N)? " %(args.name))
+        if  not (len(r)>0  and r[0].lower()=="y"):
+            return 0
+    URLrequest = "http://%s:%s/openmano/tenants/%s" %(mano_host, mano_port, todelete)
+    mano_response = requests.delete(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if mano_response.status_code == 200:
+        print content['result']
+    else:
+        print content['error']['description']
+    return result
+
+def datacenter_attach(args):
+    tenant = _get_tenant()
+    datacenter = _get_datacenter(args.name)
+    headers_req = {'Accept': 'application/json', 'content-type': 'application/json'}
+    
+    datacenter_dict={}
+    if args.vim_tenant_id != None:
+        datacenter_dict['vim_tenant'] = args.vim_tenant_id
+    if args.vim_tenant_name != None:
+        datacenter_dict['vim_tenant_name'] = args.vim_tenant_name
+    if args.user != None:
+        datacenter_dict['vim_username'] = args.user
+    if args.password != None:
+        datacenter_dict['vim_password'] = args.password
+    payload_req = json.dumps( {"datacenter": datacenter_dict })
+    
+    #print payload_req
+        
+    URLrequest = "http://%s:%s/openmano/%s/datacenters/%s" %(mano_host, mano_port, tenant, datacenter)
+    logger.debug("openmano request: %s", payload_req)
+    mano_response = requests.post(URLrequest, headers=headers_req, data=payload_req)
+    logger.debug("openmano response: %s", mano_response.text )
+    result = _print_verbose(mano_response, args.verbose)
+    #provide addional information if error
+    if mano_response.status_code != 200:
+        content = mano_response.json()
+        if "already in use for  'name'" in content['error']['description'] and \
+                "to database vim_tenants table" in content['error']['description']:
+            print "Try to specify a different name with --vim-tenant-name"
+    return result
+
+def datacenter_detach(args):
+    if args.all:
+        tenant = "any"
+    else:
+        tenant = _get_tenant()
+    datacenter = _get_datacenter(args.name, tenant)
+    headers_req = {'Accept': 'application/json', 'content-type': 'application/json'}
+    URLrequest = "http://%s:%s/openmano/%s/datacenters/%s" %(mano_host, mano_port, tenant, datacenter)
+    mano_response = requests.delete(URLrequest, headers=headers_req)
+    logger.debug("openmano response: %s", mano_response.text )
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    if mano_response.status_code == 200:
+        print content['result']
+    else:
+        print content['error']['description']
+    return result
+
+def datacenter_create(args):
+    headers_req = {'Accept': 'application/json', 'content-type': 'application/json'}
+    datacenter_dict={"name": args.name, "vim_url": args.url}
+    if args.description!=None:
+        datacenter_dict["description"] = args.description 
+    if args.type!=None:
+        datacenter_dict["type"] = args.type 
+    if args.url!=None:
+        datacenter_dict["vim_url_admin"] = args.url_admin 
+    if args.config!=None:
+        datacenter_dict["config"] = _load_file_or_yaml(args.config) 
+    payload_req = json.dumps( {"datacenter": datacenter_dict })
+    
+    #print payload_req
+        
+    URLrequest = "http://%s:%s/openmano/datacenters" %(mano_host, mano_port)
+    logger.debug("openmano request: %s", payload_req)
+    mano_response = requests.post(URLrequest, headers=headers_req, data=payload_req)
+    logger.debug("openmano response: %s", mano_response.text )
+    return _print_verbose(mano_response, args.verbose)
+
+def datacenter_delete(args):
+    #print "datacenter-delete",args
+    todelete = _get_item_uuid("datacenters", args.name, "any")
+    if not args.force:
+        r = raw_input("Delete datacenter %s (y/N)? " %(args.name))
+        if  not (len(r)>0  and r[0].lower()=="y"):
+            return 0
+    URLrequest = "http://%s:%s/openmano/datacenters/%s" %(mano_host, mano_port, todelete)
+    mano_response = requests.delete(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    result = 0 if mano_response.status_code==200 else mano_response.status_code
+    content = mano_response.json()
+    #print json.dumps(content, indent=4)
+    if mano_response.status_code == 200:
+        print content['result']
+    else:
+        print content['error']['description']
+    return result
+
+def datacenter_list(args):
+    #print "datacenter-list",args
+    tenant='any' if args.all else _get_tenant()
+    
+    if args.name:
+        toshow = _get_item_uuid("datacenters", args.name, tenant) 
+        URLrequest = "http://%s:%s/openmano/%s/datacenters/%s" %(mano_host, mano_port, tenant, toshow)
+    else:
+        URLrequest = "http://%s:%s/openmano/%s/datacenters" %(mano_host, mano_port, tenant)
+    mano_response = requests.get(URLrequest)
+    logger.debug("openmano response: %s", mano_response.text )
+    if args.verbose==None:
+        args.verbose=0
+    if args.name!=None:
+        args.verbose += 1
+    return _print_verbose(mano_response, args.verbose)
+
+def vim_action(args):
+    #print "datacenter-net-action",args
+    tenant = _get_tenant()
+    datacenter = _get_datacenter(args.datacenter, tenant)
+    if args.verbose==None:
+        args.verbose=0
+    if args.action=="list":
+        URLrequest = "http://%s:%s/openmano/%s/vim/%s/%ss" %(mano_host, mano_port, tenant, datacenter, args.item)
+        if args.name!=None:
+            args.verbose += 1
+            URLrequest += "/" + args.name
+        mano_response = requests.get(URLrequest)
+        logger.debug("openmano response: %s", mano_response.text )
+        return _print_verbose(mano_response, args.verbose)
+    elif args.action=="delete":
+        URLrequest = "http://%s:%s/openmano/%s/vim/%s/%ss/%s" %(mano_host, mano_port, tenant, datacenter, args.item, args.name)
+        mano_response = requests.delete(URLrequest)
+        logger.debug("openmano response: %s", mano_response.text )
+        result = 0 if mano_response.status_code==200 else mano_response.status_code
+        content = mano_response.json()
+        #print json.dumps(content, indent=4)
+        if mano_response.status_code == 200:
+            print content['result']
+        else:
+            print content['error']['description']
+        return result
+    elif args.action=="create":
+        headers_req = {'content-type': 'application/yaml'}
+        if args.file:
+            create_dict = _load_file_or_yaml(args.file)
+            if args.item not in create_dict:
+                create_dict = {args.item: create_dict}
+        else:
+            create_dict = {args.item:{}}
+        if args.name:
+            create_dict[args.item]['name'] = args.name
+        #if args.description:
+        #    create_dict[args.item]['description'] = args.description
+        if args.item=="vim-net":
+            if args.bind_net:
+                create_dict[args.item]['bind_net'] = args.bind_net
+            if args.bind_type:
+                create_dict[args.item]['bind_type'] = args.bind_type
+            if args.shared:
+                create_dict[args.item]['shared'] = args.shared
+        if "name" not in create_dict[args.item]:
+            print "You must provide a name in the descriptor file or with the --name option"
+            return
+        payload_req = yaml.safe_dump(create_dict, explicit_start=True, indent=4, default_flow_style=False, tags=False, encoding='utf-8', allow_unicode=True)
+        logger.debug("openmano request: %s", payload_req)
+        URLrequest = "http://%s:%s/openmano/%s/vim/%s/%ss" %(mano_host, mano_port, mano_tenant, datacenter, args.item)
+        mano_response = requests.post(URLrequest, headers = headers_req, data=payload_req)
+        logger.debug("openmano response: %s", mano_response.text )
+        if args.verbose==None:
+            args.verbose=0
+        return _print_verbose(mano_response, args.verbose)
+
+
+def datacenter_net_action(args):
+    if args.action == "net-update":
+        print "This command is deprecated, use 'openmano datacenter-netmap-delete --all' and 'openmano datacenter-netmap-upload' instead!!!"
+        print
+        args.action = "netmap-delete"
+        args.netmap = None
+        args.all = True
+        r = datacenter_netmap_action(args)
+        if r == 0:
+            args.force = True
+            args.action = "netmap-upload"
+            r = datacenter_netmap_action(args)
+        return r
+
+    if args.action == "net-edit":
+        args.netmap = args.net
+        args.name = None
+    elif args.action == "net-list":
+        args.netmap = None
+    elif args.action == "net-delete":
+        args.netmap = args.net
+        args.all = False
+          
+    args.action = "netmap" + args.action[3:]
+    args.vim_name=None
+    args.vim_id=None
+    print "This command is deprecated, use 'openmano datacenter-%s' instead!!!" % args.action
+    print
+    return datacenter_netmap_action(args)
+
+def datacenter_netmap_action(args):
+    tenant = _get_tenant()
+    datacenter = _get_datacenter(args.datacenter, tenant)
+    #print "datacenter_netmap_action",args
+    payload_req = None
+    if args.verbose==None:
+        args.verbose=0
+    headers_req = {'Accept': 'application/json', 'content-type': 'application/json'}
+    URLrequest = "http://%s:%s/openmano/%s/datacenters/%s/netmaps" %(mano_host, mano_port, tenant, datacenter)
+        
+    if args.action=="netmap-list":
+        if args.netmap:
+            URLrequest += "/" + args.netmap
+            args.verbose += 1
+        mano_response = requests.get(URLrequest)
+            
+    elif args.action=="netmap-delete":
+        if args.netmap and args.all:
+            print "you can not use a netmap name and the option --all at the same time"
+            return 1
+        if args.netmap:
+            force_text= "Delete default netmap '%s' from datacenter '%s' (y/N)? " % (args.netmap, datacenter)
+            URLrequest += "/" + args.netmap
+        elif args.all: 
+            force_text="Delete all default netmaps from datacenter '%s' (y/N)? " % (datacenter)
+        else:
+            print "you must a netmap name or the option --all"
+            return 1
+        if not args.force:
+            r = raw_input(force_text)
+            if  len(r)>0  and r[0].lower()=="y":
+                pass
+            else:
+                return 0
+        mano_response = requests.delete(URLrequest, headers=headers_req)
+    elif args.action=="netmap-upload":
+        if not args.force:
+            r = raw_input("Create all the available networks from datacenter '%s' as default netmaps (y/N)? " % (datacenter))
+            if  len(r)>0  and r[0].lower()=="y":
+                pass
+            else:
+                return 0
+        URLrequest += "/upload"
+        mano_response = requests.post(URLrequest, headers=headers_req)
+    elif args.action=="netmap-edit" or args.action=="netmap-create":
+        if args.file:
+            payload = _load_file_or_yaml(args.file)
+        else:
+            payload = {}
+        if "netmap" not in payload:
+            payload = {"netmap": payload}
+        if args.name:
+            payload["netmap"]["name"] = args.name
+        if args.vim_id:
+            payload["netmap"]["vim_id"] = args.vim_id
+        if args.action=="netmap-create" and args.vim_name:
+            payload["netmap"]["vim_name"] = args.vim_name
+        payload_req = json.dumps(payload)
+        logger.debug("openmano request: %s", payload_req)
+        
+        if args.action=="netmap-edit" and not args.force:
+            if len(payload["netmap"]) == 0:
+                print "You must supply some parameter to edit"
+                return 1
+            r = raw_input("Edit default netmap '%s' from datacenter '%s' (y/N)? " % (args.netmap, datacenter))
+            if  len(r)>0  and r[0].lower()=="y":
+                pass
+            else:
+                return 0
+            URLrequest += "/" + args.netmap
+            mano_response = requests.put(URLrequest, headers=headers_req, data=payload_req)
+        else: #netmap-create
+            if "vim_name" not in payload["netmap"] and "vim_id" not in payload["netmap"]:
+                print "You must supply either --vim-id or --vim-name option; or include one of them in the file descriptor"
+                return 1
+            mano_response = requests.post(URLrequest, headers=headers_req, data=payload_req)
+
+    logger.debug("openmano response: %s", mano_response.text )
+    return _print_verbose(mano_response, args.verbose)
+
+def element_edit(args):
+    element = _get_item_uuid(args.element, args.name)
+    headers_req = {'Accept': 'application/json', 'content-type': 'application/json'}
+    URLrequest = "http://%s:%s/openmano/%s/%s" %(mano_host, mano_port, args.element, element)
+    payload=_load_file_or_yaml(args.file)
+    if args.element[:-1] not in payload:
+        payload = {args.element[:-1]: payload }
+    payload_req = json.dumps(payload)
+    
+    #print payload_req
+    if not args.force or (args.name==None and args.filer==None):
+        r = raw_input(" Edit " + args.element[:-1] + " " + args.name + " (y/N)? ")
+        if  len(r)>0  and r[0].lower()=="y":
+            pass
+        else:
+            return 0
+    logger.debug("openmano request: %s", payload_req)
+    mano_response = requests.put(URLrequest, headers=headers_req, data=payload_req)
+    logger.debug("openmano response: %s", mano_response.text )
+    if args.verbose==None:
+        args.verbose=0
+    if args.name!=None:
+        args.verbose += 1
+    return _print_verbose(mano_response, args.verbose)
+
+
+global mano_host
+global mano_port
+global mano_tenant
+
+if __name__=="__main__":
+    
+    mano_tenant = os.getenv('OPENMANO_TENANT', None)
+    mano_host = os.getenv('OPENMANO_HOST',"localhost")
+    mano_port = os.getenv('OPENMANO_PORT',"9090")
+    mano_datacenter = os.getenv('OPENMANO_DATACENTER',None)
+    
+    main_parser = ThrowingArgumentParser(description='User program to interact with OPENMANO-SERVER (openmanod)')
+    main_parser.add_argument('--version', action='version', version='%(prog)s ' + __version__ )
+    
+    subparsers = main_parser.add_subparsers(help='commands')
+    
+    config_parser = subparsers.add_parser('config', help="prints configuration values")
+    config_parser.set_defaults(func=config)
+
+    parent_parser = argparse.ArgumentParser(add_help=False)
+    parent_parser.add_argument('--verbose', '-v', action='count', help="increase verbosity level. Use several times")
+    parent_parser.add_argument('--debug', '-d', action='store_true', help="show debug information")
+
+    vnf_create_parser = subparsers.add_parser('vnf-create', parents=[parent_parser], help="adds a vnf into the catalogue")
+    vnf_create_parser.add_argument("file", action="store", help="location of the JSON file describing the VNF").completer = FilesCompleter
+    vnf_create_parser.add_argument("--name", action="store", help="name of the VNF (if it exists in the VNF descriptor, it is overwritten)")
+    vnf_create_parser.add_argument("--description", action="store", help="description of the VNF (if it exists in the VNF descriptor, it is overwritten)")
+    vnf_create_parser.add_argument("--image-path", action="store",  help="change image path locations (overwritten)")
+    vnf_create_parser.set_defaults(func=vnf_create)
+
+    vnf_list_parser = subparsers.add_parser('vnf-list', parents=[parent_parser], help="lists information about a vnf")
+    vnf_list_parser.add_argument("name", nargs='?', help="name of the VNF")
+    vnf_list_parser.add_argument("-a", "--all", action="store_true", help="shows all vnfs, not only the owned or public ones")
+    #vnf_list_parser.add_argument('--descriptor', help="prints the VNF descriptor", action="store_true")
+    vnf_list_parser.set_defaults(func=vnf_list)
+    
+    vnf_delete_parser = subparsers.add_parser('vnf-delete', parents=[parent_parser], help="deletes a vnf from the catalogue")
+    vnf_delete_parser.add_argument("name", action="store", help="name or uuid of the VNF to be deleted")
+    vnf_delete_parser.add_argument("-f", "--force", action="store_true", help="forces deletion without asking")
+    vnf_delete_parser.add_argument("-a", "--all", action="store_true", help="allow delete not owned or privated one")
+    vnf_delete_parser.set_defaults(func=vnf_delete)
+    
+    scenario_create_parser = subparsers.add_parser('scenario-create', parents=[parent_parser], help="adds a scenario into the OPENMANO DB")
+    scenario_create_parser.add_argument("file", action="store", help="location of the YAML file describing the scenario").completer = FilesCompleter
+    scenario_create_parser.add_argument("--name", action="store", help="name of the scenario (if it exists in the YAML scenario, it is overwritten)")
+    scenario_create_parser.add_argument("--description", action="store", help="description of the scenario (if it exists in the YAML scenario, it is overwritten)")
+    scenario_create_parser.set_defaults(func=scenario_create)
+
+    scenario_list_parser = subparsers.add_parser('scenario-list', parents=[parent_parser], help="lists information about a scenario")
+    scenario_list_parser.add_argument("name", nargs='?', help="name of the scenario")
+    #scenario_list_parser.add_argument('--descriptor', help="prints the scenario descriptor", action="store_true")
+    scenario_list_parser.add_argument("-a", "--all", action="store_true", help="shows all scenarios, not only the owned or public ones")
+    scenario_list_parser.set_defaults(func=scenario_list)
+    
+    scenario_delete_parser = subparsers.add_parser('scenario-delete', parents=[parent_parser], help="deletes a scenario from the OPENMANO DB")
+    scenario_delete_parser.add_argument("name", action="store", help="name or uuid of the scenario to be deleted")
+    scenario_delete_parser.add_argument("-f", "--force", action="store_true", help="forces deletion without asking")
+    scenario_delete_parser.add_argument("-a", "--all", action="store_true", help="allow delete not owned or privated one")
+    scenario_delete_parser.set_defaults(func=scenario_delete)
+
+    scenario_deploy_parser = subparsers.add_parser('scenario-deploy', parents=[parent_parser], help="deploys a scenario")
+    scenario_deploy_parser.add_argument("scenario", action="store", help="name or uuid of the scenario to be deployed")
+    scenario_deploy_parser.add_argument("name", action="store", help="name of the instance")
+    scenario_deploy_parser.add_argument("--nostart", action="store_true", help="does not start the vms, just reserve resources")
+    scenario_deploy_parser.add_argument("--datacenter", action="store", help="specifies the datacenter. Needed if several datacenters are available")
+    scenario_deploy_parser.add_argument("--description", action="store", help="description of the instance")
+    scenario_deploy_parser.set_defaults(func=scenario_deploy)
+    
+    scenario_deploy_parser = subparsers.add_parser('scenario-verify', help="verifies if a scenario can be deployed (deploys it and deletes it)")
+    scenario_deploy_parser.add_argument("scenario", action="store", help="name or uuid of the scenario to be verified")
+    scenario_deploy_parser.add_argument('--debug', '-d', action='store_true', help="show debug information")
+    scenario_deploy_parser.set_defaults(func=scenario_verify)
+    
+    instance_scenario_create_parser = subparsers.add_parser('instance-scenario-create', parents=[parent_parser], help="deploys a scenario")
+    instance_scenario_create_parser.add_argument("file", nargs='?', help="descriptor of the instance. Must be a file or yaml/json text")
+    instance_scenario_create_parser.add_argument("--scenario", action="store", help="name or uuid of the scenario to be deployed")
+    instance_scenario_create_parser.add_argument("--name", action="store", help="name of the instance")
+    instance_scenario_create_parser.add_argument("--nostart", action="store_true", help="does not start the vms, just reserve resources")
+    instance_scenario_create_parser.add_argument("--datacenter", action="store", help="specifies the datacenter. Needed if several datacenters are available")
+    instance_scenario_create_parser.add_argument("--netmap-use", action="append", type=str, dest="netmap_use", help="indicates a datacenter network to map a scenario network 'scenario-network=datacenter-network'. Can be used several times")
+    instance_scenario_create_parser.add_argument("--netmap-create", action="append", type=str, dest="netmap_create", help="the scenario network must be created at datacenter 'scenario-network[=datacenter-network-name]' . Can be used several times")
+    instance_scenario_create_parser.add_argument("--description", action="store", help="description of the instance")
+    instance_scenario_create_parser.set_defaults(func=instance_create)
+
+    instance_scenario_list_parser = subparsers.add_parser('instance-scenario-list', parents=[parent_parser], help="lists information about a scenario instance")
+    instance_scenario_list_parser.add_argument("name", nargs='?', help="name of the scenario instance")
+    instance_scenario_list_parser.add_argument("-a", "--all", action="store_true", help="shows all instance-scenarios, not only the owned")
+    instance_scenario_list_parser.set_defaults(func=instance_scenario_list)
+
+    instance_scenario_delete_parser = subparsers.add_parser('instance-scenario-delete', parents=[parent_parser], help="deletes a scenario instance (and deletes all VM and net instances in VIM)")
+    instance_scenario_delete_parser.add_argument("name", action="store", help="name or uuid of the scenario instance to be deleted")
+    instance_scenario_delete_parser.add_argument("-f", "--force", action="store_true", help="forces deletion without asking")
+    instance_scenario_delete_parser.add_argument("-a", "--all", action="store_true", help="allow delete not owned or privated one")
+    instance_scenario_delete_parser.set_defaults(func=instance_scenario_delete)
+    
+    instance_scenario_action_parser = subparsers.add_parser('instance-scenario-action', parents=[parent_parser], help="invoke an action over part or the whole scenario instance")
+    instance_scenario_action_parser.add_argument("name", action="store", help="name or uuid of the scenario instance")
+    instance_scenario_action_parser.add_argument("action", action="store", type=str, \
+            choices=["start","pause","resume","shutoff","shutdown","forceOff","rebuild","reboot", "console"],\
+            help="action to send")
+    instance_scenario_action_parser.add_argument("param", nargs='?', help="addional param of the action. e.g. console type (novnc, ...), reboot type (TODO)")
+    instance_scenario_action_parser.add_argument("--vnf", action="append", help="VNF to act on (can use several entries)")
+    instance_scenario_action_parser.add_argument("--vm", action="append", help="VM to act on (can use several entries)")
+    instance_scenario_action_parser.set_defaults(func=instance_scenario_action)
+
+    #instance_scenario_status_parser = subparsers.add_parser('instance-scenario-status', help="show the status of a scenario instance")
+    #instance_scenario_status_parser.add_argument("name", action="store", help="name or uuid of the scenario instance")
+    #instance_scenario_status_parser.set_defaults(func=instance_scenario_status)
+    
+    tenant_create_parser = subparsers.add_parser('tenant-create', parents=[parent_parser], help="creates a new tenant")
+    tenant_create_parser.add_argument("name", action="store", help="name for the tenant")
+    tenant_create_parser.add_argument("--description", action="store", help="description of the tenant")
+    tenant_create_parser.set_defaults(func=tenant_create)
+
+    tenant_delete_parser = subparsers.add_parser('tenant-delete', parents=[parent_parser], help="deletes a tenant from the catalogue")
+    tenant_delete_parser.add_argument("name", action="store", help="name or uuid of the tenant to be deleted")
+    tenant_delete_parser.add_argument("-f", "--force", action="store_true", help="forces deletion without asking")
+    tenant_delete_parser.set_defaults(func=tenant_delete)
+
+    tenant_list_parser = subparsers.add_parser('tenant-list', parents=[parent_parser], help="lists information about a tenant")
+    tenant_list_parser.add_argument("name", nargs='?', help="name or uuid of the tenant")
+    tenant_list_parser.set_defaults(func=tenant_list)
+
+    item_list=('tenant','datacenter') #put tenant before so that help appear in order
+    for item in item_list:
+        element_edit_parser = subparsers.add_parser(item+'-edit', parents=[parent_parser], help="edits one "+item)
+        element_edit_parser.add_argument("name", help="name or uuid of the "+item)
+        element_edit_parser.add_argument("file", help="json/yaml text or file with the changes").completer = FilesCompleter
+        element_edit_parser.add_argument("-f","--force", action="store_true", help="do not prompt for confirmation")
+        element_edit_parser.set_defaults(func=element_edit, element=item + 's')
+
+    datacenter_create_parser = subparsers.add_parser('datacenter-create', parents=[parent_parser], help="creates a new datacenter")
+    datacenter_create_parser.add_argument("name", action="store", help="name for the datacenter")
+    datacenter_create_parser.add_argument("url", action="store", help="url for the datacenter")
+    datacenter_create_parser.add_argument("--url_admin", action="store", help="url for administration for the datacenter")
+    datacenter_create_parser.add_argument("--type", action="store", help="datacenter type: openstack or openvim (default)")
+    datacenter_create_parser.add_argument("--config", action="store", help="aditional configuration in json/yaml format")
+    datacenter_create_parser.add_argument("--description", action="store", help="description of the datacenter")
+    datacenter_create_parser.set_defaults(func=datacenter_create)
+
+    datacenter_delete_parser = subparsers.add_parser('datacenter-delete', parents=[parent_parser], help="deletes a datacenter from the catalogue")
+    datacenter_delete_parser.add_argument("name", action="store", help="name or uuid of the datacenter to be deleted")
+    datacenter_delete_parser.add_argument("-f", "--force", action="store_true", help="forces deletion without asking")
+    datacenter_delete_parser.set_defaults(func=datacenter_delete)
+
+    datacenter_list_parser = subparsers.add_parser('datacenter-list', parents=[parent_parser], help="lists information about a datacenter")
+    datacenter_list_parser.add_argument("name", nargs='?', help="name or uuid of the datacenter")
+    datacenter_list_parser.add_argument("-a", "--all", action="store_true", help="shows all datacenters, not only datacenters attached to tenant")
+    datacenter_list_parser.set_defaults(func=datacenter_list)
+
+    datacenter_attach_parser = subparsers.add_parser('datacenter-attach', parents=[parent_parser], help="associates a datacenter to the operating tenant")
+    datacenter_attach_parser.add_argument("name", help="name or uuid of the datacenter")
+    datacenter_attach_parser.add_argument('--vim-tenant-id', action='store', help="specify a datacenter tenant to use. A new one is created by default")
+    datacenter_attach_parser.add_argument('--vim-tenant-name', action='store', help="specify a datacenter tenant name.")
+    datacenter_attach_parser.add_argument("--user", action="store", help="user credentials for the datacenter")
+    datacenter_attach_parser.add_argument("--password", action="store", help="password credentials for the datacenter")
+    datacenter_attach_parser.set_defaults(func=datacenter_attach)
+
+    datacenter_detach_parser = subparsers.add_parser('datacenter-detach', parents=[parent_parser], help="removes the association between a datacenter and the operating tenant")
+    datacenter_detach_parser.add_argument("name", help="name or uuid of the datacenter")
+    datacenter_detach_parser.add_argument("-a", "--all", action="store_true", help="removes all associations from this datacenter")
+    datacenter_detach_parser.set_defaults(func=datacenter_detach)
+
+
+    action_dict={'net-update': 'retrieves external networks from datacenter',
+                 'net-edit': 'edits an external network',
+                 'net-delete': 'deletes an external network',
+                 'net-list': 'lists external networks from a datacenter'
+                 }
+    for item in action_dict:
+        datacenter_action_parser = subparsers.add_parser('datacenter-'+item, parents=[parent_parser], help=action_dict[item])
+        datacenter_action_parser.add_argument("datacenter", help="name or uuid of the datacenter")
+        if item=='net-edit' or item=='net-delete':
+            datacenter_action_parser.add_argument("net", help="name or uuid of the datacenter net")
+        if item=='net-edit':
+            datacenter_action_parser.add_argument("file", help="json/yaml text or file with the changes").completer = FilesCompleter
+        if item!='net-list':
+            datacenter_action_parser.add_argument("-f","--force", action="store_true", help="do not prompt for confirmation")
+        datacenter_action_parser.set_defaults(func=datacenter_net_action, action=item)
+
+
+    action_dict={'netmap-upload': 'create network senario netmap base on the datacenter networks',
+                 'netmap-create': 'create a new network senario netmap',
+                 'netmap-edit':   'edit name of a network senario netmap',
+                 'netmap-delete': 'deletes a network scenario netmap (--all for clearing all)',
+                 'netmap-list':   'list/show network scenario netmaps'
+                 }
+    for item in action_dict:
+        datacenter_action_parser = subparsers.add_parser('datacenter-'+item, parents=[parent_parser], help=action_dict[item])
+        datacenter_action_parser.add_argument("--datacenter", help="name or uuid of the datacenter")
+        #if item=='net-add':
+        #    datacenter_action_parser.add_argument("net", help="name of the network")
+        if item=='netmap-delete':
+            datacenter_action_parser.add_argument("netmap", nargs='?',help="name or uuid of the datacenter netmap to delete")
+            datacenter_action_parser.add_argument("--all", action="store_true", help="delete all netmap of this datacenter")
+            datacenter_action_parser.add_argument("-f","--force", action="store_true", help="do not prompt for confirmation")
+        if item=='netmap-edit':
+            datacenter_action_parser.add_argument("netmap", help="name or uuid of the datacenter netmap do edit")
+            datacenter_action_parser.add_argument("file", nargs='?', help="json/yaml text or file with the changes").completer = FilesCompleter
+            datacenter_action_parser.add_argument("--name", action='store', help="name to assign to the datacenter netmap")
+            datacenter_action_parser.add_argument('--vim-id', action='store', help="specify vim network uuid")
+            datacenter_action_parser.add_argument("-f","--force", action="store_true", help="do not prompt for confirmation")
+        if item=='netmap-list':
+            datacenter_action_parser.add_argument("netmap", nargs='?',help="name or uuid of the datacenter netmap to show")
+        if item=='netmap-create':
+            datacenter_action_parser.add_argument("file", nargs='?', help="json/yaml text or file descriptor with the changes").completer = FilesCompleter
+            datacenter_action_parser.add_argument("--name", action='store', help="name to assign to the datacenter netmap, by default same as vim-name")
+            datacenter_action_parser.add_argument('--vim-id', action='store', help="specify vim network uuid")
+            datacenter_action_parser.add_argument('--vim-name', action='store', help="specify vim network name")
+        if item=='netmap-upload':
+            datacenter_action_parser.add_argument("-f","--force", action="store_true", help="do not prompt for confirmation")
+        datacenter_action_parser.set_defaults(func=datacenter_netmap_action, action=item)
+    
+    for item in ("network", "tenant"):
+        if item=="network":
+            commnad_name = 'vim-net'
+        else:
+            commnad_name = 'vim-'+item
+        vim_item_list_parser = subparsers.add_parser(commnad_name + '-list', parents=[parent_parser], help="list the vim " + item + "s")
+        vim_item_list_parser.add_argument("name", nargs='?', help="name or uuid of the " + item + "s")
+        vim_item_list_parser.add_argument("--datacenter", action="store", help="specifies the datacenter")
+        vim_item_list_parser.set_defaults(func=vim_action, item=item, action="list")
+
+        vim_item_del_parser = subparsers.add_parser(commnad_name + '-delete', parents=[parent_parser], help="list the vim " + item + "s")
+        vim_item_del_parser.add_argument("name", help="name or uuid of the " + item + "s")
+        vim_item_del_parser.add_argument("--datacenter", action="store", help="specifies the datacenter")
+        vim_item_del_parser.set_defaults(func=vim_action, item=item, action="delete")
+
+        vim_item_create_parser = subparsers.add_parser(commnad_name + '-create', parents=[parent_parser], help="create a "+item+" at vim")
+        vim_item_create_parser.add_argument("file", nargs='?', help="descriptor of the %s. Must be a file or yaml/json text" % item).completer = FilesCompleter
+        vim_item_create_parser.add_argument("--name", action="store", help="name of the %s" % item  )
+        vim_item_create_parser.add_argument("--datacenter", action="store", help="specifies the datacenter")
+        if item=="network":
+            vim_item_create_parser.add_argument("--type", action="store", help="type of network, data, ptp, bridge")
+            vim_item_create_parser.add_argument("--shared", action="store_true", help="Private or shared")
+            vim_item_create_parser.add_argument("--bind-net", action="store", help="For openvim datacenter type, net to be bind to, for vlan type, use sufix ':<vlan_tag>'")
+        else:
+            vim_item_create_parser.add_argument("--description", action="store", help="description of the %s" % item)
+        vim_item_create_parser.set_defaults(func=vim_action, item=item, action="create")
+
+    argcomplete.autocomplete(main_parser)
+    
+    try:
+        args = main_parser.parse_args()
+        #logging info
+        level = logging.CRITICAL
+        streamformat = "%(asctime)s %(name)s %(levelname)s: %(message)s"
+        if "debug" in args and args.debug:
+            level = logging.DEBUG
+        logging.basicConfig(format=streamformat, level= level)
+        logger = logging.getLogger('mano')
+        logger.setLevel(level)
+        result = args.func(args)
+        if result == None:
+            result = 0
+        #for some reason it fails if call exit inside try instance. Need to call exit at the end !?
+    except (requests.exceptions.ConnectionError):
+        print "Connection error: not possible to contact OPENMANO-SERVER (openmanod)"
+        result = -2
+    except (KeyboardInterrupt):
+        print 'Exiting openmano'
+        result = -3
+    except (SystemExit, ArgumentParserError):
+        result = -4
+    except OpenmanoCLIError as e:
+        print str(e)
+        result = -5
+    
+    #print result
+    exit(result)
+
diff --git a/openmano_schemas.py b/openmano_schemas.py
new file mode 100644 (file)
index 0000000..8ab251b
--- /dev/null
@@ -0,0 +1,737 @@
+# -*- 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
+##
+
+'''
+JSON schemas used by openmano httpserver.py module to parse the different files and messages sent through the API 
+'''
+__author__="Alfonso Tierno, Gerardo Garcia"
+__date__ ="$09-oct-2014 09:09:48$"
+
+#Basis schemas
+passwd_schema={"type" : "string", "minLength":1, "maxLength":60}
+nameshort_schema={"type" : "string", "minLength":1, "maxLength":60, "pattern" : "^[^,;()'\"]+$"}
+name_schema={"type" : "string", "minLength":1, "maxLength":255, "pattern" : "^[^,;()'\"]+$"}
+xml_text_schema={"type" : "string", "minLength":1, "maxLength":1000, "pattern" : "^[^']+$"}
+description_schema={"type" : ["string","null"], "maxLength":255, "pattern" : "^[^'\"]+$"}
+id_schema_fake = {"type" : "string", "minLength":2, "maxLength":36 }  #"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
+id_schema = {"type" : "string", "pattern": "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$"}
+pci_schema={"type":"string", "pattern":"^[0-9a-fA-F]{4}(:[0-9a-fA-F]{2}){2}\.[0-9a-fA-F]$"}
+http_schema={"type":"string", "pattern":"^https?://[^'\"=]+$"}
+bandwidth_schema={"type":"string", "pattern" : "^[0-9]+ *([MG]bps)?$"}
+memory_schema={"type":"string", "pattern" : "^[0-9]+ *([MG]i?[Bb])?$"}
+integer0_schema={"type":"integer","minimum":0}
+integer1_schema={"type":"integer","minimum":1}
+path_schema={"type":"string", "pattern":"^(\.(\.?))?(/[^/"":{}\ \(\)]+)+$"}
+vlan_schema={"type":"integer","minimum":1,"maximum":4095}
+vlan1000_schema={"type":"integer","minimum":1000,"maximum":4095}
+mac_schema={"type":"string", "pattern":"^[0-9a-fA-F][02468aceACE](:[0-9a-fA-F]{2}){5}$"}  #must be unicast LSB bit of MSB byte ==0 
+#mac_schema={"type":"string", "pattern":"^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"}
+ip_schema={"type":"string","pattern":"^([0-9]{1,3}.){3}[0-9]{1,3}$"}
+port_schema={"type":"integer","minimum":1,"maximum":65534}
+object_schema={"type":"object"}
+
+metadata_schema={
+    "type":"object",
+    "properties":{
+        "architecture": {"type":"string"},
+        "use_incremental": {"type":"string","enum":["yes","no"]},
+        "vpci": pci_schema,
+        "os_distro": {"type":"string"},
+        "os_type": {"type":"string"},
+        "os_version": {"type":"string"},
+        "bus": {"type":"string"},
+        "topology": {"type":"string", "enum": ["oneSocket"]}
+    }
+}
+
+#Schema for the configuration file
+config_schema = {
+    "title":"configuration response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "http_port": port_schema,
+        "http_admin_port": port_schema,
+        "http_host": nameshort_schema,
+        "vnf_repository": path_schema,
+        "db_host": nameshort_schema,
+        "db_user": nameshort_schema,
+        "db_passwd": {"type":"string"},
+        "db_name": nameshort_schema,
+        # Next fields will disappear once the MANO API includes appropriate primitives
+        "vim_url": http_schema,
+        "vim_url_admin": http_schema,
+        "vim_name": nameshort_schema,
+        "vim_tenant_name": nameshort_schema,
+        "mano_tenant_name": nameshort_schema,
+        "mano_tenant_id": id_schema, 
+        "http_console_ports": {
+            "type": "array", 
+            "items": {"OneOf" : [
+                port_schema, 
+                {"type":"object", "properties":{"from": port_schema, "to": port_schema}, "required": ["from","to"]} 
+            ]}
+        },
+    },
+    "required": ['db_host', 'db_user', 'db_passwd', 'db_name'],
+    "additionalProperties": False
+}
+
+tenant_schema = {
+    "title":"tenant information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "tenant":{
+            "type":"object",
+            "properties":{
+                "name": nameshort_schema,
+                "description": description_schema,
+            },
+            "required": ["name"],
+            "additionalProperties": True
+        }
+    },
+    "required": ["tenant"],
+    "additionalProperties": False
+}
+tenant_edit_schema = {
+    "title":"tenant edit information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "tenant":{
+            "type":"object",
+            "properties":{
+                "name": name_schema,
+                "description": description_schema,
+            },
+            "additionalProperties": False
+        }
+    },
+    "required": ["tenant"],
+    "additionalProperties": False
+}
+
+datacenter_schema_properties={
+                "name": name_schema,
+                "description": description_schema,
+                "type": nameshort_schema, #currently "openvim" or "openstack", can be enlarge with plugins
+                "vim_url": description_schema,
+                "vim_url_admin": description_schema,
+                "config": { "type":"object" }
+            }
+
+datacenter_schema = {
+    "title":"datacenter information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "datacenter":{
+            "type":"object",
+            "properties":datacenter_schema_properties,
+            "required": ["name", "vim_url"],
+            "additionalProperties": True
+        }
+    },
+    "required": ["datacenter"],
+    "additionalProperties": False
+}
+
+
+datacenter_edit_schema = {
+    "title":"datacenter edit nformation schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "datacenter":{
+            "type":"object",
+            "properties":datacenter_schema_properties,
+            "additionalProperties": False
+        }
+    },
+    "required": ["datacenter"],
+    "additionalProperties": False
+}
+
+
+netmap_new_schema = {
+    "title":"netmap new information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "netmap":{   #delete from datacenter
+            "type":"object",
+            "properties":{
+                "name": name_schema,  #name or uuid of net to change
+                "vim_id": id_schema,
+                "vim_name": name_schema
+            },
+            "minProperties": 1,
+            "additionalProperties": False
+        },
+    },
+    "required": ["netmap"],
+    "additionalProperties": False
+}
+
+netmap_edit_schema = {
+    "title":"netmap edit information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "netmap":{   #delete from datacenter
+            "type":"object",
+            "properties":{
+                "name": name_schema,  #name or uuid of net to change
+            },
+            "minProperties": 1,
+            "additionalProperties": False
+        },
+    },
+    "required": ["netmap"],
+    "additionalProperties": False
+}
+
+datacenter_action_schema = {
+    "title":"datacenter action information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "net-update":{"type":"null",},
+        "net-edit":{
+            "type":"object",
+            "properties":{
+                "net": name_schema,  #name or uuid of net to change
+                "name": name_schema,
+                "description": description_schema,
+                "shared": {"type": "boolean"}
+            },
+            "minProperties": 1,
+            "additionalProperties": False
+        },
+        "net-delete":{
+            "type":"object",
+            "properties":{
+                "net": name_schema,  #name or uuid of net to change
+            },
+            "required": ["net"],
+            "additionalProperties": False
+        },
+    },
+    "minProperties": 1,
+    "maxProperties": 1,
+    "additionalProperties": False
+}
+
+
+datacenter_associate_schema={
+    "title":"datacenter associate information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "datacenter":{
+            "type":"object",
+            "properties":{
+                "vim_tenant": id_schema,
+                "vim_tenant_name": nameshort_schema,
+                "vim_username": nameshort_schema,
+                "vim_password": nameshort_schema,
+            },
+#            "required": ["vim_tenant"],
+            "additionalProperties": True
+        }
+    },
+    "required": ["datacenter"],
+    "additionalProperties": False
+}
+
+internal_connection_element_schema = {
+    "type":"object",
+    "properties":{
+        "VNFC": name_schema,
+        "local_iface_name": name_schema
+    }
+}
+
+internal_connection_schema = {
+    "type":"object",
+    "properties":{
+        "name": name_schema,
+        "description":description_schema,
+        "type":{"type":"string", "enum":["bridge","data","ptp"]},
+        "elements": {"type" : "array", "items": internal_connection_element_schema, "minItems":2}
+    },
+    "required": ["name", "type", "elements"],
+    "additionalProperties": False
+}
+
+external_connection_schema = {
+    "type":"object",
+    "properties":{
+        "name": name_schema,
+        "type":{"type":"string", "enum":["mgmt","bridge","data"]},
+        "VNFC": name_schema,
+        "local_iface_name": name_schema ,
+        "description":description_schema
+    },
+    "required": ["name", "type", "VNFC", "local_iface_name"],
+    "additionalProperties": False
+}
+
+interfaces_schema={
+    "type":"array",
+    "items":{
+        "type":"object",
+        "properties":{
+            "name":name_schema,
+            "dedicated":{"type":"string","enum":["yes","no","yes:sriov"]},
+            "bandwidth":bandwidth_schema,
+            "vpci":pci_schema,
+            "mac_address": mac_schema
+        },
+        "additionalProperties": False,
+        "required": ["name","dedicated", "bandwidth"]
+    }
+}
+
+bridge_interfaces_schema={
+    "type":"array",
+    "items":{
+        "type":"object",
+        "properties":{
+            "name": name_schema,
+            "bandwidth":bandwidth_schema,
+            "vpci":pci_schema,
+            "mac_address": mac_schema,
+            "model": {"type":"string", "enum":["virtio","e1000","ne2k_pci","pcnet","rtl8139"]}
+        },
+        "additionalProperties": False,
+        "required": ["name"]
+    }
+}
+
+devices_schema={
+    "type":"array",
+    "items":{
+        "type":"object",
+        "properties":{
+            "type":{"type":"string", "enum":["disk","cdrom","xml"] },
+            "image": path_schema,
+            "image metadata": metadata_schema, 
+            "vpci":pci_schema,
+            "xml":xml_text_schema,
+        },
+        "additionalProperties": False,
+        "required": ["type"]
+    }
+}
+
+
+numa_schema = {
+    "type": "object",
+    "properties": {
+        "memory":integer1_schema,
+        "cores":integer1_schema,
+        "paired-threads":integer1_schema,
+        "threads":integer1_schema,
+        "cores-id":{"type":"array","items":integer0_schema},
+        "paired-threads-id":{"type":"array","items":{"type":"array","minItems":2,"maxItems":2,"items":integer0_schema}},
+        "threads-id":{"type":"array","items":integer0_schema},
+        "interfaces":interfaces_schema
+    },
+    "additionalProperties": False,
+    #"required": ["memory"]
+}
+
+vnfc_schema = {
+    "type":"object",
+    "properties":{
+        "name": name_schema,
+        "description": description_schema,
+        "VNFC image": {"oneOf": [path_schema, http_schema]},
+        "image metadata": metadata_schema, 
+        "processor": {
+            "type":"object",
+            "properties":{
+                "model":description_schema,
+                "features":{"type":"array","items":nameshort_schema}
+            },
+            "required": ["model"],
+            "additionalProperties": False
+        },
+        "hypervisor": {
+            "type":"object",
+            "properties":{
+                "type":nameshort_schema,
+                "version":description_schema
+            },
+        },
+        "ram":integer0_schema,
+        "vcpus":integer0_schema,
+        "disk": integer1_schema,
+        "numas": {
+            "type": "array",
+            "items":numa_schema
+        },
+        "bridge-ifaces": bridge_interfaces_schema,
+        "devices": devices_schema
+    },
+    "required": ["name", "VNFC image"],
+    "additionalProperties": False
+}
+
+vnfd_schema_v01 = {
+    "title":"vnfd information schema v0.1",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "vnf":{
+            "type":"object",
+            "properties":{
+                "name": name_schema,
+                "description": description_schema,
+                "class": nameshort_schema,
+                "public": {"type" : "boolean"},
+                "physical": {"type" : "boolean"},
+                "tenant_id": id_schema, #only valid for admin
+                "external-connections": {"type" : "array", "items": external_connection_schema, "minItems":1},
+                "internal-connections": {"type" : "array", "items": internal_connection_schema, "minItems":1},
+                "VNFC":{"type" : "array", "items": vnfc_schema, "minItems":1}
+            },
+            "required": ["name","external-connections"],
+            "additionalProperties": True
+        }
+    },
+    "required": ["vnf"],
+    "additionalProperties": False
+}
+
+#Future VNFD schema to be defined
+vnfd_schema_v02 = {
+    "title":"vnfd information schema v0.2",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "version": {"type": "string", "pattern":"^v0.2$"},
+        "vnf":{
+            "type":"object",
+            "properties":{
+                "name": name_schema,
+            },
+            "required": ["name"],
+            "additionalProperties": True
+        }
+    },
+    "required": ["vnf", "version"],
+    "additionalProperties": False
+}
+
+#vnfd_schema = vnfd_schema_v01
+#{
+#    "title":"vnfd information schema v0.2",
+#    "$schema": "http://json-schema.org/draft-04/schema#",
+#    "oneOf": [vnfd_schema_v01, vnfd_schema_v02]
+#}
+
+graph_schema = {
+    "title":"graphical scenario descriptor information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "x":      integer0_schema,
+        "y":      integer0_schema,
+        "ifaces": {
+            "type":"object",
+            "properties":{
+                "left": {"type":"array"},
+                "right": {"type":"array"},
+                "bottom": {"type":"array"},
+            }
+        }
+    },
+    "required": ["x","y"]
+}
+
+nsd_schema_v01 = {
+    "title":"network scenario descriptor information schema v0.1",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "name":name_schema,
+        "description": description_schema,
+        "tenant_id": id_schema, #only valid for admin
+        "topology":{
+            "type":"object",
+            "properties":{
+                "nodes": {
+                    "type":"object",
+                    "patternProperties":{
+                        ".": {
+                            "type": "object",
+                            "properties":{
+                                "type":{"type":"string", "enum":["VNF", "other_network", "network", "external_network"]},
+                                "vnf_id": id_schema,
+                                "graph": graph_schema,
+                            },
+                            "patternProperties":{
+                                "^(VNF )?model$": {"type": "string"}
+                            },
+                            "required": ["type"]
+                        }
+                    }
+                },
+                "connections": {
+                    "type":"object",
+                    "patternProperties":{
+                        ".": {
+                            "type": "object",
+                            "properties":{
+                                "nodes":{"oneOf":[{"type":"object", "minProperties":2}, {"type":"array", "minLength":1}]},
+                                "type": {"type": "string", "enum":["link", "external_network", "dataplane_net", "bridge_net"]},
+                                "graph": graph_schema
+                            },
+                            "required": ["nodes"]
+                        },
+                    }
+                }
+            },
+            "required": ["nodes"],
+            "additionalProperties": False
+        }
+    },
+    "required": ["name","topology"],
+    "additionalProperties": False
+}
+
+#Future NSD schema to be defined
+nsd_schema_v02 = {
+    "title":"network scenario descriptor information schema v0.2",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "schema_version": {"type": "string", "enum": ["0.2"]},
+        "name":name_schema,
+        "description": description_schema,
+        "tenant_id": id_schema, #only valid for admin
+        "vnfs": {
+            "type":"object",
+            "patternProperties":{
+                ".": {
+                    "type": "object",
+                    "properties":{
+                        "vnf_id": id_schema,
+                        "graph": graph_schema,
+                        "vnf_model": name_schema,
+                    },
+                }
+            },
+            "minProperties": 1
+        },
+        "networks": {
+            "type":"object",
+            "patternProperties":{
+                ".": {
+                    "type": "object",
+                    "properties":{
+                        "interfaces":{"type":"array", "minLength":1},
+                        "type": {"type": "string", "enum":["link", "external_network", "dataplane_net", "bridge_net"]},
+                        "graph": graph_schema
+                    },
+                    "required": ["interfaces"]
+                },
+            }
+        },
+    },
+    "required": ["vnfs", "networks","name", "schema_version"],
+    "additionalProperties": False
+}
+
+#scenario_new_schema = {
+#    "title":"new scenario information schema",
+#    "$schema": "http://json-schema.org/draft-04/schema#",
+#    #"oneOf": [nsd_schema_v01, nsd_schema_v02]
+#    "oneOf": [nsd_schema_v01]
+#}
+
+scenario_edit_schema = {
+    "title":"edit scenario information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "name":name_schema,
+        "description": description_schema,
+        "topology":{
+            "type":"object",
+            "properties":{
+                "nodes": {
+                    "type":"object",
+                    "patternProperties":{
+                        "^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$": {
+                            "type":"object",
+                            "properties":{
+                                "graph":{
+                                    "type": "object",
+                                    "properties":{
+                                        "x": integer0_schema,
+                                        "y": integer0_schema,
+                                        "ifaces":{ "type": "object"}
+                                    }
+                                },
+                                "description": description_schema,
+                                "name": name_schema
+                            }
+                        }
+                    }
+                }
+            },
+            "required": ["nodes"],
+            "additionalProperties": False
+        }
+    },
+    "additionalProperties": False
+}
+
+scenario_action_schema = {
+    "title":"scenario action information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "start":{
+            "type": "object",
+            "properties": {
+                "instance_name":name_schema,
+                "description":description_schema,
+                "datacenter": {"type": "string"}
+            },
+            "required": ["instance_name"]
+        },
+        "deploy":{
+            "type": "object",
+            "properties": {
+                "instance_name":name_schema,
+                "description":description_schema,
+                "datacenter": {"type": "string"}
+            },
+            "required": ["instance_name"]
+        },
+        "reserve":{
+            "type": "object",
+            "properties": {
+                "instance_name":name_schema,
+                "description":description_schema,
+                "datacenter": {"type": "string"}
+            },
+            "required": ["instance_name"]
+        },
+        "verify":{
+            "type": "object",
+            "properties": {
+                "instance_name":name_schema,
+                "description":description_schema,
+                "datacenter": {"type": "string"}
+            },
+            "required": ["instance_name"]
+        }
+    },
+    "minProperties": 1,
+    "maxProperties": 1,
+    "additionalProperties": False
+}
+
+instance_scenario_create_schema = {
+    "title":"instance scenario create information schema v0.1",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "schema_version": {"type": "string", "enum": ["0.1"]},
+        "instance":{
+            "type":"object",
+            "properties":{
+                "name":name_schema,
+                "description":description_schema,
+                "datacenter": name_schema,
+                "scenario" : name_schema, #can be an UUID or name
+                "action":{"enum": ["deploy","reserve","verify" ]},
+                "connect_mgmt_interfaces": {"oneOff": [{"type":"boolean"}, {"type":"object"}]},# can be true or a dict with datacenter: net_name
+                "vnfs":{             #mapping from scenario to datacenter
+                    "type": "object",
+                    "patternProperties":{
+                        ".": {
+                            "type": "object",
+                            "properties":{
+                                "name":   name_schema,#override vnf name
+                                "datacenter": name_schema,
+                                "metadata": {"type": "object"},
+                                "user_data": {"type": "string"}
+                            }
+                        }
+                    },
+                },
+                "networks":{             #mapping from scenario to datacenter
+                    "type": "object",
+                    "patternProperties":{
+                        ".": {
+                            "type": "object",
+                            "properties":{
+                                "netmap-create": {"oneOf":[name_schema,{"type": "null"}]}, #datacenter network to use. Null if must be created as an internal net
+                                "netmap-use": name_schema,
+                                "name":   name_schema,#override network name
+                                "datacenter": name_schema,
+                            }
+                        }
+                    },
+                },
+            },
+            "additionalProperties": False,
+            "required": ["scenario", "name"]
+        },
+    },
+    "required": ["instance"],
+    "additionalProperties": False
+    
+}
+
+instance_scenario_action_schema = {
+    "title":"instance scenario action information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "start":{"type": "null"},
+        "pause":{"type": "null"},
+        "resume":{"type": "null"},
+        "shutoff":{"type": "null"},
+        "shutdown":{"type": "null"},
+        "forceOff":{"type": "null"},
+        "rebuild":{"type": "null"},
+        "reboot":{
+            "type": ["object","null"],
+        },
+        "console": {"type": ["string", "null"], "enum": ["novnc", "xvpvnc", "rdp-html5", "spice-html5", None]},
+        "vnfs":{"type": "array", "items":{"type":"string"}},
+        "vms":{"type": "array", "items":{"type":"string"}}
+    },
+    "minProperties": 1,
+    #"maxProperties": 1,
+    "additionalProperties": False
+}
diff --git a/openmanoconfig.sh b/openmanoconfig.sh
new file mode 100755 (executable)
index 0000000..696ad93
--- /dev/null
@@ -0,0 +1,29 @@
+#!/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
+
diff --git a/openmanod.cfg b/openmanod.cfg
new file mode 100644 (file)
index 0000000..8670fe4
--- /dev/null
@@ -0,0 +1,46 @@
+##
+# 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
+##
+
+#HTTP Server parameters (MANO API). IP address and port where openmanod listens
+# IPtables/firewalld must allow this port
+# for CentOS/Redhad firewalld is configured at '/etc/firewalld/services/openmanod.xml'
+# edit this file and reload firewalld with 'firewall-cmd --reload' if port is changed
+http_host:       0.0.0.0      # IP address, (by default, 0.0.0.0 means that it will listen in all interfaces)
+http_port:       9090         # General port (by default, 9090)
+#http_admin_port: 9095        # Admin port where openmano is listening (when missing, no administration server is launched)
+                              # Not used in current version!
+
+# Ports to be used. Comma separated list. Can contain a {"from":<port>, "to":<port>} entry
+#e.g. from 9000 to 9005: [{"from":9000, "to":9005}], or also [9000,9001,9002,9003,9004,9005]
+#e.g. from 9000 to 9100 apart from 9050,9053: [{"from":9000, "to":9049},9051,9052,{"from":9054, "to":9099}]
+http_console_ports: [{"from":9096, "to":9110}]
+
+#Database parameters
+db_host:   localhost          # by default localhost
+db_user:   mano               # DB user
+db_passwd: manopw             # DB password
+db_name:   mano_db            # Name of the MANO DB
+
+#other MANO parameters
+#  Folder where the VNF descriptors will be stored
+#  The folder will be created in the execution folder if it does not exist
+vnf_repository: "./vnfrepo"   # Use an absolute path to avoid misunderstandings 
+
diff --git a/openmanod.py b/openmanod.py
new file mode 100755 (executable)
index 0000000..522a657
--- /dev/null
@@ -0,0 +1,226 @@
+#!/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
+##
+
+'''
+openmano server.
+Main program that implements a reference NFVO (Network Functions Virtualisation Orchestrator).
+It interfaces with an NFV VIM through its API and offers a northbound interface, based on REST (openmano API),
+where NFV services are offered including the creation and deletion of VNF templates, VNF instances,
+network service templates and network service instances. 
+
+It loads the configuration file and launches the http_server thread that will listen requests using openmano API.
+'''
+__author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes"
+__date__ ="$26-aug-2014 11:09:29$"
+__version__="0.4.36-r467"
+version_date="Mar 2016"
+database_version="0.10"      #expected database schema version
+
+import httpserver
+import time
+import os
+import sys
+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
+from openmano_schemas import config_schema
+import nfvo
+
+global global_config
+
+def load_configuration(configuration_file):
+    default_tokens ={'http_port':9090, 'http_host':'localhost'}
+    try:
+        #Check config file exists
+        if not os.path.isfile(configuration_file):
+            return (False, "Error: Configuration file '"+configuration_file+"' does not exists.")
+            
+        #Read file
+        (return_status, code) = af.read_file(configuration_file)
+        if not return_status:
+            return (return_status, "Error loading configuration file '"+configuration_file+"': "+code)
+        #Parse configuration file
+        try:
+            config = yaml.load(code)
+        except yaml.YAMLError, exc:
+            error_pos = ""
+            if hasattr(exc, 'problem_mark'):
+                mark = exc.problem_mark
+                error_pos = " at position: (%s:%s)" % (mark.line+1, mark.column+1)
+            return (False, "Error loading configuration file '"+configuration_file+"'"+error_pos+": content format error: Failed to parse yaml format")
+
+        #Validate configuration file with the config_schema
+        try:
+            js_v(config, config_schema)
+        except js_e.ValidationError, exc:
+            error_pos = ""
+            if len(exc.path)>0: error_pos=" at '" + ":".join(map(str, exc.path))+"'"
+            return False, "Error loading configuration file '"+configuration_file+"'"+error_pos+": "+exc.message 
+        
+        #Check default values tokens
+        for k,v in default_tokens.items():
+            if k not in config: config[k]=v
+    
+    except Exception,e:
+        return (False, "Error loading configuration file '"+configuration_file+"': "+str(e))
+                
+    return (True, config)
+
+def console_port_iterator():
+    '''this iterator deals with the http_console_ports 
+    returning the ports one by one
+    '''
+    index = 0
+    while index < len(global_config["http_console_ports"]):
+        port = global_config["http_console_ports"][index]
+        #print "ports -> ", port
+        if type(port) is int:
+            yield port
+        else: #this is dictionary with from to keys
+            port2 = port["from"]
+            #print "ports -> ", port, port2
+            while port2 <= port["to"]:
+                print "ports -> ", port, port2
+                yield port2
+                port2 += 1
+        index += 1
+    
+    
+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"
+    print "      -p|--port [port_number]: changes port number and overrides the port number in the configuration file (default: 9090)"
+    print "      -P|--adminport [port_number]: changes admin port number and overrides the port number in the configuration file (default: 9095)"
+    print "      -V|--vnf-repository: changes the path of the vnf-repository and overrides the path in the configuration file"
+    return
+    
+if __name__=="__main__":
+    # Read parameters and configuration file 
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], "hvc:V:p:P:", ["config", "help", "version", "port", "vnf-repository", "adminport"])
+    except getopt.GetoptError, err:
+        # print help information and exit:
+        print "Error:", err # will print something like "option -a not recognized"
+        usage()
+        sys.exit(2)
+    
+    port=None
+    port_admin = None
+    config_file = 'openmanod.cfg'
+    vnf_repository = None
+    
+    for o, a in opts:
+        if o in ("-v", "--version"):
+            print "openmanod version", __version__, version_date
+            print "(c) Copyright Telefonica"
+            sys.exit()
+        elif o in ("-h", "--help"):
+            usage()
+            sys.exit()
+        elif o in ("-V", "--vnf-repository"):
+            vnf_repository = a
+        elif o in ("-c", "--config"):
+            config_file = a
+        elif o in ("-p", "--port"):
+            port = a
+        elif o in ("-P", "--adminport"):
+            port_admin = a
+        else:
+            assert False, "Unhandled option"
+
+    try:
+        r, global_config = load_configuration(config_file)
+        #print global_config
+        if not r:
+            print global_config
+            exit(-1)
+        # Override parameters obtained by command line
+        if port is not None: global_config['http_port'] = port
+        if port_admin is not None: global_config['http_admin_port'] = port_admin
+        if vnf_repository is not None:
+            global_config['vnf_repository'] = vnf_repository
+        else:
+            if not 'vnf_repository' in global_config:  
+                print os.getcwd()
+                global_config['vnf_repository'] = os.getcwd()+'/vnfrepo'
+        #print global_config
+        
+        if not os.path.exists(global_config['vnf_repository']):
+            print "Creating folder vnf_repository folder: '%s'." % global_config['vnf_repository']
+            try:
+                os.makedirs(global_config['vnf_repository'])
+            except Exception,e:
+                print "Error '%s'. Ensure the path 'vnf_repository' is properly set at %s" %(e.args[1], config_file)
+                exit(-1)
+        
+        global_config["console_port_iterator"] = console_port_iterator
+        global_config["console_thread"]={}
+        global_config["console_ports"]={}
+        # 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:
+            print "Error connecting to database", global_config['db_name'], "at", global_config['db_user'], "@", global_config['db_host']
+            exit(-1)
+        r = mydb.get_db_version()
+        if r[0]<0:
+            print "Error 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:
+            print "Error 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'])
+        
+        httpthread.start()
+        if 'http_admin_port' in global_config: 
+            httpthreadadmin = httpserver.httpserver(mydb, True, global_config['http_host'], global_config['http_admin_port'])
+            httpthreadadmin.start()
+        time.sleep(1)      
+        print 'Waiting for http clients'
+        print 'openmanod ready'
+        print '===================='
+        time.sleep(20)
+        sys.stdout.flush()
+
+        #TODO: Interactive console must be implemented here instead of join or sleep
+
+        #httpthread.join()
+        #if 'http_admin_port' in global_config: 
+        #    httpthreadadmin.join()
+        while True:
+            time.sleep(86400)
+        for thread in global_config["console_thread"]:
+            thread.terminate = True
+
+    except (KeyboardInterrupt, SystemExit):
+        print 'Exiting openmanod'
+        exit()
+
+
diff --git a/scenarios/examples/complex.yaml b/scenarios/examples/complex.yaml
new file mode 100755 (executable)
index 0000000..074c801
--- /dev/null
@@ -0,0 +1,57 @@
+---
+name:            complex
+description:     Complex network scenario consisting of 4 VNFs interconnected
+topology: 
+  nodes: 
+    linux1:                   # vnf/net name in the scenario
+      type:      VNF          # VNF, network, external_network (if it is a datacenter network)
+      VNF model: linux        # VNF name as introduced in OPENMANO DB
+    node1:
+      type:      VNF
+      VNF model: dataplaneVNF1
+    node2:
+      type:      VNF
+      VNF model: dataplaneVNF2
+    node3:
+      type:      VNF
+      VNF model: dataplaneVNF2
+    bridge1: 
+      type:      network
+      model:     bridge_net
+    default: 
+      type:      external_network # Datacenter net
+      model:     default
+  connections: 
+    dataconn1:
+      nodes: 
+      -   node1: xe0
+      -   node2: xe0
+    dataconn2:
+      nodes: 
+      -   node1: xe1
+      -   node2: xe1
+    dataconn3:
+      nodes: 
+      -   node1: xe2
+      -   node3: xe0
+    dataconn4:
+      nodes: 
+      -   node1: xe3
+      -   node3: xe1
+    data-sriov1:
+      nodes: 
+      -   node2: xe2
+      -   node3: xe2
+    bridgeconn1:
+      nodes: 
+      -   bridge1: null
+      -   linux1:  eth0
+      -   node2:   control
+      -   node3:   control
+    mngmt-net:
+      nodes: 
+      -   default: null
+      -   node1:   mgmt
+      -   node2:   mgmt
+      -   node3:   mgmt
+
diff --git a/scenarios/examples/complex2.yaml b/scenarios/examples/complex2.yaml
new file mode 100755 (executable)
index 0000000..fc4bd72
--- /dev/null
@@ -0,0 +1,30 @@
+---
+name:            complex2
+description:     Complex network scenario consisting of 2 VNFs interconnected
+topology: 
+  nodes: 
+    VNF2vms:                        # vnf/net name in the scenario
+      type:      VNF                # VNF, network, external_network (if it is a datacenter network)
+      VNF model: dataplaneVNF_2VMs  # VNF name as introduced in OPENMANO DB
+    VNF3:
+      type:      VNF
+      VNF model: dataplaneVNF3
+    default: 
+      type:      external_network # Datacenter net
+      model:     default
+  connections: 
+    dataconn1:
+      nodes: 
+      -   VNF2vms: in
+      -   VNF3:    data0
+    dataconn2:
+      nodes: 
+      -   VNF2vms: out
+      -   VNF3:    data1
+    mngmt-net:
+      nodes: 
+      -   default: null
+      -   VNF2vms: control0
+      -   VNF2vms: control1
+      -   VNF3:    mgmt
+
diff --git a/scenarios/examples/simple.yaml b/scenarios/examples/simple.yaml
new file mode 100755 (executable)
index 0000000..7ea2b4b
--- /dev/null
@@ -0,0 +1,17 @@
+---
+name:            simple
+description:     Simple network scenario consisting of a single VNF connected to an external network
+topology: 
+  nodes: 
+    linux1:                   # vnf/net name in the scenario
+      type:      VNF          # VNF, network, external_network (if it is a datacenter network)
+      VNF model: linux        # VNF name as introduced in OPENMANO DB
+    default: 
+      type:      external_network # Datacenter net
+      model:     default
+  connections: 
+    conn1:                   # provide a name for this net or connection
+      nodes: 
+      -   default: null      # Datacenter net
+      -   linux1: eth0       # Node and its interface
+
diff --git a/scenarios/scenario-template.yaml b/scenarios/scenario-template.yaml
new file mode 100755 (executable)
index 0000000..34c6fb1
--- /dev/null
@@ -0,0 +1,39 @@
+---
+name:            insert a name for the scenario
+description:     insert a description for the scenario
+topology:
+  nodes:
+    vnf1:                     # vnf/net name in the scenario
+      type:      VNF          # VNF, network, external_network (if it is a datacenter network)
+      VNF model: vnf_model1   # VNF name as introduced in OPENMANO DB
+      # vnf_id:    519f03ee-8ab6-11e4-ab4c-52540056c317   # Optionally, instead of the VNF name, the VNF id in Openmano DB can be used
+    vnf2:
+      type:      VNF
+      VNF model: vnf_model2
+      # Optional information for display in the openmano-gui: graphical position of the node and its interfaces
+      graph:     {"y":399,"x":632,"ifaces":{"left":[["xe0","d"],["xe1","d"]],"bottom":[["eth0","v"],["eth1","m"]]}}
+    bridge1:                  # Bridge networks must be declared in this section if we want to interconnect VNFs using a Linux bridge
+      type:      network
+      model:     bridge_net   # 'bridge_net' or 'dataplane_net' for 'network' type
+    default:                  # External networks (datacenter nets) must be declared in this section if we want to interconnect VNFs to them
+      type:      external_network
+      model:     default      # datacenter net name, as introduced in OPENMANO DB
+  connections:                # In this section, connections between VNFs and networks are explicited
+    datanet:                  # name
+      # Data plane connections do not need to include a bridge since they are built through the Openflow Controller
+      nodes:                  # nodes that will be connected: one or several vnfs, and optionally one additional network declared in nodes section
+      -  vnf1: xe0            # First node and its interface to be connected (interfaces must match to one in the VNF descriptor)
+      -  vnf2: xe0            # Second node and its interface
+    control net:
+      # Control plane connections must include a bridge network in the list of nodes
+      nodes:
+      -   bridge1: null       # Bridge networks must be included if we want to interconnect the nodes to that network
+      -   vnf1: eth1
+      -   vnf2: eth1
+    external net:
+      # Connections based on external networks (datacenter nets) must include the external network in the list of nodes
+      nodes:
+      -   default: null       # Datacenter networks (external networks) must be included if we want to interconnect the nodes to that network
+      -   vnf1: eth0
+      -   vnf1: eth0
+
diff --git a/scripts/openmano-report.sh b/scripts/openmano-report.sh
new file mode 100755 (executable)
index 0000000..c7bfe7e
--- /dev/null
@@ -0,0 +1,170 @@
+#!/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
+##
+
+#It generates a report for debugging
+
+DIRNAME=$(readlink -f ${BASH_SOURCE[0]})
+DIRNAME=$(dirname $DIRNAME )
+OMCLIENT=$DIRNAME/../openmano/openmano
+OVCLIENT=$DIRNAME/../openvim/openvim
+
+#get screen log files at the beginning
+echo
+echo "-------------------------------"
+echo "log files"
+echo "-------------------------------"
+echo "-------------------------------"
+echo "OPENMANO"
+echo "-------------------------------"
+echo "cat $DIRNAME/../logs/openmano.?"
+cat $DIRNAME/../logs/openmano.?
+echo
+echo "-------------------------------"
+echo "OPENVIM"
+echo "-------------------------------"
+echo "cat $DIRNAME/../logs/openvim.?"
+cat $DIRNAME/../logs/openvim.?
+echo
+echo "-------------------------------"
+echo
+
+#get version
+echo
+echo "-------------------------------"
+echo "version"
+echo "-------------------------------"
+echo "-------------------------------"
+echo "OPENMANO"
+echo "-------------------------------"
+echo "cat $DIRNAME/../openmano/openmanod.py|grep ^__version__"
+cat $DIRNAME/../openmano/openmanod.py|grep ^__version__
+echo
+echo "-------------------------------"
+echo "OPENVIM"
+echo "-------------------------------"
+echo "cat $DIRNAME/../openvim/openvimd.py|grep ^__version__"
+cat $DIRNAME/../openvim/openvimd.py|grep ^__version__
+echo
+echo "-------------------------------"
+echo
+
+#get configuration files
+echo "-------------------------------"
+echo "Configuration files"
+echo "-------------------------------"
+echo "-------------------------------"
+echo "OPENMANO"
+echo "-------------------------------"
+echo "cat $DIRNAME/../openmano/openmanod.cfg"
+cat $DIRNAME/../openmano/openmanod.cfg
+echo "-------------------------------"
+echo "OPENVIM"
+echo "-------------------------------"
+echo "cat $DIRNAME/../openvim/openvimd.cfg"
+cat $DIRNAME/../openvim/openvimd.cfg
+echo "-------------------------------"
+echo
+
+#get list of items
+for verbose in "" "-vvv"
+do
+  echo "-------------------------------"
+  echo "OPENMANO$verbose"
+  echo "-------------------------------"
+  echo "$OMCLIENT config $verbose"
+  $OMCLIENT config
+  echo "-------------------------------"
+  echo "$OMCLIENT tenant-list $verbose"
+  $OMCLIENT tenant-list $verbose
+  echo "-------------------------------"
+  echo "$OMCLIENT datacenter-list --all"
+  $OMCLIENT datacenter-list --all
+  echo "-------------------------------"
+  echo "$OMCLIENT datacenter-list $verbose"
+  $OMCLIENT datacenter-list $verbose
+  echo "-------------------------------"
+  dclist=`$OMCLIENT datacenter-list |awk '{print $1}'`
+  for dc in $dclist; do
+    echo "$OMCLIENT datacenter-net-list $dc $verbose"
+    $OMCLIENT datacenter-net-list $dc $verbose
+    echo "-------------------------------"
+  done
+  echo "$OMCLIENT vnf-list $verbose"
+  $OMCLIENT vnf-list $verbose
+  echo "-------------------------------"
+  vnflist=`$OMCLIENT vnf-list |awk '$1!="No" {print $1}'`
+  for vnf in $vnflist; do
+    echo "$OMCLIENT vnf-list $vnf $verbose"
+    $OMCLIENT vnf-list $vnf $verbose
+    echo "-------------------------------"
+  done
+  echo "$OMCLIENT scenario-list $verbose"
+  $OMCLIENT scenario-list $verbose
+  echo "-------------------------------"
+  scenariolist=`$OMCLIENT scenario-list |awk '$1!="No" {print $1}'`
+  for sce in $scenariolist; do
+    echo "$OMCLIENT scenario-list $sce $verbose"
+    $OMCLIENT scenario-list $sce $verbose
+    echo "-------------------------------"
+  done
+  echo "$OMCLIENT instance-scenario-list $verbose"
+  $OMCLIENT instance-scenario-list $verbose
+  echo "-------------------------------"
+  instancelist=`$OMCLIENT instance-scenario-list |awk '$1!="No" {print $1}'`
+  for i in $instancelist; do
+    echo "$OMCLIENT instance-scenario-list $i $verbose"
+    $OMCLIENT instance-scenario-list $i $verbose
+    echo "-------------------------------"
+  done
+  echo
+  echo "-------------------------------"
+  echo "OPENVIM$verbose"
+  echo "-------------------------------"
+  echo "$OVCLIENT config"
+  $OVCLIENT config
+  echo "-------------------------------"
+  echo "$OVCLIENT tenant-list $verbose"
+  $OVCLIENT tenant-list $verbose
+  echo "-------------------------------"
+  echo "$OVCLIENT host-list $verbose"
+  $OVCLIENT host-list $verbose
+  echo "-------------------------------"
+  echo "$OVCLIENT net-list $verbose"
+  $OVCLIENT net-list $verbose
+  echo "-------------------------------"
+  echo "$OVCLIENT port-list $verbose"
+  $OVCLIENT port-list $verbose
+  echo "-------------------------------"
+  echo "$OVCLIENT flavor-list $verbose"
+  $OVCLIENT flavor-list $verbose
+  echo "-------------------------------"
+  echo "$OVCLIENT image-list $verbose"
+  $OVCLIENT image-list $verbose
+  echo "-------------------------------"
+  echo "$OVCLIENT vm-list $verbose"
+  $OVCLIENT vm-list $verbose
+  echo "-------------------------------"
+  echo
+
+done
+echo
diff --git a/scripts/service-openmano.sh b/scripts/service-openmano.sh
new file mode 100755 (executable)
index 0000000..243d13f
--- /dev/null
@@ -0,0 +1,163 @@
+#!/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
+##
+
+
+
+DIRNAME=$(readlink -f ${BASH_SOURCE[0]})
+DIRNAME=$(dirname $DIRNAME )
+DIR_OM=$(dirname $DIRNAME )
+
+function usage(){
+    echo -e "Usage: $0 [openmano/mano] start|stop|restart|status"
+    echo -e "  Launch|Removes|Restart|Getstatus openmano on a screen"
+}
+
+function kill_pid(){
+    #send TERM signal and wait 5 seconds and send KILL signal ir still running
+    #PARAMS: $1: PID of process to terminate
+    kill $1 #send TERM signal
+    WAIT=5
+    while [ $WAIT -gt 0 ] && ps -o pid -U $USER -u $USER | grep -q $1
+    do
+        sleep 1
+        WAIT=$((WAIT-1))
+        [ $WAIT -eq 0 ] && echo -n "sending SIGKILL...  " &&  kill -9 $1  #kill when count reach 0
+    done
+    echo "done"
+   
+}
+
+#obtain parameters
+om_list=""
+#om_action="start"  #uncoment to get a default action
+for param in $*
+do
+    [ "$param" == "start" -o "$param" == "stop"  -o "$param" == "restart" -o "$param" == "status" ] && om_action=$param  && continue
+    [ "$param" == "openmano" -o "$param" == "mano" ]   && om_list="$om_list mano"             && continue
+    [ "$param" == "-h" -o "$param" == "--help" ] && usage && exit 0
+    
+    #if none of above, reach this line because a param is incorrect
+    echo "Unknown param '$param' type $0 --help" >&2
+    exit -1
+done
+
+#check action is provided
+[ -z "$om_action" ] && usage >&2 && exit -1
+
+#if no componenets supplied assume all
+[ -z "$om_list" ] && om_list="mano"
+for om_component in $om_list
+do
+    [ "${om_component}" == "mano" ] && om_cmd="openmanod.py"   && om_name="openmano  " && om_dir=$(readlink -f ${DIR_OM}/openmano)
+    #obtain PID of program
+    component_id=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'`
+
+    #status
+    if [ "$om_action" == "status" ]
+    then
+        [ -n "$component_id" ] && echo "    $om_name running, pid $component_id"
+        [ -z "$component_id" ] && echo "    $om_name stopped"
+    fi
+
+    #stop
+    if [ "$om_action" == "stop" -o "$om_action" == "restart" ]
+    then
+        #terminates program
+        [ -n "$component_id" ] && echo -n "    stopping $om_name ... " && kill_pid $component_id 
+        component_id=""
+        #terminates screen
+        if screen -wipe | grep -Fq .$om_component
+        then
+            screen -S $om_component -p 0 -X stuff "exit\n"
+            sleep 1
+        fi
+    fi
+
+    #start
+    if [ "$om_action" == "start" -o "$om_action" == "restart" ]
+    then
+        #calculates log file name
+        logfile=""
+        mkdir -p $DIR_OM/logs && logfile=$DIR_OM/logs/open${om_component} || echo "can not create logs directory  $DIR_OM/logs"
+        #check already running
+        [ -n "$component_id" ] && echo "    $om_name is already running. Skipping" && continue
+        #create screen if not created
+        echo -n "    starting $om_name ... "
+        if ! screen -wipe | grep -Fq .${om_component}
+        then
+            pushd ${om_dir} > /dev/null
+            screen -dmS ${om_component}  bash
+            sleep 1
+            popd > /dev/null
+        else
+            echo -n " using existing screen '${om_component}' ... "
+            screen -S ${om_component} -p 0 -X log off
+            screen -S ${om_component} -p 0 -X stuff "cd ${om_dir}\n"
+            sleep 1
+        fi
+        #move old log file index one number up and log again in index 0
+        if [[ -n $logfile ]]
+        then
+            for index in 8 7 6 5 4 3 2 1 0
+            do
+                [[ -f ${logfile}.${index} ]] && mv ${logfile}.${index} ${logfile}.$((index+1))
+            done
+            screen -S ${om_component} -p 0 -X logfile ${logfile}.0
+            screen -S ${om_component} -p 0 -X log on
+        fi
+        #launch command to screen
+        screen -S ${om_component} -p 0 -X stuff "./${om_cmd}\n"
+        #check if is running
+        [[ -n $logfile ]] && timeout=120 #2 minute
+        [[ -z $logfile ]] && timeout=20
+        while [[ $timeout -gt 0 ]]
+        do
+           #check if is running
+           #echo timeout $timeout
+           #if !  ps -f -U $USER -u $USER | grep -v grep | grep -q ${om_cmd}
+           log_lines=0
+           [[ -n $logfile ]] && log_lines=`head ${logfile}.0 | wc -l`
+           component_id=`ps -o pid,cmd -U $USER -u $USER | grep -v grep | grep ${om_cmd} | awk '{print $1}'`
+           if [[ -z $component_id ]]
+           then #process not started or finished
+               [[ $log_lines -ge 2 ]] &&  echo -n "ERROR, it has exited." && break
+               #started because writted serveral lines at log so report error
+           fi
+           [[ -n $logfile ]] && grep -q "open${om_component}d ready" ${logfile}.0 && break
+           sleep 1
+           timeout=$((timeout -1))
+        done
+        if [[ -n $logfile ]] && [[ $timeout == 0 ]] 
+        then 
+           echo -n "timeout!"
+        else
+           echo -n "running on 'screen -x ${om_component}'."
+        fi
+        [[ -n $logfile ]] && echo "  Logging at '${logfile}.0'" || echo
+    fi
+done
+
+
+
+
diff --git a/test/test-multivim.sh b/test/test-multivim.sh
new file mode 100755 (executable)
index 0000000..d08ceb9
--- /dev/null
@@ -0,0 +1,195 @@
+#!/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
+##
+
+#This script is a basic test for openmano, that deals with two openvim
+#stopping on an error
+#WARNING: It destroy the database content
+
+
+function usage(){
+    echo -e "usage: ${BASH_SOURCE[0]} [-f]\n  Deletes openvim/openmano content and make automatically the wiki steps"
+    echo -e "  at 'https://github.com/nfvlabs/openmano/wiki/Getting-started#how-to-use-it'"
+    echo -e "  OPTIONS:"
+    echo -e "    -f --force : does not prompt for confirmation"
+    echo -e "    -h --help  : shows this help"
+}
+
+function is_valid_uuid(){
+    echo "$1" | grep -q -E '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$' && return 0
+    return 1
+}
+
+
+#detect if is called with a source to use the 'exit'/'return' command for exiting
+[[ ${BASH_SOURCE[0]} != $0 ]] && _exit="return" || _exit="exit"
+
+#check correct arguments
+[[ -n $1 ]] && [[ $1 != -h ]] && [[ $1 != --help ]] && [[ $1 != -f ]] && [[ $1 != --force ]] && \
+   echo "invalid argument '$1'?" &&  usage >&2 && $_exit 1
+[[ $1 == -h ]] || [[ $1 == --help ]]  && usage && $_exit 0
+
+#ask for confirmation if argument is not -f --force
+force=""
+[[ $1 == -f ]] || [[ $1 == --force ]] && force=y
+[[ $force != y ]] && read -e -p "WARNING: openmano and openvim database content will be lost!!!  Continue(y/N)" force
+[[ $force != y ]] && [[ $force != yes ]] && echo "aborted!" && $_exit
+
+DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
+DIR_BASE=$(dirname $DIRNAME)
+DIR_BASE=$(dirname $DIR_BASE)
+DIRvim=$DIR_BASE/openvim
+DIRmano=$DIR_BASE/openmano
+DIRscripts=$DIR_BASE/scripts
+
+echo "deleting deployed vm"
+openvim vm-delete -f | grep -q deleted && sleep 10 #give some time to get virtual machines deleted
+
+echo "Stopping openmano"
+$DIRscripts/service-openmano.sh stop
+
+echo "Initializing databases"
+$DIRvim/database_utils/init_vim_db.sh -u vim -p vimpw
+$DIRmano/database_utils/init_mano_db.sh -u mano -p manopw
+
+echo "Starting openmano"
+$DIRscripts/service-openmano.sh start
+
+echo "Creating openmano tenant 'mytenant'"
+nfvotenant=`openmano tenant-create mytenant --description=mytenant |gawk '{print $1}'`
+#check a valid uuid is obtained
+is_valid_uuid $nfvotenant || ! echo "fail" >&2 || $_exit 1 
+export OPENMANO_TENANT=$nfvotenant
+echo "  $nfvotenant"
+
+echo "Adding example hosts"
+openvim host-add $DIRvim/test/hosts/host-example0.json || ! echo "fail" >&2 || $_exit 1
+openvim host-add $DIRvim/test/hosts/host-example1.json || ! echo "fail" >&2 || $_exit 1
+openvim host-add $DIRvim/test/hosts/host-example2.json || ! echo "fail" >&2 || $_exit 1
+openvim host-add $DIRvim/test/hosts/host-example3.json || ! echo "fail" >&2 || $_exit 1
+echo "Adding example nets"
+openvim net-create $DIRvim/test/networks/net-example0.yaml || ! echo "fail" >&2 || $_exit 1
+openvim net-create $DIRvim/test/networks/net-example1.yaml || ! echo "fail" >&2 || $_exit 1
+openvim net-create $DIRvim/test/networks/net-example2.yaml || ! echo "fail" >&2 || $_exit 1
+openvim net-create $DIRvim/test/networks/net-example3.yaml || ! echo "fail" >&2 || $_exit 1
+
+echo "Creating openvim tenant 'admin'"
+vimtenant=`openvim tenant-create '{"tenant": {"name":"admin", "description":"admin"}}' |gawk '{print $1}'`
+#check a valid uuid is obtained
+is_valid_uuid $vimtenant || ! echo "fail" >&2 || $_exit 1
+echo "  $vimtenant"
+OPENVIM_TENANT_1=$vimtenant && export OPENVIM_TENANT=$vimtenant
+
+echo "Creating datacenter 'mydc1' in openmano"
+datacenter=`openmano datacenter-create mydc1 http://localhost:9080/openvim |gawk '{print $1}'`
+#check a valid uuid is obtained
+is_valid_uuid $datacenter || ! echo "fail" >&2 || $_exit 1 
+echo "  $datacenter"
+OPENMANO_DATACENTER_1=$datacenter && export OPENMANO_DATACENTER=$datacenter
+
+echo "Attaching openmano tenant to the datacenter and the openvim tenant"
+openmano datacenter-attach mydc1 --vim-tenant-id $vimtenant || ! echo "fail" >&2 || $_exit 1 
+
+echo "Updating external nets in openmano"
+openmano datacenter-net-update -f mydc1 || ! echo "fail" >&2 || $_exit 1
+
+echo "Creating a second fake datacenter 'mydc2' in openmano"
+datacenter2=`openmano datacenter-create mydc2 http://localhost:9082/openvim |gawk '{print $1}'`
+#check a valid uuid is obtained
+is_valid_uuid $datacenter || ! echo "fail" >&2 || $_exit 1 
+echo "  $datacenter2"
+OPENMANO_DATACENTER_2=$datacenter2
+echo "Attaching a second fake openvim 'mydc2'"
+openmano datacenter-attach mydc2 --vim-tenant-id $vimtenant || ! echo "fail" >&2 || $_exit 1
+
+echo "Creating VNFs, must fail in second openvim"
+openmano vnf-create $DIRmano/vnfs/examples/linux.yaml         || ! echo "fail" >&2 || $_exit 1
+openmano vnf-create $DIRmano/vnfs/examples/dataplaneVNF1.yaml || ! echo "fail" >&2 || $_exit 1
+openmano vnf-create $DIRmano/vnfs/examples/dataplaneVNF2.yaml || ! echo "fail" >&2 || $_exit 1
+
+echo "Checking images and flavors created at openvim"
+nb=`openvim image-list | wc -l`
+echo -n " $nb images "
+[[ $nb -eq 3 ]] || ! echo "fail" >&2 || $_exit 1
+echo " $nb flavors "
+[[ $nb -eq 3 ]] || ! echo "fail" >&2 || $_exit 1
+
+echo "Creating Scenarios"
+openmano scenario-create $DIRmano/scenarios/examples/simple.yaml  || ! echo "fail" >&2 || $_exit 1
+openmano scenario-create $DIRmano/scenarios/examples/complex.yaml || ! echo "fail" >&2 || $_exit 1
+
+echo "Deleting openvim images and flavors to force reload again"
+openvim image-delete -f
+openvim flavor-delete -f
+
+echo "Launching scenarios"
+openmano scenario-deploy simple simple-instance   || ! echo "fail" >&2 || $_exit 1
+openmano scenario-deploy complex complex-instance || ! echo "fail" >&2 || $_exit 1
+
+echo "Checking that openvim has 5 VM running"
+nb=`openvim vm-list | wc -l`
+[[ $nb -eq 5 ]] || ! echo "fail" >&2 || $_exit 1
+while openvim vm-list | grep -q CREATING ; do sleep 1; done
+openvim vm-list | grep -v -q ERROR || ! echo "fail: VM with error" >&2 || $_exit 1
+
+echo "Removing scenarios"
+for scenario in `openmano instance-scenario-list  | awk '{print $2}'`
+do
+  openmano instance-scenario-delete -f $scenario
+done
+
+echo "Editing datacenters so that Changing openvim Working with the second openvim"
+openmano datacenter-edit -f mydc1 'vim_url: http://localhost:9083/openvim'
+openmano datacenter-edit -f mydc2 'vim_url: http://localhost:9080/openvim'
+export OPENMANO_DATACENTER=$OPENMANO_DATACENTER_2
+
+echo "Updating external nets in openmano for second datacenter"
+openmano datacenter-net-update -f mydc2 || ! echo "fail" >&2 || $_exit 1
+
+echo "Launching Scenario instances"
+openmano scenario-deploy simple simple-instance   || ! echo "fail" >&2 || $_exit 1
+openmano scenario-deploy complex complex-instance || ! echo "fail" >&2 || $_exit 1
+
+echo "Checking images and flavors created at openvim"
+nb=`openvim image-list | wc -l`
+echo -n " $nb images "
+[[ $nb -eq 3 ]] || ! echo "fail" >&2 || $_exit 1
+echo " $nb flavors "
+[[ $nb -eq 3 ]] || ! echo "fail" >&2 || $_exit 1
+
+echo "Checking that openvim has 5 VM running"
+nb=`openvim vm-list | wc -l`
+[[ $nb -eq 5 ]] || ! echo "fail" >&2 || $_exit 1
+while openvim vm-list | grep -q CREATING ; do sleep 1; done
+openvim vm-list | grep -v -q ERROR || ! echo "fail: VM with error" >&2 || $_exit 1
+
+
+echo
+echo DONE
+#echo "Listing VNFs"
+#openmano vnf-list
+#echo "Listing scenarios"
+#openmano scenario-list
+#echo "Listing scenario instances"
+#openmano instance-scenario-list
+
+
diff --git a/test/test_os.sh b/test/test_os.sh
new file mode 100755 (executable)
index 0000000..ae092c2
--- /dev/null
@@ -0,0 +1,253 @@
+#!/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
+##
+
+#This script can be used as a basic test of openmano deployment over openstack.
+#in order to use you need to set the OS_XXXX bash variables with openstack values
+#    OS_USERNAME     e.g.: admin
+#    OS_PASSWORD     
+#    OS_AUTH_URL     url to access openstack VIM e.g. http:/openstack:35357/v2.0
+#    OS_TENANT_NAME  e.g.: admin
+#    OS_CONFIG       e.g.: "'network_vlan_ranges: sriov_net'"
+#    OS_TEST_IMAGE_PATH_LINUX  image path(location) to use by the VNF linux
+#    OS_TEST_IMAGE_PATH_LINUXDATA image path(location) to use by the VNF dataplaneVNF_2VMs and dataplaneVNF3
+
+#it should be used with source. It can modifies /home/$USER/.bashrc appending the variables
+#you need to delete them manually if desired
+
+function usage(){
+    echo -e "usage: ${BASH_SOURCE[0]} [OPTIONS] <action>\n  test openmano using a openstack VIM"
+    echo -e "  <action> is a list of the following items (by default 'reset create')"
+    echo -e "    reset     reset the openmano database content"
+    echo -e "    create    creates items at openstack VIM"
+    echo -e "    delete    delete created items"
+    echo -e "  OPTIONS:"
+    echo -e "    -f --force       does not prompt for confirmation"
+    echo -e "    -h --help        shows this help"
+    echo -e "    --insert-bashrc  insert the created tenant,datacenter variables at"
+    echo -e "                     ~/.bashrc to be available by openmano config"
+}
+
+function is_valid_uuid(){
+    echo "$1" | grep -q -E '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$' && return 0
+    return 1
+}
+
+#detect if is called with a source to use the 'exit'/'return' command for exiting
+[[ ${BASH_SOURCE[0]} != $0 ]] && _exit="return" || _exit="exit"
+
+#detect if environment variables are set
+fail=""
+[[ -z $OS_USERNAME ]] && echo "OS_USERNAME variable not defined" >&2 && fail=1
+[[ -z $OS_PASSWORD ]] && echo "OS_PASSWORD variable not defined" >&2 && fail=1
+[[ -z $OS_AUTH_URL ]] && echo "OS_AUTH_URL variable not defined" >&2 && fail=1
+[[ -z $OS_TENANT_NAME ]] && echo "OS_TENANT_NAME variable not defined" >&2 && fail=1
+[[ -z $OS_CONFIG ]] && echo "OS_CONFIG variable not defined" >&2 && fail=1
+[[ -z $OS_TEST_IMAGE_PATH_LINUX ]] && echo "OS_TEST_IMAGE_PATH_LINUX variable not defined" >&2 && fail=1
+[[ -z $OS_TEST_IMAGE_PATH_LINUXDATA ]] && echo "OS_TEST_IMAGE_PATH_LINUXDATA variable not defined" >&2 && fail=1
+[[ -n $fail ]] && $_exit 1
+
+#check correct arguments
+action_list=""
+for param in $*
+do
+   if [[ $param == reset ]] || [[ $param == create ]] || [[ $param == delete ]]
+   then 
+       action_list="$action_list $param"
+   elif [[ $param == -h ]] || [[ $param == --help ]]
+   then
+       usage
+       $_exit 0
+   elif [[ $param == -f ]] || [[ $param == --force ]]
+   then
+       force=y
+   elif [[ $param == --insert-bashrc ]]
+   then
+       insert_bashrc=y
+   else
+       echo "invalid argument '$param'?" &&  usage >&2 && $_exit 1
+   fi
+done
+
+DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
+DIRmano=$(dirname $DIRNAME)
+DIRscript=$(dirname $DIRmano)/scripts
+#by default action should be reset create
+[[ -z $action_list ]] && action_list="reset create"
+
+for action in $action_list
+do
+if [[ $action == "reset" ]] 
+then 
+
+    #ask for confirmation if argument is not -f --force
+    [[ $force != y ]] && read -e -p "WARNING: reset openmano database, content will be lost!!! Continue(y/N)" force
+    [[ $force != y ]] && [[ $force != yes ]] && echo "aborted!" && $_exit
+
+    echo "Stopping openmano"
+    $DIRscript/service-openmano.sh mano stop
+    echo "Initializing openmano database"
+    $DIRmano/database_utils/init_mano_db.sh -u mano -p manopw
+    echo "Starting openmano"
+    $DIRscript/service-openmano.sh mano start
+
+elif [[ $action == "delete" ]]
+then
+    result=`openmano tenant-list TOS-tenant`
+    nfvotenant=`echo $result |gawk '{print $1}'`
+    #check a valid uuid is obtained
+    is_valid_uuid $nfvotenant || ! echo "Tenant TOS-tenant not found. Already delete?" >&2 || $_exit 1
+    export OPENMANO_TENANT=$nfvotenant
+    openmano instance-scenario-delete -f simple-instance     || echo "fail"
+    openmano instance-scenario-delete -f complex2-instance   || echo "fail"
+    openmano scenario-delete -f simple       || echo "fail"
+    openmano scenario-delete -f complex2     || echo "fail"
+    openmano vnf-delete -f linux             || echo "fail"
+    openmano vnf-delete -f dataplaneVNF_2VMs || echo "fail"
+    openmano vnf-delete -f dataplaneVNF3     || echo "fail"
+    openmano vnf-delete -f TOS-VNF1          || echo "fail"
+    openmano datacenter-detach TOS-dc        || echo "fail"
+    openmano datacenter-delete -f TOS-dc     || echo "fail"
+    openmano tenant-delete -f TOS-tenant     || echo "fail"
+
+elif [[ $action == "create" ]]
+then 
+
+    printf "%-50s" "Creating openmano tenant 'TOS-tenant': "
+    result=`openmano tenant-create TOS-tenant --description="created by test_os.sh"`
+    nfvotenant=`echo $result |gawk '{print $1}'`
+    #check a valid uuid is obtained
+    ! is_valid_uuid $nfvotenant && echo "FAIL" && echo "    $result" && $_exit 1 
+    export OPENMANO_TENANT=$nfvotenant
+    [[ $insert_bashrc == y ]] && echo -e "\nexport OPENMANO_TENANT=$nfvotenant"  >> ~/.bashrc
+    echo $nfvotenant
+
+    printf "%-50s" "Creating datacenter 'TOS-dc' in openmano:"
+    result=`openmano datacenter-create TOS-dc "${OS_AUTH_URL}" "--type=openstack" "--config=${OS_CONFIG}"`
+    datacenter=`echo $result |gawk '{print $1}'`
+    #check a valid uuid is obtained
+    ! is_valid_uuid $datacenter && echo "FAIL" && echo "    $result" && $_exit 1 
+    echo $datacenter
+    export OPENMANO_DATACENTER=$datacenter
+    [[ $insert_bashrc == y ]] && echo -e "\nexport OPENMANO_DATACENTER=$datacenter"  >> ~/.bashrc
+
+    printf "%-50s" "Attaching openmano tenant to the datacenter:"
+    result=`openmano datacenter-attach TOS-dc "--user=$OS_USERNAME" "--password=$OS_PASSWORD" "--vim-tenant-name=$OS_TENANT_NAME"`
+    [[ $? != 0 ]] && echo  "FAIL" && echo "    $result" && $_exit 1
+    echo OK
+
+    printf "%-50s" "Updating external nets in openmano: "
+    result=`openmano datacenter-net-update -f TOS-dc`
+    [[ $? != 0 ]] && echo  "FAIL" && echo "    $result"  && $_exit 1
+    echo OK
+
+    printf "%-50s" "Creating VNF 'linux': "
+    #glance image-create --file=./US1404dpdk.qcow2 --name=US1404dpdk --disk-format=qcow2 --min-disk=2 --is-public=True --container-format=bare
+    #nova image-meta US1404dpdk set location=/mnt/powervault/virtualization/vnfs/os/US1404dpdk.qcow2
+    #glance image-create --file=./US1404user.qcow2 --min-disk=2 --is-public=True --container-format=bare --name=US1404user --disk-format=qcow2
+    #nova image-meta US1404user  set location=/mnt/powervault/virtualization/vnfs/os/US1404user.qcow2
+    result=`openmano vnf-create $DIRmano/vnfs/examples/linux.yaml "--image-path=$OS_TEST_IMAGE_PATH_LINUX"`
+    vnf=`echo $result |gawk '{print $1}'`
+    #check a valid uuid is obtained
+    ! is_valid_uuid $vnf && echo FAIL && echo "    $result" &&  $_exit 1
+    echo $vnf
+    
+    printf "%-50s" "Creating VNF 1PF,1VF,2GHP,4PThreads: "
+    result=`openmano vnf-create "vnf:
+        name: TOS-VNF1
+        external-connections:
+        - name: eth0
+          type: mgmt
+          VNFC: TOS-VNF1-VM
+          local_iface_name: eth0
+        - name: PF0
+          type: data
+          VNFC: TOS-VNF1-VM
+          local_iface_name: PF0
+        - name: VF0
+          type: data
+          VNFC: TOS-VNF1-VM
+          local_iface_name: VF0
+        VNFC: 
+        - name: TOS-VNF1-VM
+          VNFC image: $OS_TEST_IMAGE_PATH_LINUXDATA
+          numas:
+          - paired-threads: 2
+            paired-threads-id: [ [0,2], [1,3] ]
+            memory: 2
+            interfaces:
+            - name:  PF0
+              vpci: '0000:00:11.0'
+              dedicated: 'yes'
+              bandwidth: 10 Gbps
+              mac_address: '20:33:45:56:77:44'
+            - name:  VF0
+              vpci:  '0000:00:12.0'
+              dedicated: 'no'
+              bandwidth: 1 Gbps
+              mac_address: '20:33:45:56:77:45'
+          bridge-ifaces:
+          - name: eth0
+            vpci: '0000:00:09.0'
+            bandwidth: 1 Mbps
+            mac_address: '20:33:45:56:77:46'
+            model: e1000
+       "`
+    vnf=`echo $result |gawk '{print $1}'`
+    ! is_valid_uuid $vnf && echo FAIL && echo "    $result" && $_exit 1
+    echo $vnf
+    printf "%-50s" "Creating VNF 'dataplaneVNF_2VMs': "
+    result=`openmano vnf-create $DIRmano/vnfs/examples/dataplaneVNF_2VMs.yaml "--image-path=$OS_TEST_IMAGE_PATH_LINUXDATA,$OS_TEST_IMAGE_PATH_LINUXDATA"`
+    vnf=`echo $result |gawk '{print $1}'`
+    ! is_valid_uuid $vnf && echo FAIL && echo "    $result" && $_exit 1
+    echo $vnf
+    printf "%-50s" "Creating VNF 'dataplaneVNF3.yaml': "
+    result=`openmano vnf-create $DIRmano/vnfs/examples/dataplaneVNF3.yaml "--image-path=$OS_TEST_IMAGE_PATH_LINUXDATA"`
+    vnf=`echo $result |gawk '{print $1}'`
+    ! is_valid_uuid $vnf && echo FAIL && echo "    $result" && $_exit 1
+    echo $vnf
+
+    for sce in simple complex2
+    do
+      printf "%-50s" "Creating scenario '$sce':"
+      result=`openmano scenario-create $DIRmano/scenarios/examples/${sce}.yaml`
+      scenario=`echo $result |gawk '{print $1}'`
+      ! is_valid_uuid $scenario && echo FAIL && echo "    $result" &&  $_exit 1
+      echo $scenario
+    done
+
+    for sce in simple complex2
+    do 
+      printf "%-50s" "Deploying scenario '$sce':"
+      result=`openmano scenario-deploy $sce ${sce}-instance`
+      instance=`echo $result |gawk '{print $1}'`
+      ! is_valid_uuid $instance && echo FAIL && echo "    $result" && $_exit 1
+      echo $instance
+    done
+
+    echo
+    echo DONE
+fi
+done
+
diff --git a/test/test_osconnector.py b/test/test_osconnector.py
new file mode 100755 (executable)
index 0000000..152155b
--- /dev/null
@@ -0,0 +1,274 @@
+#!/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
+##
+
+'''
+test_osconnector.py makes a test over osconnector.py (openstack connector)
+credentiasl must be provided with environment bash variables or arguments
+'''
+__author__="Alfonso Tierno, Gerardo Garcia"
+__date__ ="$22-jun-2014 11:19:29$"
+
+
+import os
+import sys
+import getopt
+#import yaml
+#from jsonschema import validate as js_v, exceptions as js_e
+
+#load osconnector, insert openmano directory in the path
+r=sys.argv[0].rfind('/')
+if r<0:
+    osconnector_path=".."
+else:
+    osconnector_path=sys.argv[0][:r+1]+".."
+sys.path.insert(0, osconnector_path)
+#sys.path.insert(0, '/home/atierno/workspace/openmano/openmano')
+import osconnector
+
+version="0.1"
+
+def usage():
+    print "Usage: ", sys.argv[0], "[options]"
+    print "  -v|--version            openstack version (by default 2)"
+    print "  -u|--username USER      user to authenticate (by default bash:OS_USERNAME)"
+    print "  -p|--password PASSWD    password to authenticate (by default bash:OS_PASSWORD)"
+    print "  -U|--auth_url URL       url of authentication over keystone (by default bash:OS_AUTH_URL)"
+    print "  -t|--tenant_name TENANT password to authenticate (by default bash:OS_TENANT_NAME)"
+    print "  -i|--image IMAGE        use this local path or url for loading image (by default cirros)"
+    print "  --skip-admin-tests      skip tests that requires administrative permissions, like create tenants"
+    print "  -h|--help               shows this help"
+    return
+
+def delete_items():
+    global myvim
+    global rollback_list
+    print "Making rollback, deleting items"
+    for i in range(len(rollback_list)-1, -1, -1):
+        item,name,id_ = rollback_list[i]
+        if item=="creds":
+            print ("changing credentials %s='%s'" % (name, id_)).ljust(50),
+        else:
+            print ("deleting %s '%s'" % (item, name)).ljust(50),
+        sys.stdout.flush()
+        if item=="flavor":
+            result,message=myvim.delete_tenant_flavor(id_)
+        elif item=="image":
+            result,message=myvim.delete_tenant_image(id_)
+        elif item=="tenant":
+            result,message=myvim.delete_tenant(id_)
+        elif item=="user":
+            result,message=myvim.delete_user(id_)
+        elif item=="network":
+            result,message=myvim.delete_tenant_network(id_)
+        elif item=="vm":
+            result,message=myvim.delete_tenant_vminstance(id_)
+        elif item=="creds":
+            try:
+                myvim[name]=id_
+                result=1
+            except Exception, e:
+                result=-1
+                message= "  " + str(type(e))[6:-1] + ": "+  str(e)
+        else:
+            print "Internal error unknown item rollback %s,%s,%s" % (item,name,id_)
+            continue
+        if result<0:
+            print " Fail"
+            print "  VIM response:", message
+            continue
+        else:
+            print " Ok"
+
+if __name__=="__main__":
+    global myvim
+    global rollback_list
+    #print "(c) Copyright Telefonica"
+    rollback_list=[]
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], "hv:u:U:p:t:i:",
+                 ["username=", "help", "version=", "password=", "tenant=", "url=","skip-admin-tests",'image='])
+    except getopt.GetoptError, err:
+        # print help information and exit:
+        print "Error:", err # will print something like "option -a not recognized"
+        usage()
+        sys.exit(2)
+        
+    creds = {}
+    creds['version'] = os.environ.get('OS_VERSION', '2')
+    creds['username'] = os.environ.get('OS_USERNAME')
+    creds['password'] = os.environ.get('OS_PASSWORD')
+    creds['auth_url'] = os.environ.get('OS_AUTH_URL')
+    creds['tenant_name'] = os.environ.get('OS_TENANT_NAME')
+    skip_admin_tests=False
+    image_path="http://download.cirros-cloud.net/0.3.3/cirros-0.3.3-x86_64-disk.img"
+    for o, a in opts:
+        if o in ("-h", "--help"):
+            usage()
+            sys.exit()
+        elif o in ("-v", "--version"):
+            creds['version']=a
+        elif o in ("-u", "--username"):
+            creds['username']=a
+        elif o in ("-p", "--password"):
+            creds['password']=a
+        elif o in ("-U", "--auth_url"):
+            creds['auth_url']=a
+        elif o in ("-t", "--tenant_name"):
+            creds['tenant_name']=a
+        elif o in ("-i", "--image"):
+            image_path=a
+        elif o=="--skip-admin-tests":
+            skip_admin_tests=True
+        else:
+            assert False, "Unhandled option"
+    if creds['auth_url']==None:
+        print "you must provide openstack url with -U or bash OS_AUTH_URL"
+        sys.exit()
+    print "creds:", creds
+    
+
+    try:
+        print 'load osconnector class'.ljust(50),
+        sys.stdout.flush()
+        try:
+            myvim=osconnector.osconnector(uuid=None, name='test-openstack', tenant=creds['tenant_name'], 
+                url=creds['auth_url'], url_admin=None,
+                user=creds['username'], passwd=creds['password'],
+                debug = False, config={'network_vlan_ranges':'physnet_sriov'} )
+            print " Ok"
+        except Exception, e:
+            print " Fail"
+            print str(type(e))[6:-1] + ": "+  str(e) 
+            exit(-1)
+        
+        if not skip_admin_tests:
+            tenant_name="tos-tenant"
+            print ("creating new tenant '%s'" % tenant_name).ljust(50),
+            sys.stdout.flush()
+            result,new_tenant=myvim.new_tenant(tenant_name, "test tenant_description, trying a long description to get the limit. 2 trying a long description to get the limit. 3. trying a long description to get the limit.")
+            if result<0:
+                print " Fail"
+                print "  you can skip tenant creation with param'--skip-admin-tests'"
+                print "  VIM response:", new_tenant
+                exit(-1)
+            else:
+                print " Ok", new_tenant
+                rollback_list.append(("tenant",tenant_name,new_tenant))
+
+            user_name="tos-user"
+            print ("creating new user '%s'" % user_name).ljust(50),
+            sys.stdout.flush()
+            result,new_user=myvim.new_user(user_name, user_name, tenant_id=new_tenant)
+            if result<0:
+                print " Fail"
+                print "  VIM response:", new_user
+                exit(-1)
+            else:
+                print " Ok", new_user
+                rollback_list.append(("user",user_name,new_user))
+                    
+        name="tos-fl1"
+        print ("creating new flavor '%s'"%name).ljust(50),
+        sys.stdout.flush()
+        flavor={}
+        flavor['name']=name
+        result,new_flavor1=myvim.new_tenant_flavor(flavor, True)
+        if result<0:
+            print " Fail"
+            print "  VIM response:", new_flavor1
+            exit(-1)
+        else:
+            print " Ok", new_flavor1
+            rollback_list.append(("flavor",name,new_flavor1))
+            
+        name="tos-cirros"
+        print ("creating new image '%s'"%name).ljust(50),
+        sys.stdout.flush()
+        image={}
+        image['name']=name
+        image['location']=image_path #"/home/atierno/cirros-0.3.3-x86_64-disk.img"
+        result,new_image1=myvim.new_tenant_image(image)
+        if result<0:
+            print " Fail"
+            print "  VIM response:", new_image1
+            exit(-1)
+        else:
+            print " Ok", new_image1
+            rollback_list.append(("image",name, new_image1))
+
+        if not skip_admin_tests:
+            try:
+                print 'changing credentials to new tenant'.ljust(50),
+                sys.stdout.flush()
+                myvim['tenant']  =tenant_name
+                myvim['user']=user_name
+                myvim['passwd']=user_name
+                print " Ok"
+                rollback_list.append(("creds", "tenant", creds["tenant_name"]))
+                rollback_list.append(("creds", "user",   creds["username"]))
+                rollback_list.append(("creds", "passwd", creds["password"]))
+            except Exception, e:
+                print " Fail"
+                print " Error setting osconnector to new tenant:", str(type(e))[6:-1] + ": "+  str(e)
+                exit(-1)
+
+        name="tos-net-bridge"
+        print ("creating new net '%s'"%name).ljust(50),
+        sys.stdout.flush()
+        result,new_net1=myvim.new_tenant_network(name, "bridge")
+        if result<0:
+            print " Fail"
+            print "  VIM response:", new_net1
+            exit(-1)
+        else:
+            print " Ok", new_net1
+            rollback_list.append(("network",name, new_net1))
+
+        name="tos-vm-cloud"
+        print ("creating new VM '%s'"%name).ljust(50),
+        sys.stdout.flush()
+        result,new_vm1=myvim.new_tenant_vminstance(name, "vm-cloud-description", False,new_image1,new_flavor1,
+                                    [{"net_id":new_net1, "type":"virtio"}] )
+        if result<0:
+            print " Fail"
+            print "  VIM response:", new_vm1
+            exit(-1)
+        else:
+            print " Ok", new_vm1
+            rollback_list.append(("vm",name, new_vm1))
+
+            
+        print 'DONE  Ok'
+        print "Type ENTER to delete items"
+        raw_input('> ')  
+        exit()      
+              
+    except KeyboardInterrupt:
+        print " Canceled!"
+    except SystemExit:
+        pass
+    if len(rollback_list):
+        delete_items()
+
diff --git a/utils/__init__.py b/utils/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/utils/auxiliary_functions.py b/utils/auxiliary_functions.py
new file mode 100755 (executable)
index 0000000..81a0158
--- /dev/null
@@ -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,<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
+    
diff --git a/vimconn.py b/vimconn.py
new file mode 100755 (executable)
index 0000000..6702c5f
--- /dev/null
@@ -0,0 +1,269 @@
+# -*- 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
+##
+
+'''
+vimconn implement an Abstract class for the vim connector plugins
+ with the definition of the method to be implemented.
+'''
+__author__="Alfonso Tierno"
+__date__ ="$16-oct-2015 11:09:29$"
+
+#Error variables 
+HTTP_Bad_Request = 400
+HTTP_Unauthorized = 401 
+HTTP_Not_Found = 404 
+HTTP_Method_Not_Allowed = 405 
+HTTP_Request_Timeout = 408
+HTTP_Conflict = 409
+HTTP_Service_Unavailable = 503 
+HTTP_Internal_Server_Error = 500 
+
+
+class vimconnector():
+    '''Abstract base class for all the VIM connector plugins
+    These plugins must implement a vimconnector class deribed from this 
+    and all these methods
+    ''' 
+    def __init__(self, uuid, name, tenant, url, url_admin=None, user=None, passwd=None,debug=True,config={}):
+        self.id        = uuid
+        self.name      = name
+        self.url       = url
+        self.url_admin = url_admin
+        self.tenant    = tenant
+        self.user      = user
+        self.passwd    = passwd
+        self.config    = config
+        self.debug     = debug
+    
+    def __getitem__(self,index):
+        if index=='tenant':
+            return self.tenant
+        elif index=='id':
+            return self.id
+        elif index=='name':
+            return self.name
+        elif index=='user':
+            return self.user
+        elif index=='passwd':
+            return self.passwd
+        elif index=='url':
+            return self.url
+        elif index=='url_admin':
+            return self.url_admin
+        elif index=="config":
+            return self.config
+        else:
+            raise KeyError("Invalid key '%s'" %str(index))
+        
+    def __setitem__(self,index, value):
+        if index=='tenant':
+            self.tenant = value
+        elif index=='id':
+            self.id = value
+        elif index=='name':
+            self.name = value
+        elif index=='user':
+            self.user = value
+        elif index=='passwd':
+            self.passwd = value
+        elif index=='url':
+            self.url = value
+        elif index=='url_admin':
+            self.url_admin = value
+        else:
+            raise KeyError("Invalid key '%s'" %str(index))
+
+    def new_host(self, host_data):
+        '''Adds a new host to VIM'''
+        '''Returns status code of the VIM response'''
+        raise NotImplementedError( "Should have implemented this" )
+    
+    def new_external_port(self, port_data):
+        '''Adds a external port to VIM'''
+        '''Returns the port identifier'''
+        raise NotImplementedError( "Should have implemented this" )
+        
+    def new_external_network(self,net_name,net_type):
+        '''Adds a external network to VIM (shared)'''
+        '''Returns the network identifier'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def connect_port_network(self, port_id, network_id, admin=False):
+        '''Connects a external port to a network'''
+        '''Returns status code of the VIM response'''
+        raise NotImplementedError( "Should have implemented this" )
+        
+    def new_tenant(self,tenant_name,tenant_description):
+        '''Adds a new tenant to VIM'''
+        '''Returns the tenant identifier'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def delete_tenant(self,tenant_id,):
+        '''Delete a tenant from VIM'''
+        '''Returns the tenant identifier'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def new_tenant_network(self,net_name,net_type):
+        '''Adds a tenant network to VIM'''
+        '''Returns the network identifier'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def get_network_list(self, filter_dict={}):
+        '''Obtain tenant networks of VIM
+        Filter_dict can be:
+            name: network name
+            id: network uuid
+            shared: boolean
+            tenant_id: tenant
+            admin_state_up: boolean
+            status: 'ACTIVE'
+        Returns the network list of dictionaries
+        '''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def get_tenant_network(self, net_id, tenant_id=None):
+        '''Obtain tenant networks of VIM'''
+        '''Returns the network information from a network id'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def delete_tenant_network(self, net_id):
+        '''Deletes a tenant network from VIM'''
+        '''Returns the network identifier'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def refresh_tenant_network(self, net_id):
+        '''Refreshes the status of the tenant network'''
+        '''Returns: 0 if no error,
+                    <0 if error'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def get_tenant_flavor(self, flavor_id):
+        '''Obtain flavor details from the  VIM
+            Returns the flavor dict details
+        '''
+        raise NotImplementedError( "Should have implemented this" )
+        
+    def new_tenant_flavor(self, flavor_data):
+        '''Adds a tenant flavor to VIM'''
+        '''Returns the flavor identifier'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def delete_tenant_flavor(self,flavor_id):
+        '''Deletes a tenant flavor from VIM'''
+        '''Returns the HTTP response code and a message indicating details of the success or fail'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def new_tenant_image(self,image_dict):
+        '''
+        Adds a tenant image to VIM
+        Returns:
+            200, image-id        if the image is created
+            <0, message          if there is an error
+        '''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def delete_tenant_image(self, image_id):
+        '''Deletes a tenant image from VIM'''
+        '''Returns the HTTP response code and a message indicating details of the success or fail'''
+        raise NotImplementedError( "Should have implemented this" )
+        
+    def new_tenant_vminstancefromJSON(self, vm_data):
+        '''Adds a VM instance to VIM'''
+        '''Returns the instance identifier'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def new_tenant_vminstance(self,name,description,start,image_id,flavor_id,net_list):
+        '''Adds a VM instance to VIM
+        Params:
+            start: indicates if VM must start or boot in pause mode. Ignored
+            image_id,flavor_id: image and flavor uuid
+            net_list: list of interfaces, each one is a dictionary with:
+                name:
+                net_id: network uuid to connect
+                vpci: virtual vcpi to assign
+                model: interface model, virtio, e2000, ...
+                mac_address: 
+                use: 'data', 'bridge',  'mgmt'
+                type: 'virtual', 'PF', 'VF', 'VFnotShared'
+                vim_id: filled/added by this function
+                #TODO ip, security groups
+        Returns >=0, the instance identifier
+                <0, error_text
+        '''
+        raise NotImplementedError( "Should have implemented this" )
+        
+    def get_tenant_vminstance(self,vm_id):
+        '''Returns the VM instance information from VIM'''
+        raise NotImplementedError( "Should have implemented this" )
+        
+    def delete_tenant_vminstance(self, vm_id):
+        '''Removes a VM instance from VIM'''
+        '''Returns the instance identifier'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def refresh_tenant_vms_and_nets(self, vmDict, netDict):
+        '''Refreshes the status of the dictionaries of VM instances and nets passed as arguments. It modifies the dictionaries'''
+        '''Returns:
+            - result: 0 if all elements could be refreshed (even if its status didn't change)
+                      n>0, the number of elements that couldn't be refreshed,
+                      <0 if error (foreseen)
+            - error_msg: text with reference to possible errors
+        '''
+        raise NotImplementedError( "Should have implemented this" )
+    
+    def action_tenant_vminstance(self, vm_id, action_dict):
+        '''Send and action over a VM instance from VIM'''
+        '''Returns the status'''
+        raise NotImplementedError( "Should have implemented this" )
+        
+    def host_vim2gui(self, host, server_dict):
+        '''Transform host dictionary from VIM format to GUI format,
+        and append to the server_dict
+        '''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def get_hosts_info(self):
+        '''Get the information of deployed hosts
+        Returns the hosts content'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def get_hosts(self, vim_tenant):
+        '''Get the hosts and deployed instances
+        Returns the hosts content'''
+        raise NotImplementedError( "Should have implemented this" )
+
+    def get_processor_rankings(self):
+        '''Get the processor rankings in the VIM database'''
+        raise NotImplementedError( "Should have implemented this" )
+    
+    def get_image_id_from_path(self, path):
+        '''Get the image id from image path in the VIM database'''
+        '''Returns:
+             0,"Image not found"   if there are no images with that path
+             1,image-id            if there is one image with that path
+             <0,message            if there was an error (Image not found, error contacting VIM, more than 1 image with that path, etc.) 
+        '''
+        raise NotImplementedError( "Should have implemented this" )
+
+        
+
diff --git a/vimconn_openstack.py b/vimconn_openstack.py
new file mode 100644 (file)
index 0000000..3a323a8
--- /dev/null
@@ -0,0 +1,1097 @@
+# -*- 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
+##
+
+'''
+osconnector implements all the methods to interact with openstack using the python-client.
+'''
+__author__="Alfonso Tierno, Gerardo Garcia"
+__date__ ="$22-jun-2014 11:19:29$"
+
+import vimconn
+import json
+import yaml
+
+from novaclient import client as nClient, exceptions as nvExceptions
+import keystoneclient.v2_0.client as ksClient
+import keystoneclient.exceptions as ksExceptions
+import glanceclient.v2.client as glClient
+import glanceclient.client as gl1Client
+import glanceclient.exc as gl1Exceptions
+from httplib import HTTPException
+from neutronclient.neutron import client as neClient
+from neutronclient.common import exceptions as neExceptions
+from requests.exceptions import ConnectionError
+
+'''contain the openstack virtual machine status to openmano status'''
+vmStatus2manoFormat={'ACTIVE':'ACTIVE',
+                     'PAUSED':'PAUSED',
+                     'SUSPENDED': 'SUSPENDED',
+                     'SHUTOFF':'INACTIVE',
+                     'BUILD':'BUILD',
+                     'ERROR':'ERROR','DELETED':'DELETED'
+                     }
+netStatus2manoFormat={'ACTIVE':'ACTIVE','PAUSED':'PAUSED','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED'
+                     }
+
+class vimconnector(vimconn.vimconnector):
+    def __init__(self, uuid, name, tenant, url, url_admin=None, user=None, passwd=None, debug=True, config={}):
+        '''using common constructor parameters. In this case 
+        'url' is the keystone authorization url,
+        'url_admin' is not use
+        '''
+        vimconn.vimconnector.__init__(self, uuid, name, tenant, url, url_admin, user, passwd, debug, config)
+        
+        self.k_creds={}
+        self.n_creds={}
+        if not url:
+            raise TypeError, 'url param can not be NoneType'
+        self.k_creds['auth_url'] = url
+        self.n_creds['auth_url'] = url
+        if tenant:
+            self.k_creds['tenant_name'] = tenant
+            self.n_creds['project_id']  = tenant
+        if user:
+            self.k_creds['username'] = user
+            self.n_creds['username'] = user
+        if passwd:
+            self.k_creds['password'] = passwd
+            self.n_creds['api_key']  = passwd
+        self.reload_client       = True
+    
+    def __setitem__(self,index, value):
+        '''Set individuals parameters 
+        Throw TypeError, KeyError
+        '''
+        if index=='tenant':
+            self.reload_client=True
+            self.tenant = value
+            if value:
+                self.k_creds['tenant_name'] = value
+                self.n_creds['project_id']  = value
+            else:
+                del self.k_creds['tenant_name']
+                del self.n_creds['project_id']
+        elif index=='user':
+            self.reload_client=True
+            self.user = value
+            if value:
+                self.k_creds['username'] = value
+                self.n_creds['username'] = value
+            else:
+                del self.k_creds['username']
+                del self.n_creds['username']
+        elif index=='passwd':
+            self.reload_client=True
+            self.passwd = value
+            if value:
+                self.k_creds['password'] = value
+                self.n_creds['api_key']  = value
+            else:
+                del self.k_creds['password']
+                del self.n_creds['api_key']
+        elif index=='url':
+            self.reload_client=True
+            self.url = value
+            if value:
+                self.k_creds['auth_url'] = value
+                self.n_creds['auth_url'] = value
+            else:
+                raise TypeError, 'url param can not be NoneType'
+        else:
+            vimconn.vimconnector.__setitem__(self,index, value)
+     
+    def _reload_connection(self):
+        '''Called before any operation, it check if credentials has changed
+        Throw keystoneclient.apiclient.exceptions.AuthorizationFailure
+        '''
+        #TODO control the timing and possible token timeout, but it seams that python client does this task for us :-) 
+        if self.reload_client:
+            #test valid params
+            if len(self.n_creds) <4:
+                raise ksExceptions.ClientException("Not enough parameters to connect to openstack")
+            self.nova = nClient.Client(2, **self.n_creds)
+            self.keystone = ksClient.Client(**self.k_creds)
+            self.glance_endpoint = self.keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL')
+            self.glance = glClient.Client(self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds)  #TODO check k_creds vs n_creds
+            self.ne_endpoint=self.keystone.service_catalog.url_for(service_type='network', endpoint_type='publicURL')
+            self.neutron = neClient.Client('2.0', endpoint_url=self.ne_endpoint, token=self.keystone.auth_token, **self.k_creds)
+            self.reload_client = False
+    
+    def new_external_port(self, port_data):
+        #TODO openstack if needed
+        '''Adds a external port to VIM'''
+        '''Returns the port identifier'''
+        return -vimconn.HTTP_Internal_Server_Error, "osconnector.new_external_port() not implemented" 
+        
+    def connect_port_network(self, port_id, network_id, admin=False):
+        #TODO openstack if needed
+        '''Connects a external port to a network'''
+        '''Returns status code of the VIM response'''
+        return -vimconn.HTTP_Internal_Server_Error, "osconnector.connect_port_network() not implemented" 
+    
+    def new_user(self, user_name, user_passwd, tenant_id=None):
+        '''Adds a new user to openstack VIM'''
+        '''Returns the user identifier'''
+        if self.debug:
+            print "osconnector: Adding a new user to VIM"
+        try:
+            self._reload_connection()
+            user=self.keystone.users.create(user_name, user_passwd, tenant_id=tenant_id)
+            #self.keystone.tenants.add_user(self.k_creds["username"], #role)
+            return 1, user.id
+        except ksExceptions.ConnectionError as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        except ksExceptions.ClientException as e: #TODO remove
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "new_tenant " + error_text
+        return error_value, error_text        
+
+    def delete_user(self, user_id):
+        '''Delete a user from openstack VIM'''
+        '''Returns the user identifier'''
+        if self.debug:
+            print "osconnector: Deleting  a  user from VIM"
+        try:
+            self._reload_connection()
+            self.keystone.users.delete(user_id)
+            return 1, user_id
+        except ksExceptions.ConnectionError as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        except ksExceptions.NotFound as e:
+            error_value=-vimconn.HTTP_Not_Found
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        except ksExceptions.ClientException as e: #TODO remove
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "delete_tenant " + error_text
+        return error_value, error_text
+        
+    def new_tenant(self,tenant_name,tenant_description):
+        '''Adds a new tenant to openstack VIM'''
+        '''Returns the tenant identifier'''
+        if self.debug:
+            print "osconnector: Adding a new tenant to VIM"
+        try:
+            self._reload_connection()
+            tenant=self.keystone.tenants.create(tenant_name, tenant_description)
+            #self.keystone.tenants.add_user(self.k_creds["username"], #role)
+            return 1, tenant.id
+        except ksExceptions.ConnectionError as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        except ksExceptions.ClientException as e: #TODO remove
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "new_tenant " + error_text
+        return error_value, error_text
+
+    def delete_tenant(self,tenant_id):
+        '''Delete a tenant from openstack VIM'''
+        '''Returns the tenant identifier'''
+        if self.debug:
+            print "osconnector: Deleting  a  tenant from VIM"
+        try:
+            self._reload_connection()
+            self.keystone.tenants.delete(tenant_id)
+            #self.keystone.tenants.add_user(self.k_creds["username"], #role)
+            return 1, tenant_id
+        except ksExceptions.ConnectionError as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        except ksExceptions.ClientException as e: #TODO remove
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "delete_tenant " + error_text
+        return error_value, error_text
+
+    def __net_os2mano(self, net_list_dict):
+        '''Transform the net openstack format to mano format
+        net_list_dict can be a list of dict or a single dict'''
+        if type(net_list_dict) is dict:
+            net_list_=(net_list_dict,)
+        elif type(net_list_dict) is list:
+            net_list_=net_list_dict
+        else:
+            raise TypeError("param net_list_dict must be a list or a dictionary")
+        for net in net_list_:
+            if net.get('provider:network_type') == "vlan":
+                net['type']='data'
+            else:
+                net['type']='bridge'
+        
+    def new_tenant_network(self,net_name,net_type,public=False,cidr=None,vlan=None):
+        '''Adds a tenant network to VIM'''
+        '''Returns the network identifier'''
+        if self.debug:
+            print "osconnector: Adding a new tenant network to VIM (tenant: " + self.tenant + ", type: " + net_type + "): "+ net_name
+        try:
+            self._reload_connection()
+            network_dict = {'name': net_name, 'admin_state_up': True}
+            if net_type=="data" or net_type=="ptp":
+                if self.config.get('dataplane_physical_net') == None:
+                    return -vimconn.HTTP_Bad_Request, "You must provide a 'dataplane_physical_net' at config value before creating sriov network "
+                    
+                network_dict["provider:physical_network"] = self.config['dataplane_physical_net'] #"physnet_sriov" #TODO physical
+                network_dict["provider:network_type"]     = "vlan"
+                if vlan!=None:
+                    network_dict["provider:network_type"] = vlan
+            network_dict["shared"]=public
+            new_net=self.neutron.create_network({'network':network_dict})
+            #print new_net
+            #create fake subnetwork
+            if not cidr:
+                cidr="192.168.111.0/24"
+            subnet={"name":net_name+"-subnet",
+                    "network_id": new_net["network"]["id"],
+                    "ip_version": 4,
+                    "cidr": cidr
+                    }
+            self.neutron.create_subnet({"subnet": subnet} )
+            return 1, new_net["network"]["id"]
+        except neExceptions.ConnectionFailed as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        except (ksExceptions.ClientException, neExceptions.NeutronException) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "new_tenant_network " + error_text
+        return error_value, error_text
+
+    def get_network_list(self, filter_dict={}):
+        '''Obtain tenant networks of VIM
+        Filter_dict can be:
+            name: network name
+            id: network uuid
+            shared: boolean
+            tenant_id: tenant
+            admin_state_up: boolean
+            status: 'ACTIVE'
+        Returns the network list of dictionaries
+        '''
+        if self.debug:
+            print "osconnector.get_network_list(): Getting network from VIM (filter: " + str(filter_dict) + "): "
+        try:
+            self._reload_connection()
+            net_dict=self.neutron.list_networks(**filter_dict)
+            net_list=net_dict["networks"]
+            self.__net_os2mano(net_list)
+            return 1, net_list
+        except neClient.exceptions.ConnectionFailed as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        except (ksExceptions.ClientException, neExceptions.NeutronException) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "get_network_list " + error_text
+        return error_value, error_text
+
+    def get_tenant_network(self, net_id, tenant_id=None):
+        '''Obtain tenant networks of VIM'''
+        '''Returns the network information from a network id'''
+        if self.debug:
+            print "osconnector.get_tenant_network(): Getting tenant network %s from VIM" % net_id
+        filter_dict={"id": net_id}
+        if tenant_id:
+            filter_dict["tenant_id"] = tenant_id
+        r, net_list = self.get_network_list(filter_dict)
+        if r<0:
+            return r, net_list
+        if len(net_list)==0:
+            return -vimconn.HTTP_Not_Found, "Network '%s' not found" % net_id
+        elif len(net_list)>1:
+            return -vimconn.HTTP_Conflict, "Found more than one network with this criteria"
+        net = net_list[0]
+        subnets=[]
+        for subnet_id in net.get("subnets", () ):
+            try:
+                subnet = self.neutron.show_subnet(subnet_id)
+            except Exception as e:
+                error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+                print "osconnector.get_tenant_network(): Error getting subnet %s %s" % (net_id, error_text)
+                subnet = {"id": subnet_id, "fault": error_text}
+            subnets.append(subnet)
+        net["subnets"] = subnets
+        return 1, net
+
+
+    def delete_tenant_network(self, net_id):
+        '''Deletes a tenant network from VIM'''
+        '''Returns the network identifier'''
+        if self.debug:
+            print "osconnector: Deleting a new tenant network from VIM tenant: " + self.tenant + ", id: " + net_id
+        try:
+            self._reload_connection()
+            #delete VM ports attached to this networks before the network
+            ports = self.neutron.list_ports(network_id=net_id)
+            for p in ports['ports']:
+                try:
+                    self.neutron.delete_port(p["id"])
+                except Exception as e:
+                    print "Error deleting port: " + type(e).__name__ + ": "+  str(e)
+            self.neutron.delete_network(net_id)
+            return 1, net_id
+        except neClient.exceptions.ConnectionFailed as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        except neExceptions.NetworkNotFoundClient as e:
+            error_value=-vimconn.HTTP_Not_Found
+            error_text= type(e).__name__ + ": "+  str(e.args[0])
+        except (ksExceptions.ClientException, neExceptions.NeutronException) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "delete_tenant_network " + error_text
+        return error_value, error_text
+
+    def get_tenant_flavor(self, flavor_id):
+        '''Obtain flavor details from the  VIM
+            Returns the flavor dict details
+        '''
+        print "VIMConnector: Getting flavor from VIM"
+        try:
+            self._reload_connection()
+            flavor = self.nova.flavors.find(id=flavor_id)
+            #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
+            return 1, {"flavor": flavor.to_dict()}
+        except nvExceptions.NotFound as e:
+            error_value=-vimconn.HTTP_Not_Found
+            error_text= "flavor instance %s not found" % flavor_id
+        except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "get_tenant_flavor " + error_text
+        return error_value, error_text        
+
+    def new_tenant_flavor(self, flavor_dict, change_name_if_used=True):
+        '''Adds a tenant flavor to openstack VIM
+        if change_name_if_used is True, it will change name in case of conflict
+        Returns the flavor identifier
+        '''
+        retry=0
+        name_suffix = 0
+        name=flavor_dict['name']
+        while retry<2:
+            retry+=1
+            try:
+                self._reload_connection()
+                if change_name_if_used:
+                    #get used names
+                    fl_names=[]
+                    fl=self.nova.flavors.list()
+                    for f in fl:
+                        fl_names.append(f.name)
+                    while name in fl_names:
+                        name_suffix += 1
+                        name = flavor_dict['name']+"-" + str(name_suffix)
+                        
+                ram = flavor_dict.get('ram',64)
+                vcpus = flavor_dict.get('vcpus',1)
+                numa_properties=None
+
+                extended=flavor_dict.get("extended")
+                if extended:
+                    numas=extended.get("numas")
+                    if numas:
+                        numa_nodes = len(numas)
+                        if numa_nodes > 1:
+                            return -1, "Can not add flavor with more than one numa"
+                        numa_properties = {"hw:numa_nodes":str(numa_nodes)}
+                        numa_properties["hw:mem_page_size"] = "large"
+                        numa_properties["hw:cpu_policy"] = "dedicated"
+                        numa_properties["hw:numa_mempolicy"] = "strict"
+                        for numa in numas:
+                            #overwrite ram and vcpus
+                            ram = numa['memory']*1024
+                            if 'paired-threads' in numa:
+                                vcpus = numa['paired-threads']*2
+                                numa_properties["hw:cpu_threads_policy"] = "prefer"
+                            elif 'cores' in numa:
+                                vcpus = numa['cores']
+                                #numa_properties["hw:cpu_threads_policy"] = "prefer"
+                            elif 'threads' in numa:
+                                vcpus = numa['threads']
+                                numa_properties["hw:cpu_policy"] = "isolated"
+                            for interface in numa.get("interfaces",() ):
+                                if interface["dedicated"]=="yes":
+                                    error_value=-vimconn.HTTP_Bad_Request
+                                    error_text= "Passthrough interfaces are not supported for the openstack connector"
+                                    break
+                                #TODO, add the key 'pci_passthrough:alias"="<label at config>:<number ifaces>"' when a way to connect it is available
+                                
+                #create flavor                 
+                new_flavor=self.nova.flavors.create(name, 
+                                ram, 
+                                vcpus, 
+                                flavor_dict.get('disk',1),
+                                is_public=flavor_dict.get('is_public', True)
+                            ) 
+                #add metadata
+                if numa_properties:
+                    new_flavor.set_keys(numa_properties)
+                return 1, new_flavor.id
+            except nvExceptions.Conflict as e:
+                error_value=-vimconn.HTTP_Conflict
+                error_text= str(e)
+                if change_name_if_used:
+                    continue
+                break
+            #except nvExceptions.BadRequest as e:
+            except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError) as e:
+                error_value=-vimconn.HTTP_Bad_Request
+                error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+                break
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "new_tenant_flavor " + error_text
+        return error_value, error_text
+
+    def delete_tenant_flavor(self,flavor_id):
+        '''Deletes a tenant flavor from openstack VIM
+           Returns >0,id if ok; or <0,error_text if error
+        '''
+        retry=0
+        while retry<2:
+            retry+=1
+            try:
+                self._reload_connection()
+                self.nova.flavors.delete(flavor_id)
+                return 1, flavor_id
+            except nvExceptions.NotFound as e:
+                error_value = -vimconn.HTTP_Not_Found
+                error_text  = "flavor '%s' not found" % flavor_id
+                break
+            #except nvExceptions.BadRequest as e:
+            except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
+                error_value=-vimconn.HTTP_Bad_Request
+                error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+                break
+        if self.debug:
+            print "delete_tenant_flavor " + error_text
+        #if reaching here is because an exception
+        return error_value, error_text
+
+    def new_tenant_image(self,image_dict):
+        '''
+        Adds a tenant image to VIM
+        if change_name_if_used is True, it will change name in case of conflict
+        Returns:
+            >1, image-id        if the image is created
+            <0, message          if there is an error
+        '''
+        retry=0
+        #using version 1 of glance client
+        glancev1 = gl1Client.Client('1',self.glance_endpoint, token=self.keystone.auth_token, **self.k_creds)  #TODO check k_creds vs n_creds
+        while retry<2:
+            retry+=1
+            try:
+                self._reload_connection()
+                #determine format  http://docs.openstack.org/developer/glance/formats.html
+                if "disk_format" in image_dict:
+                    disk_format=image_dict["disk_format"]
+                else: #autodiscover base on extention
+                    if image_dict['location'][-6:]==".qcow2":
+                        disk_format="qcow2"
+                    elif image_dict['location'][-4:]==".vhd":
+                        disk_format="vhd"
+                    elif image_dict['location'][-5:]==".vmdk":
+                        disk_format="vmdk"
+                    elif image_dict['location'][-4:]==".vdi":
+                        disk_format="vdi"
+                    elif image_dict['location'][-4:]==".iso":
+                        disk_format="iso"
+                    elif image_dict['location'][-4:]==".aki":
+                        disk_format="aki"
+                    elif image_dict['location'][-4:]==".ari":
+                        disk_format="ari"
+                    elif image_dict['location'][-4:]==".ami":
+                        disk_format="ami"
+                    else:
+                        disk_format="raw"
+                print "new_tenant_image: '%s' loading from '%s'" % (image_dict['name'], image_dict['location'])
+                if image_dict['location'][0:4]=="http":
+                    new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
+                            container_format="bare", location=image_dict['location'], disk_format=disk_format)
+                else: #local path
+                    with open(image_dict['location']) as fimage:
+                        new_image = glancev1.images.create(name=image_dict['name'], is_public=image_dict.get('public',"yes")=="yes",
+                            container_format="bare", data=fimage, disk_format=disk_format)
+                #insert metadata. We cannot use 'new_image.properties.setdefault' 
+                #because nova and glance are "INDEPENDENT" and we are using nova for reading metadata
+                new_image_nova=self.nova.images.find(id=new_image.id)
+                new_image_nova.metadata.setdefault('location',image_dict['location'])
+                metadata_to_load = image_dict.get('metadata')
+                if metadata_to_load:
+                    for k,v in yaml.load(metadata_to_load).iteritems():
+                        new_image_nova.metadata.setdefault(k,v)
+                return 1, new_image.id
+            except nvExceptions.Conflict as e:
+                error_value=-vimconn.HTTP_Conflict
+                error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+                break
+            except (HTTPException, gl1Exceptions.HTTPException, gl1Exceptions.CommunicationError) as e:
+                error_value=-vimconn.HTTP_Bad_Request
+                error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+                continue
+            except IOError as e:  #can not open the file
+                error_value=-vimconn.HTTP_Bad_Request
+                error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0])) + " for " + image_dict['location']
+                break
+            except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
+                error_value=-vimconn.HTTP_Bad_Request
+                error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+                break
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "new_tenant_image " + error_text
+        return error_value, error_text
+     
+    def delete_tenant_image(self, image_id):
+        '''Deletes a tenant image from openstack VIM
+        Returns >0,id if ok; or <0,error_text if error
+        '''
+        retry=0
+        while retry<2:
+            retry+=1
+            try:
+                self._reload_connection()
+                self.nova.images.delete(image_id)
+                return 1, image_id
+            except nvExceptions.NotFound as e:
+                error_value = -vimconn.HTTP_Not_Found
+                error_text  = "flavor '%s' not found" % image_id
+                break
+            #except nvExceptions.BadRequest as e:
+            except (ksExceptions.ClientException, nvExceptions.ClientException) as e: #TODO remove
+                error_value=-vimconn.HTTP_Bad_Request
+                error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+                break
+        if self.debug:
+            print "delete_tenant_image " + error_text
+        #if reaching here is because an exception
+        return error_value, error_text
+        
+    def new_tenant_vminstance(self,name,description,start,image_id,flavor_id,net_list):
+        '''Adds a VM instance to VIM
+        Params:
+            start: indicates if VM must start or boot in pause mode. Ignored
+            image_id,flavor_id: iamge and flavor uuid
+            net_list: list of interfaces, each one is a dictionary with:
+                name:
+                net_id: network uuid to connect
+                vpci: virtual vcpi to assign, ignored because openstack lack #TODO
+                model: interface model, ignored #TODO
+                mac_address: used for  SR-IOV ifaces #TODO for other types
+                use: 'data', 'bridge',  'mgmt'
+                type: 'virtual', 'PF', 'VF', 'VFnotShared'
+                vim_id: filled/added by this function
+                #TODO ip, security groups
+        Returns >=0, the instance identifier
+                <0, error_text
+        '''
+        if self.debug:
+            print "osconnector: Creating VM into VIM"
+            print "   image %s  flavor %s   nics=%s" %(image_id, flavor_id,net_list)
+        try:
+            metadata=[]
+            net_list_vim=[]
+            self._reload_connection()
+            metadata_vpci={}
+            for net in net_list:
+                if not net.get("net_id"): #skip non connected iface
+                    continue
+                if net["type"]=="virtual":
+                    net_list_vim.append({'net-id': net["net_id"]})
+                    if "vpci" in net:
+                        metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]]
+                elif net["type"]=="PF":
+                    print "new_tenant_vminstance: Warning, can not connect a passthrough interface "
+                    #TODO insert this when openstack consider passthrough ports as openstack neutron ports
+                else: #VF
+                    if "vpci" in net:
+                        if "VF" not in metadata_vpci:
+                            metadata_vpci["VF"]=[]
+                        metadata_vpci["VF"].append([ net["vpci"], "" ])
+                    port_dict={
+                         "network_id": net["net_id"],
+                         "name": net.get("name"),
+                         "binding:vnic_type": "direct", 
+                         "admin_state_up": True
+                    }
+                    if not port_dict["name"]:
+                        port_dict["name"] = name
+                    if net.get("mac_address"):
+                        port_dict["mac_address"]=net["mac_address"]
+                    #TODO: manage having SRIOV without vlan tag
+                    #if net["type"] == "VFnotShared"
+                    #    port_dict["vlan"]=0
+                    new_port = self.neutron.create_port({"port": port_dict })
+                    net["mac_adress"] = new_port["port"]["mac_address"]
+                    net["vim_id"] = new_port["port"]["id"]
+                    net["ip"] = new_port["port"].get("fixed_ips",[{}])[0].get("ip_address")
+                    net_list_vim.append({"port-id": new_port["port"]["id"]})
+            if metadata_vpci:
+                metadata = {"pci_assignement": json.dumps(metadata_vpci)}
+            
+            print "name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s' metadata %s"\
+                 % (name, image_id, flavor_id, str(net_list_vim), description, str(metadata))
+            
+            security_groups   = self.config.get('security_groups')
+            if type(security_groups) is str:
+                security_groups = ( security_groups, )
+            server = self.nova.servers.create(name, image_id, flavor_id, nics=net_list_vim, meta=metadata,
+                                              security_groups   = security_groups,
+                                              availability_zone = self.config.get('availability_zone'),
+                                              key_name          = self.config.get('keypair'),
+                                        ) #, description=description)
+            
+            
+            print "DONE :-)", server
+            
+#             #TODO   server.add_floating_ip("10.95.87.209")
+#             #To look for a free floating_ip
+#             free_floating_ip = None
+#             for floating_ip in self.neutron.list_floatingips().get("floatingips", () ):
+#                 if not floating_ip["port_id"]:
+#                     free_floating_ip = floating_ip["floating_ip_address"]
+#                     break
+#             if free_floating_ip:
+#                 server.add_floating_ip(free_floating_ip)
+                
+            
+            return 1, server.id
+#        except nvExceptions.NotFound as e:
+#            error_value=-vimconn.HTTP_Not_Found
+#            error_text= "vm instance %s not found" % vm_id
+        except (ksExceptions.ClientException, nvExceptions.ClientException, ConnectionError, TypeError) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        except neClient.exceptions.ConnectionFailed as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "new_tenant_vminstance Exception",e, error_text
+        return error_value, error_text    
+
+    def get_tenant_vminstance(self,vm_id):
+        '''Returns the VM instance information from VIM'''
+        if self.debug:
+            print "osconnector: Getting VM from VIM"
+        try:
+            self._reload_connection()
+            server = self.nova.servers.find(id=vm_id)
+            #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
+            return 1, {"server": server.to_dict()}
+        except nvExceptions.NotFound as e:
+            error_value=-vimconn.HTTP_Not_Found
+            error_text= "vm instance %s not found" % vm_id
+        except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "get_tenant_vminstance " + error_text
+        return error_value, error_text        
+                
+    def get_tenant_vminstance_console(self,vm_id, console_type="vnc"):
+        '''
+        Get a console for the virtual machine
+        Params:
+            vm_id: uuid of the VM
+            console_type, can be:
+                "novnc" (by default), "xvpvnc" for VNC types, 
+                "rdp-html5" for RDP types, "spice-html5" for SPICE types
+        Returns <0, text on error, for example not available
+                1, URL/text with the console parameters
+        '''
+        if self.debug:
+            print "osconnector: Getting VM CONSOLE from VIM"
+        try:
+            self._reload_connection()
+            server = self.nova.servers.find(id=vm_id)
+            if console_type == None or console_type == "novnc":
+                console_dict = server.get_vnc_console("novnc")
+            elif console_type == "xvpvnc":
+                console_dict = server.get_vnc_console(console_type)
+            elif console_type == "rdp-html5":
+                console_dict = server.get_rdp_console(console_type)
+            elif console_type == "spice-html5":
+                console_dict = server.get_spice_console(console_type)
+            else:
+                return -vimconn.HTTP_Bad_Request, "console type '%s' not allowed" % console_type
+            
+            console_dict1 = console_dict.get("console")
+            if console_dict1:
+                console_url = console_dict1.get("url")
+                if console_url:
+                    #parse console_url
+                    protocol_index = console_url.find("//")
+                    suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
+                    port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
+                    if protocol_index < 0 or port_index<0 or suffix_index<0:
+                        return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
+                    console_dict={"protocol": console_url[0:protocol_index],
+                                  "server":   console_url[protocol_index+2:port_index], 
+                                  "port":     console_url[port_index:suffix_index], 
+                                  "suffix":   console_url[suffix_index+1:] 
+                                  }
+                    protocol_index += 2
+                    return 1, console_dict
+            return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
+            
+            #TODO parse input and translate to VIM format (openmano_schemas.new_vminstance_response_schema)
+            return 1, {"server": server.to_dict()}
+        except nvExceptions.NotFound as e:
+            error_value=-vimconn.HTTP_Not_Found
+            error_text= "vm instance %s not found" % vm_id
+        except (ksExceptions.ClientException, nvExceptions.ClientException, nvExceptions.BadRequest) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "get_tenant_vminstance " + error_text
+        return error_value, error_text        
+
+
+    def delete_tenant_vminstance(self, vm_id):
+        '''Removes a VM instance from VIM
+        Returns >0, the instance identifier
+                <0, error_text
+        '''
+        if self.debug:
+            print "osconnector: Getting VM from VIM"
+        try:
+            self._reload_connection()
+            #delete VM ports attached to this networks before the virtual machine
+            ports = self.neutron.list_ports(device_id=vm_id)
+            for p in ports['ports']:
+                try:
+                    self.neutron.delete_port(p["id"])
+                except Exception as e:
+                    print "Error deleting port: " + type(e).__name__ + ": "+  str(e)
+            self.nova.servers.delete(vm_id)
+            return 1, vm_id
+        except nvExceptions.NotFound as e:
+            error_value=-vimconn.HTTP_Not_Found
+            error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
+        except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "get_tenant_vminstance " + error_text
+        return error_value, error_text        
+
+    def refresh_tenant_vms_and_nets(self, vmDict, netDict):
+        '''Refreshes the status of the dictionaries of VM instances and nets passed as arguments. It modifies the dictionaries
+        Returns:
+            - result: 0 if all elements could be refreshed (even if its status didn't change)
+                      n>0, the number of elements that couldn't be refreshed,
+                      <0 if error (foreseen)
+            - error_msg: text with reference to possible errors
+        '''
+        #vms_refreshed = []
+        #nets_refreshed = []
+        vms_unrefreshed = []
+        nets_unrefreshed = []
+        if self.debug:
+            print "osconnector refresh_tenant_vms and nets: Getting tenant VM instance information from VIM"
+        for vm_id in vmDict:
+            vmDict[vm_id] = {'error_msg':None, 'vim_info':None}
+            r,c = self.get_tenant_vminstance(vm_id)
+            if r<0:
+                print "osconnector refresh_tenant_vm. Error getting vm_id '%s' status: %s" % (vm_id, c)
+                if r==-vimconn.HTTP_Not_Found:
+                    vmDict[vm_id]['status'] = "DELETED" 
+                else:
+                    vmDict[vm_id]['status'] = "VIM_ERROR"
+                    vmDict[vm_id]['error_msg'] = c
+                    vms_unrefreshed.append(vm_id)
+            else:
+                try:
+                    vmDict[vm_id]['status']    =  vmStatus2manoFormat[ c['server']['status'] ]
+                    vmDict[vm_id]['vim_info']  = yaml.safe_dump(c['server'])
+                    vmDict[vm_id]["interfaces"] = []
+                    if c['server'].get('fault'):
+                        vmDict[vm_id]['error_msg'] = str(c['server']['fault'])
+                    #get interfaces
+                    try:
+                        self._reload_connection()
+                        port_dict=self.neutron.list_ports(device_id=vm_id)
+                        for port in port_dict["ports"]:
+                            interface={}
+                            interface['vim_info']  = yaml.safe_dump(port)
+                            interface["mac_address"] = port.get("mac_address")
+                            interface["vim_net_id"] = port["network_id"]
+                            interface["vim_interface_id"] = port["id"]
+                            ips=[]
+                            #look for floating ip address
+                            floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])
+                            if floating_ip_dict.get("floatingips"):
+                                ips.append(floating_ip_dict["floatingips"][0].get("floating_ip_address") )
+
+                            for subnet in port["fixed_ips"]:
+                                ips.append(subnet["ip_address"])
+                            interface["ip_address"] = ";".join(ips)
+                            vmDict[vm_id]["interfaces"].append(interface)
+                    except Exception as e:
+                        print type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+                
+                    
+                    #error message at server.fault["message"]
+                except KeyError as e:
+                    print "osconnector refresh_tenant_elements KeyError %s getting vm_id '%s' status  %s" % (str(e), vm_id, c['server']['status'])
+                    vmDict[vm_id]['status'] = "VIM_ERROR"
+                    vmDict[vm_id]['error_msg'] = str(e)
+                    vms_unrefreshed.append(vm_id)
+        
+        #print "VMs refreshed: %s" % str(vms_refreshed)
+        for net_id in netDict:
+            netDict[net_id]={'error_msg':None, 'vim_info':None}
+            r,c = self.get_tenant_network(net_id)
+            if r<0:
+                print "osconnector refresh_tenant_network. Error getting net_id '%s' status: %s" % (net_id, c)
+                if r==-vimconn.HTTP_Not_Found:
+                    netDict[net_id]['status'] = "DELETED" #TODO check exit status
+                else:
+                    netDict[vm_id]['status'] = "VIM_ERROR"
+                    netDict[vm_id]['error_msg'] = c
+                    nets_unrefreshed.append(net_id)
+            else:
+                try:
+                    netDict[net_id]['status'] = netStatus2manoFormat[ c['status'] ]
+                    if c['status'] == "ACIVE" and not c['admin_state_up']:
+                        netDict[net_id]['status'] = 'DOWN'
+                    netDict[net_id]['vim_info']  = yaml.safe_dump(c)
+                    if c.get('fault'):  #TODO
+                        netDict[net_id]['error_msg'] = str(c['fault'])
+                except KeyError as e:
+                    print "osconnector refresh_tenant_elements KeyError %s getting vm_id '%s' status  %s" % (str(e), vm_id, c['network']['status'])
+                    netDict[net_id]['status'] = "VIM_ERROR"
+                    netDict[net_id]['error_msg'] = str(e)
+                    nets_unrefreshed.append(net_id)
+
+        #print "Nets refreshed: %s" % str(nets_refreshed)
+        
+        error_msg=""
+        if len(vms_unrefreshed)+len(nets_unrefreshed)>0:
+            error_msg += "VMs unrefreshed: " + str(vms_unrefreshed) + "; nets unrefreshed: " + str(nets_unrefreshed)
+            print error_msg
+
+        #return len(vms_unrefreshed)+len(nets_unrefreshed), error_msg, vms_refreshed, nets_refreshed
+        return len(vms_unrefreshed)+len(nets_unrefreshed), error_msg
+    
+    def action_tenant_vminstance(self, vm_id, action_dict):
+        '''Send and action over a VM instance from VIM
+        Returns the status'''
+        if self.debug:
+            print "osconnector: Action over VM instance from VIM " + vm_id
+        try:
+            self._reload_connection()
+            server = self.nova.servers.find(id=vm_id)
+            if "start" in action_dict:
+                if action_dict["start"]=="rebuild":  
+                    server.rebuild()
+                else:
+                    if server.status=="PAUSED":
+                        server.unpause()
+                    elif server.status=="SUSPENDED":
+                        server.resume()
+                    elif server.status=="SHUTOFF":
+                        server.start()
+            elif "pause" in action_dict:
+                server.pause()
+            elif "resume" in action_dict:
+                server.resume()
+            elif "shutoff" in action_dict or "shutdown" in action_dict:
+                server.stop()
+            elif "forceOff" in action_dict:
+                server.stop() #TODO
+            elif "terminate" in action_dict:
+                server.delete()
+            elif "createImage" in action_dict:
+                server.create_image()
+                #"path":path_schema,
+                #"description":description_schema,
+                #"name":name_schema,
+                #"metadata":metadata_schema,
+                #"imageRef": id_schema,
+                #"disk": {"oneOf":[{"type": "null"}, {"type":"string"}] },
+            elif "rebuild" in action_dict:
+                server.rebuild(server.image['id'])
+            elif "reboot" in action_dict:
+                server.reboot() #reboot_type='SOFT'
+            elif "console" in action_dict:
+                console_type = action_dict["console"]
+                if console_type == None or console_type == "novnc":
+                    console_dict = server.get_vnc_console("novnc")
+                elif console_type == "xvpvnc":
+                    console_dict = server.get_vnc_console(console_type)
+                elif console_type == "rdp-html5":
+                    console_dict = server.get_rdp_console(console_type)
+                elif console_type == "spice-html5":
+                    console_dict = server.get_spice_console(console_type)
+                else:
+                    return -vimconn.HTTP_Bad_Request, "console type '%s' not allowed" % console_type
+                
+                try:
+                    console_url = console_dict["console"]["url"]
+                    #parse console_url
+                    protocol_index = console_url.find("//")
+                    suffix_index = console_url[protocol_index+2:].find("/") + protocol_index+2
+                    port_index = console_url[protocol_index+2:suffix_index].find(":") + protocol_index+2
+                    if protocol_index < 0 or port_index<0 or suffix_index<0:
+                        print "action_tenant_vminstance, console: response", str(console_dict)
+                        return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
+                    console_dict2={"protocol": console_url[0:protocol_index],
+                                  "server":   console_url[protocol_index+2 : port_index], 
+                                  "port":     int(console_url[port_index+1 : suffix_index]), 
+                                  "suffix":   console_url[suffix_index+1:] 
+                                  }
+                    protocol_index += 2
+                    return 1, console_dict2               
+                except:
+                    print "action_tenant_vminstance, console: response", str(console_dict)
+                    return -vimconn.HTTP_Internal_Server_Error, "Unexpected response from VIM"
+            
+            return 1, vm_id
+        except nvExceptions.NotFound as e:
+            error_value=-vimconn.HTTP_Not_Found
+            error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
+        except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "action_tenant_vminstance " + error_text
+        return error_value, error_text        
+        
+    def get_hosts_info(self):
+        '''Get the information of deployed hosts
+        Returns the hosts content'''
+        if self.debug:
+            print "osconnector: Getting Host info from VIM"
+        try:
+            h_list=[]
+            self._reload_connection()
+            hypervisors = self.nova.hypervisors.list()
+            for hype in hypervisors:
+                h_list.append( hype.to_dict() )
+            return 1, {"hosts":h_list}
+        except nvExceptions.NotFound as e:
+            error_value=-vimconn.HTTP_Not_Found
+            error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
+        except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "get_hosts_info " + error_text
+        return error_value, error_text        
+
+    def get_hosts(self, vim_tenant):
+        '''Get the hosts and deployed instances
+        Returns the hosts content'''
+        r, hype_dict = self.get_hosts_info()
+        if r<0:
+            return r, hype_dict
+        hypervisors = hype_dict["hosts"]
+        try:
+            servers = self.nova.servers.list()
+            for hype in hypervisors:
+                for server in servers:
+                    if server.to_dict()['OS-EXT-SRV-ATTR:hypervisor_hostname']==hype['hypervisor_hostname']:
+                        if 'vm' in hype:
+                            hype['vm'].append(server.id)
+                        else:
+                            hype['vm'] = [server.id]
+            return 1, hype_dict
+        except nvExceptions.NotFound as e:
+            error_value=-vimconn.HTTP_Not_Found
+            error_text= (str(e) if len(e.args)==0 else str(e.args[0]))
+        except (ksExceptions.ClientException, nvExceptions.ClientException) as e:
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        #TODO insert exception vimconn.HTTP_Unauthorized
+        #if reaching here is because an exception
+        if self.debug:
+            print "get_hosts " + error_text
+        return error_value, error_text        
+  
+    def get_image_id_from_path(self, path):
+        '''Get the image id from image path in the VIM database'''
+        '''Returns:
+             0,"Image not found"   if there are no images with that path
+             1,image-id            if there is one image with that path
+             <0,message            if there was an error (Image not found, error contacting VIM, more than 1 image with that path, etc.) 
+        '''
+        try:
+            self._reload_connection()
+            images = self.nova.images.list()
+            for image in images:
+                if image.metadata.get("location")==path:
+                    return 1, image.id
+            return 0, "image with location '%s' not found" % path
+        except (ksExceptions.ClientException, nvExceptions.ClientException) as e: #TODO remove
+            error_value=-vimconn.HTTP_Bad_Request
+            error_text= type(e).__name__ + ": "+  (str(e) if len(e.args)==0 else str(e.args[0]))
+        if self.debug:
+            print "get_image_id_from_path " + error_text
+        #if reaching here is because an exception
+        return error_value, error_text
+        
+
diff --git a/vimconn_openvim.py b/vimconn_openvim.py
new file mode 100755 (executable)
index 0000000..6dfd474
--- /dev/null
@@ -0,0 +1,1386 @@
+# -*- 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
+##
+
+'''
+vimconnector implements all the methods to interact with openvim using the openvim API.
+
+For interacting with Openstack refer to osconnector.
+'''
+__author__="Alfonso Tierno, Gerardo Garcia"
+__date__ ="$26-aug-2014 11:09:29$"
+
+import vimconn
+import requests
+import json
+import yaml
+from openmano_schemas import id_schema, name_schema, nameshort_schema, description_schema, \
+                            vlan1000_schema, integer0_schema
+from jsonschema import validate as js_v, exceptions as js_e
+
+'''contain the openvim virtual machine status to openmano status'''
+vmStatus2manoFormat={'ACTIVE':'ACTIVE',
+                     'PAUSED':'PAUSED',
+                     'SUSPENDED': 'SUSPENDED',
+                     'INACTIVE':'INACTIVE',
+                     'CREATING':'BUILD',
+                     'ERROR':'ERROR','DELETED':'DELETED'
+                     }
+netStatus2manoFormat={'ACTIVE':'ACTIVE','INACTIVE':'INACTIVE','BUILD':'BUILD','ERROR':'ERROR','DELETED':'DELETED', 'DOWN':'DOWN'
+                     }
+
+
+host_schema = {
+    "type":"object",
+    "properties":{
+        "id": id_schema,
+        "name": name_schema,
+    },
+    "required": ["id"]
+}
+image_schema = {
+    "type":"object",
+    "properties":{
+        "id": id_schema,
+        "name": name_schema,
+    },
+    "required": ["id","name"]
+}
+server_schema = {
+    "type":"object",
+    "properties":{
+        "id":id_schema,
+        "name": name_schema,
+    },
+    "required": ["id","name"]
+}
+new_host_response_schema = {
+    "title":"host response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "host": host_schema
+    },
+    "required": ["host"],
+    "additionalProperties": False
+}
+
+get_images_response_schema = {
+    "title":"openvim images response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "images":{
+            "type":"array",
+            "items": image_schema,
+        }
+    },
+    "required": ["images"],
+    "additionalProperties": False
+}
+
+get_hosts_response_schema = {
+    "title":"openvim hosts response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "hosts":{
+            "type":"array",
+            "items": host_schema,
+        }
+    },
+    "required": ["hosts"],
+    "additionalProperties": False
+}
+
+get_host_detail_response_schema = new_host_response_schema # TODO: Content is not parsed yet
+
+get_server_response_schema = {
+    "title":"openvim server response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "servers":{
+            "type":"array",
+            "items": server_schema,
+        }
+    },
+    "required": ["servers"],
+    "additionalProperties": False
+}
+
+new_tenant_response_schema = {
+    "title":"tenant response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "tenant":{
+            "type":"object",
+            "properties":{
+                "id": id_schema,
+                "name": nameshort_schema,
+                "description":description_schema,
+                "enabled":{"type" : "boolean"}
+            },
+            "required": ["id"]
+        }
+    },
+    "required": ["tenant"],
+    "additionalProperties": False
+}
+
+new_network_response_schema = {
+    "title":"network response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "network":{
+            "type":"object",
+            "properties":{
+                "id":id_schema,
+                "name":name_schema,
+                "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
+                "shared":{"type":"boolean"},
+                "tenant_id":id_schema,
+                "admin_state_up":{"type":"boolean"},
+                "vlan":vlan1000_schema
+            },
+            "required": ["id"]
+        }
+    },
+    "required": ["network"],
+    "additionalProperties": False
+}
+
+
+# get_network_response_schema = {
+#     "title":"get network response information schema",
+#     "$schema": "http://json-schema.org/draft-04/schema#",
+#     "type":"object",
+#     "properties":{
+#         "network":{
+#             "type":"object",
+#             "properties":{
+#                 "id":id_schema,
+#                 "name":name_schema,
+#                 "type":{"type":"string", "enum":["bridge_man","bridge_data","data", "ptp"]},
+#                 "shared":{"type":"boolean"},
+#                 "tenant_id":id_schema,
+#                 "admin_state_up":{"type":"boolean"},
+#                 "vlan":vlan1000_schema
+#             },
+#             "required": ["id"]
+#         }
+#     },
+#     "required": ["network"],
+#     "additionalProperties": False
+# }
+
+
+new_port_response_schema = {
+    "title":"port response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "port":{
+            "type":"object",
+            "properties":{
+                "id":id_schema,
+            },
+            "required": ["id"]
+        }
+    },
+    "required": ["port"],
+    "additionalProperties": False
+}
+
+get_flavor_response_schema = {
+    "title":"openvim flavors response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "flavor":{
+            "type":"object",
+            "properties":{
+                "id":   id_schema,
+                "name": name_schema,
+                "extended": {"type":"object"},
+            },
+            "required": ["id", "name"],
+        }
+    },
+    "required": ["flavor"],
+    "additionalProperties": False
+}
+
+new_flavor_response_schema = {
+    "title":"flavor response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "flavor":{
+            "type":"object",
+            "properties":{
+                "id":id_schema,
+            },
+            "required": ["id"]
+        }
+    },
+    "required": ["flavor"],
+    "additionalProperties": False
+}
+
+new_image_response_schema = {
+    "title":"image response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "image":{
+            "type":"object",
+            "properties":{
+                "id":id_schema,
+            },
+            "required": ["id"]
+        }
+    },
+    "required": ["image"],
+    "additionalProperties": False
+}
+
+new_vminstance_response_schema = {
+    "title":"server response information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "server":{
+            "type":"object",
+            "properties":{
+                "id":id_schema,
+            },
+            "required": ["id"]
+        }
+    },
+    "required": ["server"],
+    "additionalProperties": False
+}
+
+get_processor_rankings_response_schema = {
+    "title":"processor rankings information schema",
+    "$schema": "http://json-schema.org/draft-04/schema#",
+    "type":"object",
+    "properties":{
+        "rankings":{
+            "type":"array",
+            "items":{
+                "type":"object",
+                "properties":{
+                    "model": description_schema,
+                    "value": integer0_schema
+                },
+                "additionalProperties": False,
+                "required": ["model","value"]
+            }
+        },
+        "additionalProperties": False,
+        "required": ["rankings"]
+    }
+}
+
+class vimconnector(vimconn.vimconnector):
+    def __init__(self, uuid, name, tenant, url, url_admin=None, user=None, passwd=None,debug=True,config={}):
+        vimconn.vimconnector.__init__(self, uuid, name, tenant, url, url_admin, user, passwd, debug, config)
+    
+    def _format_jsonerror(self,http_response):
+        try:
+            data = http_response.json()
+            return data["error"]["description"]
+        except:
+            return http_response.text
+
+    def _format_in(self, 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(self, data, schema):
+        deleted=[]
+        if type(data) is tuple or type(data) is list:
+            for d in data:
+                a= self._remove_extra_items(d, schema['items'])
+                if a is not None: deleted.append(a)
+        elif type(data) is dict:
+            for k in data.keys():
+                if 'properties' not in schema or k not in schema['properties'].keys():
+                    del data[k]
+                    deleted.append(k)
+                else:
+                    a = self._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 new_host(self, host_data):
+        '''Adds a new host to VIM'''
+        '''Returns status code of the VIM response'''
+        print "VIMConnector: Adding a new host"
+        headers_req = {'content-type': 'application/json'}
+        payload_req = host_data
+        try:
+            vim_response = requests.post(self.url_admin+'/hosts', headers = headers_req, data=payload_req)
+        except requests.exceptions.RequestException, e:
+            print "new_host Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+        #print vim_response.json()
+        #print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, new_host_response_schema)
+            #print http_content
+            if res :
+                r = self._remove_extra_items(http_content, new_host_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                host_id = http_content['host']['id']
+                #print "Host id: ",host_id
+                return vim_response.status_code,host_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+        else:
+            #print vim_response.text
+            jsonerror = self.__format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to add new host. HTTP Response: %d. Error: %s' % (self.url_admin, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+    
+    def new_external_port(self, port_data):
+        '''Adds a external port to VIM'''
+        '''Returns the port identifier'''
+        print "VIMConnector: Adding a new external port"
+        headers_req = {'content-type': 'application/json'}
+        payload_req = port_data
+        try:
+            vim_response = requests.post(self.url_admin+'/ports', headers = headers_req, data=payload_req)
+        except requests.exceptions.RequestException, e:
+            print "new_external_port Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+        #print vim_response.json()
+        #print json.dumps(vim_response.json(), indent=4)
+            res, http_content = self.__format_in(vim_response, new_port_response_schema)
+        #print http_content
+            if res:
+                r = self._remove_extra_items(http_content, new_port_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                port_id = http_content['port']['id']
+                print "Port id: ",port_id
+                return vim_response.status_code,port_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to add new external port. HTTP Response: %d. Error: %s' % (self.url_admin, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+        
+    def new_external_network(self,net_name,net_type):
+        '''Adds a external network to VIM (shared)'''
+        '''Returns the network identifier'''
+        print "VIMConnector: Adding external shared network to VIM (type " + net_type + "): "+ net_name
+        
+        headers_req = {'content-type': 'application/json'}
+        payload_req = '{"network":{"name": "' + net_name + '","shared":true,"type": "' + net_type + '"}}'
+        try:
+            vim_response = requests.post(self.url+'/networks', headers = headers_req, data=payload_req)
+        except requests.exceptions.RequestException, e:
+            print "new_external_network Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, new_network_response_schema)
+            #print http_content
+            if res:
+                r = self._remove_extra_items(http_content, new_network_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                network_id = http_content['network']['id']
+                print "Network id: ",network_id
+                return vim_response.status_code,network_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to add new external network. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+        
+    def connect_port_network(self, port_id, network_id, admin=False):
+        '''Connects a external port to a network'''
+        '''Returns status code of the VIM response'''
+        print "VIMConnector: Connecting external port to network"
+        
+        headers_req = {'content-type': 'application/json'}
+        payload_req = '{"port":{"network_id":"' + network_id + '"}}'
+        if admin:
+            if self.url_admin==None:
+                return -vimconn.HTTP_Unauthorized, "datacenter cannot contain  admin URL"
+            url= self.url_admin
+        else:
+            url= self.url
+        try:
+            vim_response = requests.put(url +'/ports/'+port_id, headers = headers_req, data=payload_req)
+        except requests.exceptions.RequestException, e:
+            print "connect_port_network Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, new_port_response_schema)
+            #print http_content
+            if res:
+                r = self._remove_extra_items(http_content, new_port_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                port_id = http_content['port']['id']
+                print "Port id: ",port_id
+                return vim_response.status_code,port_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+        else:
+            print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to connect external port to network. HTTP Response: %d. Error: %s' % (self.url_admin, vim_response.status_code, jsonerror)
+            print text
+            return -vim_response.status_code,text
+        
+    def new_tenant(self,tenant_name,tenant_description):
+        '''Adds a new tenant to VIM'''
+        '''Returns the tenant identifier'''
+        print "VIMConnector: Adding a new tenant to VIM"
+        headers_req = {'content-type': 'application/json'}
+        payload_dict = {"tenant": {"name":tenant_name,"description": tenant_description, "enabled": True}}
+        payload_req = json.dumps(payload_dict)
+        #print payload_req
+
+        try:
+            vim_response = requests.post(self.url+'/tenants', headers = headers_req, data=payload_req)
+        except requests.exceptions.RequestException, e:
+            print "new_tenant Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        #print vim_response
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, new_tenant_response_schema)
+            #print http_content
+            if res:
+                r = self._remove_extra_items(http_content, new_tenant_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                tenant_id = http_content['tenant']['id']
+                #print "Tenant id: ",tenant_id
+                return vim_response.status_code,tenant_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to add new tenant. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+
+    def delete_tenant(self,tenant_id,):
+        '''Delete a tenant from VIM'''
+        '''Returns the tenant identifier'''
+        print "VIMConnector: Deleting  a  tenant from VIM"
+        headers_req = {'content-type': 'application/json'}
+        try:
+            vim_response = requests.delete(self.url+'/tenants/'+tenant_id, headers = headers_req)
+        except requests.exceptions.RequestException, e:
+            print "delete_tenant Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        #print vim_response
+        if vim_response.status_code == 200:
+            return vim_response.status_code,tenant_id
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to delete tenant. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+
+    def new_tenant_network(self,net_name,net_type,public=False, **vim_specific):
+        '''Adds a tenant network to VIM'''
+        '''Returns the network identifier'''
+        print "vim_specific", vim_specific
+        if net_type=="bridge":
+            net_type="bridge_data"
+        print "VIMConnector: Adding a new tenant network to VIM (tenant: " + self.tenant + ", type: " + net_type + "): "+ net_name
+
+        headers_req = {'content-type': 'application/json'}
+        payload_req = {"name": net_name, "type": net_type, "tenant_id": self.tenant, "shared": public}
+        payload_req.update(vim_specific)
+        try:
+            vim_response = requests.post(self.url+'/networks', headers = headers_req, data=json.dumps({"network": payload_req}) )
+        except requests.exceptions.RequestException, e:
+            print "new_tenant_network Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, new_network_response_schema)
+            #print http_content
+            if res:
+                r = self._remove_extra_items(http_content, new_network_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                network_id = http_content['network']['id']
+                print "Tenant Network id: ",network_id
+                return vim_response.status_code,network_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to add new tenant network. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+
+    def get_tenant_list(self, filter_dict={}):
+        '''Obtain tenants of VIM
+        Filter_dict can be:
+            name: network name
+            id: network uuid
+        Returns the network list of dictionaries
+        '''
+        print "VIMConnector.get_tenant_list: Getting tenants from VIM (filter: " + str(filter_dict) + "): "
+        filterquery=[]
+        filterquery_text=''
+        for k,v in filter_dict.iteritems():
+            filterquery.append(str(k)+'='+str(v))
+        if len(filterquery)>0:
+            filterquery_text='?'+ '&'.join(filterquery)
+        headers_req = {'content-type': 'application/json'}
+        try:
+            print self.url+'/tenants'+filterquery_text
+            vim_response = requests.get(self.url+'/tenants'+filterquery_text, headers = headers_req)
+        except requests.exceptions.RequestException, e:
+            print "get_tenant_list Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            #TODO: parse input datares,http_content = self._format_in(vim_response, new_network_response_schema)
+            #print http_content
+            return vim_response.status_code, vim_response.json()["tenants"]
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to get tenant list. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+        
+    def get_network_list(self, filter_dict={}):
+        '''Obtain tenant networks of VIM
+        Filter_dict can be:
+            name: network name
+            id: network uuid
+            shared: boolean
+            tenant_id: tenant
+            admin_state_up: boolean
+            status: 'ACTIVE'
+        Returns the network list of dictionaries
+        '''
+        print "VIMConnector.get_network_list: Getting tenant network from VIM (filter: " + str(filter_dict) + "): "
+        filterquery=[]
+        filterquery_text=''
+        for k,v in filter_dict.iteritems():
+            filterquery.append(str(k)+'='+str(v))
+        if len(filterquery)>0:
+            filterquery_text='?'+ '&'.join(filterquery)
+        headers_req = {'content-type': 'application/json'}
+        try:
+            print self.url+'/networks'+filterquery_text
+            vim_response = requests.get(self.url+'/networks'+filterquery_text, headers = headers_req)
+        except requests.exceptions.RequestException, e:
+            print "get_network_list Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            #TODO: parse input datares,http_content = self._format_in(vim_response, new_network_response_schema)
+            #print http_content
+            return vim_response.status_code, vim_response.json()["networks"]
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to get network list. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+
+    def get_tenant_network(self, net_id, tenant_id=None):
+        '''Obtain tenant networks of VIM'''
+        '''Returns the network information from a network id'''
+        if self.debug:
+            print "VIMconnector.get_tenant_network(): Getting tenant network %s from VIM" % net_id
+        filter_dict={"id": net_id}
+        if tenant_id:
+            filter_dict["tenant_id"] = tenant_id
+        r, net_list = self.get_network_list(filter_dict)
+        if r<0:
+            return r, net_list
+        if len(net_list)==0:
+            return -vimconn.HTTP_Not_Found, "Network '%s' not found" % net_id
+        elif len(net_list)>1:
+            return -vimconn.HTTP_Conflict, "Found more than one network with this criteria"
+        return 1, net_list[0]
+
+    def delete_tenant_network(self, net_id):
+        '''Deletes a tenant network from VIM'''
+        '''Returns the network identifier'''
+        print "VIMConnector: Deleting a new tenant network from VIM tenant: " + self.tenant + ", id: " + net_id
+
+        headers_req = {'content-type': 'application/json'}
+        try:
+            vim_response = requests.delete(self.url+'/networks/'+net_id, headers=headers_req)
+        except requests.exceptions.RequestException, e:
+            print "delete_tenant_network Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+                return vim_response.status_code,net_id
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to delete tenant network. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+
+    def refresh_tenant_network(self, net_id):
+        '''Refreshes the status of the tenant network'''
+        '''Returns: 0 if no error,
+                    <0 if error'''
+        return 0
+
+    def get_tenant_flavor(self, flavor_id):
+        '''Obtain flavor details from the  VIM
+            Returns the flavor dict details
+        '''
+        print "VIMConnector: Getting flavor from VIM"
+        #print "VIM URL:",self.url
+        #print "Tenant id:",self.tenant
+        #print "Flavor:",flavor_data
+        headers_req = {'content-type': 'application/json'}
+        try:
+            vim_response = requests.get(self.url+'/'+self.tenant+'/flavors/'+flavor_id, headers = headers_req)
+        except requests.exceptions.RequestException, e:
+            print "get_tenant_flavor Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, get_flavor_response_schema)
+            #print http_content
+            if res:
+                r = self._remove_extra_items(http_content, get_flavor_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                flavor_id = http_content['flavor']['id']
+                print "Flavor id: ",flavor_id
+                return vim_response.status_code,flavor_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to get flavor. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text    
+        
+    def new_tenant_flavor(self, flavor_data):
+        '''Adds a tenant flavor to VIM'''
+        '''Returns the flavor identifier'''
+        print "VIMConnector: Adding a new flavor to VIM"
+        #print "VIM URL:",self.url
+        #print "Tenant id:",self.tenant
+        #print "Flavor:",flavor_data
+        headers_req = {'content-type': 'application/json'}
+        payload_req = json.dumps({'flavor': flavor_data})
+        try:
+            vim_response = requests.post(self.url+'/'+self.tenant+'/flavors', headers = headers_req, data=payload_req)
+        except requests.exceptions.RequestException, e:
+            print "new_tenant_flavor Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, new_flavor_response_schema)
+            #print http_content
+            if res:
+                r = self._remove_extra_items(http_content, new_flavor_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                flavor_id = http_content['flavor']['id']
+                print "Flavor id: ",flavor_id
+                return vim_response.status_code,flavor_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to add new flavor. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+
+    def delete_tenant_flavor(self,flavor_id):
+        '''Deletes a tenant flavor from VIM'''
+        '''Returns the HTTP response code and a message indicating details of the success or fail'''
+        print "VIMConnector: Deleting a flavor from VIM"
+        print "VIM URL:",self.url
+        print "Tenant id:",self.tenant
+        print "Flavor id:",flavor_id
+        #headers_req = {'content-type': 'application/json'}
+        #payload_req = flavor_data
+        try:
+            vim_response = requests.delete(self.url+'/'+self.tenant+'/flavors/'+flavor_id)
+        except requests.exceptions.RequestException, e:
+            print "delete_tenant_flavor Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        print vim_response.status_code
+        if vim_response.status_code == 200:
+            result = vim_response.json()["result"]
+            return 200,result
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to delete flavor. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+
+    def new_tenant_image(self,image_dict):
+        '''
+        Adds a tenant image to VIM
+        Returns:
+            200, image-id        if the image is created
+            <0, message          if there is an error
+        '''
+        print "VIMConnector: Adding a new image to VIM", image_dict['location']
+        headers_req = {'content-type': 'application/json'}
+        new_image_dict={'name': image_dict['name']}
+        if 'description' in image_dict and image_dict['description'] != None:
+            new_image_dict['description'] = image_dict['description']
+        if 'metadata' in image_dict and image_dict['metadata'] != None:
+            new_image_dict['metadata'] = yaml.load(image_dict['metadata'])
+        if 'location' in image_dict and image_dict['location'] != None:
+            new_image_dict['path'] = image_dict['location']
+        payload_req = json.dumps({"image":new_image_dict})
+        url=self.url + '/' + self.tenant + '/images'
+        try:
+            vim_response = requests.post(url, headers = headers_req, data=payload_req)
+        except requests.exceptions.RequestException, e:
+            print "new_tenant_image Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, new_image_response_schema)
+            #print http_content
+            if res:
+                r = self._remove_extra_items(http_content, new_image_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                image_id = http_content['image']['id']
+                print "Image id: ",image_id
+                return vim_response.status_code,image_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to add new image. HTTP Response: %d. Error: %s' % (url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+
+    def delete_tenant_image(self, image_id):
+        '''Deletes a tenant image from VIM'''
+        '''Returns the HTTP response code and a message indicating details of the success or fail'''
+        print "VIMConnector: Deleting an image from VIM"
+        #headers_req = {'content-type': 'application/json'}
+        #payload_req = flavor_data
+        url=self.url + '/'+ self.tenant +'/images/'+image_id
+        try:
+            vim_response = requests.delete(url)
+        except requests.exceptions.RequestException, e:
+            print "delete_tenant_image Exception url '%s': " % url, e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        print vim_response.status_code
+        if vim_response.status_code == 200:
+            result = vim_response.json()["result"]
+            return 200,result
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to delete image. HTTP Response: %d. Error: %s' % (url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+        
+    def new_tenant_vminstancefromJSON(self, vm_data):
+        '''Adds a VM instance to VIM'''
+        '''Returns the instance identifier'''
+        print "VIMConnector: Adding a new VM instance from JSON to VIM"
+        headers_req = {'content-type': 'application/json'}
+        payload_req = vm_data
+        try:
+            vim_response = requests.post(self.url+'/'+self.tenant+'/servers', headers = headers_req, data=payload_req)
+        except requests.exceptions.RequestException, e:
+            print "new_tenant_vminstancefromJSON Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print vim_response.json()
+            #print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, new_image_response_schema)
+            #print http_content
+            if res:
+                r = self._remove_extra_items(http_content, new_image_response_schema)
+                if r is not None: print "Warning: remove extra items ", r
+                #print http_content
+                vminstance_id = http_content['server']['id']
+                print "Tenant image id: ",vminstance_id
+                return vim_response.status_code,vminstance_id
+            else: return -vimconn.HTTP_Bad_Request,http_content
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to add new vm instance. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+
+    def new_tenant_vminstance(self,name,description,start,image_id,flavor_id,net_list):
+        '''Adds a VM instance to VIM
+        Params:
+            start: indicates if VM must start or boot in pause mode. Ignored
+            image_id,flavor_id: image and flavor uuid
+            net_list: list of interfaces, each one is a dictionary with:
+                name:
+                net_id: network uuid to connect
+                vpci: virtual vcpi to assign
+                model: interface model, virtio, e2000, ...
+                mac_address: 
+                use: 'data', 'bridge',  'mgmt'
+                type: 'virtual', 'PF', 'VF', 'VFnotShared'
+                vim_id: filled/added by this function
+                #TODO ip, security groups
+        Returns >=0, the instance identifier
+                <0, error_text
+        '''
+        print "VIMConnector: Adding a new VM instance to VIM"
+        headers_req = {'content-type': 'application/json'}
+        
+#         net_list = []
+#         for k,v in net_dict.items():
+#             print k,v
+#             net_list.append('{"name":"' + k + '", "uuid":"' + v + '"}')
+#         net_list_string = ', '.join(net_list) 
+        virtio_net_list=[]
+        for net in net_list:
+            if not net.get("net_id"):
+                continue
+            net_dict={'uuid': net["net_id"]}
+            if net.get("type"):        net_dict["type"] = net["type"]
+            if net.get("name"):        net_dict["name"] = net["name"]
+            if net.get("vpci"):        net_dict["vpci"] = net["vpci"]
+            if net.get("model"):       net_dict["model"] = net["model"]
+            if net.get("mac_address"): net_dict["mac_address"] = net["mac_address"]
+            virtio_net_list.append(net_dict)
+        payload_dict={  "name":        name,
+                        "description": description,
+                        "imageRef":    image_id,
+                        "flavorRef":   flavor_id,
+                        "networks": virtio_net_list
+                    }
+        if start != None:
+            payload_dict["start"] = start
+        payload_req = json.dumps({"server": payload_dict})
+        print self.url+'/'+self.tenant+'/servers'+payload_req
+        try:
+            vim_response = requests.post(self.url+'/'+self.tenant+'/servers', headers = headers_req, data=payload_req)
+        except requests.exceptions.RequestException, e:
+            print "new_tenant_vminstance Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code != 200:
+            print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to add new vm instance. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+        #ok
+        print vim_response.json()
+        print json.dumps(vim_response.json(), indent=4)
+        res,http_content = self._format_in(vim_response, new_vminstance_response_schema)
+        #print http_content
+        if  not res:
+            return -vimconn.HTTP_Bad_Request,http_content
+        #r = self._remove_extra_items(http_content, new_vminstance_response_schema)
+        #if r is not None: print "Warning: remove extra items ", r
+        vminstance_id = http_content['server']['id']
+        print json.dumps(http_content, indent=4)
+        #connect data plane interfaces to network
+        for net in net_list:
+            if net["type"]=="virtual":
+                if not net.get("net_id"):
+                    continue
+                for iface in http_content['server']['networks']:
+                    if "name" in net:
+                        if net["name"]==iface["name"]:
+                            net["vim_id"] = iface['iface_id']
+                            break
+                    elif "net_id" in net:
+                        if net["net_id"]==iface["net_id"]:
+                            net["vim_id"] = iface['iface_id']
+                            break
+            else: #dataplane
+                for numa in http_content['server'].get('extended',{}).get('numas',() ):
+                    for iface in numa.get('interfaces',() ):
+                        if net['name'] == iface['name']:
+                            net['vim_id'] = iface['iface_id']
+                            #Code bellow is not needed, current openvim connect dataplane interfaces 
+                            #if net.get("net_id"):
+                            ##connect dataplane interface
+                            #    result, port_id = self.connect_port_network(iface['iface_id'], net["net_id"])
+                            #    if result < 0:
+                            #        error_text = "Error attaching port %s to network %s: %s." % (iface['iface_id'], net["net_id"], port_id)
+                            #        print "new_tenant_vminstance: " + error_text
+                            #        self.delete_tenant_vminstance(vminstance_id)
+                            #        return result, error_text
+                            break
+        
+        print "VM instance id: ",vminstance_id
+        return vim_response.status_code,vminstance_id
+        
+    def get_tenant_vminstance(self,vm_id):
+        '''Returns the VM instance information from VIM'''
+        print "VIMConnector: Getting tenant VM instance information from VIM"
+        headers_req = {'content-type': 'application/json'}
+        
+        url = self.url+'/'+self.tenant+'/servers/'+vm_id
+        print url
+        try:
+            vim_response = requests.get(url, headers = headers_req)
+        except requests.exceptions.RequestException, e:
+            print "get_tenant_vminstance Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print vim_response
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            print vim_response.json()
+            print json.dumps(vim_response.json(), indent=4)
+            res,http_content = self._format_in(vim_response, new_vminstance_response_schema)
+            #print http_content
+            if res:
+                print json.dumps(http_content, indent=4)
+                return vim_response.status_code,http_content
+            else: return -vimconn.HTTP_Bad_Request,http_content
+        else:
+            print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to get vm instance. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code,text
+        
+    def delete_tenant_vminstance(self, vm_id):
+        '''Removes a VM instance from VIM'''
+        '''Returns the instance identifier'''
+        print "VIMConnector: Delete a VM instance from VIM " + vm_id
+        headers_req = {'content-type': 'application/json'}
+        
+        try:
+            vim_response = requests.delete(self.url+'/'+self.tenant+'/servers/'+vm_id, headers = headers_req)
+        except requests.exceptions.RequestException, e:
+            print "delete_tenant_vminstance Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            print json.dumps(vim_response.json(), indent=4)
+            return vim_response.status_code, vm_id
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": not possible to delete vm instance. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return -vim_response.status_code, text
+
+    def refresh_tenant_vms_and_nets(self, vmDict, netDict):
+        '''Refreshes the status of the dictionaries of VM instances and nets passed as arguments. It modifies the dictionaries'''
+        '''Returns:
+            - result: 0 if all elements could be refreshed (even if its status didn't change)
+                      n>0, the number of elements that couldn't be refreshed,
+                      <0 if error (foreseen)
+            - error_msg: text with reference to possible errors
+        '''
+        #vms_refreshed = []
+        #nets_refreshed = []
+        vms_unrefreshed = []
+        nets_unrefreshed = []
+        for vm_id in vmDict:
+            vmDict[vm_id]={'error_msg':None, 'vim_info':None}
+            print "VIMConnector refresh_tenant_vms and nets: Getting tenant VM instance information from VIM"
+            headers_req = {'content-type': 'application/json'}
+        
+            url = self.url+'/'+self.tenant+'/servers/'+ vm_id
+            print url
+            try:
+                vim_response = requests.get(url, headers = headers_req)
+            except requests.exceptions.RequestException, e:
+                print "VIMConnector refresh_tenant_elements. Exception: ", e.args
+                vmDict[vm_id]['status'] = "VIM_ERROR"
+                vmDict[vm_id]['error_msg'] = str(e)
+                vms_unrefreshed.append(vm_id)
+                continue
+            #print vim_response
+            #print vim_response.status_code
+            if vim_response.status_code == 200:
+                #print vim_response.json()
+                #print json.dumps(vim_response.json(), indent=4)
+                management_ip = False
+                res,http_content = self._format_in(vim_response, new_vminstance_response_schema)
+                if res:
+                    try:
+                        vmDict[vm_id]['status'] = vmStatus2manoFormat[ http_content['server']['status']  ]
+                        if http_content['server'].get('last_error'):
+                            vmDict[vm_id]['error_msg'] = http_content['server']['last_error']
+                        vmDict[vm_id]["vim_info"] = yaml.safe_dump(http_content['server'])
+                        vmDict[vm_id]["interfaces"]=[]
+                        #get interfaces info
+                        url2 = self.url+'/ports?device_id='+ vm_id
+                        try:
+                            vim_response2 = requests.get(url2, headers = headers_req)
+                            if vim_response.status_code == 200:
+                                client_data = vim_response2.json()
+                                for port in client_data.get("ports"):
+                                    print "VACAport", port
+                                    interface={}
+                                    interface['vim_info']  = yaml.safe_dump(port)
+                                    interface["mac_address"] = port.get("mac_address")
+                                    interface["vim_net_id"] = port["network_id"]
+                                    interface["vim_interface_id"] = port["id"]
+                                    interface["ip_address"] = port.get("ip_address")
+                                    if interface["ip_address"]:
+                                        management_ip = True
+                                    if interface["ip_address"] == "0.0.0.0":
+                                        interface["ip_address"] = None
+                                    vmDict[vm_id]["interfaces"].append(interface)
+                                
+                        except Exception as e:
+                            print "VIMConnector refresh_tenant_elements. Port get %s: %s", (type(e).__name__, (str(e) if len(e.args)==0 else str(e.args[0])))
+
+                        if vmDict[vm_id]['status'] == "ACTIVE" and not management_ip:
+                            vmDict[vm_id]['status'] = "ACTIVE:NoMgmtIP"
+                        
+                    except Exception as e:
+                        vmDict[vm_id]['status'] = "VIM_ERROR"
+                        vmDict[vm_id]['error_msg'] = str(e)
+                        vms_unrefreshed.append(vm_id)
+                else:
+                    vmDict[vm_id]['status'] = "VIM_ERROR"
+                    vmDict[vm_id]['error_msg'] = str(http_content)
+                    vms_unrefreshed.append(vm_id)
+            else:
+                #print vim_response.text
+                jsonerror = self._format_jsonerror(vim_response)
+                print 'VIMConnector refresh_tenant_vms_and_nets. Error in VIM "%s": not possible to get VM instance. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+                if vim_response.status_code == 404: # HTTP_Not_Found
+                    vmDict[vm_id]['status'] = "DELETED"
+                else:
+                    vmDict[vm_id]['status'] = "VIM_ERROR"
+                    vmDict[vm_id]['error_msg'] = jsonerror
+                    vms_unrefreshed.append(vm_id)
+        
+        #print "VMs refreshed: %s" % str(vms_refreshed)
+        for net_id in netDict:
+            netDict[net_id] = {'error_msg':None, 'vim_info':None}
+            print "VIMConnector refresh_tenant_vms_and_nets: Getting tenant network from VIM (tenant: " + str(self.tenant) + "): "
+            headers_req = {'content-type': 'application/json'}
+            r,c = self.get_tenant_network(net_id)
+            if r<0:
+                print "VIMconnector refresh_tenant_network. Error getting net_id '%s' status: %s" % (net_id, c)
+                if r==-vimconn.HTTP_Not_Found:
+                    netDict[net_id]['status'] = "DELETED" #TODO check exit status
+                else:
+                    netDict[net_id]['status'] = "VIM_ERROR"
+                    netDict[net_id]['error_msg'] = c
+                    nets_unrefreshed.append(net_id)
+            else:
+                try: 
+                    net_status = netStatus2manoFormat[ c['status'] ]
+                    if net_status == "ACTIVE" and not c['admin_state_up']:
+                        net_status = "DOWN"
+                    netDict[net_id]['status'] = net_status
+                    if c.get('last_error'):
+                        netDict[net_id]['error_msg'] = c['last_error']
+                    netDict[net_id]["vim_info"] = yaml.safe_dump(c)
+                except Exception as e:
+                    netDict[net_id]['status'] = "VIM_ERROR"
+                    netDict[net_id]['error_msg'] = str(e)
+                    nets_unrefreshed.append(net_id)
+
+        #print "Nets refreshed: %s" % str(nets_refreshed)
+        
+        error_msg=""
+        if len(vms_unrefreshed)+len(nets_unrefreshed)>0:
+            error_msg += "VMs unrefreshed: " + str(vms_unrefreshed) + "; nets unrefreshed: " + str(nets_unrefreshed)
+            print error_msg
+
+        #return len(vms_unrefreshed)+len(nets_unrefreshed), error_msg, vms_refreshed, nets_refreshed
+        return len(vms_unrefreshed)+len(nets_unrefreshed), error_msg
+    
+    def action_tenant_vminstance(self, vm_id, action_dict):
+        '''Send and action over a VM instance from VIM'''
+        '''Returns the status'''
+        print "VIMConnector: Action over VM instance from VIM " + vm_id
+        headers_req = {'content-type': 'application/json'}
+        
+        try:
+            if "console" in action_dict:
+                return -vimconn.HTTP_Service_Unavailable, "getting console is not available at openvim"
+            
+            vim_response = requests.post(self.url+'/'+self.tenant+'/servers/'+vm_id+"/action", headers = headers_req, data=json.dumps(action_dict) )
+        except requests.exceptions.RequestException, e:
+            print "action_tenant_vminstance Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+
+        #print vim_response.status_code
+        if vim_response.status_code == 200:
+            #print "vimconnector.action_tenant_vminstance():", json.dumps(vim_response.json(), indent=4)
+            return vim_response.status_code, vm_id
+        else:
+            #print vim_response.text
+            jsonerror = self._format_jsonerror(vim_response)
+            text = 'Error in VIM "%s": action over vm instance. HTTP Response: %d. Error: %s' % (self.url, vim_response.status_code, jsonerror)
+            #print text
+            return vim_response.status_code, text
+        
+    def host_vim2gui(self, host, server_dict):
+        '''Transform host dictionary from VIM format to GUI format,
+        and append to the server_dict
+        '''
+        if type(server_dict) is not dict: 
+            print 'vimconnector.host_vim2gui() ERROR, param server_dict must be a dictionary'
+            return
+        RAD={}
+        occupation={}
+        for numa in host['host']['numas']:
+            RAD_item={}
+            occupation_item={}
+            #memory
+            RAD_item['memory']={'size': str(numa['memory'])+'GB', 'eligible': str(numa['hugepages'])+'GB'}
+            occupation_item['memory']= str(numa['hugepages_consumed'])+'GB'
+            #cpus
+            RAD_item['cpus']={}
+            RAD_item['cpus']['cores'] = []
+            RAD_item['cpus']['eligible_cores'] = []
+            occupation_item['cores']=[]
+            for _ in range(0, len(numa['cores']) / 2):
+                RAD_item['cpus']['cores'].append( [] )
+            for core in numa['cores']:
+                RAD_item['cpus']['cores'][core['core_id']].append(core['thread_id'])
+                if not 'status' in core: RAD_item['cpus']['eligible_cores'].append(core['thread_id'])
+                if 'instance_id' in core: occupation_item['cores'].append(core['thread_id'])
+            #ports
+            RAD_item['ports']={}
+            occupation_item['ports']={}
+            for iface in numa['interfaces']:
+                RAD_item['ports'][ iface['pci'] ] = 'speed:'+str(iface['Mbps'])+'M'
+                occupation_item['ports'][ iface['pci'] ] = { 'occupied': str(100*iface['Mbps_consumed'] / iface['Mbps']) + "%" }
+                
+            RAD[ numa['numa_socket'] ] = RAD_item
+            occupation[ numa['numa_socket'] ] = occupation_item
+        server_dict[ host['host']['name'] ] = {'RAD':RAD, 'occupation':occupation}
+
+    def get_hosts_info(self):
+        '''Get the information of deployed hosts
+        Returns the hosts content'''
+    #obtain hosts list
+        url=self.url+'/hosts'
+        try:
+            vim_response = requests.get(url)
+        except requests.exceptions.RequestException, e:
+            print "get_hosts_info Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print "vim get", url, "response:",  vim_response.status_code, vim_response.json()
+        #print vim_response.status_code
+        #print json.dumps(vim_response.json(), indent=4)
+        if vim_response.status_code != 200:
+            #TODO: get error
+            print 'vimconnector.get_hosts_info error getting host list %d %s' %(vim_response.status_code, vim_response.json())
+            return -vim_response.status_code, "Error getting host list"
+        
+        res,hosts = self._format_in(vim_response, get_hosts_response_schema)
+            
+        if res==False:
+            print "vimconnector.get_hosts_info error parsing GET HOSTS vim response", hosts
+            return vimconn.HTTP_Internal_Server_Error, hosts
+    #obtain hosts details
+        hosts_dict={}
+        for host in hosts['hosts']:
+            url=self.url+'/hosts/'+host['id']
+            try:
+                vim_response = requests.get(url)
+            except requests.exceptions.RequestException, e:
+                print "get_hosts_info Exception: ", e.args
+                return -vimconn.HTTP_Not_Found, str(e.args[0])
+            print "vim get", url, "response:",  vim_response.status_code, vim_response.json()
+            if vim_response.status_code != 200:
+                print 'vimconnector.get_hosts_info error getting detailed host %d %s' %(vim_response.status_code, vim_response.json())
+                continue
+            res,host_detail = self._format_in(vim_response, get_host_detail_response_schema)
+            if res==False:
+                print "vimconnector.get_hosts_info error parsing GET HOSTS/%s vim response" % host['id'], host_detail
+                continue
+            #print 'host id '+host['id'], json.dumps(host_detail, indent=4)
+            self.host_vim2gui(host_detail, hosts_dict)
+        return 200, hosts_dict
+
+    def get_hosts(self, vim_tenant):
+        '''Get the hosts and deployed instances
+        Returns the hosts content'''
+    #obtain hosts list
+        url=self.url+'/hosts'
+        try:
+            vim_response = requests.get(url)
+        except requests.exceptions.RequestException, e:
+            print "get_hosts Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print "vim get", url, "response:",  vim_response.status_code, vim_response.json()
+        #print vim_response.status_code
+        #print json.dumps(vim_response.json(), indent=4)
+        if vim_response.status_code != 200:
+            #TODO: get error
+            print 'vimconnector.get_hosts error getting host list %d %s' %(vim_response.status_code, vim_response.json())
+            return -vim_response.status_code, "Error getting host list"
+        
+        res,hosts = self._format_in(vim_response, get_hosts_response_schema)
+            
+        if res==False:
+            print "vimconnector.get_host error parsing GET HOSTS vim response", hosts
+            return vimconn.HTTP_Internal_Server_Error, hosts
+    #obtain instances from hosts
+        for host in hosts['hosts']:
+            url=self.url+'/' + vim_tenant + '/servers?hostId='+host['id']
+            try:
+                vim_response = requests.get(url)
+            except requests.exceptions.RequestException, e:
+                print "get_hosts Exception: ", e.args
+                return -vimconn.HTTP_Not_Found, str(e.args[0])
+            print "vim get", url, "response:",  vim_response.status_code, vim_response.json()
+            if vim_response.status_code != 200:
+                print 'vimconnector.get_hosts error getting instances at host %d %s' %(vim_response.status_code, vim_response.json())
+                continue
+            res,servers = self._format_in(vim_response, get_server_response_schema)
+            if res==False:
+                print "vimconnector.get_host error parsing GET SERVERS/%s vim response" % host['id'], servers
+                continue
+            #print 'host id '+host['id'], json.dumps(host_detail, indent=4)
+            host['instances'] = servers['servers']
+        return 200, hosts['hosts']
+
+    def get_processor_rankings(self):
+        '''Get the processor rankings in the VIM database'''
+        url=self.url+'/processor_ranking'
+        try:
+            vim_response = requests.get(url)
+        except requests.exceptions.RequestException, e:
+            print "get_processor_rankings Exception: ", e.args
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print "vim get", url, "response:", vim_response.status_code, vim_response.json()
+        #print vim_response.status_code
+        #print json.dumps(vim_response.json(), indent=4)
+        if vim_response.status_code != 200:
+            #TODO: get error
+            print 'vimconnector.get_processor_rankings error getting processor rankings %d %s' %(vim_response.status_code, vim_response.json())
+            return -vim_response.status_code, "Error getting processor rankings"
+        
+        res,rankings = self._format_in(vim_response, get_processor_rankings_response_schema)
+        return res, rankings['rankings']
+    
+    def get_image_id_from_path(self, path):
+        '''Get the image id from image path in the VIM database'''
+        '''Returns:
+             0,"Image not found"   if there are no images with that path
+             1,image-id            if there is one image with that path
+             <0,message            if there was an error (Image not found, error contacting VIM, more than 1 image with that path, etc.) 
+        '''
+        url=self.url + '/' + self.tenant + '/images?path='+path
+        try:
+            vim_response = requests.get(url)
+        except requests.exceptions.RequestException, e:
+            print "get_image_id_from_path url='%s'Exception: '%s'" % (url, str(e.args))
+            return -vimconn.HTTP_Not_Found, str(e.args[0])
+        print "vim get_image_id_from_path", url, "response:", vim_response.status_code, vim_response.json()
+        #print vim_response.status_code
+        #print json.dumps(vim_response.json(), indent=4)
+        if vim_response.status_code != 200:
+            #TODO: get error
+            print 'vimconnector.get_image_id_from_path error getting image id from path. Error code: %d Description: %s' %(vim_response.status_code, vim_response.json())
+            return -vim_response.status_code, "Error getting image id from path"
+        
+        res,image = self._format_in(vim_response, get_images_response_schema)
+        if not res:
+            print "vimconnector.get_image_id_from_path error"
+            return -vimconn.HTTP_Bad_Request, image
+        if len(image['images'])==0:
+            return 0,"Image not found"
+        elif len(image['images'])>1:
+            print "vimconnector.get_image_id_from_path error. More than one images with the path %s." %(path)
+            return -vimconn.HTTP_Internal_Server_Error,"More than one images with that path"
+        return 1, image['images'][0]['id']
+        
+
diff --git a/vnfs/examples/dataplaneVNF1.yaml b/vnfs/examples/dataplaneVNF1.yaml
new file mode 100644 (file)
index 0000000..fdcf197
--- /dev/null
@@ -0,0 +1,61 @@
+---\r
+vnf:\r
+    name:        dataplaneVNF1\r
+    description: "Example of a dataplane VNF consisting of a single VM for data plane workloads with high I/O performance requirements: 14 HW threads, 16 GB hugepages and 4 10G interfaces"\r
+    external-connections:\r
+    -   name:              mgmt\r
+        type:              mgmt       # "mgmt"(autoconnect to management net)\r
+        VNFC:              dataplaneVNF1-VM\r
+        local_iface_name:  eth0\r
+        description:       Management interface for general use\r
+    -   name:              xe0\r
+        type:              data\r
+        VNFC:              dataplaneVNF1-VM\r
+        local_iface_name:  xe0\r
+        description:       Dataplane interface 1\r
+    -   name:              xe1\r
+        type:              data\r
+        VNFC:              dataplaneVNF1-VM\r
+        local_iface_name:  xe1\r
+        description:       Dataplane interface 2\r
+    -   name:              xe2\r
+        type:              data\r
+        VNFC:              dataplaneVNF1-VM\r
+        local_iface_name:  xe2\r
+        description:       Dataplane interface 3\r
+    -   name:              xe3\r
+        type:              data\r
+        VNFC:              dataplaneVNF1-VM\r
+        local_iface_name:  xe3\r
+        description:       Dataplane interface 4\r
+    VNFC:\r
+    -   name:        dataplaneVNF1-VM\r
+        description: "Dataplane VM with high I/O performance requirements: 14 HW threads, 16 GB hugepages and 4 10G interfaces"\r
+        #Copy the image to a compute path and edit this path\r
+        VNFC image:  /path/to/imagefolder/dataplaneVNF1.qcow2\r
+        numas: \r
+        -   paired-threads: 7          # "cores", "paired-threads", "threads"\r
+            paired-threads-id: [ [0,1], [2,3], [4,5], [6,7], [8,9], [10,11], [12, 13] ]\r
+            memory: 16                 # GBytes\r
+            interfaces:\r
+            -   name:      xe0\r
+                vpci:      "0000:00:11.0"\r
+                dedicated: "yes"         # "yes"(passthrough), "no"(sriov with vlan tags), "yes:sriov"(sriovi, but exclusive and without vlan tag)\r
+                bandwidth: 10 Gbps\r
+            -   name:      xe1\r
+                vpci:      "0000:00:12.0"\r
+                dedicated: "yes"\r
+                bandwidth: 10 Gbps\r
+            -   name:      xe2\r
+                vpci:      "0000:00:13.0"\r
+                dedicated: "yes"\r
+                bandwidth: 10 Gbps\r
+            -   name:      xe3\r
+                vpci:      "0000:00:14.0"\r
+                dedicated: "yes"\r
+                bandwidth: 10 Gbps\r
+        bridge-ifaces:\r
+        -   name:      eth0\r
+            vpci:      "0000:00:09.0"\r
+            bandwidth: 1 Mbps          # Optional, informative only\r
+\r
diff --git a/vnfs/examples/dataplaneVNF2.yaml b/vnfs/examples/dataplaneVNF2.yaml
new file mode 100644 (file)
index 0000000..6196a8d
--- /dev/null
@@ -0,0 +1,59 @@
+---\r
+vnf:\r
+    name:        dataplaneVNF2\r
+    description: "Example of a dataplane VNF consisting of a single VM for data plane workloads with high I/O performance requirements: 3 cores, 8 GB hugepages, 2 10G interfaces and 1 SR-IOV interface"\r
+    external-connections:\r
+    -   name:              mgmt\r
+        type:              mgmt       # "mgmt"(autoconnect to management net)\r
+        VNFC:              dataplaneVNF2-VM\r
+        local_iface_name:  eth0\r
+        description:       Management interface for general use\r
+    -   name:              control\r
+        type:              bridge\r
+        VNFC:              dataplaneVNF2-VM\r
+        local_iface_name:  eth1\r
+        description:       Bridge interface\r
+    -   name:              xe0\r
+        type:              data\r
+        VNFC:              dataplaneVNF2-VM\r
+        local_iface_name:  xe0\r
+        description:       Dataplane interface 1\r
+    -   name:              xe1\r
+        type:              data\r
+        VNFC:              dataplaneVNF2-VM\r
+        local_iface_name:  xe1\r
+        description:       Dataplane interface 2\r
+    -   name:              xe2\r
+        type:              data\r
+        VNFC:              dataplaneVNF2-VM\r
+        local_iface_name:  xe2\r
+        description:       Dataplane interface 3 (SR-IOV)\r
+    VNFC:\r
+    -   name:        dataplaneVNF2-VM\r
+        description: "Dataplane VM with high I/O performance requirements: 3 cores (no hyperthreading), 8 GB hugepages, 2 10G interfaces and 1 SR-IOV interface"\r
+        #Copy the image to a compute path and edit this path\r
+        VNFC image:  /path/to/imagefolder/dataplaneVNF2.qcow2\r
+        numas: \r
+        -   cores: 3          # "cores", "paired-threads", "threads"\r
+            memory: 8                 # GBytes\r
+            interfaces:\r
+            -   name:      xe0\r
+                vpci:      "0000:00:11.0"\r
+                dedicated: "yes"         # "yes"(passthrough), "no"(sriov with vlan tags), "yes:sriov"(sriovi, but exclusive and without vlan tag)\r
+                bandwidth: 10 Gbps\r
+            -   name:      xe1\r
+                vpci:      "0000:00:12.0"\r
+                dedicated: "yes"\r
+                bandwidth: 10 Gbps\r
+            -   name:      xe2\r
+                vpci:      "0000:00:13.0"\r
+                dedicated: "no"\r
+                bandwidth: 1 Gbps\r
+        bridge-ifaces:\r
+        -   name:      eth0\r
+            vpci:      "0000:00:09.0"\r
+            bandwidth: 1 Mbps          # Optional, informative only\r
+        -   name:      eth1\r
+            vpci:      "0000:00:10.0"\r
+            bandwidth: 1 Mbps          # Optional, informative only\r
+\r
diff --git a/vnfs/examples/dataplaneVNF3.yaml b/vnfs/examples/dataplaneVNF3.yaml
new file mode 100644 (file)
index 0000000..ce59035
--- /dev/null
@@ -0,0 +1,45 @@
+---\r
+vnf:\r
+    name:        dataplaneVNF3\r
+    description: "Example of a dataplane VNF consisting of one VM with two SR-IOV"\r
+    # class: parent      # Optional. Used to organize VNFs\r
+    external-connections:\r
+    -   name:              mgmt\r
+        type:              mgmt\r
+        VNFC:              dataplaneVNF3-VM\r
+        local_iface_name:  eth0\r
+        description:       control interface VM1\r
+    -   name:              data0\r
+        type:              data\r
+        VNFC:              dataplaneVNF3-VM\r
+        local_iface_name:  xe0\r
+        description:       Dataplane interface\r
+    -   name:              data1\r
+        type:              data\r
+        VNFC:              dataplaneVNF3-VM\r
+        local_iface_name:  xe1\r
+        description:       Dataplane interface\r
+    VNFC:\r
+    -   name:        dataplaneVNF3-VM\r
+        description: "Dataplane VM with 2 threads, 2 GB hugepages, 2 SR-IOV interface"\r
+        #Copy the image to a compute path and edit this path\r
+        VNFC image:  /path/to/imagefolder/dataplaneVNF3.qcow2\r
+        disk: 10\r
+        numas: \r
+        -   threads: 2         # "cores", "paired-threads", "threads"\r
+            memory: 2                 # GBytes\r
+            interfaces:\r
+            -   name:      xe0\r
+                vpci:      "0000:00:11.0"\r
+                dedicated: "no"         # "yes"(passthrough), "no"(sriov with vlan tags), "yes:sriov"(sriovi, but exclusive and without vlan tag)\r
+                bandwidth: 1 Gbps\r
+            -   name:      xe1\r
+                vpci:      "0000:00:12.0"\r
+                dedicated: "no"\r
+                bandwidth: 1 Gbps\r
+        bridge-ifaces:\r
+        -   name:      eth0\r
+            vpci:      "0000:00:09.0"\r
+            bandwidth: 1 Mbps          # Optional, informative only\r
+\r
+\r
diff --git a/vnfs/examples/dataplaneVNF_2VMs.yaml b/vnfs/examples/dataplaneVNF_2VMs.yaml
new file mode 100644 (file)
index 0000000..bd84aba
--- /dev/null
@@ -0,0 +1,80 @@
+---\r
+vnf:\r
+    name:        dataplaneVNF_2VMs\r
+    description: "Example of a dataplane VNF consisting of two VMs for data plane workloads with one internal network"\r
+    # class: parent      # Optional. Used to organize VNFs\r
+    internal-connections:\r
+    -   name:        datanet\r
+        description: datanet\r
+        type:        data\r
+        elements:\r
+        -   VNFC:             VNF_2VMs-VM1\r
+            local_iface_name: xe0\r
+        -   VNFC:             VNF_2VMs-VM2\r
+            local_iface_name: xe0\r
+    external-connections:\r
+    -   name:              control0\r
+        type:              mgmt\r
+        VNFC:              VNF_2VMs-VM1\r
+        local_iface_name:  eth0\r
+        description:       control interface VM1\r
+    -   name:              control1\r
+        type:              mgmt\r
+        VNFC:              VNF_2VMs-VM2\r
+        local_iface_name:  eth0\r
+        description:       control interface VM2\r
+    -   name:              in\r
+        type:              data\r
+        VNFC:              VNF_2VMs-VM1\r
+        local_iface_name:  xe1\r
+        description:       Dataplane interface input\r
+    -   name:              out\r
+        type:              data\r
+        VNFC:              VNF_2VMs-VM2\r
+        local_iface_name:  xe1\r
+        description:       Dataplane interface output\r
+    VNFC:\r
+    -   name:        VNF_2VMs-VM1\r
+        description: "Dataplane VM1 with 4 threads, 2 GB hugepages, 2 SR-IOV interface"\r
+        #Copy the image to a compute path and edit this path\r
+        VNFC image:  /path/to/imagefolder/dataplaneVNF_2VMs.qcow2\r
+        disk: 10\r
+        numas: \r
+        -   paired-threads: 2         # "cores", "paired-threads", "threads"\r
+            memory: 2                 # GBytes\r
+            interfaces:\r
+            -   name:      xe0\r
+                vpci:      "0000:00:11.0"\r
+                dedicated: "no"         # "yes"(passthrough), "no"(sriov with vlan tags), "yes:sriov"(sriovi, but exclusive and without vlan tag)\r
+                bandwidth: 1 Gbps\r
+            -   name:      xe1\r
+                vpci:      "0000:00:12.0"\r
+                dedicated: "no"\r
+                bandwidth: 1 Gbps\r
+        bridge-ifaces:\r
+        -   name:      eth0\r
+            vpci:      "0000:00:09.0"\r
+            bandwidth: 1 Mbps          # Optional, informative only\r
+\r
+    -   name:        VNF_2VMs-VM2\r
+        description: "Dataplane VM1 with 2 threads, 2 GB hugepages, 2 SR-IOV interface"\r
+        #Copy the image to a compute path and edit this path\r
+        VNFC image:  /path/to/imagefolder/dataplaneVNF_2VMs.qcow2\r
+        disk: 10\r
+        numas: \r
+        -   paired-threads: 1         # "cores", "paired-threads", "threads"\r
+            memory: 2                 # GBytes\r
+            interfaces:\r
+            -   name:      xe0\r
+                vpci:      "0000:00:11.0"\r
+                dedicated: "no"         # "yes"(passthrough), "no"(sriov with vlan tags), "yes:sriov"(sriovi, but exclusive and without vlan tag)\r
+                bandwidth: 1 Gbps\r
+            -   name:      xe1\r
+                vpci:      "0000:00:12.0"\r
+                dedicated: "no"\r
+                bandwidth: 1 Gbps\r
+        bridge-ifaces:\r
+        -   name:      eth0\r
+            vpci:      "0000:00:09.0"\r
+            bandwidth: 1 Mbps          # Optional, informative only\r
+\r
diff --git a/vnfs/examples/linux.yaml b/vnfs/examples/linux.yaml
new file mode 100644 (file)
index 0000000..c157618
--- /dev/null
@@ -0,0 +1,23 @@
+---\r
+vnf:\r
+    name:        linux\r
+    description: Single-VM VNF with a traditional cloud VM based on generic Linux OS\r
+    external-connections:\r
+    -   name:              eth0\r
+        type:              bridge\r
+        VNFC:              linux-VM\r
+        local_iface_name:  eth0\r
+        description:       General purpose interface\r
+    VNFC:\r
+    -   name:        linux-VM\r
+        description: Generic Linux Virtual Machine\r
+        #Copy the image to a compute path and edit this path\r
+        VNFC image:  /path/to/imagefolder/linux.qcow2\r
+        vcpus: 1          # Only for traditional cloud VMs. Number of virtual CPUs (oversubscription is allowed).\r
+        ram: 1024         # Only for traditional cloud VMs. Memory in MBytes (not from hugepages, oversubscription is allowed)\r
+        disk: 10\r
+        bridge-ifaces:\r
+        -   name:      eth0\r
+            vpci:      "0000:00:11.0"\r
+        numas: []\r
+\r
diff --git a/vnfs/vnf-template-2vm.yaml b/vnfs/vnf-template-2vm.yaml
new file mode 100644 (file)
index 0000000..b12476b
--- /dev/null
@@ -0,0 +1,164 @@
+---\r
+vnf:\r
+    name: TEMPLATE-2VM\r
+    description: This is a template to help in the creation of multi-VM VNFs\r
+    # class: parent      # Optional. Used to organize VNFs\r
+    internal-connections:\r
+    -   name:        datanet\r
+        description: datanet\r
+        type:        data\r
+        elements:\r
+        -   VNFC:             VirtualMachine-1\r
+            local_iface_name: xe0\r
+        -   VNFC:             VirtualMachine-2\r
+            local_iface_name: xe0\r
+    -   name:        controlnet\r
+        description: controlnet\r
+        type:        bridge\r
+        elements:\r
+        -   VNFC:             VirtualMachine-1\r
+            local_iface_name: ge0\r
+        -   VNFC:             VirtualMachine-2\r
+            local_iface_name: ge0\r
+    external-connections:\r
+    -   name:              mgmt0\r
+        type:              mgmt              # "mgmt" (autoconnect to management net), "bridge", "data"\r
+        VNFC:              VirtualMachine-1  # Virtual Machine this interface belongs to\r
+        local_iface_name:  mgmt0             # interface name inside this Virtual Machine (must be defined in the VNFC section)\r
+        description:       Management interface 1\r
+    -   name:              mgmt1\r
+        type:              mgmt              # "mgmt" (autoconnect to management net), "bridge", "data"\r
+        VNFC:              VirtualMachine-2  # Virtual Machine this interface belongs to\r
+        local_iface_name:  mgmt0             # interface name inside this Virtual Machine (must be defined in the VNFC section)\r
+        description:       Management interface 2\r
+    -   name:              data0\r
+        type:              data              # "mgmt" (autoconnect to management net), "bridge", "data"\r
+        VNFC:              VirtualMachine-1  # Virtual Machine this interface belongs to\r
+        local_iface_name:  xe1               # interface name inside this Virtual Machine (must be defined in the VNFC section)\r
+        description:       Data interface 1\r
+    -   name:              data1\r
+        type:              data              # "mgmt" (autoconnect to management net), "bridge", "data"\r
+        VNFC:              VirtualMachine-2  # Virtual Machine this interface belongs to\r
+        local_iface_name:  xe1               # interface name inside this Virtual Machine (must be defined in the VNFC section)\r
+        description:       Data interface 2\r
+    VNFC:                              # Virtual machine array \r
+    # First Virtual Machine\r
+    -   name:        VirtualMachine-1       # name of Virtual Machine\r
+        description: VM 1 in the MultiVM template\r
+        VNFC image: /path/to/imagefolder/TEMPLATE-2VM-VM1.qcow2\r
+        # image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" } #Optional\r
+        # processor:                     #Optional\r
+        #     model: Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz\r
+        #     features: ["64b", "iommu", "lps", "tlbps", "hwsv", "dioc", "ht"]\r
+        # hypervisor:                    #Optional\r
+        #     type: QEMU-kvm\r
+        #     version: "10002|12001|2.6.32-358.el6.x86_64"\r
+        # vcpus: 1          # Only for traditional cloud VMs. Number of virtual CPUs (oversubscription is allowed).\r
+        # ram: 1024         # Only for traditional cloud VMs. Memory in MBytes (not from hugepages, oversubscription is allowed)\r
+        # disk: 10          # disk size in GiB, by default 1\r
+        numas: \r
+        -   paired-threads: 5          # "cores", "paired-threads", "threads"\r
+            paired-threads-id: [ [0,1], [2,3], [4,5], [6,7], [8,9] ] # By default follows incremental order\r
+            memory: 14                 # GBytes\r
+            interfaces:\r
+            -   name:      xe0\r
+                vpci:      "0000:00:11.0"     \r
+                dedicated: "yes"         # "yes"(passthrough), "no"(sriov with vlan tags), "yes:sriov"(sriovi, but exclusive and without vlan tag)\r
+                bandwidth: 10 Gbps\r
+                #mac_address: '20:33:45:56:77:44' #avoid this option if possible\r
+            -   name:      xe1\r
+                vpci:      "0000:00:12.0"\r
+                dedicated: "yes"\r
+                bandwidth: 10 Gbps\r
+                #mac_address: '20:33:45:56:77:45' #avoid this option if possible\r
+        bridge-ifaces:\r
+        -   name:      mgmt0\r
+            vpci:      "0000:00:09.0"  # Optional. Virtual PCI address \r
+            bandwidth: 1 Mbps          # Optional. Informative only\r
+            # mac_address: '20:33:45:56:77:46' #avoid this option if possible\r
+            # model:       'virtio'      # ("virtio","e1000","ne2k_pci","pcnet","rtl8139") By default, it is automatically filled by libvirt\r
+        -   name:      ge0\r
+            vpci:      "0000:00:10.0"\r
+            bandwidth: 1 Mbps\r
+            # mac_address: '20:33:45:56:77:47' # avoid this option if possible\r
+            # model:       'virtio'            # ("virtio","e1000","ne2k_pci","pcnet","rtl8139") By default, it is automatically filled by libvirt\r
+        devices:                       # Optional, order determines device letter asignation (hda, hdb, ...)\r
+        -   type:      disk            # "disk","cdrom","xml"\r
+            image:     /path/to/imagefolder/SECOND-DISK.qcow2\r
+            # image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" }\r
+            # vpci:      "0000:00:03.0"   # Optional, not for disk or cdrom\r
+        -   type:      cdrom\r
+            image:     /path/to/imagefolder/CDROM-IMAGE.qcow2\r
+            # image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" }\r
+        -   type:      xml\r
+            image:     /path/to/imagefolder/ADDITIONAL-DISK.qcow2   # Optional, depending on the device type\r
+            image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" }  # Optional, depending on the device type\r
+            vpci:      "0000:00:03.0"                          # Optional, depending on the device type (not needed for disk or cdrom)\r
+            xml:   '   xml text for XML described devices. Do not use single quotes inside\r
+                       The following words, if found, will be replaced:\r
+                       __file__    by image path, (image must be provided)\r
+                       __format__  by qcow2 or raw (image must be provided)\r
+                       __dev__     by device letter (b, c, d ...) \r
+                       __vpci__    by vpci (vpci must be provided)\r
+                   '\r
+    # Second Virtual Machine\r
+    -   name:        VirtualMachine-2       # name of Virtual Machine\r
+        description: VM 2 in the MultiVM template\r
+        VNFC image: /path/to/imagefolder/TEMPLATE-2VM-VM1.qcow2   # In this case, it is the same as VirtualMachine-1, but it could have been different\r
+        # image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" } #Optional\r
+        # processor:                     #Optional\r
+        #     model: Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz\r
+        #     features: ["64b", "iommu", "lps", "tlbps", "hwsv", "dioc", "ht"]\r
+        # hypervisor:                    #Optional\r
+        #     type: QEMU-kvm\r
+        #     version: "10002|12001|2.6.32-358.el6.x86_64"\r
+        # vcpus: 1          # Only for traditional cloud VMs. Number of virtual CPUs (oversubscription is allowed).\r
+        # ram: 1024         # Only for traditional cloud VMs. Memory in MBytes (not from hugepages, oversubscription is allowed)\r
+        # disk: 10          # disk size in GiB, by default 1\r
+        numas:\r
+        -   paired-threads: 5          # "cores", "paired-threads", "threads"\r
+            paired-threads-id: [ [0,1], [2,3], [4,5], [6,7], [8,9] ] # By default follows incremental order\r
+            memory: 14                 # GBytes\r
+            interfaces:\r
+            -   name:      xe0\r
+                vpci:      "0000:00:11.0"\r
+                dedicated: "yes"         # "yes"(passthrough), "no"(sriov with vlan tags), "yes:sriov"(sriovi, but exclusive and without vlan tag)\r
+                bandwidth: 10 Gbps\r
+                #mac_address: '20:33:45:56:77:44' #avoid this option if possible\r
+            -   name:      xe1\r
+                vpci:      "0000:00:12.0"\r
+                dedicated: "yes"\r
+                bandwidth: 10 Gbps\r
+                #mac_address: '20:33:45:56:77:45' #avoid this option if possible\r
+        bridge-ifaces:\r
+        -   name:      mgmt0\r
+            vpci:      "0000:00:09.0"    # Optional\r
+            bandwidth: 1 Mbps          # Optional, informative only\r
+            # mac_address: '20:33:45:56:77:46' #avoid this option if possible\r
+            # model:       'virtio'      # ("virtio","e1000","ne2k_pci","pcnet","rtl8139") By default, it is automatically filled by libvirt\r
+        -   name:      ge0\r
+            vpci:      "0000:00:10.0"\r
+            bandwidth: 1 Mbps\r
+            # mac_address: '20:33:45:56:77:47' #avoid this option if possible\r
+            # model:       'virtio'      # ("virtio","e1000","ne2k_pci","pcnet","rtl8139") By default, it is automatically filled by libvirt\r
+        devices:                       # Optional, order determines device letter asignation (hda, hdb, ...)\r
+        -   type:      disk            # "disk","cdrom","xml"\r
+            image:     /path/to/imagefolder/SECOND-DISK.qcow2\r
+            # image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" }\r
+            # vpci:      "0000:00:03.0"   # Optional, not for disk or cdrom\r
+        -   type:      cdrom\r
+            image:     /path/to/imagefolder/CDROM-IMAGE.qcow2\r
+            #image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" }\r
+        -   type:      xml\r
+            image:     /path/to/imagefolder/ADDITIONAL-DISK.qcow2   # Optional, depending on the device type\r
+            image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" }  # Optional, depending on the device type\r
+            vpci:      "0000:00:03.0"                          # Optional, depending on the device type (not needed for disk or cdrom)\r
+            xml:   '   xml text for XML described devices. Do not use single quotes inside\r
+                       The following words, if found, will be replaced:\r
+                       __file__    by image path, (image must be provided)\r
+                       __format__  by qcow2 or raw (image must be provided)\r
+                       __dev__     by device letter (b, c, d ...)\r
+                       __vpci__    by vpci (vpci must be provided)\r
+                   '\r
+    # Additional Virtual Machines can be included here\r
+\r
diff --git a/vnfs/vnf-template.yaml b/vnfs/vnf-template.yaml
new file mode 100644 (file)
index 0000000..31c8655
--- /dev/null
@@ -0,0 +1,87 @@
+---\r
+vnf:\r
+    name: TEMPLATE\r
+    description: This is a template to help in the creation of your own VNFs\r
+    # class: parent      # Optional. Used to organize VNFs\r
+    external-connections:\r
+    -   name:              mgmt0\r
+        type:              mgmt        # "mgmt" (autoconnect to management net), "bridge", "data"\r
+        VNFC:              TEMPLATE-VM # Virtual Machine this interface belongs to\r
+        local_iface_name:  mgmt0       # interface name inside this Virtual Machine (must be defined in the VNFC section)\r
+        description:       Management interface\r
+    -   name:              xe0\r
+        type:              data\r
+        VNFC:              TEMPLATE-VM\r
+        local_iface_name:  xe0\r
+        description:       Data interface 1\r
+    -   name:              xe1\r
+        type:              data\r
+        VNFC:              TEMPLATE-VM\r
+        local_iface_name:  xe1\r
+        description:       Data interface 2\r
+    -   name:              ge0\r
+        type:              bridge\r
+        VNFC:              TEMPLATE-VM\r
+        local_iface_name:  ge0\r
+        description:       Bridge interface\r
+    VNFC:                              # Virtual machine array \r
+    -   name:        TEMPLATE-VM       # name of Virtual Machine\r
+        description: TEMPLATE description\r
+        VNFC image: /path/to/imagefolder/TEMPLATE-VM.qcow2\r
+        # image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" } #Optional\r
+        # processor:                     #Optional\r
+        #     model: Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz\r
+        #     features: ["64b", "iommu", "lps", "tlbps", "hwsv", "dioc", "ht"]\r
+        # hypervisor:                    #Optional\r
+        #     type: QEMU-kvm\r
+        #     version: "10002|12001|2.6.32-358.el6.x86_64"\r
+        # vcpus: 1          # Only for traditional cloud VMs. Number of virtual CPUs (oversubscription is allowed).\r
+        # ram: 1024         # Only for traditional cloud VMs. Memory in MBytes (not from hugepages, oversubscription is allowed)\r
+        # disk: 10          # disk size in GiB, by default 1\r
+        numas: \r
+        -   paired-threads: 5          # "cores", "paired-threads", "threads"\r
+            paired-threads-id: [ [0,1], [2,3], [4,5], [6,7], [8,9] ] # By default follows incremental order\r
+            memory: 14                 # GBytes\r
+            interfaces:\r
+            -   name:      xe0\r
+                vpci:      "0000:00:11.0"     \r
+                dedicated: "yes"         # "yes"(passthrough), "no"(sriov with vlan tags), "yes:sriov"(sriovi, but exclusive and without vlan tag)\r
+                bandwidth: 10 Gbps\r
+                # mac_address: '20:33:45:56:77:44' #avoid this option if possible\r
+            -   name:      xe1\r
+                vpci:      "0000:00:12.0"\r
+                dedicated: "yes"\r
+                bandwidth: 10 Gbps\r
+                # mac_address: '20:33:45:56:77:45' #avoid this option if possible\r
+        bridge-ifaces:\r
+        -   name:      mgmt0\r
+            vpci:      "0000:00:09.0"    # Optional. Virtual PCI address\r
+            bandwidth: 1 Mbps            # Optional. Informative only\r
+            # mac_address: '20:33:45:56:77:46' #avoid this option if possible\r
+            # model:       'virtio'      # ("virtio","e1000","ne2k_pci","pcnet","rtl8139") By default, it is automatically filled by libvirt\r
+        -   name:      ge0\r
+            vpci:      "0000:00:10.0"\r
+            bandwidth: 1 Mbps\r
+            # mac_address: '20:33:45:56:77:47' #avoid this option if possible\r
+            # model:       'virtio'      # ("virtio","e1000","ne2k_pci","pcnet","rtl8139") By default, it is automatically filled by libvirt\r
+        devices:                       # Optional, order determines device letter asignation (hda, hdb, ...)\r
+        -   type:      disk            # "disk","cdrom","xml"\r
+            image:     /path/to/imagefolder/SECOND-DISK.qcow2\r
+            # image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" }\r
+            # vpci:      "0000:00:03.0"   # Optional, not for disk or cdrom\r
+        -   type:      cdrom\r
+            image:     /path/to/imagefolder/CDROM-IMAGE.qcow2\r
+            # image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" }\r
+        -   type:      xml\r
+            image:     /path/to/imagefolder/ADDITIONAL-DISK.qcow2   # Optional, depending on the device type\r
+            image metadata: {"bus":"ide", "os_type":"windows", "use_incremental": "no" }  # Optional, depending on the device type\r
+            vpci:      "0000:00:03.0"                          # Optional, depending on the device type (not needed for disk or cdrom)\r
+            xml:   '   xml text for XML described devices. Do not use single quotes inside\r
+                       The following words, if found, will be replaced:\r
+                       __file__    by image path, (image must be provided)\r
+                       __format__  by qcow2 or raw (image must be provided)\r
+                       __dev__     by device letter (b, c, d ...) \r
+                       __vpci__    by vpci (vpci must be provided)\r
+                   '\r
+    # Additional Virtual Machines would be included here\r
+\r