Bug 173 - Creation of additional networks in running NS does not publish VLRs
[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, nsd):
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 self.nsd = nsd
168
169 @property
170 def vnfd(self):
171 return rift2openmano.RiftVNFD(self._vnfr.vnfd)
172
173 @property
174 def vnfr(self):
175 return self._vnfr
176
177 @property
178 def rift_vnfd_id(self):
179 return self._vnfd_id
180
181 @property
182 def openmano_vnfd_id(self):
183 return self._vnf_id
184
185 @property
186 def openmano_vnfd(self):
187 self._log.debug("Converting vnfd %s from rift to openmano", self.vnfd.id)
188 openmano_vnfd = rift2openmano.rift2openmano_vnfd(self.vnfd, self.nsd)
189 return openmano_vnfd
190
191 @property
192 def openmano_vnfd_yaml(self):
193 return yaml.safe_dump(self.openmano_vnfd, default_flow_style=False)
194
195 @asyncio.coroutine
196 def create(self):
197 self._log.debug("Creating openmano vnfd")
198 openmano_vnfd = self.openmano_vnfd
199 name = openmano_vnfd["vnf"]["name"]
200
201 # If the name already exists, get the openmano vnfd id
202 name_uuid_map = yield from self._loop.run_in_executor(
203 None,
204 self._cli_api.vnf_list,
205 )
206
207 if name in name_uuid_map:
208 vnf_id = name_uuid_map[name]
209 self._log.debug("Vnf already created. Got existing openmano vnfd id: %s", vnf_id)
210 self._vnf_id = vnf_id
211 return
212
213 self._vnf_id, _ = yield from self._loop.run_in_executor(
214 None,
215 self._cli_api.vnf_create,
216 self.openmano_vnfd_yaml,
217 )
218
219 fpath = dump_openmano_descriptor(
220 "{}_vnf".format(name),
221 self.openmano_vnfd_yaml
222 )
223
224 self._log.debug("Dumped Openmano VNF descriptor to: %s", fpath)
225
226 self._created = True
227
228 @asyncio.coroutine
229 def delete(self):
230 if not self._created:
231 return
232
233 self._log.debug("Deleting openmano vnfd")
234 if self._vnf_id is None:
235 self._log.warning("Openmano vnf id not set. Cannot delete.")
236 return
237
238 yield from self._loop.run_in_executor(
239 None,
240 self._cli_api.vnf_delete,
241 self._vnf_id,
242 )
243
244
245 class OpenmanoNSRecordState(Enum):
246 """ Network Service Record State """
247 # Make sure the values match with NetworkServiceRecordState
248 INIT = 101
249 INSTANTIATION_PENDING = 102
250 RUNNING = 106
251 SCALING_OUT = 107
252 SCALING_IN = 108
253 TERMINATE = 109
254 TERMINATE_RCVD = 110
255 TERMINATED = 114
256 FAILED = 115
257 VL_INSTANTIATE = 116
258 VL_TERMINATE = 117
259
260
261 class OpenmanoNsr(object):
262 TIMEOUT_SECS = 300
263
264 def __init__(self, dts, log, loop, publisher, cli_api, http_api, nsd_msg, nsr_config_msg,key_pairs):
265 self._dts = dts
266 self._log = log
267 self._loop = loop
268 self._publisher = publisher
269 self._cli_api = cli_api
270 self._http_api = http_api
271
272 self._nsd_msg = nsd_msg
273 self._nsr_config_msg = nsr_config_msg
274
275 self._vlrs = []
276 self._vnfrs = []
277 self._vdur_console_handler = {}
278 self._key_pairs = key_pairs
279
280 self._nsd_uuid = None
281 self._nsr_uuid = None
282
283 self._nsr_msg = None
284
285 self._created = False
286
287 self._monitor_task = None
288 self._state = OpenmanoNSRecordState.INIT
289
290 @property
291 def nsd(self):
292 return rift2openmano.RiftNSD(self._nsd_msg)
293
294 @property
295 def vnfds(self):
296 return {v.rift_vnfd_id: v.vnfd for v in self._vnfrs}
297
298 @property
299 def vnfr_ids(self):
300 return {v.rift_vnfd_id: v.openmano_vnfd_id for v in self._vnfrs}
301
302 @property
303 def vnfrs(self):
304 return self._vnfrs
305
306 @property
307 def openmano_nsd_yaml(self):
308 self._log.debug("Converting nsd %s from rift to openmano", self.nsd.id)
309 openmano_nsd = rift2openmano.rift2openmano_nsd(self.nsd, self.vnfds,self.vnfr_ids)
310 return yaml.safe_dump(openmano_nsd, default_flow_style=False)
311
312 def get_ssh_key_pairs(self):
313 cloud_config = {}
314 key_pairs = list()
315 for authorized_key in self._nsr_config_msg.ssh_authorized_key:
316 self._log.debug("Key pair ref present is %s",authorized_key.key_pair_ref)
317 if authorized_key.key_pair_ref in self._key_pairs:
318 key_pairs.append(self._key_pairs[authorized_key.key_pair_ref].key)
319
320 for authorized_key in self._nsd_msg.key_pair:
321 self._log.debug("Key pair NSD is %s",authorized_key)
322 key_pairs.append(authorized_key.key)
323
324 if key_pairs:
325 cloud_config["key-pairs"] = key_pairs
326
327 users = list()
328 for user_entry in self._nsr_config_msg.user:
329 self._log.debug("User present is %s",user_entry)
330 user = {}
331 user["name"] = user_entry.name
332 user["key-pairs"] = list()
333 for ssh_key in user_entry.ssh_authorized_key:
334 if ssh_key.key_pair_ref in self._key_pairs:
335 user["key-pairs"].append(self._key_pairs[ssh_key.key_pair_ref].key)
336 users.append(user)
337
338 for user_entry in self._nsd_msg.user:
339 self._log.debug("User present in NSD is %s",user_entry)
340 user = {}
341 user["name"] = user_entry.name
342 user["key-pairs"] = list()
343 for ssh_key in user_entry.key_pair:
344 user["key-pairs"].append(ssh_key.key)
345 users.append(user)
346
347 if users:
348 cloud_config["users"] = users
349
350 self._log.debug("Cloud config formed is %s",cloud_config)
351 return cloud_config
352
353
354 @property
355 def openmano_instance_create_yaml(self):
356 self._log.debug("Creating instance-scenario-create input file for nsd %s with name %s", self.nsd.id, self._nsr_config_msg.name)
357 openmano_instance_create = {}
358 openmano_instance_create["name"] = self._nsr_config_msg.name
359 openmano_instance_create["description"] = self._nsr_config_msg.description
360 openmano_instance_create["scenario"] = self._nsd_uuid
361
362 cloud_config = self.get_ssh_key_pairs()
363 if cloud_config:
364 openmano_instance_create["cloud-config"] = cloud_config
365 if self._nsr_config_msg.has_field("om_datacenter"):
366 openmano_instance_create["datacenter"] = self._nsr_config_msg.om_datacenter
367 openmano_instance_create["vnfs"] = {}
368 for vnfr in self._vnfrs:
369 if "om_datacenter" in vnfr.vnfr.vnfr_msg:
370 vnfr_name = vnfr.vnfr.vnfd.name + "__" + str(vnfr.vnfr.vnfr_msg.member_vnf_index_ref)
371 openmano_instance_create["vnfs"][vnfr_name] = {"datacenter": vnfr.vnfr.vnfr_msg.om_datacenter}
372 openmano_instance_create["networks"] = {}
373 for vld_msg in self._nsd_msg.vld:
374 openmano_instance_create["networks"][vld_msg.name] = {}
375 openmano_instance_create["networks"][vld_msg.name]["sites"] = list()
376 for vlr in self._vlrs:
377 if vlr.vld_msg.name == vld_msg.name:
378 self._log.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr.vld_msg.name,
379 vlr.om_datacenter_name,vld_msg.name)
380 #network["vim-network-name"] = vld_msg.name
381 network = {}
382 ip_profile = {}
383 if vld_msg.vim_network_name:
384 network["netmap-use"] = vld_msg.vim_network_name
385 elif vlr._ip_profile and vlr._ip_profile.has_field("ip_profile_params"):
386 ip_profile_params = vlr._ip_profile.ip_profile_params
387 if ip_profile_params.ip_version == "ipv6":
388 ip_profile['ip-version'] = "IPv6"
389 else:
390 ip_profile['ip-version'] = "IPv4"
391 if ip_profile_params.has_field('subnet_address'):
392 ip_profile['subnet-address'] = ip_profile_params.subnet_address
393 if ip_profile_params.has_field('gateway_address'):
394 ip_profile['gateway-address'] = ip_profile_params.gateway_address
395 if ip_profile_params.has_field('dns_server') and len(ip_profile_params.dns_server) > 0:
396 ip_profile['dns-address'] = ip_profile_params.dns_server[0].address
397 if ip_profile_params.has_field('dhcp_params'):
398 ip_profile['dhcp'] = {}
399 ip_profile['dhcp']['enabled'] = ip_profile_params.dhcp_params.enabled
400 ip_profile['dhcp']['start-address'] = ip_profile_params.dhcp_params.start_address
401 ip_profile['dhcp']['count'] = ip_profile_params.dhcp_params.count
402 else:
403 network["netmap-create"] = vlr.name
404 if vlr.om_datacenter_name:
405 network["datacenter"] = vlr.om_datacenter_name
406 elif vld_msg.has_field("om_datacenter"):
407 network["datacenter"] = vld_msg.om_datacenter
408 elif "datacenter" in openmano_instance_create:
409 network["datacenter"] = openmano_instance_create["datacenter"]
410 if network:
411 openmano_instance_create["networks"][vld_msg.name]["sites"].append(network)
412 if ip_profile:
413 openmano_instance_create["networks"][vld_msg.name]['ip-profile'] = ip_profile
414
415 return yaml.safe_dump(openmano_instance_create, default_flow_style=False,width=1000)
416
417 @asyncio.coroutine
418 def add_vlr(self, vlr):
419 self._vlrs.append(vlr)
420 yield from self._publisher.publish_vlr(None, vlr.vlr_msg)
421 yield from asyncio.sleep(1, loop=self._loop)
422
423 @asyncio.coroutine
424 def remove_vlr(self, vlr):
425 if vlr in self._vlrs:
426 self._vlrs.remove(vlr)
427 yield from self._publisher.unpublish_vlr(None, vlr.vlr_msg)
428 yield from asyncio.sleep(1, loop=self._loop)
429
430 @asyncio.coroutine
431 def delete_vlr(self, vlr):
432 if vlr in self._vlrs:
433 self._vlrs.remove(vlr)
434 if not vlr.vld_msg.vim_network_name:
435 yield from self._loop.run_in_executor(
436 None,
437 self._cli_api.ns_vim_network_delete,
438 vlr.name,
439 vlr.om_datacenter_name)
440 yield from self._publisher.unpublish_vlr(None, vlr.vlr_msg)
441 yield from asyncio.sleep(1, loop=self._loop)
442
443 @asyncio.coroutine
444 def add_vnfr(self, vnfr):
445 vnfr = OpenmanoVnfr(self._log, self._loop, self._cli_api, vnfr, nsd=self.nsd)
446 yield from vnfr.create()
447 self._vnfrs.append(vnfr)
448
449 @asyncio.coroutine
450 def delete(self):
451 if not self._created:
452 self._log.debug("NSD wasn't created. Skipping delete.")
453 return
454
455 self._log.debug("Deleting openmano nsr")
456
457 yield from self._loop.run_in_executor(
458 None,
459 self._cli_api.ns_delete,
460 self._nsd_uuid,
461 )
462
463 self._log.debug("Deleting openmano vnfrs")
464 for vnfr in self._vnfrs:
465 yield from vnfr.delete()
466
467
468 @asyncio.coroutine
469 def create(self):
470 self._log.debug("Creating openmano scenario")
471 name_uuid_map = yield from self._loop.run_in_executor(
472 None,
473 self._cli_api.ns_list,
474 )
475
476 if self._nsd_msg.name in name_uuid_map:
477 self._log.debug("Found existing openmano scenario")
478 self._nsd_uuid = name_uuid_map[self._nsd_msg.name]
479 return
480
481
482 # Use the nsd uuid as the scenario name to rebind to existing
483 # scenario on reload or to support muliple instances of the name
484 # nsd
485 self._nsd_uuid, _ = yield from self._loop.run_in_executor(
486 None,
487 self._cli_api.ns_create,
488 self.openmano_nsd_yaml,
489 self._nsd_msg.name
490 )
491 fpath = dump_openmano_descriptor(
492 "{}_nsd".format(self._nsd_msg.name),
493 self.openmano_nsd_yaml,
494 )
495
496 self._log.debug("Dumped Openmano NS descriptor to: %s", fpath)
497
498 self._created = True
499
500 @asyncio.coroutine
501 def instance_monitor_task(self):
502 self._log.debug("Starting Instance monitoring task")
503
504 start_time = time.time()
505 active_vnfs = []
506
507 while True:
508 yield from asyncio.sleep(1, loop=self._loop)
509
510 try:
511 instance_resp_json = yield from self._loop.run_in_executor(
512 None,
513 self._http_api.get_instance,
514 self._nsr_uuid,
515 )
516
517 self._log.debug("Got instance response: %s for NSR ID %s",
518 instance_resp_json,
519 self._nsr_uuid)
520
521 except openmano_client.InstanceStatusError as e:
522 self._log.error("Could not get NS instance status: %s", str(e))
523 continue
524
525 def all_vms_active(vnf):
526 for vm in vnf["vms"]:
527 vm_status = vm["status"]
528 vm_uuid = vm["uuid"]
529 if vm_status != "ACTIVE":
530 self._log.debug("VM is not yet active: %s (status: %s)", vm_uuid, vm_status)
531 return False
532
533 return True
534
535 def any_vm_active_nomgmtip(vnf):
536 for vm in vnf["vms"]:
537 vm_status = vm["status"]
538 vm_uuid = vm["uuid"]
539 if vm_status != "ACTIVE":
540 self._log.debug("VM is not yet active: %s (status: %s)", vm_uuid, vm_status)
541 return False
542
543 return True
544
545 def any_vms_error(vnf):
546 for vm in vnf["vms"]:
547 vm_status = vm["status"]
548 vm_vim_info = vm["vim_info"]
549 vm_uuid = vm["uuid"]
550 if vm_status == "ERROR":
551 self._log.error("VM Error: %s (vim_info: %s)", vm_uuid, vm_vim_info)
552 return True
553
554 return False
555
556 def get_vnf_ip_address(vnf):
557 if "ip_address" in vnf:
558 return vnf["ip_address"].strip()
559 return None
560
561 def get_vnf_mac_address(vnf):
562 if "mac_address" in vnf:
563 return vnf["mac_address"].strip()
564 return None
565
566 def get_ext_cp_info(vnf):
567 cp_info_list = []
568 for vm in vnf["vms"]:
569 if "interfaces" not in vm:
570 continue
571
572 for intf in vm["interfaces"]:
573 if "external_name" not in intf:
574 continue
575
576 if not intf["external_name"]:
577 continue
578
579 ip_address = intf["ip_address"]
580 if ip_address is None:
581 ip_address = "0.0.0.0"
582
583 mac_address = intf["mac_address"]
584 if mac_address is None:
585 mac_address="00:00:00:00:00:00"
586
587 cp_info_list.append((intf["external_name"], ip_address, mac_address))
588
589 return cp_info_list
590
591 def get_vnf_status(vnfr):
592 # When we create an openmano descriptor we use <name>__<idx>
593 # to come up with openmano constituent VNF name. Use this
594 # knowledge to map the vnfr back.
595 openmano_vnfr_suffix = "__{}".format(
596 vnfr.vnfr.vnfr_msg.member_vnf_index_ref
597 )
598
599 for vnf in instance_resp_json["vnfs"]:
600 if vnf["vnf_name"].endswith(openmano_vnfr_suffix):
601 return vnf
602
603 self._log.warning("Could not find vnf status with name that ends with: %s",
604 openmano_vnfr_suffix)
605 return None
606
607 for vnfr in self._vnfrs:
608 if vnfr in active_vnfs:
609 # Skipping, so we don't re-publish the same VNF message.
610 continue
611
612 vnfr_msg = vnfr.vnfr.vnfr_msg.deep_copy()
613 vnfr_msg.operational_status = "init"
614
615 try:
616 vnf_status = get_vnf_status(vnfr)
617 self._log.debug("Found VNF status: %s", vnf_status)
618 if vnf_status is None:
619 self._log.error("Could not find VNF status from openmano")
620 self._state = OpenmanoNSRecordState.FAILED
621 vnfr_msg.operational_status = "failed"
622 yield from self._publisher.publish_vnfr(None, vnfr_msg)
623 return
624
625 # If there was a VNF that has a errored VM, then just fail the VNF and stop monitoring.
626 if any_vms_error(vnf_status):
627 self._log.debug("VM was found to be in error state. Marking as failed.")
628 self._state = OpenmanoNSRecordState.FAILED
629 vnfr_msg.operational_status = "failed"
630 yield from self._publisher.publish_vnfr(None, vnfr_msg)
631 return
632
633 if (time.time() - start_time) > OpenmanoNsr.TIMEOUT_SECS:
634 self._log.error("NSR timed out before reaching running state")
635 self._state = OpenmanoNSRecordState.FAILED
636 vnfr_msg.operational_status = "failed"
637 yield from self._publisher.publish_vnfr(None, vnfr_msg)
638 return
639
640 if all_vms_active(vnf_status):
641 vnf_ip_address = get_vnf_ip_address(vnf_status)
642 vnf_mac_address = get_vnf_mac_address(vnf_status)
643
644 if vnf_ip_address is None:
645 self._log.warning("No IP address obtained "
646 "for VNF: {}, will retry.".format(
647 vnf_status['vnf_name']))
648 continue
649
650 self._log.debug("All VMs in VNF are active. Marking as running.")
651 vnfr_msg.operational_status = "running"
652
653 self._log.debug("Got VNF ip address: %s, mac-address: %s", vnf_ip_address, vnf_mac_address)
654 vnfr_msg.mgmt_interface.ip_address = vnf_ip_address
655 vnfr_msg.vnf_configuration.config_access.mgmt_ip_address = vnf_ip_address
656
657
658 for vm in vnf_status["vms"]:
659 if vm["uuid"] not in self._vdur_console_handler:
660 vdur_console_handler = VnfrConsoleOperdataDtsHandler(self._dts, self._log, self._loop,
661 self, vnfr_msg.id,vm["uuid"],vm["name"])
662 yield from vdur_console_handler.register()
663 self._vdur_console_handler[vm["uuid"]] = vdur_console_handler
664
665 vdur_msg = vnfr_msg.vdur.add()
666 vdur_msg.vim_id = vm["vim_vm_id"]
667 vdur_msg.id = vm["uuid"]
668
669 # Add connection point information for the config manager
670 cp_info_list = get_ext_cp_info(vnf_status)
671 for (cp_name, cp_ip, cp_mac_addr) in cp_info_list:
672 cp = vnfr_msg.connection_point.add()
673 cp.name = cp_name
674 cp.short_name = cp_name
675 cp.ip_address = cp_ip
676 cp.mac_address = cp_mac_addr
677
678 yield from self._publisher.publish_vnfr(None, vnfr_msg)
679 active_vnfs.append(vnfr)
680
681
682 except Exception as e:
683 vnfr_msg.operational_status = "failed"
684 self._state = OpenmanoNSRecordState.FAILED
685 yield from self._publisher.publish_vnfr(None, vnfr_msg)
686 self._log.exception("Caught exception publishing vnfr info: %s", str(e))
687 return
688
689 if len(active_vnfs) == len(self._vnfrs):
690 self._state = OpenmanoNSRecordState.RUNNING
691 self._log.info("All VNF's are active. Exiting NSR monitoring task")
692 return
693
694 @asyncio.coroutine
695 def deploy(self,nsr_msg):
696 if self._nsd_uuid is None:
697 raise ValueError("Cannot deploy an uncreated nsd")
698
699 self._log.debug("Deploying openmano scenario")
700
701 name_uuid_map = yield from self._loop.run_in_executor(
702 None,
703 self._cli_api.ns_instance_list,
704 )
705
706 if self._nsr_config_msg.name in name_uuid_map:
707 self._log.debug("Found existing instance with nsr name: %s", self._nsr_config_msg.name)
708 self._nsr_uuid = name_uuid_map[self._nsr_config_msg.name]
709 else:
710 self._nsr_msg = nsr_msg
711 fpath = dump_openmano_descriptor(
712 "{}_instance_sce_create".format(self._nsr_config_msg.name),
713 self.openmano_instance_create_yaml,
714 )
715 self._log.debug("Dumped Openmano NS Scenario Cretae to: %s", fpath)
716
717 self._nsr_uuid = yield from self._loop.run_in_executor(
718 None,
719 self._cli_api.ns_instance_scenario_create,
720 self.openmano_instance_create_yaml)
721
722 self._state = OpenmanoNSRecordState.INSTANTIATION_PENDING
723
724 self._monitor_task = asyncio.ensure_future(
725 self.instance_monitor_task(), loop=self._loop
726 )
727
728 @asyncio.coroutine
729 def terminate(self):
730
731 for _,handler in self._vdur_console_handler.items():
732 handler._regh.deregister()
733
734 if self._nsr_uuid is None:
735 self._log.warning("Cannot terminate an un-instantiated nsr")
736 return
737
738 if self._monitor_task is not None:
739 self._monitor_task.cancel()
740 self._monitor_task = None
741
742 self._log.debug("Terminating openmano nsr")
743 yield from self._loop.run_in_executor(
744 None,
745 self._cli_api.ns_terminate,
746 self._nsr_uuid,
747 )
748
749 @asyncio.coroutine
750 def create_vlr(self,vlr):
751 self._log.debug("Creating openmano vim network VLR name %s, VLR DC: %s",vlr.vld_msg.name,
752 vlr.om_datacenter_name)
753 net_create = {}
754 net = {}
755 net['name'] = vlr.name
756 net['shared'] = True
757 net['type'] = 'bridge'
758 self._log.debug("Received ip profile is %s",vlr._ip_profile)
759 if vlr._ip_profile and vlr._ip_profile.has_field("ip_profile_params"):
760 ip_profile_params = vlr._ip_profile.ip_profile_params
761 ip_profile = {}
762 if ip_profile_params.ip_version == "ipv6":
763 ip_profile['ip_version'] = "IPv6"
764 else:
765 ip_profile['ip_version'] = "IPv4"
766 if ip_profile_params.has_field('subnet_address'):
767 ip_profile['subnet_address'] = ip_profile_params.subnet_address
768 if ip_profile_params.has_field('gateway_address'):
769 ip_profile['gateway_address'] = ip_profile_params.gateway_address
770 if ip_profile_params.has_field('dns_server') and len(ip_profile_params.dns_server) > 0:
771 ip_profile['dns_address'] = ip_profile_params.dns_server[0].address
772 if ip_profile_params.has_field('dhcp_params'):
773 ip_profile['dhcp_enabled'] = ip_profile_params.dhcp_params.enabled
774 ip_profile['dhcp_start_address'] = ip_profile_params.dhcp_params.start_address
775 ip_profile['dhcp_count'] = ip_profile_params.dhcp_params.count
776 net['ip_profile'] = ip_profile
777 net_create["network"]= net
778
779 net_create_msg = yaml.safe_dump(net_create,default_flow_style=False)
780 fpath = dump_openmano_descriptor(
781 "{}_vim_net_create_{}".format(self._nsr_config_msg.name,vlr.name),
782 net_create_msg)
783 self._log.debug("Dumped Openmano VIM Net create to: %s", fpath)
784
785 vim_network_uuid = yield from self._loop.run_in_executor(
786 None,
787 self._cli_api.ns_vim_network_create,
788 net_create_msg,
789 vlr.om_datacenter_name)
790 self._vlrs.append(vlr)
791
792
793
794 class OpenmanoNsPlugin(rwnsmplugin.NsmPluginBase):
795 """
796 RW Implentation of the NsmPluginBase
797 """
798 def __init__(self, dts, log, loop, publisher, ro_account):
799 self._dts = dts
800 self._log = log
801 self._loop = loop
802 self._publisher = publisher
803
804 self._cli_api = None
805 self._http_api = None
806 self._openmano_nsrs = {}
807 self._vnfr_uptime_tasks = {}
808
809 self._set_ro_account(ro_account)
810
811 def _set_ro_account(self, ro_account):
812 self._log.debug("Setting openmano plugin cloud account: %s", ro_account)
813 self._cli_api = openmano_client.OpenmanoCliAPI(
814 self.log,
815 ro_account.openmano.host,
816 ro_account.openmano.port,
817 ro_account.openmano.tenant_id,
818 )
819
820 self._http_api = openmano_client.OpenmanoHttpAPI(
821 self.log,
822 ro_account.openmano.host,
823 ro_account.openmano.port,
824 ro_account.openmano.tenant_id,
825 )
826
827 def set_state(self, nsr_id, state):
828 # Currently we update only during terminate to
829 # decide how to handle VL terminate
830 if state.value == OpenmanoNSRecordState.TERMINATE.value:
831 self._openmano_nsrs[nsr_id]._state = \
832 [member.value for name, member in \
833 OpenmanoNSRecordState.__members__.items() \
834 if member.value == state.value]
835
836 def create_nsr(self, nsr_config_msg, nsd_msg, key_pairs=None):
837 """
838 Create Network service record
839 """
840 openmano_nsr = OpenmanoNsr(
841 self._dts,
842 self._log,
843 self._loop,
844 self._publisher,
845 self._cli_api,
846 self._http_api,
847 nsd_msg,
848 nsr_config_msg,
849 key_pairs
850 )
851 self._openmano_nsrs[nsr_config_msg.id] = openmano_nsr
852
853 @asyncio.coroutine
854 def deploy(self, nsr_msg):
855 self._log.debug("Received NSR Deploy msg : %s", nsr_msg)
856 openmano_nsr = self._openmano_nsrs[nsr_msg.ns_instance_config_ref]
857 yield from openmano_nsr.create()
858 yield from openmano_nsr.deploy(nsr_msg)
859
860 @asyncio.coroutine
861 def instantiate_ns(self, nsr, xact):
862 """
863 Instantiate NSR with the passed nsr id
864 """
865 yield from nsr.instantiate(xact)
866
867 @asyncio.coroutine
868 def instantiate_vnf(self, nsr, vnfr):
869 """
870 Instantiate NSR with the passed nsr id
871 """
872 openmano_nsr = self._openmano_nsrs[nsr.id]
873 yield from openmano_nsr.add_vnfr(vnfr)
874
875 # Mark the VNFR as running
876 # TODO: Create a task to monitor nsr/vnfr status
877 vnfr_msg = vnfr.vnfr_msg.deep_copy()
878 vnfr_msg.operational_status = "init"
879
880 self._log.debug("Attempting to publish openmano vnf: %s", vnfr_msg)
881 with self._dts.transaction() as xact:
882 yield from self._publisher.publish_vnfr(xact, vnfr_msg)
883 self._log.debug("Creating a task to update uptime for vnfr: %s", vnfr.id)
884 self._vnfr_uptime_tasks[vnfr.id] = self._loop.create_task(self.vnfr_uptime_update(vnfr))
885
886 def vnfr_uptime_update(self, vnfr):
887 try:
888 vnfr_ = RwVnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr.from_dict({'id': vnfr.id})
889 while True:
890 vnfr_.uptime = int(time.time()) - vnfr._create_time
891 yield from self._publisher.publish_vnfr(None, vnfr_)
892 yield from asyncio.sleep(2, loop=self._loop)
893 except asyncio.CancelledError:
894 self._log.debug("Received cancellation request for vnfr_uptime_update task")
895
896 @asyncio.coroutine
897 def instantiate_vl(self, nsr, vlr):
898 """
899 Instantiate NSR with the passed nsr id
900 """
901 self._log.debug("Received instantiate VL for NSR {}; VLR {}".format(nsr.id,vlr))
902 openmano_nsr = self._openmano_nsrs[nsr.id]
903 if openmano_nsr._state == OpenmanoNSRecordState.RUNNING:
904 yield from openmano_nsr.create_vlr(vlr)
905 yield from self._publisher.publish_vlr(None, vlr.vlr_msg)
906 else:
907 yield from openmano_nsr.add_vlr(vlr)
908
909 @asyncio.coroutine
910 def terminate_ns(self, nsr):
911 """
912 Terminate the network service
913 """
914 nsr_id = nsr.id
915 openmano_nsr = self._openmano_nsrs[nsr_id]
916
917 yield from openmano_nsr.terminate()
918 yield from openmano_nsr.delete()
919
920 with self._dts.transaction() as xact:
921 for vnfr in openmano_nsr.vnfrs:
922 self._log.debug("Unpublishing VNFR: %s", vnfr.vnfr.vnfr_msg)
923 yield from self._publisher.unpublish_vnfr(xact, vnfr.vnfr.vnfr_msg)
924
925 del self._openmano_nsrs[nsr_id]
926
927 @asyncio.coroutine
928 def terminate_vnf(self, vnfr):
929 """
930 Terminate the network service
931 """
932 if vnfr.id in self._vnfr_uptime_tasks:
933 self._vnfr_uptime_tasks[vnfr.id].cancel()
934
935 @asyncio.coroutine
936 def terminate_vl(self, vlr):
937 """
938 Terminate the virtual link
939 """
940 self._log.debug("Received terminate VL for VLR {}".format(vlr))
941 openmano_nsr = self._openmano_nsrs[vlr._nsr_id]
942 if openmano_nsr._state == OpenmanoNSRecordState.RUNNING:
943 yield from openmano_nsr.delete_vlr(vlr)
944 else:
945 yield from openmano_nsr.remove_vlr(vlr)