Code Coverage

Cobertura Coverage Report > osmclient.sol005 >

sdncontroller.py

Trend

File Coverage summary

NameClassesLinesConditionals
sdncontroller.py
100%
1/1
18%
17/97
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
sdncontroller.py
18%
17/97
N/A

Source

osmclient/sol005/sdncontroller.py
1 # Copyright 2018 Telefonica
2 #
3 # All Rights Reserved.
4 #
5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
6 #    not use this file except in compliance with the License. You may obtain
7 #    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, WITHOUT
13 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 #    License for the specific language governing permissions and limitations
15 #    under the License.
16
17 1 """
18 OSM SDN controller API handling
19 """
20
21 1 from osmclient.common import utils
22 1 from osmclient.common import wait as WaitForStatus
23 1 from osmclient.common.exceptions import ClientException
24 1 from osmclient.common.exceptions import NotFound
25 1 import json
26 1 import logging
27 1 import yaml
28
29
30 1 class SdnController(object):
31 1     def __init__(self, http=None, client=None):
32 0         self._http = http
33 0         self._client = client
34 0         self._logger = logging.getLogger("osmclient")
35 0         self._apiName = "/admin"
36 0         self._apiVersion = "/v1"
37 0         self._apiResource = "/sdns"
38 0         self._apiBase = "{}{}{}".format(
39             self._apiName, self._apiVersion, self._apiResource
40         )
41
42     # SDNC '--wait' option
43 1     def _wait(self, id, wait_time, deleteFlag=False):
44 0         self._logger.debug("")
45 0         self._client.get_token()
46         # Endpoint to get operation status
47 0         apiUrlStatus = "{}{}{}".format(self._apiName, self._apiVersion, "/sdns")
48         # Wait for status for SDN instance creation/update/deletion
49 0         if isinstance(wait_time, bool):
50 0             wait_time = WaitForStatus.TIMEOUT_SDNC_OPERATION
51 0         WaitForStatus.wait_for_status(
52             "SDNC",
53             str(id),
54             wait_time,
55             apiUrlStatus,
56             self._http.get2_cmd,
57             deleteFlag=deleteFlag,
58         )
59
60 1     def _get_id_for_wait(self, name):
61         """Returns id of name, or the id itself if given as argument"""
62 0         self._logger.debug("")
63 0         for sdnc in self.list():
64 0             if name == sdnc["_id"]:
65 0                 return sdnc["_id"]
66 0         for sdnc in self.list():
67 0             if name == sdnc["name"]:
68 0                 return sdnc["_id"]
69 0         return ""
70
71 1     def create(self, name, sdn_controller, wait=False):
72 0         self._logger.debug("")
73 0         if "config" in sdn_controller and isinstance(sdn_controller["config"], str):
74 0             sdn_controller["config"] = yaml.safe_load(sdn_controller["config"])
75 0         self._client.get_token()
76 0         http_code, resp = self._http.post_cmd(
77             endpoint=self._apiBase, postfields_dict=sdn_controller
78         )
79         # print('HTTP CODE: {}'.format(http_code))
80         # print('RESP: {}'.format(resp))
81         # if http_code in (200, 201, 202, 204):
82 0         if resp:
83 0             resp = json.loads(resp)
84 0         if not resp or "id" not in resp:
85 0             raise ClientException("unexpected response from server - {}".format(resp))
86 0         if wait:
87             # Wait for status for SDNC instance creation
88 0             self._wait(resp.get("id"), wait)
89 0         print(resp["id"])
90         # else:
91         #    msg = ""
92         #    if resp:
93         #        try:
94         #            msg = json.loads(resp)
95         #        except ValueError:
96         #            msg = resp
97         #    raise ClientException("failed to create SDN controller {} - {}".format(name, msg))
98
99 1     def update(self, name, sdn_controller, wait=False):
100 0         self._logger.debug("")
101 0         if "config" in sdn_controller and isinstance(sdn_controller["config"], str):
102 0             sdn_controller["config"] = yaml.safe_load(sdn_controller["config"])
103 0         self._client.get_token()
104 0         sdnc = self.get(name)
105 0         sdnc_id_for_wait = self._get_id_for_wait(name)
106 0         http_code, resp = self._http.patch_cmd(
107             endpoint="{}/{}".format(self._apiBase, sdnc["_id"]),
108             postfields_dict=sdn_controller,
109         )
110         # print('HTTP CODE: {}'.format(http_code))
111         # print('RESP: {}'.format(resp))
112         # if http_code in (200, 201, 202, 204):
113 0         if wait:
114             # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
115             # Use the previously obtained id instead.
116 0             wait_id = sdnc_id_for_wait
117             # Wait for status for VI instance update
118 0             self._wait(wait_id, wait)
119         # else:
120         #     pass
121         # else:
122         #    msg = ""
123         #    if resp:
124         #        try:
125         #            msg = json.loads(resp)
126         #        except ValueError:
127         #            msg = resp
128         #    raise ClientException("failed to update SDN controller {} - {}".format(name, msg))
129
130 1     def delete(self, name, force=False, wait=False):
131 0         self._logger.debug("")
132 0         self._client.get_token()
133 0         sdn_controller = self.get(name)
134 0         sdnc_id_for_wait = self._get_id_for_wait(name)
135 0         querystring = ""
136 0         if force:
137 0             querystring = "?FORCE=True"
138 0         http_code, resp = self._http.delete_cmd(
139             "{}/{}{}".format(self._apiBase, sdn_controller["_id"], querystring)
140         )
141         # print('HTTP CODE: {}'.format(http_code))
142         # print('RESP: {}'.format(resp))
143 0         if http_code == 202:
144 0             if wait:
145                 # Wait for status for SDNC instance deletion
146 0                 self._wait(sdnc_id_for_wait, wait, deleteFlag=True)
147             else:
148 0                 print("Deletion in progress")
149 0         elif http_code == 204:
150 0             print("Deleted")
151 0         elif resp and "result" in resp:
152 0             print("Deleted")
153         else:
154 0             msg = resp or ""
155             # if resp:
156             #     try:
157             #         msg = json.loads(resp)
158             #     except ValueError:
159             #         msg = resp
160 0             raise ClientException(
161                 "failed to delete SDN controller {} - {}".format(name, msg)
162             )
163
164 1     def list(self, filter=None):
165         """Returns a list of SDN controllers"""
166 0         self._logger.debug("")
167 0         self._client.get_token()
168 0         filter_string = ""
169 0         if filter:
170 0             filter_string = "?{}".format(filter)
171 0         _, resp = self._http.get2_cmd("{}{}".format(self._apiBase, filter_string))
172         # print('RESP: {}'.format(resp))
173 0         if resp:
174 0             return json.loads(resp)
175 0         return list()
176
177 1     def get(self, name):
178         """Returns an SDN controller based on name or id"""
179 0         self._logger.debug("")
180 0         self._client.get_token()
181 0         if utils.validate_uuid4(name):
182 0             for sdnc in self.list():
183 0                 if name == sdnc["_id"]:
184 0                     return sdnc
185         else:
186 0             for sdnc in self.list():
187 0                 if name == sdnc["name"]:
188 0                     return sdnc
189 0         raise NotFound("SDN controller {} not found".format(name))