Feature 5945 Adding WIM to LCM
[osm/LCM.git] / osm_lcm / vim_sdn.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2018 Telefonica S.A.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 ##
18
19 import asyncio
20 import logging
21 import logging.handlers
22 import ROclient
23 from lcm_utils import LcmException, LcmBase
24 from osm_common.dbbase import DbException
25 from copy import deepcopy
26
27 __author__ = "Alfonso Tierno"
28
29
30 class VimLcm(LcmBase):
31 # values that are encrypted at vim config because they are passwords
32 vim_config_encrypted = ("admin_password", "nsx_password", "vcenter_password")
33
34 def __init__(self, db, msg, fs, lcm_tasks, ro_config, loop):
35 """
36 Init, Connect to database, filesystem storage, and messaging
37 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
38 :return: None
39 """
40
41 self.logger = logging.getLogger('lcm.vim')
42 self.loop = loop
43 self.lcm_tasks = lcm_tasks
44 self.ro_config = ro_config
45
46 super().__init__(db, msg, fs, self.logger)
47
48 async def create(self, vim_content, order_id):
49 vim_id = vim_content["_id"]
50 logging_text = "Task vim_create={} ".format(vim_id)
51 self.logger.debug(logging_text + "Enter")
52 db_vim = None
53 db_vim_update = {}
54 exc = None
55 RO_sdn_id = None
56 try:
57 step = "Getting vim-id='{}' from db".format(vim_id)
58 db_vim = self.db.get_one("vim_accounts", {"_id": vim_id})
59 db_vim_update["_admin.deployed.RO"] = None
60 if vim_content.get("config") and vim_content["config"].get("sdn-controller"):
61 step = "Getting sdn-controller-id='{}' from db".format(vim_content["config"]["sdn-controller"])
62 db_sdn = self.db.get_one("sdns", {"_id": vim_content["config"]["sdn-controller"]})
63 if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"):
64 RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
65 else:
66 raise LcmException("sdn-controller={} is not available. Not deployed at RO".format(
67 vim_content["config"]["sdn-controller"]))
68
69 step = "Creating vim at RO"
70 db_vim_update["_admin.detailed-status"] = step
71 self.update_db_2("vim_accounts", vim_id, db_vim_update)
72 RO = ROclient.ROClient(self.loop, **self.ro_config)
73 vim_RO = deepcopy(vim_content)
74 vim_RO.pop("_id", None)
75 vim_RO.pop("_admin", None)
76 schema_version = vim_RO.pop("schema_version", None)
77 vim_RO.pop("schema_type", None)
78 vim_RO.pop("vim_tenant_name", None)
79 vim_RO["type"] = vim_RO.pop("vim_type")
80 vim_RO.pop("vim_user", None)
81 vim_RO.pop("vim_password", None)
82 if RO_sdn_id:
83 vim_RO["config"]["sdn-controller"] = RO_sdn_id
84 desc = await RO.create("vim", descriptor=vim_RO)
85 RO_vim_id = desc["uuid"]
86 db_vim_update["_admin.deployed.RO"] = RO_vim_id
87 self.logger.debug(logging_text + "VIM created at RO_vim_id={}".format(RO_vim_id))
88
89 step = "Creating vim_account at RO"
90 db_vim_update["_admin.detailed-status"] = step
91 self.update_db_2("vim_accounts", vim_id, db_vim_update)
92
93 if vim_content.get("vim_password"):
94 vim_content["vim_password"] = self.db.decrypt(vim_content["vim_password"],
95 schema_version=schema_version,
96 salt=vim_id)
97 vim_account_RO = {"vim_tenant_name": vim_content["vim_tenant_name"],
98 "vim_username": vim_content["vim_user"],
99 "vim_password": vim_content["vim_password"]
100 }
101 if vim_RO.get("config"):
102 vim_account_RO["config"] = vim_RO["config"]
103 if "sdn-controller" in vim_account_RO["config"]:
104 del vim_account_RO["config"]["sdn-controller"]
105 if "sdn-port-mapping" in vim_account_RO["config"]:
106 del vim_account_RO["config"]["sdn-port-mapping"]
107 for p in self.vim_config_encrypted:
108 if vim_account_RO["config"].get(p):
109 vim_account_RO["config"][p] = self.db.decrypt(vim_account_RO["config"][p],
110 schema_version=schema_version,
111 salt=vim_id)
112
113 desc = await RO.attach("vim_account", RO_vim_id, descriptor=vim_account_RO)
114 db_vim_update["_admin.deployed.RO-account"] = desc["uuid"]
115 db_vim_update["_admin.operationalState"] = "ENABLED"
116 db_vim_update["_admin.detailed-status"] = "Done"
117
118 # await asyncio.sleep(15) # TODO remove. This is for test
119 self.logger.debug(logging_text + "Exit Ok VIM account created at RO_vim_account_id={}".format(desc["uuid"]))
120 return
121
122 except (ROclient.ROClientException, DbException) as e:
123 self.logger.error(logging_text + "Exit Exception {}".format(e))
124 exc = e
125 except Exception as e:
126 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
127 exc = e
128 finally:
129 if exc and db_vim:
130 db_vim_update["_admin.operationalState"] = "ERROR"
131 db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
132 if db_vim_update:
133 self.update_db_2("vim_accounts", vim_id, db_vim_update)
134 self.lcm_tasks.remove("vim_account", vim_id, order_id)
135
136 async def edit(self, vim_content, order_id):
137 vim_id = vim_content["_id"]
138 logging_text = "Task vim_edit={} ".format(vim_id)
139 self.logger.debug(logging_text + "Enter")
140 db_vim = None
141 exc = None
142 RO_sdn_id = None
143 RO_vim_id = None
144 db_vim_update = {}
145 step = "Getting vim-id='{}' from db".format(vim_id)
146 try:
147 db_vim = self.db.get_one("vim_accounts", {"_id": vim_id})
148
149 # look if previous tasks in process
150 task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account", vim_id, order_id)
151 if task_dependency:
152 step = "Waiting for related tasks to be completed: {}".format(task_name)
153 self.logger.debug(logging_text + step)
154 # TODO write this to database
155 _, pending = await asyncio.wait(task_dependency, timeout=3600)
156 if pending:
157 raise LcmException("Timeout waiting related tasks to be completed")
158
159 if db_vim.get("_admin") and db_vim["_admin"].get("deployed") and db_vim["_admin"]["deployed"].get("RO"):
160 if vim_content.get("config") and vim_content["config"].get("sdn-controller"):
161 step = "Getting sdn-controller-id='{}' from db".format(vim_content["config"]["sdn-controller"])
162 db_sdn = self.db.get_one("sdns", {"_id": vim_content["config"]["sdn-controller"]})
163
164 # look if previous tasks in process
165 task_name, task_dependency = self.lcm_tasks.lookfor_related("sdn", db_sdn["_id"])
166 if task_dependency:
167 step = "Waiting for related tasks to be completed: {}".format(task_name)
168 self.logger.debug(logging_text + step)
169 # TODO write this to database
170 _, pending = await asyncio.wait(task_dependency, timeout=3600)
171 if pending:
172 raise LcmException("Timeout waiting related tasks to be completed")
173
174 if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get(
175 "RO"):
176 RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
177 else:
178 raise LcmException("sdn-controller={} is not available. Not deployed at RO".format(
179 vim_content["config"]["sdn-controller"]))
180
181 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
182 step = "Editing vim at RO"
183 RO = ROclient.ROClient(self.loop, **self.ro_config)
184 vim_RO = deepcopy(vim_content)
185 vim_RO.pop("_id", None)
186 vim_RO.pop("_admin", None)
187 schema_version = vim_RO.pop("schema_version", None)
188 vim_RO.pop("schema_type", None)
189 vim_RO.pop("vim_tenant_name", None)
190 if "vim_type" in vim_RO:
191 vim_RO["type"] = vim_RO.pop("vim_type")
192 vim_RO.pop("vim_user", None)
193 vim_RO.pop("vim_password", None)
194 if RO_sdn_id:
195 vim_RO["config"]["sdn-controller"] = RO_sdn_id
196 # TODO make a deep update of sdn-port-mapping
197 if vim_RO:
198 await RO.edit("vim", RO_vim_id, descriptor=vim_RO)
199
200 step = "Editing vim-account at RO tenant"
201 vim_account_RO = {}
202 if "config" in vim_content:
203 if "sdn-controller" in vim_content["config"]:
204 del vim_content["config"]["sdn-controller"]
205 if "sdn-port-mapping" in vim_content["config"]:
206 del vim_content["config"]["sdn-port-mapping"]
207 if not vim_content["config"]:
208 del vim_content["config"]
209 if "vim_tenant_name" in vim_content:
210 vim_account_RO["vim_tenant_name"] = vim_content["vim_tenant_name"]
211 if "vim_password" in vim_content:
212 vim_account_RO["vim_password"] = vim_content["vim_password"]
213 if vim_content.get("vim_password"):
214 vim_account_RO["vim_password"] = self.db.decrypt(vim_content["vim_password"],
215 schema_version=schema_version,
216 salt=vim_id)
217 if "config" in vim_content:
218 vim_account_RO["config"] = vim_content["config"]
219 if vim_content.get("config"):
220 for p in self.vim_config_encrypted:
221 if vim_content["config"].get(p):
222 vim_account_RO["config"][p] = self.db.decrypt(vim_content["config"][p],
223 schema_version=schema_version,
224 salt=vim_id)
225
226 if "vim_user" in vim_content:
227 vim_content["vim_username"] = vim_content["vim_user"]
228 # vim_account must be edited always even if empty in order to ensure changes are translated to RO
229 # vim_thread. RO will remove and relaunch a new thread for this vim_account
230 await RO.edit("vim_account", RO_vim_id, descriptor=vim_account_RO)
231 db_vim_update["_admin.operationalState"] = "ENABLED"
232
233 self.logger.debug(logging_text + "Exit Ok RO_vim_id={}".format(RO_vim_id))
234 return
235
236 except (ROclient.ROClientException, DbException) as e:
237 self.logger.error(logging_text + "Exit Exception {}".format(e))
238 exc = e
239 except Exception as e:
240 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
241 exc = e
242 finally:
243 if exc and db_vim:
244 db_vim_update["_admin.operationalState"] = "ERROR"
245 db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
246 if db_vim_update:
247 self.update_db_2("vim_accounts", vim_id, db_vim_update)
248 self.lcm_tasks.remove("vim_account", vim_id, order_id)
249
250 async def delete(self, vim_id, order_id):
251 logging_text = "Task vim_delete={} ".format(vim_id)
252 self.logger.debug(logging_text + "Enter")
253 db_vim = None
254 db_vim_update = {}
255 exc = None
256 step = "Getting vim from db"
257 try:
258 db_vim = self.db.get_one("vim_accounts", {"_id": vim_id})
259 if db_vim.get("_admin") and db_vim["_admin"].get("deployed") and db_vim["_admin"]["deployed"].get("RO"):
260 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
261 RO = ROclient.ROClient(self.loop, **self.ro_config)
262 step = "Detaching vim from RO tenant"
263 try:
264 await RO.detach("vim_account", RO_vim_id)
265 except ROclient.ROClientException as e:
266 if e.http_code == 404: # not found
267 self.logger.debug(logging_text + "RO_vim_id={} already detached".format(RO_vim_id))
268 else:
269 raise
270
271 step = "Deleting vim from RO"
272 try:
273 await RO.delete("vim", RO_vim_id)
274 except ROclient.ROClientException as e:
275 if e.http_code == 404: # not found
276 self.logger.debug(logging_text + "RO_vim_id={} already deleted".format(RO_vim_id))
277 else:
278 raise
279 else:
280 # nothing to delete
281 self.logger.error(logging_text + "Nohing to remove at RO")
282 self.db.del_one("vim_accounts", {"_id": vim_id})
283 self.logger.debug(logging_text + "Exit Ok")
284 return
285
286 except (ROclient.ROClientException, DbException) as e:
287 self.logger.error(logging_text + "Exit Exception {}".format(e))
288 exc = e
289 except Exception as e:
290 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
291 exc = e
292 finally:
293 self.lcm_tasks.remove("vim_account", vim_id, order_id)
294 if exc and db_vim:
295 db_vim_update["_admin.operationalState"] = "ERROR"
296 db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
297 if db_vim_update:
298 self.update_db_2("vim_accounts", vim_id, db_vim_update)
299 self.lcm_tasks.remove("vim_account", vim_id, order_id)
300
301
302 class WimLcm(LcmBase):
303 # values that are encrypted at wim config because they are passwords
304 wim_config_encrypted = ()
305
306 def __init__(self, db, msg, fs, lcm_tasks, ro_config, loop):
307 """
308 Init, Connect to database, filesystem storage, and messaging
309 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
310 :return: None
311 """
312
313 self.logger = logging.getLogger('lcm.vim')
314 self.loop = loop
315 self.lcm_tasks = lcm_tasks
316 self.ro_config = ro_config
317
318 super().__init__(db, msg, fs, self.logger)
319
320 async def create(self, wim_content, order_id):
321 wim_id = wim_content["_id"]
322 logging_text = "Task wim_create={} ".format(wim_id)
323 self.logger.debug(logging_text + "Enter")
324 db_wim = None
325 db_wim_update = {}
326 exc = None
327 try:
328 step = "Getting wim-id='{}' from db".format(wim_id)
329 db_wim = self.db.get_one("wim_accounts", {"_id": wim_id})
330 db_wim_update["_admin.deployed.RO"] = None
331
332 step = "Creating wim at RO"
333 db_wim_update["_admin.detailed-status"] = step
334 self.update_db_2("wim_accounts", wim_id, db_wim_update)
335 RO = ROclient.ROClient(self.loop, **self.ro_config)
336 wim_RO = deepcopy(wim_content)
337 wim_RO.pop("_id", None)
338 wim_RO.pop("_admin", None)
339 schema_version = wim_RO.pop("schema_version", None)
340 wim_RO.pop("schema_type", None)
341 wim_RO.pop("wim_tenant_name", None)
342 wim_RO["type"] = wim_RO.pop("wim_type")
343 wim_RO.pop("wim_user", None)
344 wim_RO.pop("wim_password", None)
345 desc = await RO.create("wim", descriptor=wim_RO)
346 RO_wim_id = desc["uuid"]
347 db_wim_update["_admin.deployed.RO"] = RO_wim_id
348 self.logger.debug(logging_text + "WIM created at RO_wim_id={}".format(RO_wim_id))
349
350 step = "Creating wim_account at RO"
351 db_wim_update["_admin.detailed-status"] = step
352 self.update_db_2("wim_accounts", wim_id, db_wim_update)
353
354 if wim_content.get("wim_password"):
355 wim_content["wim_password"] = self.db.decrypt(wim_content["wim_password"],
356 schema_version=schema_version,
357 salt=wim_id)
358 wim_account_RO = {"name": wim_content["name"],
359 "user": wim_content["user"],
360 "password": wim_content["password"]
361 }
362 if wim_RO.get("config"):
363 wim_account_RO["config"] = wim_RO["config"]
364 if "wim_port_mapping" in wim_account_RO["config"]:
365 del wim_account_RO["config"]["wim_port_mapping"]
366 for p in self.wim_config_encrypted:
367 if wim_account_RO["config"].get(p):
368 wim_account_RO["config"][p] = self.db.decrypt(wim_account_RO["config"][p],
369 schema_version=schema_version,
370 salt=wim_id)
371
372 desc = await RO.attach("wim_account", RO_wim_id, descriptor=wim_account_RO)
373 db_wim_update["_admin.deployed.RO-account"] = desc["uuid"]
374 db_wim_update["_admin.operationalState"] = "ENABLED"
375 db_wim_update["_admin.detailed-status"] = "Done"
376
377 self.logger.debug(logging_text + "Exit Ok WIM account created at RO_wim_account_id={}".format(desc["uuid"]))
378 return
379
380 except (ROclient.ROClientException, DbException) as e:
381 self.logger.error(logging_text + "Exit Exception {}".format(e))
382 exc = e
383 except Exception as e:
384 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
385 exc = e
386 finally:
387 if exc and db_wim:
388 db_wim_update["_admin.operationalState"] = "ERROR"
389 db_wim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
390 if db_wim_update:
391 self.update_db_2("wim_accounts", wim_id, db_wim_update)
392 self.lcm_tasks.remove("wim_account", wim_id, order_id)
393
394 async def edit(self, wim_content, order_id):
395 wim_id = wim_content["_id"]
396 logging_text = "Task wim_edit={} ".format(wim_id)
397 self.logger.debug(logging_text + "Enter")
398 db_wim = None
399 exc = None
400 RO_wim_id = None
401 db_wim_update = {}
402 step = "Getting wim-id='{}' from db".format(wim_id)
403 try:
404 db_wim = self.db.get_one("wim_accounts", {"_id": wim_id})
405
406 # look if previous tasks in process
407 task_name, task_dependency = self.lcm_tasks.lookfor_related("wim_account", wim_id, order_id)
408 if task_dependency:
409 step = "Waiting for related tasks to be completed: {}".format(task_name)
410 self.logger.debug(logging_text + step)
411 # TODO write this to database
412 _, pending = await asyncio.wait(task_dependency, timeout=3600)
413 if pending:
414 raise LcmException("Timeout waiting related tasks to be completed")
415
416 if db_wim.get("_admin") and db_wim["_admin"].get("deployed") and db_wim["_admin"]["deployed"].get("RO"):
417
418 RO_wim_id = db_wim["_admin"]["deployed"]["RO"]
419 step = "Editing wim at RO"
420 RO = ROclient.ROClient(self.loop, **self.ro_config)
421 wim_RO = deepcopy(wim_content)
422 wim_RO.pop("_id", None)
423 wim_RO.pop("_admin", None)
424 schema_version = wim_RO.pop("schema_version", None)
425 wim_RO.pop("schema_type", None)
426 wim_RO.pop("wim_tenant_name", None)
427 if "wim_type" in wim_RO:
428 wim_RO["type"] = wim_RO.pop("wim_type")
429 wim_RO.pop("wim_user", None)
430 wim_RO.pop("wim_password", None)
431 # TODO make a deep update of wim_port_mapping
432 if wim_RO:
433 await RO.edit("wim", RO_wim_id, descriptor=wim_RO)
434
435 step = "Editing wim-account at RO tenant"
436 wim_account_RO = {}
437 if "config" in wim_content:
438 if "wim_port_mapping" in wim_content["config"]:
439 del wim_content["config"]["wim_port_mapping"]
440 if not wim_content["config"]:
441 del wim_content["config"]
442 if "wim_tenant_name" in wim_content:
443 wim_account_RO["wim_tenant_name"] = wim_content["wim_tenant_name"]
444 if "wim_password" in wim_content:
445 wim_account_RO["wim_password"] = wim_content["wim_password"]
446 if wim_content.get("wim_password"):
447 wim_account_RO["wim_password"] = self.db.decrypt(wim_content["wim_password"],
448 schema_version=schema_version,
449 salt=wim_id)
450 if "config" in wim_content:
451 wim_account_RO["config"] = wim_content["config"]
452 if wim_content.get("config"):
453 for p in self.wim_config_encrypted:
454 if wim_content["config"].get(p):
455 wim_account_RO["config"][p] = self.db.decrypt(wim_content["config"][p],
456 schema_version=schema_version,
457 salt=wim_id)
458
459 if "wim_user" in wim_content:
460 wim_content["wim_username"] = wim_content["wim_user"]
461 # wim_account must be edited always even if empty in order to ensure changes are translated to RO
462 # wim_thread. RO will remove and relaunch a new thread for this wim_account
463 await RO.edit("wim_account", RO_wim_id, descriptor=wim_account_RO)
464 db_wim_update["_admin.operationalState"] = "ENABLED"
465
466 self.logger.debug(logging_text + "Exit Ok RO_wim_id={}".format(RO_wim_id))
467 return
468
469 except (ROclient.ROClientException, DbException) as e:
470 self.logger.error(logging_text + "Exit Exception {}".format(e))
471 exc = e
472 except Exception as e:
473 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
474 exc = e
475 finally:
476 if exc and db_wim:
477 db_wim_update["_admin.operationalState"] = "ERROR"
478 db_wim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
479 if db_wim_update:
480 self.update_db_2("wim_accounts", wim_id, db_wim_update)
481 self.lcm_tasks.remove("wim_account", wim_id, order_id)
482
483 async def delete(self, wim_id, order_id):
484 logging_text = "Task wim_delete={} ".format(wim_id)
485 self.logger.debug(logging_text + "Enter")
486 db_wim = None
487 db_wim_update = {}
488 exc = None
489 step = "Getting wim from db"
490 try:
491 db_wim = self.db.get_one("wim_accounts", {"_id": wim_id})
492 if db_wim.get("_admin") and db_wim["_admin"].get("deployed") and db_wim["_admin"]["deployed"].get("RO"):
493 RO_wim_id = db_wim["_admin"]["deployed"]["RO"]
494 RO = ROclient.ROClient(self.loop, **self.ro_config)
495 step = "Detaching wim from RO tenant"
496 try:
497 await RO.detach("wim_account", RO_wim_id)
498 except ROclient.ROClientException as e:
499 if e.http_code == 404: # not found
500 self.logger.debug(logging_text + "RO_wim_id={} already detached".format(RO_wim_id))
501 else:
502 raise
503
504 step = "Deleting wim from RO"
505 try:
506 await RO.delete("wim", RO_wim_id)
507 except ROclient.ROClientException as e:
508 if e.http_code == 404: # not found
509 self.logger.debug(logging_text + "RO_wim_id={} already deleted".format(RO_wim_id))
510 else:
511 raise
512 else:
513 # nothing to delete
514 self.logger.error(logging_text + "Nohing to remove at RO")
515 self.db.del_one("wim_accounts", {"_id": wim_id})
516 self.logger.debug(logging_text + "Exit Ok")
517 return
518
519 except (ROclient.ROClientException, DbException) as e:
520 self.logger.error(logging_text + "Exit Exception {}".format(e))
521 exc = e
522 except Exception as e:
523 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
524 exc = e
525 finally:
526 self.lcm_tasks.remove("wim_account", wim_id, order_id)
527 if exc and db_wim:
528 db_wim_update["_admin.operationalState"] = "ERROR"
529 db_wim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
530 if db_wim_update:
531 self.update_db_2("wim_accounts", wim_id, db_wim_update)
532 self.lcm_tasks.remove("wim_account", wim_id, order_id)
533
534
535 class SdnLcm(LcmBase):
536
537 def __init__(self, db, msg, fs, lcm_tasks, ro_config, loop):
538 """
539 Init, Connect to database, filesystem storage, and messaging
540 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
541 :return: None
542 """
543
544 self.logger = logging.getLogger('lcm.sdn')
545 self.loop = loop
546 self.lcm_tasks = lcm_tasks
547 self.ro_config = ro_config
548
549 super().__init__(db, msg, fs, self.logger)
550
551 async def create(self, sdn_content, order_id):
552 sdn_id = sdn_content["_id"]
553 logging_text = "Task sdn_create={} ".format(sdn_id)
554 self.logger.debug(logging_text + "Enter")
555 db_sdn = None
556 db_sdn_update = {}
557 RO_sdn_id = None
558 exc = None
559 try:
560 step = "Getting sdn from db"
561 db_sdn = self.db.get_one("sdns", {"_id": sdn_id})
562 db_sdn_update["_admin.deployed.RO"] = None
563
564 step = "Creating sdn at RO"
565 RO = ROclient.ROClient(self.loop, **self.ro_config)
566 sdn_RO = deepcopy(sdn_content)
567 sdn_RO.pop("_id", None)
568 sdn_RO.pop("_admin", None)
569 schema_version = sdn_RO.pop("schema_version", None)
570 sdn_RO.pop("schema_type", None)
571 sdn_RO.pop("description", None)
572 if sdn_RO.get("password"):
573 sdn_RO["password"] = self.db.decrypt(sdn_RO["password"], schema_version=schema_version, salt=sdn_id)
574
575 desc = await RO.create("sdn", descriptor=sdn_RO)
576 RO_sdn_id = desc["uuid"]
577 db_sdn_update["_admin.deployed.RO"] = RO_sdn_id
578 db_sdn_update["_admin.operationalState"] = "ENABLED"
579 self.logger.debug(logging_text + "Exit Ok RO_sdn_id={}".format(RO_sdn_id))
580 return
581
582 except (ROclient.ROClientException, DbException) as e:
583 self.logger.error(logging_text + "Exit Exception {}".format(e))
584 exc = e
585 except Exception as e:
586 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
587 exc = e
588 finally:
589 if exc and db_sdn:
590 db_sdn_update["_admin.operationalState"] = "ERROR"
591 db_sdn_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
592 if db_sdn_update:
593 self.update_db_2("sdns", sdn_id, db_sdn_update)
594 self.lcm_tasks.remove("sdn", sdn_id, order_id)
595
596 async def edit(self, sdn_content, order_id):
597 sdn_id = sdn_content["_id"]
598 logging_text = "Task sdn_edit={} ".format(sdn_id)
599 self.logger.debug(logging_text + "Enter")
600 db_sdn = None
601 db_sdn_update = {}
602 exc = None
603 step = "Getting sdn from db"
604 try:
605 db_sdn = self.db.get_one("sdns", {"_id": sdn_id})
606 RO_sdn_id = None
607 if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"):
608 RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
609 RO = ROclient.ROClient(self.loop, **self.ro_config)
610 step = "Editing sdn at RO"
611 sdn_RO = deepcopy(sdn_content)
612 sdn_RO.pop("_id", None)
613 sdn_RO.pop("_admin", None)
614 schema_version = sdn_RO.pop("schema_version", None)
615 sdn_RO.pop("schema_type", None)
616 sdn_RO.pop("description", None)
617 if sdn_RO.get("password"):
618 sdn_RO["password"] = self.db.decrypt(sdn_RO["password"], schema_version=schema_version, salt=sdn_id)
619 if sdn_RO:
620 await RO.edit("sdn", RO_sdn_id, descriptor=sdn_RO)
621 db_sdn_update["_admin.operationalState"] = "ENABLED"
622
623 self.logger.debug(logging_text + "Exit Ok RO_sdn_id={}".format(RO_sdn_id))
624 return
625
626 except (ROclient.ROClientException, DbException) as e:
627 self.logger.error(logging_text + "Exit Exception {}".format(e))
628 exc = e
629 except Exception as e:
630 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
631 exc = e
632 finally:
633 if exc and db_sdn:
634 db_sdn["_admin.operationalState"] = "ERROR"
635 db_sdn["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
636 if db_sdn_update:
637 self.update_db_2("sdns", sdn_id, db_sdn_update)
638 self.lcm_tasks.remove("sdn", sdn_id, order_id)
639
640 async def delete(self, sdn_id, order_id):
641 logging_text = "Task sdn_delete={} ".format(sdn_id)
642 self.logger.debug(logging_text + "Enter")
643 db_sdn = None
644 db_sdn_update = {}
645 exc = None
646 step = "Getting sdn from db"
647 try:
648 db_sdn = self.db.get_one("sdns", {"_id": sdn_id})
649 if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"):
650 RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
651 RO = ROclient.ROClient(self.loop, **self.ro_config)
652 step = "Deleting sdn from RO"
653 try:
654 await RO.delete("sdn", RO_sdn_id)
655 except ROclient.ROClientException as e:
656 if e.http_code == 404: # not found
657 self.logger.debug(logging_text + "RO_sdn_id={} already deleted".format(RO_sdn_id))
658 else:
659 raise
660 else:
661 # nothing to delete
662 self.logger.error(logging_text + "Skipping. There is not RO information at database")
663 self.db.del_one("sdns", {"_id": sdn_id})
664 self.logger.debug("sdn_delete task sdn_id={} Exit Ok".format(sdn_id))
665 return
666
667 except (ROclient.ROClientException, DbException) as e:
668 self.logger.error(logging_text + "Exit Exception {}".format(e))
669 exc = e
670 except Exception as e:
671 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
672 exc = e
673 finally:
674 if exc and db_sdn:
675 db_sdn["_admin.operationalState"] = "ERROR"
676 db_sdn["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
677 if db_sdn_update:
678 self.update_db_2("sdns", sdn_id, db_sdn_update)
679 self.lcm_tasks.remove("sdn", sdn_id, order_id)