Merge "CLI for OSM"
[osm/SO.git] / rwlaunchpad / plugins / rwnsm / rift / tasklets / rwnsmtasklet / openmano_nsm.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain 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,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import asyncio
19 import os
20 import sys
21 import time
22 import yaml
23
24 import gi
25 gi.require_version('RwDts', '1.0')
26 gi.require_version('RwVnfrYang', '1.0')
27 from gi.repository import (
28 RwDts as rwdts,
29 RwVnfrYang,
30 )
31
32 import rift.openmano.rift2openmano as rift2openmano
33 import rift.openmano.openmano_client as openmano_client
34 from . import rwnsmplugin
35
36 import rift.tasklets
37
38 if sys.version_info < (3, 4, 4):
39 asyncio.ensure_future = asyncio.async
40
41
42 DUMP_OPENMANO_DIR = os.path.join(
43 os.environ["RIFT_ARTIFACTS"],
44 "openmano_descriptors"
45 )
46
47
48 def dump_openmano_descriptor(name, descriptor_str):
49 filename = "{}_{}.yaml".format(
50 time.strftime("%Y%m%d-%H%M%S"),
51 name
52 )
53
54 filepath = os.path.join(
55 DUMP_OPENMANO_DIR,
56 filename
57 )
58
59 try:
60 if not os.path.exists(DUMP_OPENMANO_DIR):
61 os.makedirs(DUMP_OPENMANO_DIR)
62
63 with open(filepath, 'w') as hdl:
64 hdl.write(descriptor_str)
65
66 except OSError as e:
67 print("Failed to dump openmano descriptor: %s" % str(e))
68
69 return filepath
70
71 class VnfrConsoleOperdataDtsHandler(object):
72 """ registers 'D,/vnfr:vnfr-console/vnfr:vnfr[id]/vdur[id]' and handles CRUD from DTS"""
73 @property
74 def vnfr_vdu_console_xpath(self):
75 """ path for resource-mgr"""
76 return ("D,/rw-vnfr:vnfr-console/rw-vnfr:vnfr[rw-vnfr:id='{}']/rw-vnfr:vdur[vnfr:id='{}']".format(self._vnfr_id,self._vdur_id))
77
78 def __init__(self, dts, log, loop, nsr, vnfr_id, vdur_id, vdu_id):
79 self._dts = dts
80 self._log = log
81 self._loop = loop
82 self._regh = None
83 self._nsr = nsr
84
85 self._vnfr_id = vnfr_id
86 self._vdur_id = vdur_id
87 self._vdu_id = vdu_id
88
89 @asyncio.coroutine
90 def register(self):
91 """ Register for VNFR VDU Operational Data read from dts """
92
93 @asyncio.coroutine
94 def on_prepare(xact_info, action, ks_path, msg):
95 """ prepare callback from dts """
96 xpath = ks_path.to_xpath(RwVnfrYang.get_schema())
97 self._log.debug(
98 "Got VNFR VDU Opdata xact_info: %s, action: %s): %s:%s",
99 xact_info, action, xpath, msg
100 )
101
102 if action == rwdts.QueryAction.READ:
103 schema = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur.schema()
104 path_entry = schema.keyspec_to_entry(ks_path)
105
106 try:
107 console_url = yield from self._loop.run_in_executor(
108 None,
109 self._nsr._http_api.get_instance_vm_console_url,
110 self._nsr._nsr_uuid,
111 self._vdur_id
112 )
113
114 self._log.debug("Got console response: %s for NSR ID %s vdur ID %s",
115 console_url,
116 self._nsr._nsr_uuid,
117 self._vdur_id
118 )
119 vdur_console = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur()
120 vdur_console.id = self._vdur_id
121 if console_url:
122 vdur_console.console_url = console_url
123 else:
124 vdur_console.console_url = 'none'
125 self._log.debug("Recevied console URL for vdu {} is {}".format(self._vdu_id,vdur_console))
126 except openmano_client.InstanceStatusError as e:
127 self._log.error("Could not get NS instance console URL: %s",
128 str(e))
129 vdur_console = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur()
130 vdur_console.id = self._vdur_id
131 vdur_console.console_url = 'none'
132
133 xact_info.respond_xpath(rsp_code=rwdts.XactRspCode.ACK,
134 xpath=self.vnfr_vdu_console_xpath,
135 msg=vdur_console)
136 else:
137 #raise VnfRecordError("Not supported operation %s" % action)
138 self._log.error("Not supported operation %s" % action)
139 xact_info.respond_xpath(rsp_code=rwdts.XactRspCode.ACK)
140 return
141
142 self._log.debug("Registering for VNFR VDU using xpath: %s",
143 self.vnfr_vdu_console_xpath)
144 hdl = rift.tasklets.DTS.RegistrationHandler(on_prepare=on_prepare,)
145 with self._dts.group_create() as group:
146 self._regh = group.register(xpath=self.vnfr_vdu_console_xpath,
147 handler=hdl,
148 flags=rwdts.Flag.PUBLISHER,
149 )
150
151
152
153 class OpenmanoVnfr(object):
154 def __init__(self, log, loop, cli_api, vnfr):
155 self._log = log
156 self._loop = loop
157 self._cli_api = cli_api
158 self._vnfr = vnfr
159 self._vnfd_id = vnfr.vnfd.id
160
161 self._vnf_id = None
162
163 self._created = False
164
165 @property
166 def vnfd(self):
167 return rift2openmano.RiftVNFD(self._vnfr.vnfd)
168
169 @property
170 def vnfr(self):
171 return self._vnfr
172
173 @property
174 def rift_vnfd_id(self):
175 return self._vnfd_id
176
177 @property
178 def openmano_vnfd_id(self):
179 return self._vnf_id
180
181 @property
182 def openmano_vnfd(self):
183 self._log.debug("Converting vnfd %s from rift to openmano", self.vnfd.id)
184 openmano_vnfd = rift2openmano.rift2openmano_vnfd(self.vnfd)
185 return openmano_vnfd
186
187 @property
188 def openmano_vnfd_yaml(self):
189 return yaml.safe_dump(self.openmano_vnfd, default_flow_style=False)
190
191 @asyncio.coroutine
192 def create(self):
193 self._log.debug("Creating openmano vnfd")
194 openmano_vnfd = self.openmano_vnfd
195 name = openmano_vnfd["vnf"]["name"]
196
197 # If the name already exists, get the openmano vnfd id
198 name_uuid_map = yield from self._loop.run_in_executor(
199 None,
200 self._cli_api.vnf_list,
201 )
202
203 if name in name_uuid_map:
204 vnf_id = name_uuid_map[name]
205 self._log.debug("Vnf already created. Got existing openmano vnfd id: %s", vnf_id)
206 self._vnf_id = vnf_id
207 return
208
209 self._vnf_id, _ = yield from self._loop.run_in_executor(
210 None,
211 self._cli_api.vnf_create,
212 self.openmano_vnfd_yaml,
213 )
214
215 fpath = dump_openmano_descriptor(
216 "{}_vnf".format(name),
217 self.openmano_vnfd_yaml
218 )
219
220 self._log.debug("Dumped Openmano VNF descriptor to: %s", fpath)
221
222 self._created = True
223
224 @asyncio.coroutine
225 def delete(self):
226 if not self._created:
227 return
228
229 self._log.debug("Deleting openmano vnfd")
230 if self._vnf_id is None:
231 self._log.warning("Openmano vnf id not set. Cannot delete.")
232 return
233
234 yield from self._loop.run_in_executor(
235 None,
236 self._cli_api.vnf_delete,
237 self._vnf_id,
238 )
239
240
241 class OpenmanoNsr(object):
242 TIMEOUT_SECS = 300
243
244 def __init__(self, dts, log, loop, publisher, cli_api, http_api, nsd_msg, nsr_config_msg,key_pairs):
245 self._dts = dts
246 self._log = log
247 self._loop = loop
248 self._publisher = publisher
249 self._cli_api = cli_api
250 self._http_api = http_api
251
252 self._nsd_msg = nsd_msg
253 self._nsr_config_msg = nsr_config_msg
254
255 self._vlrs = []
256 self._vnfrs = []
257 self._vdur_console_handler = {}
258 self._key_pairs = key_pairs
259
260 self._nsd_uuid = None
261 self._nsr_uuid = None
262
263 self._nsr_msg = None
264
265 self._created = False
266
267 self._monitor_task = None
268
269 @property
270 def nsd(self):
271 return rift2openmano.RiftNSD(self._nsd_msg)
272
273 @property
274 def vnfds(self):
275 return {v.rift_vnfd_id: v.vnfd for v in self._vnfrs}
276
277 @property
278 def vnfr_ids(self):
279 return {v.rift_vnfd_id: v.openmano_vnfd_id for v in self._vnfrs}
280
281 @property
282 def vnfrs(self):
283 return self._vnfrs
284
285 @property
286 def openmano_nsd_yaml(self):
287 self._log.debug("Converting nsd %s from rift to openmano", self.nsd.id)
288 openmano_nsd = rift2openmano.rift2openmano_nsd(self.nsd, self.vnfds,self.vnfr_ids)
289 return yaml.safe_dump(openmano_nsd, default_flow_style=False)
290
291 def get_ssh_key_pairs(self):
292 cloud_config = {}
293 key_pairs = list()
294 for authorized_key in self._nsr_config_msg.ssh_authorized_key:
295 self._log.debug("Key pair ref present is %s",authorized_key.key_pair_ref)
296 if authorized_key.key_pair_ref in self._key_pairs:
297 key_pairs.append(self._key_pairs[authorized_key.key_pair_ref].key)
298
299 for authorized_key in self._nsd_msg.key_pair:
300 self._log.debug("Key pair NSD is %s",authorized_key)
301 key_pairs.append(authorized_key.key)
302
303 if key_pairs:
304 cloud_config["key-pairs"] = key_pairs
305
306 users = list()
307 for user_entry in self._nsr_config_msg.user:
308 self._log.debug("User present is %s",user_entry)
309 user = {}
310 user["name"] = user_entry.name
311 user["key-pairs"] = list()
312 for ssh_key in user_entry.key_pair:
313 user["key-pairs"].append(ssh_key.key)
314 users.append(user)
315
316 for user_entry in self._nsd_msg.user:
317 self._log.debug("User present in NSD is %s",user_entry)
318 user = {}
319 user["name"] = user_entry.name
320 user["key-pairs"] = list()
321 for ssh_key in user_entry.key_pair:
322 user["key-pairs"].append(ssh_key.key)
323 users.append(user)
324
325 if users:
326 cloud_config["users"] = users
327
328 self._log.debug("Cloud config formed is %s",cloud_config)
329 return cloud_config
330
331
332 @property
333 def openmano_instance_create_yaml(self):
334 self._log.debug("Creating instance-scenario-create input file for nsd %s with name %s", self.nsd.id, self._nsr_config_msg.name)
335 openmano_instance_create = {}
336 openmano_instance_create["name"] = self._nsr_config_msg.name
337 openmano_instance_create["description"] = self._nsr_config_msg.description
338 openmano_instance_create["scenario"] = self._nsd_uuid
339
340 cloud_config = self.get_ssh_key_pairs()
341 if cloud_config:
342 openmano_instance_create["cloud-config"] = cloud_config
343 if self._nsr_config_msg.has_field("om_datacenter"):
344 openmano_instance_create["datacenter"] = self._nsr_config_msg.om_datacenter
345 openmano_instance_create["vnfs"] = {}
346 for vnfr in self._vnfrs:
347 if "om_datacenter" in vnfr.vnfr.vnfr_msg:
348 vnfr_name = vnfr.vnfr.vnfd.name + "__" + str(vnfr.vnfr.vnfr_msg.member_vnf_index_ref)
349 openmano_instance_create["vnfs"][vnfr_name] = {"datacenter": vnfr.vnfr.vnfr_msg.om_datacenter}
350 openmano_instance_create["networks"] = {}
351 for vld_msg in self._nsd_msg.vld:
352 openmano_instance_create["networks"][vld_msg.name] = {}
353 openmano_instance_create["networks"][vld_msg.name]["sites"] = list()
354 for vlr in self._vlrs:
355 if vlr.vld_msg.name == vld_msg.name:
356 self._log.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr.vld_msg.name,
357 vlr.om_datacenter_name,vld_msg.name)
358 #network["vim-network-name"] = vld_msg.name
359 network = {}
360 ip_profile = {}
361 if vld_msg.vim_network_name:
362 network["netmap-use"] = vld_msg.vim_network_name
363 elif vlr._ip_profile and vlr._ip_profile.has_field("ip_profile_params"):
364 ip_profile_params = vlr._ip_profile.ip_profile_params
365 if ip_profile_params.ip_version == "ipv6":
366 ip_profile['ip-version'] = "IPv6"
367 else:
368 ip_profile['ip-version'] = "IPv4"
369 if ip_profile_params.has_field('subnet_address'):
370 ip_profile['subnet-address'] = ip_profile_params.subnet_address
371 if ip_profile_params.has_field('gateway_address'):
372 ip_profile['gateway-address'] = ip_profile_params.gateway_address
373 if ip_profile_params.has_field('dns_server') and len(ip_profile_params.dns_server) > 0:
374 ip_profile['dns-address'] = ip_profile_params.dns_server[0].address
375 if ip_profile_params.has_field('dhcp_params'):
376 ip_profile['dhcp'] = {}
377 ip_profile['dhcp']['enabled'] = ip_profile_params.dhcp_params.enabled
378 ip_profile['dhcp']['start-address'] = ip_profile_params.dhcp_params.start_address
379 ip_profile['dhcp']['count'] = ip_profile_params.dhcp_params.count
380 else:
381 network["netmap-create"] = vlr.name
382 if vlr.om_datacenter_name:
383 network["datacenter"] = vlr.om_datacenter_name
384 elif vld_msg.has_field("om_datacenter"):
385 network["datacenter"] = vld_msg.om_datacenter
386 elif "datacenter" in openmano_instance_create:
387 network["datacenter"] = openmano_instance_create["datacenter"]
388 if network:
389 openmano_instance_create["networks"][vld_msg.name]["sites"].append(network)
390 if ip_profile:
391 openmano_instance_create["networks"][vld_msg.name]['ip-profile'] = ip_profile
392
393 return yaml.safe_dump(openmano_instance_create, default_flow_style=False,width=1000)
394
395 @asyncio.coroutine
396 def add_vlr(self, vlr):
397 self._vlrs.append(vlr)
398 yield from asyncio.sleep(1, loop=self._loop)
399
400 @asyncio.coroutine
401 def add_vnfr(self, vnfr):
402 vnfr = OpenmanoVnfr(self._log, self._loop, self._cli_api, vnfr)
403 yield from vnfr.create()
404 self._vnfrs.append(vnfr)
405
406 @asyncio.coroutine
407 def delete(self):
408 if not self._created:
409 self._log.debug("NSD wasn't created. Skipping delete.")
410 return
411
412 self._log.debug("Deleting openmano nsr")
413
414 yield from self._loop.run_in_executor(
415 None,
416 self._cli_api.ns_delete,
417 self._nsd_uuid,
418 )
419
420 self._log.debug("Deleting openmano vnfrs")
421 for vnfr in self._vnfrs:
422 yield from vnfr.delete()
423
424
425 @asyncio.coroutine
426 def create(self):
427 self._log.debug("Creating openmano scenario")
428 name_uuid_map = yield from self._loop.run_in_executor(
429 None,
430 self._cli_api.ns_list,
431 )
432
433 if self._nsd_msg.name in name_uuid_map:
434 self._log.debug("Found existing openmano scenario")
435 self._nsd_uuid = name_uuid_map[self._nsd_msg.name]
436 return
437
438
439 # Use the nsd uuid as the scenario name to rebind to existing
440 # scenario on reload or to support muliple instances of the name
441 # nsd
442 self._nsd_uuid, _ = yield from self._loop.run_in_executor(
443 None,
444 self._cli_api.ns_create,
445 self.openmano_nsd_yaml,
446 self._nsd_msg.name
447 )
448 fpath = dump_openmano_descriptor(
449 "{}_nsd".format(self._nsd_msg.name),
450 self.openmano_nsd_yaml,
451 )
452
453 self._log.debug("Dumped Openmano NS descriptor to: %s", fpath)
454
455 self._created = True
456
457 @asyncio.coroutine
458 def instance_monitor_task(self):
459 self._log.debug("Starting Instance monitoring task")
460
461 start_time = time.time()
462 active_vnfs = []
463
464 while True:
465 yield from asyncio.sleep(1, loop=self._loop)
466
467 try:
468 instance_resp_json = yield from self._loop.run_in_executor(
469 None,
470 self._http_api.get_instance,
471 self._nsr_uuid,
472 )
473
474 self._log.debug("Got instance response: %s for NSR ID %s",
475 instance_resp_json,
476 self._nsr_uuid)
477
478 except openmano_client.InstanceStatusError as e:
479 self._log.error("Could not get NS instance status: %s", str(e))
480 continue
481
482 def all_vms_active(vnf):
483 for vm in vnf["vms"]:
484 vm_status = vm["status"]
485 vm_uuid = vm["uuid"]
486 if vm_status != "ACTIVE":
487 self._log.debug("VM is not yet active: %s (status: %s)", vm_uuid, vm_status)
488 return False
489
490 return True
491
492 def any_vm_active_nomgmtip(vnf):
493 for vm in vnf["vms"]:
494 vm_status = vm["status"]
495 vm_uuid = vm["uuid"]
496 if vm_status != "ACTIVE":
497 self._log.debug("VM is not yet active: %s (status: %s)", vm_uuid, vm_status)
498 return False
499
500 return True
501
502 def any_vms_error(vnf):
503 for vm in vnf["vms"]:
504 vm_status = vm["status"]
505 vm_vim_info = vm["vim_info"]
506 vm_uuid = vm["uuid"]
507 if vm_status == "ERROR":
508 self._log.error("VM Error: %s (vim_info: %s)", vm_uuid, vm_vim_info)
509 return True
510
511 return False
512
513 def get_vnf_ip_address(vnf):
514 if "ip_address" in vnf:
515 return vnf["ip_address"].strip()
516 return None
517
518 def get_ext_cp_info(vnf):
519 cp_info_list = []
520 for vm in vnf["vms"]:
521 if "interfaces" not in vm:
522 continue
523
524 for intf in vm["interfaces"]:
525 if "external_name" not in intf:
526 continue
527
528 if not intf["external_name"]:
529 continue
530
531 ip_address = intf["ip_address"]
532 if ip_address is None:
533 ip_address = "0.0.0.0"
534
535 cp_info_list.append((intf["external_name"], ip_address))
536
537 return cp_info_list
538
539 def get_vnf_status(vnfr):
540 # When we create an openmano descriptor we use <name>__<idx>
541 # to come up with openmano constituent VNF name. Use this
542 # knowledge to map the vnfr back.
543 openmano_vnfr_suffix = "__{}".format(
544 vnfr.vnfr.vnfr_msg.member_vnf_index_ref
545 )
546
547 for vnf in instance_resp_json["vnfs"]:
548 if vnf["vnf_name"].endswith(openmano_vnfr_suffix):
549 return vnf
550
551 self._log.warning("Could not find vnf status with name that ends with: %s",
552 openmano_vnfr_suffix)
553 return None
554
555 for vnfr in self._vnfrs:
556 if vnfr in active_vnfs:
557 # Skipping, so we don't re-publish the same VNF message.
558 continue
559
560 vnfr_msg = vnfr.vnfr.vnfr_msg.deep_copy()
561 vnfr_msg.operational_status = "init"
562
563 try:
564 vnf_status = get_vnf_status(vnfr)
565 self._log.debug("Found VNF status: %s", vnf_status)
566 if vnf_status is None:
567 self._log.error("Could not find VNF status from openmano")
568 vnfr_msg.operational_status = "failed"
569 yield from self._publisher.publish_vnfr(None, vnfr_msg)
570 return
571
572 # If there was a VNF that has a errored VM, then just fail the VNF and stop monitoring.
573 if any_vms_error(vnf_status):
574 self._log.debug("VM was found to be in error state. Marking as failed.")
575 vnfr_msg.operational_status = "failed"
576 yield from self._publisher.publish_vnfr(None, vnfr_msg)
577 return
578
579 if all_vms_active(vnf_status):
580 vnf_ip_address = get_vnf_ip_address(vnf_status)
581
582 if vnf_ip_address is None:
583 self._log.warning("No IP address obtained "
584 "for VNF: {}, will retry.".format(
585 vnf_status['vnf_name']))
586 continue
587
588 self._log.debug("All VMs in VNF are active. Marking as running.")
589 vnfr_msg.operational_status = "running"
590
591 self._log.debug("Got VNF ip address: %s", vnf_ip_address)
592 vnfr_msg.mgmt_interface.ip_address = vnf_ip_address
593 vnfr_msg.vnf_configuration.config_access.mgmt_ip_address = vnf_ip_address
594
595
596 for vm in vnf_status["vms"]:
597 if vm["uuid"] not in self._vdur_console_handler:
598 vdur_console_handler = VnfrConsoleOperdataDtsHandler(self._dts, self._log, self._loop,
599 self, vnfr_msg.id,vm["uuid"],vm["name"])
600 yield from vdur_console_handler.register()
601 self._vdur_console_handler[vm["uuid"]] = vdur_console_handler
602
603 vdur_msg = vnfr_msg.vdur.add()
604 vdur_msg.vim_id = vm["vim_vm_id"]
605 vdur_msg.id = vm["uuid"]
606
607 # Add connection point information for the config manager
608 cp_info_list = get_ext_cp_info(vnf_status)
609 for (cp_name, cp_ip) in cp_info_list:
610 cp = vnfr_msg.connection_point.add()
611 cp.name = cp_name
612 cp.short_name = cp_name
613 cp.ip_address = cp_ip
614
615 yield from self._publisher.publish_vnfr(None, vnfr_msg)
616 active_vnfs.append(vnfr)
617
618 if (time.time() - start_time) > OpenmanoNsr.TIMEOUT_SECS:
619 self._log.error("NSR timed out before reaching running state")
620 vnfr_msg.operational_status = "failed"
621 yield from self._publisher.publish_vnfr(None, vnfr_msg)
622 return
623
624 except Exception as e:
625 vnfr_msg.operational_status = "failed"
626 yield from self._publisher.publish_vnfr(None, vnfr_msg)
627 self._log.exception("Caught exception publishing vnfr info: %s", str(e))
628 return
629
630 if len(active_vnfs) == len(self._vnfrs):
631 self._log.info("All VNF's are active. Exiting NSR monitoring task")
632 return
633
634 @asyncio.coroutine
635 def deploy(self,nsr_msg):
636 if self._nsd_uuid is None:
637 raise ValueError("Cannot deploy an uncreated nsd")
638
639 self._log.debug("Deploying openmano scenario")
640
641 name_uuid_map = yield from self._loop.run_in_executor(
642 None,
643 self._cli_api.ns_instance_list,
644 )
645
646 if self._nsr_config_msg.name in name_uuid_map:
647 self._log.debug("Found existing instance with nsr name: %s", self._nsr_config_msg.name)
648 self._nsr_uuid = name_uuid_map[self._nsr_config_msg.name]
649 else:
650 self._nsr_msg = nsr_msg
651 fpath = dump_openmano_descriptor(
652 "{}_instance_sce_create".format(self._nsr_config_msg.name),
653 self.openmano_instance_create_yaml,
654 )
655 self._log.debug("Dumped Openmano NS Scenario Cretae to: %s", fpath)
656
657 self._nsr_uuid = yield from self._loop.run_in_executor(
658 None,
659 self._cli_api.ns_instance_scenario_create,
660 self.openmano_instance_create_yaml)
661
662
663 self._monitor_task = asyncio.ensure_future(
664 self.instance_monitor_task(), loop=self._loop
665 )
666
667 @asyncio.coroutine
668 def terminate(self):
669
670 for _,handler in self._vdur_console_handler.items():
671 handler._regh.deregister()
672
673 if self._nsr_uuid is None:
674 self._log.warning("Cannot terminate an un-instantiated nsr")
675 return
676
677 if self._monitor_task is not None:
678 self._monitor_task.cancel()
679 self._monitor_task = None
680
681 self._log.debug("Terminating openmano nsr")
682 yield from self._loop.run_in_executor(
683 None,
684 self._cli_api.ns_terminate,
685 self._nsr_uuid,
686 )
687
688
689 class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
690 """
691 RW Implentation of the NsmPluginBase
692 """
693 def __init__(self, dts, log, loop, publisher, ro_account):
694 self._dts = dts
695 self._log = log
696 self._loop = loop
697 self._publisher = publisher
698
699 self._cli_api = None
700 self._http_api = None
701 self._openmano_nsrs = {}
702
703 self._set_ro_account(ro_account)
704
705 def _set_ro_account(self, ro_account):
706 self._log.debug("Setting openmano plugin cloud account: %s", ro_account)
707 self._cli_api = openmano_client.OpenmanoCliAPI(
708 self.log,
709 ro_account.openmano.host,
710 ro_account.openmano.port,
711 ro_account.openmano.tenant_id,
712 )
713
714 self._http_api = openmano_client.OpenmanoHttpAPI(
715 self.log,
716 ro_account.openmano.host,
717 ro_account.openmano.port,
718 ro_account.openmano.tenant_id,
719 )
720
721 def create_nsr(self, nsr_config_msg, nsd_msg, key_pairs=None):
722 """
723 Create Network service record
724 """
725 openmano_nsr = OpenmanoNsr(
726 self._dts,
727 self._log,
728 self._loop,
729 self._publisher,
730 self._cli_api,
731 self._http_api,
732 nsd_msg,
733 nsr_config_msg,
734 key_pairs
735 )
736 self._openmano_nsrs[nsr_config_msg.id] = openmano_nsr
737
738 @asyncio.coroutine
739 def deploy(self, nsr_msg):
740 self._log.debug("Received NSR Deploy msg : %s", nsr_msg)
741 openmano_nsr = self._openmano_nsrs[nsr_msg.ns_instance_config_ref]
742 yield from openmano_nsr.create()
743 yield from openmano_nsr.deploy(nsr_msg)
744
745 @asyncio.coroutine
746 def instantiate_ns(self, nsr, xact):
747 """
748 Instantiate NSR with the passed nsr id
749 """
750 yield from nsr.instantiate(xact)
751
752 @asyncio.coroutine
753 def instantiate_vnf(self, nsr, vnfr):
754 """
755 Instantiate NSR with the passed nsr id
756 """
757 openmano_nsr = self._openmano_nsrs[nsr.id]
758 yield from openmano_nsr.add_vnfr(vnfr)
759
760 # Mark the VNFR as running
761 # TODO: Create a task to monitor nsr/vnfr status
762 vnfr_msg = vnfr.vnfr_msg.deep_copy()
763 vnfr_msg.operational_status = "init"
764
765 self._log.debug("Attempting to publish openmano vnf: %s", vnfr_msg)
766 with self._dts.transaction() as xact:
767 yield from self._publisher.publish_vnfr(xact, vnfr_msg)
768
769 @asyncio.coroutine
770 def instantiate_vl(self, nsr, vlr):
771 """
772 Instantiate NSR with the passed nsr id
773 """
774 self._log.debug("Received instantiate VL for NSR {}; VLR {}".format(nsr.id,vlr))
775 openmano_nsr = self._openmano_nsrs[nsr.id]
776 yield from openmano_nsr.add_vlr(vlr)
777
778 @asyncio.coroutine
779 def terminate_ns(self, nsr):
780 """
781 Terminate the network service
782 """
783 nsr_id = nsr.id
784 openmano_nsr = self._openmano_nsrs[nsr_id]
785 yield from openmano_nsr.terminate()
786 yield from openmano_nsr.delete()
787
788 with self._dts.transaction() as xact:
789 for vnfr in openmano_nsr.vnfrs:
790 self._log.debug("Unpublishing VNFR: %s", vnfr.vnfr.vnfr_msg)
791 yield from self._publisher.unpublish_vnfr(xact, vnfr.vnfr.vnfr_msg)
792
793 del self._openmano_nsrs[nsr_id]
794
795 @asyncio.coroutine
796 def terminate_vnf(self, vnfr):
797 """
798 Terminate the network service
799 """
800 pass
801
802 @asyncio.coroutine
803 def terminate_vl(self, vlr):
804 """
805 Terminate the virtual link
806 """
807 pass
808