1 # Copyright (c) 2022 Nokia
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Subscription interface implements the subscription manager REST based interface defined in
16 # https://docs.o-ran-sc.org/projects/o-ran-sc-ric-plt-submgr/en/latest/user-guide.html
19 import ricxappframe.subsclient as subsclient
20 import ricxappframe.xapp_rest as ricrest
21 from mdclogpy import Logger
23 logging = Logger(name=__name__)
26 class NewSubscriber():
28 def __init__(self, uri, timeout=None, local_address="0.0.0.0", local_port=8088, rmr_port=4061):
35 xapp submgr service uri
39 local interface IP address for rest service binding (for response handler)
41 local service port nunber for rest service binding (for response handler)
46 self.timeout = timeout
47 self.local_address = local_address
48 self.local_port = local_port
49 self.rmr_port = rmr_port
50 self.url = "/ric/v1/subscriptions/response"
51 self.serverHandler = None
52 self.responseCB = None
54 configuration = subsclient.Configuration()
55 configuration.verify_ssl = False
56 configuration.host = "http://127.0.0.1:8088/"
57 self.api = subsclient.ApiClient(configuration)
59 def _responsePostHandler(self, name, path, data, ctype):
62 internally used subscription reponse handler it the callback function is not set
64 return "{}", 'application/json', "OK", 200
66 # following methods are wrappers to hide the swagger client
67 def SubscriptionParamsClientEndpoint(self, host=None, http_port=None, rmr_port=None):
68 return subsclient.SubscriptionParamsClientEndpoint(host, http_port, rmr_port)
70 def SubscriptionParamsE2SubscriptionDirectives(self, e2_timeout_timer_value=None, e2_retry_count=None, rmr_routing_needed=None):
71 return subsclient.SubscriptionParamsE2SubscriptionDirectives(e2_timeout_timer_value, e2_retry_count, rmr_routing_needed)
73 def SubsequentAction(self, subsequent_action_type=None, time_to_wait=None):
74 return subsclient.SubsequentAction(subsequent_action_type, time_to_wait)
76 def ActionToBeSetup(self, action_id=None, action_type=None, action_definition=None, subsequent_action=None):
77 return subsclient.ActionToBeSetup(action_id, action_type, action_definition, subsequent_action)
79 def SubscriptionDetail(self, xapp_event_instance_id=None, event_triggers=None, action_to_be_setup_list=None):
80 return subsclient.SubscriptionDetail(xapp_event_instance_id, event_triggers, action_to_be_setup_list)
82 def SubscriptionParams(self, subscription_id=None, client_endpoint=None, meid=None, ran_function_id=None, e2_subscription_directives=None, subscription_details=None):
83 return subsclient.SubscriptionParams(subscription_id, client_endpoint, meid, ran_function_id, e2_subscription_directives, subscription_details)
85 def Subscribe(self, subs_params=None):
92 subs_params: SubscriptionParams
93 structured subscription data definition defined in subsclient
97 json string of SubscriptionResponse object
99 # if subs_params is not None and type(subs_params) is subsclient.models.subscription_params.SubscriptionParams:
100 if subs_params is not None:
101 response = self.api.request(method="POST", url=self.uri, headers=None, body=subs_params.to_dict())
102 return response.data, response.reason, response.status
103 return None, "Input parameter is not SubscriptionParams{}", 500
105 def UnSubscribe(self, subs_id=None):
113 subscription id returned in SubscriptionResponse
116 response.reason: string
121 response = self.api.request(method="DELETE", url=self.uri + "/subscriptions/" + subs_id, headers=None)
122 return response.data, response.reason, response.status
124 def QuerySubscriptions(self):
127 Query all subscriptions
131 response.data: json string
133 response.reason: string
138 response = self.api.request(method="GET", url=self.uri + "/subscriptions", headers=None)
139 return response.data, response.reason, response.status
141 def ResponseHandler(self, responseCB=None, server=None):
144 Starts the response handler and set the callback
149 Set the callback handler, if not set the the default self._responsePostHandler is used
150 server: xapp_rest.ThreadedHTTPServer
151 if set then the existing xapp_rest.ThreadedHTTPServer handler is used, otherwise a new will be created
156 True = success, False = failed
158 # create the thread HTTP server
159 self.serverHandler = server
160 if self.serverHandler is None:
161 # make the serverhandler
162 self.serverHandler = ricrest.ThreadedHTTPServer(self.local_address, self.local_port)
163 self.serverHandler.start()
164 if self.serverHandler is not None:
165 if responseCB is not None:
166 self.responseCB = responseCB
167 # get http handler with object reference
168 self.serverHandler.handler.add_handler(self.serverHandler.handler, "POST", "response", self.url, responseCB)