Added vnfR support
[osm/NBI.git] / osm_nbi / nbi.py
index aa8105a..a8f891d 100644 (file)
@@ -19,7 +19,9 @@ from codecs import getreader
 from os import environ
 
 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
 from os import environ
 
 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
-__version__ = "0.3"
+
+# TODO consider to remove and provide version using the static version file
+__version__ = "0.1.3"
 version_date = "Apr 2018"
 database_version = '1.0'
 
 version_date = "Apr 2018"
 database_version = '1.0'
 
@@ -65,6 +67,8 @@ URL: /osm                                                       GET     POST
             /ns_lcm_op_occs                                     5       5
                 /<nsLcmOpOccId>                                 5                       5       5
                     TO BE COMPLETED                             5               5
             /ns_lcm_op_occs                                     5       5
                 /<nsLcmOpOccId>                                 5                       5       5
                     TO BE COMPLETED                             5               5
+            /vnfrs                                              O
+                /<vnfrId>                                       O
             /subscriptions                                      5       5
                 /<subscriptionId>                               5                       X
         /admin/v1
             /subscriptions                                      5       5
                 /<subscriptionId>                               5                       X
         /admin/v1
@@ -74,7 +78,7 @@ URL: /osm                                                       GET     POST
                 /<id>                                           O                       O     
             /projects                                           O       O
                 /<id>                                           O                       O     
                 /<id>                                           O                       O     
             /projects                                           O       O
                 /<id>                                           O                       O     
-            /vims                                               O       O
+            /vims_accounts  (also vims for compatibility)       O       O
                 /<id>                                           O                       O       O     
             /sdns                                               O       O
                 /<id>                                           O                       O       O     
                 /<id>                                           O                       O       O     
             /sdns                                               O       O
                 /<id>                                           O                       O       O     
@@ -147,6 +151,9 @@ class Server(object):
                     "vims": {"METHODS": ("GET", "POST"),
                         "<ID>": {"METHODS": ("GET", "DELETE")}
                     },
                     "vims": {"METHODS": ("GET", "POST"),
                         "<ID>": {"METHODS": ("GET", "DELETE")}
                     },
+                    "vim_accounts": {"METHODS": ("GET", "POST"),
+                        "<ID>": {"METHODS": ("GET", "DELETE")}
+                    },
                     "sdns": {"METHODS": ("GET", "POST"),
                         "<ID>": {"METHODS": ("GET", "DELETE")}
                     },
                     "sdns": {"METHODS": ("GET", "POST"),
                         "<ID>": {"METHODS": ("GET", "DELETE")}
                     },
@@ -210,7 +217,10 @@ class Server(object):
                     },
                     "ns_lcm_op_occs": {"METHODS": "GET",
                         "<ID>": {"METHODS": "GET"},
                     },
                     "ns_lcm_op_occs": {"METHODS": "GET",
                         "<ID>": {"METHODS": "GET"},
-                    }
+                    },
+                    "vnfrs": {"METHODS": ("GET"),
+                        "<ID>": {"METHODS": ("GET")}
+                    },
                 }
             },
         }
                 }
             },
         }
@@ -347,7 +357,7 @@ class Server(object):
         if data is None:
             if accept and "text/html" in accept:
                 return html.format(data, cherrypy.request, cherrypy.response, session)
         if data is None:
             if accept and "text/html" in accept:
                 return html.format(data, cherrypy.request, cherrypy.response, session)
-            cherrypy.response.status = HTTPStatus.NO_CONTENT.value
+            cherrypy.response.status = HTTPStatus.NO_CONTENT.value
             return
         elif hasattr(data, "read"):  # file object
             if _format:
             return
         elif hasattr(data, "read"):  # file object
             if _format:
@@ -396,6 +406,7 @@ class Server(object):
 
     @cherrypy.expose
     def version(self, *args, **kwargs):
 
     @cherrypy.expose
     def version(self, *args, **kwargs):
+        # TODO consider to remove and provide version using the static version file
         global __version__, version_date
         try:
             if cherrypy.request.method != "GET":
         global __version__, version_date
         try:
             if cherrypy.request.method != "GET":
@@ -597,6 +608,7 @@ class Server(object):
         _format = None
         method = "DONE"
         engine_item = None
         _format = None
         method = "DONE"
         engine_item = None
+        rollback = None
         try:
             if not topic or not version or not item:
                 raise NbiException("URL must contain at least 'topic/version/item'", HTTPStatus.METHOD_NOT_ALLOWED)
         try:
             if not topic or not version or not item:
                 raise NbiException("URL must contain at least 'topic/version/item'", HTTPStatus.METHOD_NOT_ALLOWED)
@@ -632,6 +644,10 @@ class Server(object):
                 engine_item = "nsrs"
                 if item == "ns_lcm_op_occs":
                     engine_item = "nslcmops"
                 engine_item = "nsrs"
                 if item == "ns_lcm_op_occs":
                     engine_item = "nslcmops"
+                if item == "vnfrs":
+                    engine_item = "vnfrs"
+            if engine_item == "vims":   # TODO this is for backward compatibility, it will remove in the future
+                engine_item = "vim_accounts"
 
             if method == "GET":
                 if item2 in ("nsd_content", "package_content", "artifacts", "vnfd", "nsd"):
 
             if method == "GET":
                 if item2 in ("nsd_content", "package_content", "artifacts", "vnfd", "nsd"):
@@ -655,6 +671,7 @@ class Server(object):
                     _id = cherrypy.request.headers.get("Transaction-Id")
                     if not _id:
                         _id = self.engine.new_item(session, engine_item, {}, None, cherrypy.request.headers)
                     _id = cherrypy.request.headers.get("Transaction-Id")
                     if not _id:
                         _id = self.engine.new_item(session, engine_item, {}, None, cherrypy.request.headers)
+                        rollback = {"session": session, "item": engine_item, "_id": _id, "force": True}
                     completed = self.engine.upload_content(session, engine_item, _id, indata, kwargs, cherrypy.request.headers)
                     if completed:
                         self._set_location_header(topic, version, item, _id)
                     completed = self.engine.upload_content(session, engine_item, _id, indata, kwargs, cherrypy.request.headers)
                     if completed:
                         self._set_location_header(topic, version, item, _id)
@@ -663,6 +680,7 @@ class Server(object):
                     outdata = {"id": _id}
                 elif item == "ns_instances_content":
                     _id = self.engine.new_item(session, engine_item, indata, kwargs)
                     outdata = {"id": _id}
                 elif item == "ns_instances_content":
                     _id = self.engine.new_item(session, engine_item, indata, kwargs)
+                    rollback = {"session": session, "item": engine_item, "_id": _id, "force": True}
                     self.engine.ns_action(session, _id, "instantiate", {}, None)
                     self._set_location_header(topic, version, item, _id)
                     outdata = {"id": _id}
                     self.engine.ns_action(session, _id, "instantiate", {}, None)
                     self._set_location_header(topic, version, item, _id)
                     outdata = {"id": _id}
@@ -677,17 +695,23 @@ class Server(object):
                     outdata = {"id": _id}
                     # TODO form NsdInfo when item in ("ns_descriptors", "vnf_packages")
                 cherrypy.response.status = HTTPStatus.CREATED.value
                     outdata = {"id": _id}
                     # TODO form NsdInfo when item in ("ns_descriptors", "vnf_packages")
                 cherrypy.response.status = HTTPStatus.CREATED.value
+
             elif method == "DELETE":
                 if not _id:
                     outdata = self.engine.del_item_list(session, engine_item, kwargs)
             elif method == "DELETE":
                 if not _id:
                     outdata = self.engine.del_item_list(session, engine_item, kwargs)
+                    cherrypy.response.status = HTTPStatus.OK.value
                 else:  # len(args) > 1
                     if item == "ns_instances_content":
                 else:  # len(args) > 1
                     if item == "ns_instances_content":
-                        self.engine.ns_action(session, _id, "terminate", {"autoremove": True}, None)
+                        opp_id = self.engine.ns_action(session, _id, "terminate", {"autoremove": True}, None)
+                        outdata = {"_id": opp_id}
+                        cherrypy.response.status = HTTPStatus.ACCEPTED.value
                     else:
                         force = kwargs.get("FORCE")
                         self.engine.del_item(session, engine_item, _id, force)
                     else:
                         force = kwargs.get("FORCE")
                         self.engine.del_item(session, engine_item, _id, force)
-                    # TODO return 202 ACCEPTED for nsrs vims
-                    outdata = None
+                        cherrypy.response.status = HTTPStatus.NO_CONTENT.value
+                if engine_item in ("vim_accounts", "sdns"):
+                    cherrypy.response.status = HTTPStatus.ACCEPTED.value
+
             elif method == "PUT":
                 if not indata and not kwargs:
                     raise NbiException("Nothing to update. Provide payload and/or query string",
             elif method == "PUT":
                 if not indata and not kwargs:
                     raise NbiException("Nothing to update. Provide payload and/or query string",
@@ -696,6 +720,7 @@ class Server(object):
                     completed = self.engine.upload_content(session, engine_item, _id, indata, kwargs, cherrypy.request.headers)
                     if not completed:
                         cherrypy.response.headers["Transaction-Id"] = id
                     completed = self.engine.upload_content(session, engine_item, _id, indata, kwargs, cherrypy.request.headers)
                     if not completed:
                         cherrypy.response.headers["Transaction-Id"] = id
+                    cherrypy.response.status = HTTPStatus.NO_CONTENT.value
                     outdata = None
                 else:
                     outdata = {"id": self.engine.edit_item(session, engine_item, args[1], indata, kwargs)}
                     outdata = None
                 else:
                     outdata = {"id": self.engine.edit_item(session, engine_item, args[1], indata, kwargs)}
@@ -703,10 +728,15 @@ class Server(object):
                 raise NbiException("Method {} not allowed".format(method), HTTPStatus.METHOD_NOT_ALLOWED)
             return self._format_out(outdata, session, _format)
         except (NbiException, EngineException, DbException, FsException, MsgException) as e:
                 raise NbiException("Method {} not allowed".format(method), HTTPStatus.METHOD_NOT_ALLOWED)
             return self._format_out(outdata, session, _format)
         except (NbiException, EngineException, DbException, FsException, MsgException) as e:
-            if hasattr(outdata, "close"):  # is an open file
-                outdata.close()
             cherrypy.log("Exception {}".format(e))
             cherrypy.response.status = e.http_code.value
             cherrypy.log("Exception {}".format(e))
             cherrypy.response.status = e.http_code.value
+            if hasattr(outdata, "close"):  # is an open file
+                outdata.close()
+            if rollback:
+                try:
+                    self.engine.del_item(**rollback)
+                except Exception as e2:
+                    cherrypy.log("Rollback Exception {}: {}".format(rollback, e2))
             error_text = str(e)
             if isinstance(e, MsgException):
                 error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format(
             error_text = str(e)
             if isinstance(e, MsgException):
                 error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format(