Adding Subscription and Metric feature
[ric-app/hw-python.git] / src / handler / SubscriptionHandler.py
1 # ==================================================================================
2 #
3 #       Copyright (c) 2021 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 Handles subscription messages from enbs and gnbs through rmr.
20 """
21
22 import json
23 from ricxappframe.xapp_frame import RMRXapp, rmr
24 from ..utils.constants import Constants
25 from ._BaseHandler import _BaseHandler
26 from ..manager.SdlManager import SdlManager
27 class SubscriptionHandler(_BaseHandler):
28
29     def __init__(self, rmr_xapp: RMRXapp, msgtype):
30         super().__init__(rmr_xapp, msgtype)
31
32
33     def request_handler(self, rmr_xapp, summary, sbuf):
34         """
35                 Handles subscription messages.
36
37                 Parameters
38                 ----------
39                 rmr_xapp: rmr Instance Context
40
41                 summary: dict (required)
42                     buffer content
43
44                 sbuf: str (required)
45                     length of the message
46         """
47         self._rmr_xapp.rmr_free(sbuf)
48         try:
49             req = json.loads(summary[rmr.RMR_MS_PAYLOAD])  # input should be a json encoded as bytes
50             self.logger.debug("SubscriptionHandler.resp_handler:: Handler processing request")
51         except (json.decoder.JSONDecodeError, KeyError):
52             self.logger.error("Subscription.resp_handler:: Handler failed to parse request")
53             return
54
55         if self.verifySubscription(req):
56             self.logger.info("SubscriptionHandler.resp_handler:: Handler processed request: {}".format(req))
57         else:
58             self.logger.error("SubscriptionHandler.resp_handler:: Request verification failed: {}".format(req))
59             return
60         self.logger.debug("SubscriptionHandler.resp_handler:: Request verification success: {}".format(req))
61
62
63     def verifySubscription(self, req: dict):
64         for i in ["subscription_id", "message"]:
65             if i not in req:
66                 return False
67         return True
68
69
70
71
72
73
74
75
76
77