Feature 11022,11025: Advanced Cluster Management

Change-Id: I4168366f79b11de15f6808977fb15a3ff270f519
Signed-off-by: yshah <shahithya.y@tataelxsi.co.in>
diff --git a/osm_nbi/nbi.py b/osm_nbi/nbi.py
index f031665..ea7a236 100644
--- a/osm_nbi/nbi.py
+++ b/osm_nbi/nbi.py
@@ -48,6 +48,7 @@
 nbi_server = None  # instance of Server class
 subscription_thread = None  # instance of SubscriptionThread class
 cef_logger = None
+logger = logging.getLogger("nbi.nbi")
 
 """
 North Bound Interface  (O: OSM specific; 5,X: SOL005 not implemented yet; O5: SOL005 implemented)
@@ -706,6 +707,18 @@
                         "METHODS": ("DELETE",),
                         "ROLE_PERMISSION": "k8scluster:id:deregister:",
                     },
+                    "get_creds": {
+                        "METHODS": ("GET",),
+                        "ROLE_PERMISSION": "k8scluster:id:get_creds:",
+                    },
+                    "scale": {
+                        "METHODS": ("POST",),
+                        "ROLE_PERMISSION": "k8scluster:id:scale:",
+                    },
+                    "upgrade": {
+                        "METHODS": ("POST",),
+                        "ROLE_PERMISSION": "k8scluster:id:upgrade:",
+                    },
                 },
                 "register": {
                     "METHODS": ("POST",),
@@ -746,6 +759,46 @@
             },
         }
     },
+    "ksu": {
+        "v1": {
+            "ksus": {
+                "METHODS": ("GET", "POST"),
+                "ROLE_PERMISSION": "ksu:",
+                "<ID>": {
+                    "METHODS": ("GET", "PATCH", "DELETE"),
+                    "ROLE_PERMISSION": "ksu:id:",
+                    "clone": {
+                        "METHODS": ("POST",),
+                        "ROLE_PERMISSION": "ksu:id:clone:",
+                    },
+                    "move": {
+                        "METHODS": ("POST",),
+                        "ROLE_PERMISSION": "ksu:id:move:",
+                    },
+                },
+                "update": {
+                    "METHODS": ("POST",),
+                    "ROLE_PERMISSION": "ksu:",
+                },
+                "delete": {
+                    "METHODS": ("POST",),
+                    "ROLE_PERMISSION": "ksu:",
+                },
+            },
+        }
+    },
+    "oka": {
+        "v1": {
+            "oka_packages": {
+                "METHODS": ("GET", "POST"),
+                "ROLE_PERMISSION": "oka_pkg:",
+                "<ID>": {
+                    "METHODS": ("GET", "PATCH", "DELETE", "PUT"),
+                    "ROLE_PERMISSION": "oka_pkg:id:",
+                },
+            }
+        }
+    },
 }
 
 
@@ -764,6 +817,7 @@
         self.instance += 1
         self.authenticator = Authenticator(valid_url_methods, valid_query_string)
         self.engine = Engine(self.authenticator)
+        self.logger = logging.getLogger("nbi.server")
 
     def _format_in(self, kwargs):
         error_text = ""  # error_text must be initialized outside try
@@ -793,13 +847,32 @@
                         "multipart/form-data"
                         in cherrypy.request.headers["Content-Type"]
                     ):
-                        if "descriptor_file" in kwargs:
-                            filecontent = kwargs.pop("descriptor_file")
+                        if (
+                            "descriptor_file" in kwargs
+                            or "package" in kwargs
+                            and "name" in kwargs
+                        ):
+                            filecontent = ""
+                            if "descriptor_file" in kwargs:
+                                filecontent = kwargs.pop("descriptor_file")
+                            if "package" in kwargs:
+                                filecontent = kwargs.pop("package")
                             if not filecontent.file:
                                 raise NbiException(
                                     "empty file or content", HTTPStatus.BAD_REQUEST
                                 )
-                            indata = filecontent.file  # .read()
+                            indata = filecontent
+                            if filecontent.content_type.value:
+                                cherrypy.request.headers[
+                                    "Content-Type"
+                                ] = filecontent.content_type.value
+                        elif "package" in kwargs:
+                            filecontent = kwargs.pop("package")
+                            if not filecontent.file:
+                                raise NbiException(
+                                    "empty file or content", HTTPStatus.BAD_REQUEST
+                                )
+                            indata = filecontent
                             if filecontent.content_type.value:
                                 cherrypy.request.headers[
                                     "Content-Type"
@@ -1550,6 +1623,8 @@
                 "nspm",
                 "vnflcm",
                 "k8scluster",
+                "ksu",
+                "oka",
             ):
                 raise NbiException(
                     "URL main_topic '{}' not supported".format(main_topic),
@@ -1631,6 +1706,14 @@
                     engine_topic = "resources"
                 elif topic == "app_profiles":
                     engine_topic = "apps"
+            elif main_topic == "k8scluster" and item in (
+                "upgrade",
+                "get_creds",
+                "scale",
+            ):
+                engine_topic = "k8s"
+            elif main_topic == "ksu" and engine_topic in ("ksus", "clone", "move"):
+                engine_topic = "ksus"
             if (
                 engine_topic == "vims"
             ):  # TODO this is for backward compatibility, it will be removed in the future
@@ -1686,6 +1769,10 @@
                         filter_q,
                         api_req=True,
                     )
+                elif topic == "clusters" and item == "get_creds":
+                    outdata = self.engine.get_cluster_info(
+                        engine_session, engine_topic, _id, item
+                    )
                 else:
                     if item == "reports":
                         # TODO check that project_id (_id in this context) has permissions
@@ -1706,6 +1793,7 @@
                     "ns_config_template",
                 ):
                     _id = cherrypy.request.headers.get("Transaction-Id")
+
                     if not _id:
                         _id, _ = self.engine.new_item(
                             rollback,
@@ -1728,6 +1816,33 @@
                     else:
                         cherrypy.response.headers["Transaction-Id"] = _id
                     outdata = {"id": _id}
+                elif topic == "oka_packages":
+                    _id = cherrypy.request.headers.get("Transaction-Id")
+
+                    if not _id:
+                        _id, _ = self.engine.new_item(
+                            rollback,
+                            engine_session,
+                            engine_topic,
+                            {},
+                            kwargs,
+                            cherrypy.request.headers,
+                        )
+                    cherrypy.request.headers["method"] = cherrypy.request.method
+                    if indata:
+                        completed = self.engine.upload_content(
+                            engine_session,
+                            engine_topic,
+                            _id,
+                            indata,
+                            None,
+                            cherrypy.request.headers,
+                        )
+                    if completed:
+                        self._set_location_header(main_topic, version, topic, _id)
+                    else:
+                        cherrypy.response.headers["Transaction-Id"] = _id
+                    outdata = {"_id": _id}
                 elif topic == "ns_instances_content":
                     # creates NSR
                     _id, _ = self.engine.new_item(
@@ -1865,6 +1980,39 @@
                     )
                     self._set_location_header(main_topic, version, topic, _id)
                     outdata = {"_id": _id}
+                elif topic == "ksus" and item:
+                    if item == "clone":
+                        _id = self.engine.clone(
+                            rollback,
+                            engine_session,
+                            engine_topic,
+                            _id,
+                            indata,
+                            kwargs,
+                            cherrypy.request.headers,
+                        )
+                        self._set_location_header(main_topic, version, topic, _id)
+                        outdata = {"id": _id}
+                    if item == "move":
+                        op_id = self.engine.move_ksu(
+                            engine_session, engine_topic, _id, indata, kwargs
+                        )
+                        outdata = {"op_id": op_id}
+                elif topic == "ksus" and _id == "delete":
+                    op_id = self.engine.delete_ksu(
+                        engine_session, engine_topic, _id, indata
+                    )
+                    outdata = {"op_id": op_id}
+                elif topic == "ksus" and _id == "update":
+                    op_id = self.engine.edit_item(
+                        engine_session, engine_topic, _id, indata, kwargs
+                    )
+                    outdata = {"op_id": op_id}
+                elif topic == "clusters" and item in ("upgrade", "scale"):
+                    op_id = self.engine.update_cluster(
+                        engine_session, engine_topic, _id, item, indata
+                    )
+                    outdata = {"op_id": op_id}
                 else:
                     _id, op_id = self.engine.new_item(
                         rollback,
@@ -1927,6 +2075,11 @@
                             if op_id
                             else HTTPStatus.NO_CONTENT.value
                         )
+                    elif topic == "ksus":
+                        op_id = self.engine.delete_ksu(
+                            engine_session, engine_topic, _id, indata
+                        )
+                        outdata = {"op_id": op_id}
                     # if there is not any deletion in process, delete
                     elif not op_id:
                         op_id = self.engine.del_item(engine_session, engine_topic, _id)
@@ -1974,6 +2127,41 @@
                     op_id = self.engine.edit(
                         engine_session, engine_topic, _id, item, indata, kwargs
                     )
+                elif topic == "oka_packages" and method == "PATCH":
+                    if kwargs:
+                        op_id = self.engine.edit_item(
+                            engine_session, engine_topic, _id, None, kwargs
+                        )
+                    if indata:
+                        if indata.get("name") or indata.get("description"):
+                            op_id = self.engine.edit_item(
+                                engine_session, engine_topic, _id, indata, kwargs
+                            )
+                        else:
+                            cherrypy.request.headers["method"] = cherrypy.request.method
+                            completed = self.engine.upload_content(
+                                engine_session,
+                                engine_topic,
+                                _id,
+                                indata,
+                                {},
+                                cherrypy.request.headers,
+                            )
+                            if not completed:
+                                cherrypy.response.headers["Transaction-Id"] = id
+                elif topic == "oka_packages" and method == "PUT":
+                    if indata:
+                        cherrypy.request.headers["method"] = cherrypy.request.method
+                        completed = self.engine.upload_content(
+                            engine_session,
+                            engine_topic,
+                            _id,
+                            indata,
+                            {},
+                            cherrypy.request.headers,
+                        )
+                        if not completed:
+                            cherrypy.response.headers["Transaction-Id"] = id
                 else:
                     op_id = self.engine.edit_item(
                         engine_session, engine_topic, _id, indata, kwargs