Added A1 policy handler, healthcheck handler, sdl handler and alarm
[ric-app/hw-python.git] / src / handler / A1PolicyHandler.py
1 # ==================================================================================
2 #
3 #       Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #          http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # ==================================================================================
18
19 import json
20 from ricxappframe.xapp_frame import RMRXapp, rmr
21 from ..utils.constants import Constants
22 from ._BaseHandler import _BaseHandler
23
24
25 class A1PolicyHandler(_BaseHandler):
26
27     def __init__(self, rmr_xapp: RMRXapp, msgtype):
28         super().__init__(rmr_xapp, msgtype)
29
30     def request_handler(self, rmr_xapp, summary, sbuf):
31         self._rmr_xapp.rmr_free(sbuf)
32         try:
33             req = json.loads(summary[rmr.RMR_MS_PAYLOAD])  # input should be a json encoded as bytes
34             self.logger.debug("A1PolicyHandler.resp_handler:: Handler processing request")
35         except (json.decoder.JSONDecodeError, KeyError):
36             self.logger.error("A1PolicyManager.resp_handler:: Handler failed to parse request")
37             return
38
39         if self.verifyPolicy(req):
40             self.logger.info("A1PolicyHandler.resp_handler:: Handler processed request: {}".format(req))
41         else:
42             self.logger.error("A1PolicyHandler.resp_handler:: Request verification failed: {}".format(req))
43             return
44         self.logger.debug("A1PolicyHandler.resp_handler:: Request verification success: {}".format(req))
45
46         resp = self.buildPolicyResp(req)
47         self._rmr_xapp.rmr_send(json.dumps(resp).encode(), Constants.A1_POLICY_RESP)
48         self.logger.info("A1PolicyHandler.resp_handler:: Response sent: {}".format(resp))
49
50     def verifyPolicy(self, req: dict):
51         for i in ["policy_type_id", "operation", "policy_instance_id"]:
52             if i not in req:
53                 return False
54         return True
55
56     def buildPolicyResp(self, req: dict):
57         req["handler_id"] = self._rmr_xapp.config["xapp_name"]
58         del req["operation"]
59         req["status"] = "OK"
60         return req