2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
16 OSM Subscription API handling
19 from osmclient
.common
.exceptions
import NotFound
20 from osmclient
.common
.exceptions
import ClientException
26 class Subscription(object):
27 def __init__(self
, http
=None, client
=None):
30 self
._logger
= logging
.getLogger("osmclient")
31 self
._apiName
= "/nslcm"
32 self
._apiVersion
= "/v1"
33 self
._apiResource
= "/subscriptions"
34 self
._apiBase
= "{}{}{}".format(
35 self
._apiName
, self
._apiVersion
, self
._apiResource
38 def _rebuild_apibase(self
, event_type
):
39 self
._logger
.debug("")
40 if event_type
== "ns":
41 self
._apiName
= "/nslcm"
43 raise ClientException("unsupported event_type {}".format(event_type
))
44 self
._apiBase
= "{}{}{}".format(
45 self
._apiName
, self
._apiVersion
, self
._apiResource
48 def create(self
, event_type
, event
):
50 Creates a subscription
51 :param event_type: event type to be subscribed (only ns is supported)
52 :param event: yaml string defining the event to be subscribed
53 :return: None. Exception if fail
55 self
._logger
.debug("")
56 self
._client
.get_token()
57 self
._rebuild
_apibase
(event_type
)
59 subscription_event
= yaml
.safe_load(event
)
61 http_code
, resp
= self
._http
.post_cmd(
62 endpoint
=self
._apiBase
, postfields_dict
=subscription_event
65 resp
= json
.loads(resp
)
66 if not resp
or "id" not in resp
:
67 raise ClientException("unexpected response from server - {}".format(resp
))
70 def delete(self
, event_type
, subscription_id
, force
=False):
72 Deletes a subscription
73 :param event_type: event type to be subscribed (only ns is supported)
74 :param subscription_id: identifier of the subscription
75 :param force: forces deletion
76 :return: None. Exception if fail
78 self
._logger
.debug("")
79 self
._client
.get_token()
80 self
._rebuild
_apibase
(event_type
)
83 querystring
= "?FORCE=True"
84 http_code
, resp
= self
._http
.delete_cmd(
85 "{}/{}{}".format(self
._apiBase
, subscription_id
, querystring
)
88 print("Deletion in progress")
89 elif http_code
== 204:
93 raise ClientException(
94 "failed to delete subscription {} - {}".format(subscription_id
, msg
)
97 def list(self
, event_type
, filter=None):
99 Returns a list of subscriptions
100 :param event_type: event type to be subscribed (only ns is supported)
102 :return: list of subscriptions
104 self
._logger
.debug("")
105 self
._client
.get_token()
106 self
._rebuild
_apibase
(event_type
)
109 filter_string
= "?{}".format(filter)
110 _
, resp
= self
._http
.get2_cmd("{}{}".format(self
._apiBase
, filter_string
))
112 return json
.loads(resp
)
115 def get(self
, event_type
, subscription_id
):
117 Returns a subscription from a subscription id
118 :param event_type: event type to be subscribed (only ns is supported)
119 :param subscription_id: identifier of the subscription
120 :return: dict with the subscription
122 self
._logger
.debug("")
123 self
._client
.get_token()
124 self
._rebuild
_apibase
(event_type
)
126 _
, resp
= self
._http
.get2_cmd(
127 "{}/{}".format(self
._apiBase
, subscription_id
)
129 self
._logger
.debug(yaml
.safe_dump(resp
))
131 return json
.loads(resp
)
132 if not resp
or "id" not in resp
:
133 raise ClientException(
134 "failed to get subscription info: {}".format(resp
)
138 raise NotFound("subscription '{}' not found".format(subscription_id
))