Feature 10339 - Enhanced Alarm Mgmt. (SOL005 FM Interface) 11/10511/8
authorAtul Agarwal <atul.agarwal@altran.com>
Thu, 18 Mar 2021 08:19:32 +0000 (08:19 +0000)
committerAtul Agarwal <Atul.Agarwal@Altran.com>
Thu, 27 May 2021 15:47:25 +0000 (15:47 +0000)
Change-Id: I80ee1fb48fe3ad7ac48cd3c346d2d8b8c3181c8c
Signed-off-by: Atul Agarwal <atul.agarwal@altran.com>
Signed-off-by: Atul Agarwal <Atul.Agarwal@Altran.com>
osm_nbi/auth.py
osm_nbi/nbi.py
osm_nbi/roles_to_operations.yml
requirements.txt

index 7c29f84..6c9ee92 100644 (file)
@@ -400,7 +400,6 @@ class Authenticator:
         It works in a shadow copy and replace at the end to allow other threads working with the old copy
         :return: None
         """
-
         permissions = {oper: [] for oper in self.role_permissions}
         # records = self.db.get_list(self.roles_to_operations_table)
         records = self.backend.get_role_list()
@@ -503,6 +502,7 @@ class Authenticator:
                     query_string_operations,
                     item_id,
                 )
+                self.logger.info("RBAC_auth: {}", format(RBAC_auth))
                 token_info["allow_show_user_project_role"] = RBAC_auth
 
             return token_info
index d5a81ce..67cf58b 100644 (file)
@@ -569,6 +569,16 @@ valid_url_methods = {
             },
         },
     },
+    "nsfm": {
+        "v1": {
+            "alarms": {"METHODS": ("GET", "PATCH"),
+                       "ROLE_PERMISSION": "alarms:",
+                       "<ID>": {"METHODS": ("GET", "PATCH"),
+                                "ROLE_PERMISSION": "alarms:id:",
+                                },
+                       }
+        },
+    },
 }
 
 
@@ -823,6 +833,91 @@ class Server(object):
         if token_info.get("id"):
             cherrypy.request.login += ";session=" + token_info["id"][0:12]
 
+    # NS Fault Management
+    @cherrypy.expose
+    def nsfm(self, version=None, topic=None, uuid=None, project_name=None, ns_id=None, *args, **kwargs):
+        if topic == 'alarms':
+            try:
+                method = cherrypy.request.method
+                role_permission = self._check_valid_url_method(method, "nsfm", version, topic, None, None, *args)
+                query_string_operations = self._extract_query_string_operations(kwargs, method)
+
+                self.authenticator.authorize(role_permission, query_string_operations, None)
+
+                # to handle get request
+                if cherrypy.request.method == 'GET':
+                    # if request is on basis of uuid
+                    if uuid and uuid != 'None':
+                        try:
+                            alarm = self.engine.db.get_one("alarms", {"uuid": uuid})
+                            alarm_action = self.engine.db.get_one("alarms_action", {"uuid": uuid})
+                            alarm.update(alarm_action)
+                            vnf = self.engine.db.get_one("vnfrs", {"nsr-id-ref": alarm["tags"]["ns_id"]})
+                            alarm["vnf-id"] = vnf["_id"]
+                            return self._format_out(str(alarm))
+                        except Exception:
+                            return self._format_out("Please provide valid alarm uuid")
+                    elif ns_id and ns_id != 'None':
+                        # if request is on basis of ns_id
+                        try:
+                            alarms = self.engine.db.get_list("alarms", {"tags.ns_id": ns_id})
+                            for alarm in alarms:
+                                alarm_action = self.engine.db.get_one("alarms_action", {"uuid": alarm['uuid']})
+                                alarm.update(alarm_action)
+                            return self._format_out(str(alarms))
+                        except Exception:
+                            return self._format_out("Please provide valid ns id")
+                    else:
+                        # to return only alarm which are related to given project
+                        project = self.engine.db.get_one("projects", {"name": project_name})
+                        project_id = project.get('_id')
+                        ns_list = self.engine.db.get_list("nsrs", {"_admin.projects_read": project_id})
+                        ns_ids = []
+                        for ns in ns_list:
+                            ns_ids.append(ns.get("_id"))
+                        alarms = self.engine.db.get_list("alarms")
+                        alarm_list = [alarm for alarm in alarms if alarm["tags"]["ns_id"] in ns_ids]
+                        for alrm in alarm_list:
+                            action = self.engine.db.get_one("alarms_action", {"uuid": alrm.get("uuid")})
+                            alrm.update(action)
+                        return self._format_out(str(alarm_list))
+                # to handle patch request for alarm update
+                elif cherrypy.request.method == 'PATCH':
+                    data = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader)
+                    try:
+                        # check if uuid is valid
+                        self.engine.db.get_one("alarms", {"uuid": data.get("uuid")})
+                    except Exception:
+                        return self._format_out("Please provide valid alarm uuid.")
+                    if data.get("is_enable") is not None:
+                        if data.get("is_enable"):
+                            alarm_status = 'ok'
+                        else:
+                            alarm_status = 'disabled'
+                        self.engine.db.set_one("alarms", {"uuid": data.get("uuid")},
+                                               {"alarm_status": alarm_status})
+                    else:
+                        self.engine.db.set_one("alarms", {"uuid": data.get("uuid")},
+                                               {"threshold": data.get("threshold")})
+                    return self._format_out("Alarm updated")
+            except Exception as e:
+                cherrypy.response.status = e.http_code.value
+                if isinstance(e, (NbiException, EngineException, DbException, FsException, MsgException, AuthException,
+                              ValidationError, AuthconnException)):
+                    http_code_value = cherrypy.response.status = e.http_code.value
+                    http_code_name = e.http_code.name
+                    cherrypy.log("Exception {}".format(e))
+                else:
+                    http_code_value = cherrypy.response.status = HTTPStatus.BAD_REQUEST.value  # INTERNAL_SERVER_ERROR
+                    cherrypy.log("CRITICAL: Exception {}".format(e), traceback=True)
+                    http_code_name = HTTPStatus.BAD_REQUEST.name
+                problem_details = {
+                    "code": http_code_name,
+                    "status": http_code_value,
+                    "detail": str(e),
+                }
+                return self._format_out(problem_details)
+
     @cherrypy.expose
     def token(self, method, token_id=None, kwargs=None):
         token_info = None
index 38cd620..13cbffd 100644 (file)
@@ -148,6 +148,10 @@ roles:
         pduds:        false
         pduds:get:    true
         pduds:id:get: true
+        # Alarms
+        alarms:        false
+        alarms:get:    true
+        alarms:id:get: true
 
   - name: "anonymous"
     permissions:
index 1861ebd..30dabd5 100644 (file)
@@ -158,6 +158,3 @@ zc.lockfile==2.0
 # 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.
-
-