bug 495 check descriptor dependencies at create,delete,edit
[osm/NBI.git] / osm_nbi / nbi.py
index dda9946..566fb2b 100644 (file)
@@ -8,9 +8,9 @@ import yaml
 import html_out as html
 import logging
 from engine import Engine, EngineException
-from dbbase import DbException
-from fsbase import FsException
-from msgbase import MsgException
+from osm_common.dbbase import DbException
+from osm_common.fsbase import FsException
+from osm_common.msgbase import MsgException
 from base64 import standard_b64decode
 #from os import getenv
 from http import HTTPStatus
@@ -67,6 +67,8 @@ URL: /osm                                                       GET     POST
             /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
@@ -150,10 +152,10 @@ class Server(object):
                         "<ID>": {"METHODS": ("GET", "DELETE")}
                     },
                     "vim_accounts": {"METHODS": ("GET", "POST"),
-                        "<ID>": {"METHODS": ("GET", "DELETE")}
+                        "<ID>": {"METHODS": ("GET", "DELETE", "PATCH")}
                     },
                     "sdns": {"METHODS": ("GET", "POST"),
-                        "<ID>": {"METHODS": ("GET", "DELETE")}
+                        "<ID>": {"METHODS": ("GET", "DELETE", "PATCH")}
                     },
                 }
             },
@@ -215,7 +217,10 @@ class Server(object):
                     },
                     "ns_lcm_op_occs": {"METHODS": "GET",
                         "<ID>": {"METHODS": "GET"},
-                    }
+                    },
+                    "vnfrs": {"METHODS": ("GET"),
+                        "<ID>": {"METHODS": ("GET")}
+                    },
                 }
             },
         }
@@ -338,6 +343,8 @@ class Server(object):
             raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST)
         except KeyError as exc:
             raise NbiException("Query string error: " + str(exc), HTTPStatus.BAD_REQUEST)
+        except Exception as exc:
+            raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST)
 
     @staticmethod
     def _format_out(data, session=None, _format=None):
@@ -616,6 +623,10 @@ class Server(object):
                 method = kwargs.pop("METHOD")
             else:
                 method = cherrypy.request.method
+            if kwargs and "FORCE" in kwargs:
+                force = kwargs.pop("FORCE")
+            else:
+                force = False
 
             self._check_valid_url_method(method, topic, version, item, _id, item2, *args)
 
@@ -639,6 +650,8 @@ class Server(object):
                 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"
 
@@ -663,7 +676,8 @@ class Server(object):
                 if item in ("ns_descriptors_content", "vnf_packages_content"):
                     _id = cherrypy.request.headers.get("Transaction-Id")
                     if not _id:
-                        _id = self.engine.new_item(session, engine_item, {}, None, cherrypy.request.headers)
+                        _id = self.engine.new_item(session, engine_item, {}, None, cherrypy.request.headers,
+                                                   force=force)
                         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:
@@ -672,7 +686,7 @@ class Server(object):
                         cherrypy.response.headers["Transaction-Id"] = _id
                     outdata = {"id": _id}
                 elif item == "ns_instances_content":
-                    _id = self.engine.new_item(session, engine_item, indata, kwargs)
+                    _id = self.engine.new_item(session, engine_item, indata, kwargs, force=force)
                     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)
@@ -683,7 +697,8 @@ class Server(object):
                     outdata = {"id": _id}
                     cherrypy.response.status = HTTPStatus.ACCEPTED.value
                 else:
-                    _id = self.engine.new_item(session, engine_item, indata, kwargs, cherrypy.request.headers)
+                    _id = self.engine.new_item(session, engine_item, indata, kwargs, cherrypy.request.headers,
+                                               force=force)
                     self._set_location_header(topic, version, item, _id)
                     outdata = {"id": _id}
                     # TODO form NsdInfo when item in ("ns_descriptors", "vnf_packages")
@@ -699,7 +714,6 @@ class Server(object):
                         outdata = {"_id": opp_id}
                         cherrypy.response.status = HTTPStatus.ACCEPTED.value
                     else:
-                        force = kwargs.get("FORCE")
                         self.engine.del_item(session, engine_item, _id, force)
                         cherrypy.response.status = HTTPStatus.NO_CONTENT.value
                 if engine_item in ("vim_accounts", "sdns"):
@@ -716,7 +730,12 @@ class Server(object):
                     cherrypy.response.status = HTTPStatus.NO_CONTENT.value
                     outdata = None
                 else:
-                    outdata = {"id": self.engine.edit_item(session, engine_item, args[1], indata, kwargs)}
+                    outdata = {"id": self.engine.edit_item(session, engine_item, _id, indata, kwargs, force=force)}
+            elif method == "PATCH":
+                if not indata and not kwargs:
+                    raise NbiException("Nothing to update. Provide payload and/or query string",
+                                       HTTPStatus.BAD_REQUEST)
+                outdata = {"id": self.engine.edit_item(session, engine_item, _id, indata, kwargs, force=force)}
             else:
                 raise NbiException("Method {} not allowed".format(method), HTTPStatus.METHOD_NOT_ALLOWED)
             return self._format_out(outdata, session, _format)