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