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