Fix: Make osmclient Python 3 compatible.
[osm/osmclient.git] / osmclient / sol005 / ns.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 """
18 OSM ns API handling
19 """
20
21 from osmclient.common import utils
22 from osmclient.common.exceptions import ClientException
23 from osmclient.common.exceptions import NotFound
24 import yaml
25 import json
26
27
28 class Ns(object):
29
30 def __init__(self, http=None, client=None):
31 self._http = http
32 self._client = client
33 self._apiName = '/nslcm'
34 self._apiVersion = '/v1'
35 self._apiResource = '/ns_instances_content'
36 self._apiBase = '{}{}{}'.format(self._apiName,
37 self._apiVersion, self._apiResource)
38
39 def list(self, filter=None):
40 """Returns a list of NS
41 """
42 filter_string = ''
43 if filter:
44 filter_string = '?{}'.format(filter)
45 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
46 if resp:
47 return resp
48 return list()
49
50 def get(self, name):
51 """Returns an NS based on name or id
52 """
53 if utils.validate_uuid4(name):
54 for ns in self.list():
55 if name == ns['_id']:
56 return ns
57 else:
58 for ns in self.list():
59 if name == ns['name']:
60 return ns
61 raise NotFound("ns {} not found".format(name))
62
63 def get_individual(self, name):
64 ns_id = name
65 if not utils.validate_uuid4(name):
66 for ns in self.list():
67 if name == ns['name']:
68 ns_id = ns['_id']
69 break
70 resp = self._http.get_cmd('{}/{}'.format(self._apiBase, ns_id))
71 #resp = self._http.get_cmd('{}/{}/nsd_content'.format(self._apiBase, ns_id))
72 #print yaml.safe_dump(resp)
73 if resp:
74 return resp
75 raise NotFound("ns {} not found".format(name))
76
77 def delete(self, name, force=False):
78 ns = self.get(name)
79 querystring = ''
80 if force:
81 querystring = '?FORCE=True'
82 http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
83 ns['_id'], querystring))
84 #print 'HTTP CODE: {}'.format(http_code)
85 #print 'RESP: {}'.format(resp)
86 if http_code == 202:
87 print('Deletion in progress')
88 elif http_code == 204:
89 print('Deleted')
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 delete ns {} - {}".format(name, msg))
98
99 def create(self, nsd_name, nsr_name, account, config=None,
100 ssh_keys=None, description='default description',
101 admin_status='ENABLED'):
102
103 nsd = self._client.nsd.get(nsd_name)
104
105 vim_account_id = {}
106
107 def get_vim_account_id(vim_account):
108 if vim_account_id.get(vim_account):
109 return vim_account_id[vim_account]
110
111 vim = self._client.vim.get(vim_account)
112 if vim is None:
113 raise NotFound("cannot find vim account '{}'".format(vim_account))
114 vim_account_id[vim_account] = vim['_id']
115 return vim['_id']
116
117 ns = {}
118 ns['nsdId'] = nsd['_id']
119 ns['nsName'] = nsr_name
120 ns['nsDescription'] = description
121 ns['vimAccountId'] = get_vim_account_id(account)
122 #ns['userdata'] = {}
123 #ns['userdata']['key1']='value1'
124 #ns['userdata']['key2']='value2'
125
126 if ssh_keys is not None:
127 # ssh_keys is comma separate list
128 # ssh_keys_format = []
129 # for key in ssh_keys.split(','):
130 # ssh_keys_format.append({'key-pair-ref': key})
131 #
132 # ns['ssh-authorized-key'] = ssh_keys_format
133 ns['ssh-authorized-key'] = []
134 for pubkeyfile in ssh_keys.split(','):
135 with open(pubkeyfile, 'r') as f:
136 ns['ssh-authorized-key'].append(f.read())
137 if config:
138 ns_config = yaml.load(config)
139 if "vim-network-name" in ns_config:
140 ns_config["vld"] = ns_config.pop("vim-network-name")
141 if "vld" in ns_config:
142 for vld in ns_config["vld"]:
143 if vld.get("vim-network-name"):
144 if isinstance(vld["vim-network-name"], dict):
145 vim_network_name_dict = {}
146 for vim_account, vim_net in list(vld["vim-network-name"].items()):
147 vim_network_name_dict[get_vim_account_id(vim_account)] = vim_net
148 vld["vim-network-name"] = vim_network_name_dict
149 ns["vld"] = ns_config["vld"]
150 if "vnf" in ns_config:
151 for vnf in ns_config["vnf"]:
152 if vnf.get("vim_account"):
153 vnf["vimAccountId"] = get_vim_account_id(vnf.pop("vim_account"))
154
155 ns["vnf"] = ns_config["vnf"]
156
157 #print yaml.safe_dump(ns)
158 try:
159 self._apiResource = '/ns_instances_content'
160 self._apiBase = '{}{}{}'.format(self._apiName,
161 self._apiVersion, self._apiResource)
162 http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
163 postfields_dict=ns)
164 #print 'HTTP CODE: {}'.format(http_code)
165 #print 'RESP: {}'.format(resp)
166 if http_code in (200, 201, 202, 204):
167 if resp:
168 resp = json.loads(resp)
169 if not resp or 'id' not in resp:
170 raise ClientException('unexpected response from server - {} '.format(
171 resp))
172 print(resp['id'])
173 else:
174 msg = ""
175 if resp:
176 try:
177 msg = json.loads(resp)
178 except ValueError:
179 msg = resp
180 raise ClientException(msg)
181 except ClientException as exc:
182 message="failed to create ns: {} nsd: {}\nerror:\n{}".format(
183 nsr_name,
184 nsd_name,
185 exc.message)
186 raise ClientException(message)
187
188 def list_op(self, name, filter=None):
189 """Returns the list of operations of a NS
190 """
191 ns = self.get(name)
192 try:
193 self._apiResource = '/ns_lcm_op_occs'
194 self._apiBase = '{}{}{}'.format(self._apiName,
195 self._apiVersion, self._apiResource)
196 filter_string = ''
197 if filter:
198 filter_string = '&{}'.format(filter)
199 http_code, resp = self._http.get2_cmd('{}?nsInstanceId={}'.format(
200 self._apiBase, ns['_id'],
201 filter_string) )
202 #print 'HTTP CODE: {}'.format(http_code)
203 #print 'RESP: {}'.format(resp)
204 if http_code == 200:
205 if resp:
206 resp = json.loads(resp)
207 return resp
208 else:
209 raise ClientException('unexpected response from server')
210 else:
211 msg = ""
212 if resp:
213 try:
214 resp = json.loads(resp)
215 msg = resp['detail']
216 except ValueError:
217 msg = resp
218 raise ClientException(msg)
219 except ClientException as exc:
220 message="failed to get operation list of NS {}:\nerror:\n{}".format(
221 name,
222 exc.message)
223 raise ClientException(message)
224
225 def get_op(self, operationId):
226 """Returns the status of an operation
227 """
228 try:
229 self._apiResource = '/ns_lcm_op_occs'
230 self._apiBase = '{}{}{}'.format(self._apiName,
231 self._apiVersion, self._apiResource)
232 http_code, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase, operationId))
233 #print 'HTTP CODE: {}'.format(http_code)
234 #print 'RESP: {}'.format(resp)
235 if http_code == 200:
236 if resp:
237 resp = json.loads(resp)
238 return resp
239 else:
240 raise ClientException('unexpected response from server')
241 else:
242 msg = ""
243 if resp:
244 try:
245 resp = json.loads(resp)
246 msg = resp['detail']
247 except ValueError:
248 msg = resp
249 raise ClientException(msg)
250 except ClientException as exc:
251 message="failed to get status of operation {}:\nerror:\n{}".format(
252 operationId,
253 exc.message)
254 raise ClientException(message)
255
256 def exec_op(self, name, op_name, op_data=None):
257 """Executes an operation on a NS
258 """
259 ns = self.get(name)
260 try:
261 self._apiResource = '/ns_instances'
262 self._apiBase = '{}{}{}'.format(self._apiName,
263 self._apiVersion, self._apiResource)
264 endpoint = '{}/{}/{}'.format(self._apiBase, ns['_id'], op_name)
265 #print 'OP_NAME: {}'.format(op_name)
266 #print 'OP_DATA: {}'.format(json.dumps(op_data))
267 http_code, resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=op_data)
268 #print 'HTTP CODE: {}'.format(http_code)
269 #print 'RESP: {}'.format(resp)
270 if http_code in (200, 201, 202, 204):
271 if resp:
272 resp = json.loads(resp)
273 if not resp or 'id' not in resp:
274 raise ClientException('unexpected response from server - {}'.format(
275 resp))
276 print(resp['id'])
277 else:
278 msg = ""
279 if resp:
280 try:
281 msg = json.loads(resp)
282 except ValueError:
283 msg = resp
284 raise ClientException(msg)
285 except ClientException as exc:
286 message="failed to exec operation {}:\nerror:\n{}".format(
287 name,
288 exc.message)
289 raise ClientException(message)
290
291 def create_alarm(self, alarm):
292 data = {}
293 data["create_alarm_request"] = {}
294 data["create_alarm_request"]["alarm_create_request"] = alarm
295 try:
296 http_code, resp = self._http.post_cmd(endpoint='/test/message/alarm_request',
297 postfields_dict=data)
298 #print 'HTTP CODE: {}'.format(http_code)
299 #print 'RESP: {}'.format(resp)
300 if http_code in (200, 201, 202, 204):
301 #resp = json.loads(resp)
302 print('Alarm created')
303 else:
304 msg = ""
305 if resp:
306 try:
307 msg = json.loads(resp)
308 except ValueError:
309 msg = resp
310 raise ClientException('error: code: {}, resp: {}'.format(
311 http_code, msg))
312 except ClientException as exc:
313 message="failed to create alarm: alarm {}\n{}".format(
314 alarm,
315 exc.message)
316 raise ClientException(message)
317
318 def delete_alarm(self, name):
319 data = {}
320 data["delete_alarm_request"] = {}
321 data["delete_alarm_request"]["alarm_delete_request"] = {}
322 data["delete_alarm_request"]["alarm_delete_request"]["alarm_uuid"] = name
323 try:
324 http_code, resp = self._http.post_cmd(endpoint='/test/message/alarm_request',
325 postfields_dict=data)
326 #print 'HTTP CODE: {}'.format(http_code)
327 #print 'RESP: {}'.format(resp)
328 if http_code in (200, 201, 202, 204):
329 #resp = json.loads(resp)
330 print('Alarm deleted')
331 else:
332 msg = ""
333 if resp:
334 try:
335 msg = json.loads(resp)
336 except ValueError:
337 msg = resp
338 raise ClientException('error: code: {}, resp: {}'.format(
339 http_code, msg))
340 except ClientException as exc:
341 message="failed to delete alarm: alarm {}\n{}".format(
342 name,
343 exc.message)
344 raise ClientException(message)
345
346 def export_metric(self, metric):
347 data = {}
348 data["read_metric_data_request"] = metric
349 try:
350 http_code, resp = self._http.post_cmd(endpoint='/test/message/metric_request',
351 postfields_dict=data)
352 #print 'HTTP CODE: {}'.format(http_code)
353 #print 'RESP: {}'.format(resp)
354 if http_code in (200, 201, 202, 204):
355 #resp = json.loads(resp)
356 return 'Metric exported'
357 else:
358 msg = ""
359 if resp:
360 try:
361 msg = json.loads(resp)
362 except ValueError:
363 msg = resp
364 raise ClientException('error: code: {}, resp: {}'.format(
365 http_code, msg))
366 except ClientException as exc:
367 message="failed to export metric: metric {}\n{}".format(
368 metric,
369 exc.message)
370 raise ClientException(message)
371