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