ehietala | 1248634 | 2022-06-15 12:21:41 +0300 | [diff] [blame] | 1 | # Copyright (c) 2022 Nokia |
| 2 | # |
| 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 |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 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. |
| 14 | # |
| 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 |
| 17 | # |
| 18 | |
| 19 | import ricxappframe.subsclient as subsclient |
| 20 | import ricxappframe.xapp_rest as ricrest |
| 21 | from mdclogpy import Logger |
| 22 | |
| 23 | logging = Logger(name=__name__) |
| 24 | |
| 25 | |
| 26 | class NewSubscriber(): |
| 27 | |
| 28 | def __init__(self, uri, timeout=None, local_address="0.0.0.0", local_port=8088, rmr_port=4061): |
| 29 | """ |
| 30 | init |
| 31 | |
| 32 | Parameters |
| 33 | ---------- |
| 34 | uri: string |
| 35 | xapp submgr service uri |
| 36 | timeout: int |
| 37 | rest method timeout |
| 38 | local_address: string |
| 39 | local interface IP address for rest service binding (for response handler) |
| 40 | local_port: int |
| 41 | local service port nunber for rest service binding (for response handler) |
| 42 | rmr_port: int |
| 43 | rmr port number |
| 44 | """ |
| 45 | self.uri = uri |
| 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 |
| 53 | # Configure API |
| 54 | configuration = subsclient.Configuration() |
| 55 | configuration.verify_ssl = False |
| 56 | configuration.host = "http://127.0.0.1:8088/" |
| 57 | self.api = subsclient.ApiClient(configuration) |
| 58 | |
| 59 | def _responsePostHandler(self, name, path, data, ctype): |
| 60 | """ |
| 61 | _resppnsePostHandler |
| 62 | internally used subscription reponse handler it the callback function is not set |
| 63 | """ |
| 64 | return "{}", 'application/json', "OK", 200 |
| 65 | |
| 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) |
| 69 | |
| 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) |
| 72 | |
| 73 | def SubsequentAction(self, subsequent_action_type=None, time_to_wait=None): |
| 74 | return subsclient.SubsequentAction(subsequent_action_type, time_to_wait) |
| 75 | |
| 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) |
| 78 | |
| 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) |
| 81 | |
| 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) |
| 84 | |
| 85 | def Subscribe(self, subs_params=None): |
| 86 | """ |
| 87 | Subscribe |
| 88 | subscription request |
| 89 | |
| 90 | Parameters |
| 91 | ---------- |
| 92 | subs_params: SubscriptionParams |
| 93 | structured subscription data definition defined in subsclient |
| 94 | Returns |
| 95 | ------- |
| 96 | SubscriptionResponse |
| 97 | json string of SubscriptionResponse object |
| 98 | """ |
| 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 |
| 104 | |
| 105 | def UnSubscribe(self, subs_id=None): |
| 106 | """ |
| 107 | UnSubscribe |
| 108 | subscription remove |
| 109 | |
| 110 | Parameters |
| 111 | ---------- |
| 112 | subs_id: int |
| 113 | subscription id returned in SubscriptionResponse |
| 114 | Returns |
| 115 | ------- |
| 116 | response.reason: string |
| 117 | http reason |
| 118 | response.status: int |
| 119 | http status code |
| 120 | """ |
| 121 | response = self.api.request(method="DELETE", url=self.uri + "/subscriptions/" + subs_id, headers=None) |
| 122 | return response.data, response.reason, response.status |
| 123 | |
| 124 | def QuerySubscriptions(self): |
| 125 | """ |
| 126 | QuerySubscriptions |
| 127 | Query all subscriptions |
| 128 | |
| 129 | Returns |
| 130 | ------- |
| 131 | response.data: json string |
| 132 | SubscriptionList |
| 133 | response.reason: string |
| 134 | http reason |
| 135 | response.status: int |
| 136 | http status code |
| 137 | """ |
| 138 | response = self.api.request(method="GET", url=self.uri + "/subscriptions", headers=None) |
| 139 | return response.data, response.reason, response.status |
| 140 | |
| 141 | def ResponseHandler(self, responseCB=None, server=None): |
| 142 | """ |
| 143 | ResponseHandler |
| 144 | Starts the response handler and set the callback |
| 145 | |
| 146 | Parameters |
| 147 | ---------- |
| 148 | responseCB |
| 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 |
| 152 | |
| 153 | Returns |
| 154 | ------- |
| 155 | status: boolean |
| 156 | True = success, False = failed |
| 157 | """ |
| 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) |
| 169 | return True |
| 170 | else: |
| 171 | return False |