feature 6314: Control LCM operations over ns instance
[osm/LCM.git] / osm_lcm / vim_sdn.py
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 import asyncio
5 import logging
6 import logging.handlers
7 import ROclient
8 from lcm_utils import LcmException, LcmBase
9 from osm_common.dbbase import DbException
10 from copy import deepcopy
11
12 __author__ = "Alfonso Tierno"
13
14
15 class VimLcm(LcmBase):
16
17 def __init__(self, db, msg, fs, lcm_tasks, ro_config, loop):
18 """
19 Init, Connect to database, filesystem storage, and messaging
20 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
21 :return: None
22 """
23
24 self.logger = logging.getLogger('lcm.vim')
25 self.loop = loop
26 self.lcm_tasks = lcm_tasks
27 self.ro_config = ro_config
28
29 super().__init__(db, msg, fs, self.logger)
30
31 async def create(self, vim_content, order_id):
32 vim_id = vim_content["_id"]
33 logging_text = "Task vim_create={} ".format(vim_id)
34 self.logger.debug(logging_text + "Enter")
35 db_vim = None
36 db_vim_update = {}
37 exc = None
38 RO_sdn_id = None
39 try:
40 step = "Getting vim-id='{}' from db".format(vim_id)
41 db_vim = self.db.get_one("vim_accounts", {"_id": vim_id})
42 db_vim_update["_admin.deployed.RO"] = None
43 if vim_content.get("config") and vim_content["config"].get("sdn-controller"):
44 step = "Getting sdn-controller-id='{}' from db".format(vim_content["config"]["sdn-controller"])
45 db_sdn = self.db.get_one("sdns", {"_id": vim_content["config"]["sdn-controller"]})
46 if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"):
47 RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
48 else:
49 raise LcmException("sdn-controller={} is not available. Not deployed at RO".format(
50 vim_content["config"]["sdn-controller"]))
51
52 step = "Creating vim at RO"
53 db_vim_update["_admin.detailed-status"] = step
54 self.update_db_2("vim_accounts", vim_id, db_vim_update)
55 RO = ROclient.ROClient(self.loop, **self.ro_config)
56 vim_RO = deepcopy(vim_content)
57 vim_RO.pop("_id", None)
58 vim_RO.pop("_admin", None)
59 vim_RO.pop("schema_version", None)
60 vim_RO.pop("schema_type", None)
61 vim_RO.pop("vim_tenant_name", None)
62 vim_RO["type"] = vim_RO.pop("vim_type")
63 vim_RO.pop("vim_user", None)
64 vim_RO.pop("vim_password", None)
65 if RO_sdn_id:
66 vim_RO["config"]["sdn-controller"] = RO_sdn_id
67 desc = await RO.create("vim", descriptor=vim_RO)
68 RO_vim_id = desc["uuid"]
69 db_vim_update["_admin.deployed.RO"] = RO_vim_id
70
71 step = "Creating vim_account at RO"
72 db_vim_update["_admin.detailed-status"] = step
73 self.update_db_2("vim_accounts", vim_id, db_vim_update)
74
75 vim_account_RO = {"vim_tenant_name": vim_content["vim_tenant_name"],
76 "vim_username": vim_content["vim_user"],
77 "vim_password": vim_content["vim_password"]
78 }
79 if vim_RO.get("config"):
80 vim_account_RO["config"] = vim_RO["config"]
81 if "sdn-controller" in vim_account_RO["config"]:
82 del vim_account_RO["config"]["sdn-controller"]
83 if "sdn-port-mapping" in vim_account_RO["config"]:
84 del vim_account_RO["config"]["sdn-port-mapping"]
85 desc = await RO.attach_datacenter(RO_vim_id, descriptor=vim_account_RO)
86 db_vim_update["_admin.deployed.RO-account"] = desc["uuid"]
87 db_vim_update["_admin.operationalState"] = "ENABLED"
88 db_vim_update["_admin.detailed-status"] = "Done"
89
90 # await asyncio.sleep(15) # TODO remove. This is for test
91 self.logger.debug(logging_text + "Exit Ok RO_vim_id={}".format(RO_vim_id))
92 return
93
94 except (ROclient.ROClientException, DbException) as e:
95 self.logger.error(logging_text + "Exit Exception {}".format(e))
96 exc = e
97 except Exception as e:
98 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
99 exc = e
100 finally:
101 if exc and db_vim:
102 db_vim_update["_admin.operationalState"] = "ERROR"
103 db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
104 if db_vim_update:
105 self.update_db_2("vim_accounts", vim_id, db_vim_update)
106 self.lcm_tasks.remove("vim_account", vim_id, order_id)
107
108 async def edit(self, vim_content, order_id):
109 vim_id = vim_content["_id"]
110 logging_text = "Task vim_edit={} ".format(vim_id)
111 self.logger.debug(logging_text + "Enter")
112 db_vim = None
113 exc = None
114 RO_sdn_id = None
115 RO_vim_id = None
116 db_vim_update = {}
117 step = "Getting vim-id='{}' from db".format(vim_id)
118 try:
119 db_vim = self.db.get_one("vim_accounts", {"_id": vim_id})
120
121 # look if previous tasks in process
122 task_name, task_dependency = self.lcm_tasks.lookfor_related("vim_account", vim_id, order_id)
123 if task_dependency:
124 step = "Waiting for related tasks to be completed: {}".format(task_name)
125 self.logger.debug(logging_text + step)
126 # TODO write this to database
127 _, pending = await asyncio.wait(task_dependency, timeout=3600)
128 if pending:
129 raise LcmException("Timeout waiting related tasks to be completed")
130
131 if db_vim.get("_admin") and db_vim["_admin"].get("deployed") and db_vim["_admin"]["deployed"].get("RO"):
132 if vim_content.get("config") and vim_content["config"].get("sdn-controller"):
133 step = "Getting sdn-controller-id='{}' from db".format(vim_content["config"]["sdn-controller"])
134 db_sdn = self.db.get_one("sdns", {"_id": vim_content["config"]["sdn-controller"]})
135
136 # look if previous tasks in process
137 task_name, task_dependency = self.lcm_tasks.lookfor_related("sdn", db_sdn["_id"])
138 if task_dependency:
139 step = "Waiting for related tasks to be completed: {}".format(task_name)
140 self.logger.debug(logging_text + step)
141 # TODO write this to database
142 _, pending = await asyncio.wait(task_dependency, timeout=3600)
143 if pending:
144 raise LcmException("Timeout waiting related tasks to be completed")
145
146 if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get(
147 "RO"):
148 RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
149 else:
150 raise LcmException("sdn-controller={} is not available. Not deployed at RO".format(
151 vim_content["config"]["sdn-controller"]))
152
153 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
154 step = "Editing vim at RO"
155 RO = ROclient.ROClient(self.loop, **self.ro_config)
156 vim_RO = deepcopy(vim_content)
157 vim_RO.pop("_id", None)
158 vim_RO.pop("_admin", None)
159 vim_RO.pop("schema_version", None)
160 vim_RO.pop("schema_type", None)
161 vim_RO.pop("vim_tenant_name", None)
162 if "vim_type" in vim_RO:
163 vim_RO["type"] = vim_RO.pop("vim_type")
164 vim_RO.pop("vim_user", None)
165 vim_RO.pop("vim_password", None)
166 if RO_sdn_id:
167 vim_RO["config"]["sdn-controller"] = RO_sdn_id
168 # TODO make a deep update of sdn-port-mapping
169 if vim_RO:
170 await RO.edit("vim", RO_vim_id, descriptor=vim_RO)
171
172 step = "Editing vim-account at RO tenant"
173 vim_account_RO = {}
174 if "config" in vim_content:
175 if "sdn-controller" in vim_content["config"]:
176 del vim_content["config"]["sdn-controller"]
177 if "sdn-port-mapping" in vim_content["config"]:
178 del vim_content["config"]["sdn-port-mapping"]
179 if not vim_content["config"]:
180 del vim_content["config"]
181 for k in ("vim_tenant_name", "vim_password", "config"):
182 if k in vim_content:
183 vim_account_RO[k] = vim_content[k]
184 if "vim_user" in vim_content:
185 vim_content["vim_username"] = vim_content["vim_user"]
186 # vim_account must be edited always even if empty in order to ensure changes are translated to RO
187 # vim_thread. RO will remove and relaunch a new thread for this vim_account
188 await RO.edit("vim_account", RO_vim_id, descriptor=vim_account_RO)
189 db_vim_update["_admin.operationalState"] = "ENABLED"
190
191 self.logger.debug(logging_text + "Exit Ok RO_vim_id={}".format(RO_vim_id))
192 return
193
194 except (ROclient.ROClientException, DbException) as e:
195 self.logger.error(logging_text + "Exit Exception {}".format(e))
196 exc = e
197 except Exception as e:
198 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
199 exc = e
200 finally:
201 if exc and db_vim:
202 db_vim_update["_admin.operationalState"] = "ERROR"
203 db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
204 if db_vim_update:
205 self.update_db_2("vim_accounts", vim_id, db_vim_update)
206 self.lcm_tasks.remove("vim_account", vim_id, order_id)
207
208 async def delete(self, vim_id, order_id):
209 logging_text = "Task vim_delete={} ".format(vim_id)
210 self.logger.debug(logging_text + "Enter")
211 db_vim = None
212 db_vim_update = {}
213 exc = None
214 step = "Getting vim from db"
215 try:
216 db_vim = self.db.get_one("vim_accounts", {"_id": vim_id})
217 if db_vim.get("_admin") and db_vim["_admin"].get("deployed") and db_vim["_admin"]["deployed"].get("RO"):
218 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
219 RO = ROclient.ROClient(self.loop, **self.ro_config)
220 step = "Detaching vim from RO tenant"
221 try:
222 await RO.detach_datacenter(RO_vim_id)
223 except ROclient.ROClientException as e:
224 if e.http_code == 404: # not found
225 self.logger.debug(logging_text + "RO_vim_id={} already detached".format(RO_vim_id))
226 else:
227 raise
228
229 step = "Deleting vim from RO"
230 try:
231 await RO.delete("vim", RO_vim_id)
232 except ROclient.ROClientException as e:
233 if e.http_code == 404: # not found
234 self.logger.debug(logging_text + "RO_vim_id={} already deleted".format(RO_vim_id))
235 else:
236 raise
237 else:
238 # nothing to delete
239 self.logger.error(logging_text + "Nohing to remove at RO")
240 self.db.del_one("vim_accounts", {"_id": vim_id})
241 self.logger.debug(logging_text + "Exit Ok")
242 return
243
244 except (ROclient.ROClientException, DbException) as e:
245 self.logger.error(logging_text + "Exit Exception {}".format(e))
246 exc = e
247 except Exception as e:
248 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
249 exc = e
250 finally:
251 self.lcm_tasks.remove("vim_account", vim_id, order_id)
252 if exc and db_vim:
253 db_vim_update["_admin.operationalState"] = "ERROR"
254 db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
255 if db_vim_update:
256 self.update_db_2("vim_accounts", vim_id, db_vim_update)
257 self.lcm_tasks.remove("vim_account", vim_id, order_id)
258
259
260 class SdnLcm(LcmBase):
261
262 def __init__(self, db, msg, fs, lcm_tasks, ro_config, loop):
263 """
264 Init, Connect to database, filesystem storage, and messaging
265 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
266 :return: None
267 """
268
269 self.logger = logging.getLogger('lcm.sdn')
270 self.loop = loop
271 self.lcm_tasks = lcm_tasks
272 self.ro_config = ro_config
273
274 super().__init__(db, msg, fs, self.logger)
275
276 async def create(self, sdn_content, order_id):
277 sdn_id = sdn_content["_id"]
278 logging_text = "Task sdn_create={} ".format(sdn_id)
279 self.logger.debug(logging_text + "Enter")
280 db_sdn = None
281 db_sdn_update = {}
282 RO_sdn_id = None
283 exc = None
284 try:
285 step = "Getting sdn from db"
286 db_sdn = self.db.get_one("sdns", {"_id": sdn_id})
287 db_sdn_update["_admin.deployed.RO"] = None
288
289 step = "Creating sdn at RO"
290 RO = ROclient.ROClient(self.loop, **self.ro_config)
291 sdn_RO = deepcopy(sdn_content)
292 sdn_RO.pop("_id", None)
293 sdn_RO.pop("_admin", None)
294 sdn_RO.pop("schema_version", None)
295 sdn_RO.pop("schema_type", None)
296 sdn_RO.pop("description", None)
297 desc = await RO.create("sdn", descriptor=sdn_RO)
298 RO_sdn_id = desc["uuid"]
299 db_sdn_update["_admin.deployed.RO"] = RO_sdn_id
300 db_sdn_update["_admin.operationalState"] = "ENABLED"
301 self.logger.debug(logging_text + "Exit Ok RO_sdn_id={}".format(RO_sdn_id))
302 return
303
304 except (ROclient.ROClientException, DbException) as e:
305 self.logger.error(logging_text + "Exit Exception {}".format(e))
306 exc = e
307 except Exception as e:
308 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
309 exc = e
310 finally:
311 if exc and db_sdn:
312 db_sdn_update["_admin.operationalState"] = "ERROR"
313 db_sdn_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
314 if db_sdn_update:
315 self.update_db_2("sdns", sdn_id, db_sdn_update)
316 self.lcm_tasks.remove("sdn", sdn_id, order_id)
317
318 async def edit(self, sdn_content, order_id):
319 sdn_id = sdn_content["_id"]
320 logging_text = "Task sdn_edit={} ".format(sdn_id)
321 self.logger.debug(logging_text + "Enter")
322 db_sdn = None
323 db_sdn_update = {}
324 exc = None
325 step = "Getting sdn from db"
326 try:
327 db_sdn = self.db.get_one("sdns", {"_id": sdn_id})
328 if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"):
329 RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
330 RO = ROclient.ROClient(self.loop, **self.ro_config)
331 step = "Editing sdn at RO"
332 sdn_RO = deepcopy(sdn_content)
333 sdn_RO.pop("_id", None)
334 sdn_RO.pop("_admin", None)
335 sdn_RO.pop("schema_version", None)
336 sdn_RO.pop("schema_type", None)
337 sdn_RO.pop("description", None)
338 if sdn_RO:
339 await RO.edit("sdn", RO_sdn_id, descriptor=sdn_RO)
340 db_sdn_update["_admin.operationalState"] = "ENABLED"
341
342 self.logger.debug(logging_text + "Exit Ok RO_sdn_id".format(RO_sdn_id))
343 return
344
345 except (ROclient.ROClientException, DbException) as e:
346 self.logger.error(logging_text + "Exit Exception {}".format(e))
347 exc = e
348 except Exception as e:
349 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
350 exc = e
351 finally:
352 if exc and db_sdn:
353 db_sdn["_admin.operationalState"] = "ERROR"
354 db_sdn["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
355 if db_sdn_update:
356 self.update_db_2("sdns", sdn_id, db_sdn_update)
357 self.lcm_tasks.remove("sdn", sdn_id, order_id)
358
359 async def delete(self, sdn_id, order_id):
360 logging_text = "Task sdn_delete={} ".format(sdn_id)
361 self.logger.debug(logging_text + "Enter")
362 db_sdn = None
363 db_sdn_update = {}
364 exc = None
365 step = "Getting sdn from db"
366 try:
367 db_sdn = self.db.get_one("sdns", {"_id": sdn_id})
368 if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"):
369 RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
370 RO = ROclient.ROClient(self.loop, **self.ro_config)
371 step = "Deleting sdn from RO"
372 try:
373 await RO.delete("sdn", RO_sdn_id)
374 except ROclient.ROClientException as e:
375 if e.http_code == 404: # not found
376 self.logger.debug(logging_text + "RO_sdn_id={} already deleted".format(RO_sdn_id))
377 else:
378 raise
379 else:
380 # nothing to delete
381 self.logger.error(logging_text + "Skipping. There is not RO information at database")
382 self.db.del_one("sdns", {"_id": sdn_id})
383 self.logger.debug("sdn_delete task sdn_id={} Exit Ok".format(sdn_id))
384 return
385
386 except (ROclient.ROClientException, DbException) as e:
387 self.logger.error(logging_text + "Exit Exception {}".format(e))
388 exc = e
389 except Exception as e:
390 self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
391 exc = e
392 finally:
393 if exc and db_sdn:
394 db_sdn["_admin.operationalState"] = "ERROR"
395 db_sdn["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc)
396 if db_sdn_update:
397 self.update_db_2("sdns", sdn_id, db_sdn_update)
398 self.lcm_tasks.remove("sdn", sdn_id, order_id)