3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
25 gi
.require_version('RwDts', '1.0')
26 gi
.require_version('RwVnfrYang', '1.0')
27 from gi
.repository
import (
32 import rift
.openmano
.rift2openmano
as rift2openmano
33 import rift
.openmano
.openmano_client
as openmano_client
34 from . import rwnsmplugin
40 if sys
.version_info
< (3, 4, 4):
41 asyncio
.ensure_future
= asyncio
.async
44 DUMP_OPENMANO_DIR
= os
.path
.join(
45 os
.environ
["RIFT_ARTIFACTS"],
46 "openmano_descriptors"
50 def dump_openmano_descriptor(name
, descriptor_str
):
51 filename
= "{}_{}.yaml".format(
52 time
.strftime("%Y%m%d-%H%M%S"),
56 filepath
= os
.path
.join(
62 if not os
.path
.exists(DUMP_OPENMANO_DIR
):
63 os
.makedirs(DUMP_OPENMANO_DIR
)
65 with
open(filepath
, 'w') as hdl
:
66 hdl
.write(descriptor_str
)
69 print("Failed to dump openmano descriptor: %s" % str(e
))
73 class VnfrConsoleOperdataDtsHandler(object):
74 """ registers 'D,/vnfr:vnfr-console/vnfr:vnfr[id]/vdur[id]' and handles CRUD from DTS"""
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
))
80 def __init__(self
, dts
, log
, loop
, nsr
, vnfr_id
, vdur_id
, vdu_id
):
87 self
._vnfr
_id
= vnfr_id
88 self
._vdur
_id
= vdur_id
93 """ Register for VNFR VDU Operational Data read from dts """
96 def on_prepare(xact_info
, action
, ks_path
, msg
):
97 """ prepare callback from dts """
98 xpath
= ks_path
.to_xpath(RwVnfrYang
.get_schema())
100 "Got VNFR VDU Opdata xact_info: %s, action: %s): %s:%s",
101 xact_info
, action
, xpath
, msg
104 if action
== rwdts
.QueryAction
.READ
:
105 schema
= RwVnfrYang
.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur
.schema()
106 path_entry
= schema
.keyspec_to_entry(ks_path
)
109 console_url
= yield from self
._loop
.run_in_executor(
111 self
._nsr
._http
_api
.get_instance_vm_console_url
,
116 self
._log
.debug("Got console response: %s for NSR ID %s vdur ID %s",
121 vdur_console
= RwVnfrYang
.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur()
122 vdur_console
.id = self
._vdur
_id
124 vdur_console
.console_url
= console_url
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",
131 vdur_console
= RwVnfrYang
.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur()
132 vdur_console
.id = self
._vdur
_id
133 vdur_console
.console_url
= 'none'
135 xact_info
.respond_xpath(rsp_code
=rwdts
.XactRspCode
.ACK
,
136 xpath
=self
.vnfr_vdu_console_xpath
,
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
)
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
,
150 flags
=rwdts
.Flag
.PUBLISHER
,
155 class OpenmanoVnfr(object):
156 def __init__(self
, log
, loop
, cli_api
, vnfr
, nsd
):
159 self
._cli
_api
= cli_api
161 self
._vnfd
_id
= vnfr
.vnfd
.id
165 self
._created
= False
171 return rift2openmano
.RiftVNFD(self
._vnfr
.vnfd
)
178 def rift_vnfd_id(self
):
182 def openmano_vnfd_id(self
):
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
)
192 def openmano_vnfd_yaml(self
):
193 return yaml
.safe_dump(self
.openmano_vnfd
, default_flow_style
=False)
197 self
._log
.debug("Creating openmano vnfd")
198 openmano_vnfd
= self
.openmano_vnfd
199 name
= openmano_vnfd
["vnf"]["name"]
201 # If the name already exists, get the openmano vnfd id
202 name_uuid_map
= yield from self
._loop
.run_in_executor(
204 self
._cli
_api
.vnf_list
,
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
213 self
._vnf
_id
, _
= yield from self
._loop
.run_in_executor(
215 self
._cli
_api
.vnf_create
,
216 self
.openmano_vnfd_yaml
,
219 fpath
= dump_openmano_descriptor(
220 "{}_vnf".format(name
),
221 self
.openmano_vnfd_yaml
224 self
._log
.debug("Dumped Openmano VNF descriptor to: %s", fpath
)
229 if not self
._created
:
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.")
237 self
._cli
_api
.vnf_delete(self
._vnf
_id
)
240 class OpenmanoNSRecordState(Enum
):
241 """ Network Service Record State """
242 # Make sure the values match with NetworkServiceRecordState
244 INSTANTIATION_PENDING
= 102
256 class OpenmanoNsr(object):
258 INSTANCE_TERMINATE_TIMEOUT
= 60
260 def __init__(self
, dts
, log
, loop
, publisher
, cli_api
, http_api
, nsd_msg
, nsr_config_msg
,key_pairs
,rift_vnfd_id
=None ):
264 self
._publisher
= publisher
265 self
._cli
_api
= cli_api
266 self
._http
_api
= http_api
268 self
._nsd
_msg
= nsd_msg
269 self
._nsr
_config
_msg
= nsr_config_msg
273 self
._vdur
_console
_handler
= {}
274 self
._key
_pairs
= key_pairs
276 self
._nsd
_uuid
= None
277 self
._nsr
_uuid
= None
278 self
._nsd
_msg
= nsd_msg
282 self
._created
= False
284 self
._monitor
_task
= None
285 self
._rift
_vnfd
_id
= rift_vnfd_id
286 self
._state
= OpenmanoNSRecordState
.INIT
290 return rift2openmano
.RiftNSD(self
._nsd
_msg
)
293 def rift_vnfd_id(self
):
294 return self
._rift
_vnfd
_id
301 def nsr_config_msg(self
):
302 return self
._nsr
_config
_msg
307 return {v
.rift_vnfd_id
: v
.vnfd
for v
in self
._vnfrs
}
311 return {v
.rift_vnfd_id
: v
.openmano_vnfd_id
for v
in self
._vnfrs
}
319 return self
._key
_pairs
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)
336 def openmano_scaling_yaml(self
):
337 self
._log
.debug("Creating Openmano Scaling Descriptor %s")
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
))
344 def get_ssh_key_pairs(self
):
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
)
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
)
357 cloud_config
["key-pairs"] = key_pairs
360 for user_entry
in self
._nsr
_config
_msg
.user
:
361 self
._log
.debug("User present is %s",user_entry
)
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
)
370 for user_entry
in self
._nsd
_msg
.user
:
371 self
._log
.debug("User present in NSD is %s",user_entry
)
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
)
380 cloud_config
["users"] = users
382 self
._log
.debug("Cloud config formed is %s",cloud_config
)
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
394 cloud_config
= self
.get_ssh_key_pairs()
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
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"
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
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"]
443 openmano_instance_create
["networks"][vld_msg
.name
]["sites"].append(network
)
445 openmano_instance_create
["networks"][vld_msg
.name
]['ip-profile'] = ip_profile
447 return yaml
.safe_dump(openmano_instance_create
, default_flow_style
=False,width
=1000)
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
456 scaling_instance_create
["scenario"] = self
._nsd
_uuid
+ "__" +group_list
.name
458 scaling_instance_create
["scenario"] = self
._nsd
_uuid
459 scaling_instance_create
["description"] = self
._nsr
_config
_msg
.description
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
480 if vld_msg
.vim_network_name
:
481 network
["netmap-use"] = vld_msg
.vim_network_name
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"]
491 scaling_instance_create
["networks"][vld_msg
.name
]["sites"].append(network
)
493 return yaml
.safe_dump(scaling_instance_create
, default_flow_style
=False, width
=1000)
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
)
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
)
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(
515 self
._cli
_api
.ns_vim_network_delete
,
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
)
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
)
528 def add_nsr(self
, nsr
, vnfr
):
529 self
._nsrs
[vnfr
.id] = nsr
533 if not self
._created
:
534 self
._log
.debug("NSD wasn't created. Skipping delete.")
537 self
._log
.debug("Deleting openmano nsr")
538 self
._cli
_api
.ns_delete(self
._nsd
_uuid
)
540 self
._log
.debug("Deleting openmano vnfrs")
541 deleted_vnf_id_list
= []
542 for vnfr
in self
._vnfrs
:
543 if vnfr
.vnfr
.vnfd
.id not in deleted_vnf_id_list
:
545 deleted_vnf_id_list
.append(vnfr
.vnfr
.vnfd
.id)
550 self
._log
.debug("Creating openmano scenario")
551 name_uuid_map
= yield from self
._loop
.run_in_executor(
553 self
._cli
_api
.ns_list
,
556 if self
._nsd
_msg
.name
in name_uuid_map
:
557 self
._log
.debug("Found existing openmano scenario")
558 self
._nsd
_uuid
= name_uuid_map
[self
._nsd
_msg
.name
]
562 # Use the nsd uuid as the scenario name to rebind to existing
563 # scenario on reload or to support muliple instances of the name
565 self
._nsd
_uuid
, _
= yield from self
._loop
.run_in_executor(
567 self
._cli
_api
.ns_create
,
568 self
.openmano_nsd_yaml
,
571 fpath
= dump_openmano_descriptor(
572 "{}_nsd".format(self
._nsd
_msg
.name
),
573 self
.openmano_nsd_yaml
,
576 self
._log
.debug("Dumped Openmano NS descriptor to: %s", fpath
)
581 def scaling_scenario_create(self
):
582 self
._log
.debug("Creating scaling openmano scenario")
583 self
._nsd
_uuid
, _
= yield from self
._loop
.run_in_executor(
585 self
._cli
_api
.ns_create
,
586 self
.openmano_scaling_yaml
,
589 fpath
= dump_openmano_descriptor(
590 "{}_sgd".format(self
._nsd
_msg
.name
),
591 self
.scaling_instance_create_yaml
,
595 def instance_monitor_task(self
):
596 self
._log
.debug("Starting Instance monitoring task")
598 start_time
= time
.time()
602 yield from asyncio
.sleep(1, loop
=self
._loop
)
605 instance_resp_json
= yield from self
._loop
.run_in_executor(
607 self
._http
_api
.get_instance
,
611 self
._log
.debug("Got instance response: %s for NSR ID %s",
615 except openmano_client
.InstanceStatusError
as e
:
616 self
._log
.error("Could not get NS instance status: %s", str(e
))
619 def all_vms_active(vnf
):
620 for vm
in vnf
["vms"]:
621 vm_status
= vm
["status"]
623 if vm_status
!= "ACTIVE":
624 self
._log
.debug("VM is not yet active: %s (status: %s)", vm_uuid
, vm_status
)
629 def any_vm_active_nomgmtip(vnf
):
630 for vm
in vnf
["vms"]:
631 vm_status
= vm
["status"]
633 if vm_status
!= "ACTIVE":
634 self
._log
.debug("VM is not yet active: %s (status: %s)", vm_uuid
, vm_status
)
639 def any_vms_error(vnf
):
640 for vm
in vnf
["vms"]:
641 vm_status
= vm
["status"]
642 vm_vim_info
= vm
["vim_info"]
644 if vm_status
== "ERROR":
645 self
._log
.error("VM Error: %s (vim_info: %s)", vm_uuid
, vm_vim_info
)
650 def get_vnf_ip_address(vnf
):
651 if "ip_address" in vnf
:
652 return vnf
["ip_address"].strip()
655 def get_vnf_mac_address(vnf
):
656 if "mac_address" in vnf
:
657 return vnf
["mac_address"].strip()
660 def get_ext_cp_info(vnf
):
662 for vm
in vnf
["vms"]:
663 if "interfaces" not in vm
:
666 for intf
in vm
["interfaces"]:
667 if "external_name" not in intf
:
670 if not intf
["external_name"]:
673 ip_address
= intf
["ip_address"]
674 if ip_address
is None:
675 ip_address
= "0.0.0.0"
677 mac_address
= intf
["mac_address"]
678 if mac_address
is None:
679 mac_address
="00:00:00:00:00:00"
681 cp_info_list
.append((intf
["external_name"], ip_address
, mac_address
))
685 def get_vnf_status(vnfr
):
686 # When we create an openmano descriptor we use <name>__<idx>
687 # to come up with openmano constituent VNF name. Use this
688 # knowledge to map the vnfr back.
689 openmano_vnfr_suffix
= "__{}".format(
690 vnfr
.vnfr
.vnfr_msg
.member_vnf_index_ref
693 for vnf
in instance_resp_json
["vnfs"]:
694 if vnf
["vnf_name"].endswith(openmano_vnfr_suffix
):
697 self
._log
.warning("Could not find vnf status with name that ends with: %s",
698 openmano_vnfr_suffix
)
701 for vnfr
in self
._vnfrs
:
702 if vnfr
in active_vnfs
:
703 # Skipping, so we don't re-publish the same VNF message.
706 vnfr_msg
= vnfr
.vnfr
.vnfr_msg
.deep_copy()
707 vnfr_msg
.operational_status
= "init"
710 vnf_status
= get_vnf_status(vnfr
)
711 self
._log
.debug("Found VNF status: %s", vnf_status
)
712 if vnf_status
is None:
713 self
._log
.error("Could not find VNF status from openmano")
714 self
._state
= OpenmanoNSRecordState
.FAILED
715 vnfr_msg
.operational_status
= "failed"
716 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
719 # If there was a VNF that has a errored VM, then just fail the VNF and stop monitoring.
720 if any_vms_error(vnf_status
):
721 self
._log
.debug("VM was found to be in error state. Marking as failed.")
722 self
._state
= OpenmanoNSRecordState
.FAILED
723 vnfr_msg
.operational_status
= "failed"
724 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
727 if (time
.time() - start_time
) > OpenmanoNsr
.TIMEOUT_SECS
:
728 self
._log
.error("NSR timed out before reaching running state")
729 self
._state
= OpenmanoNSRecordState
.FAILED
730 vnfr_msg
.operational_status
= "failed"
731 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
734 if all_vms_active(vnf_status
):
735 vnf_ip_address
= get_vnf_ip_address(vnf_status
)
736 vnf_mac_address
= get_vnf_mac_address(vnf_status
)
738 if vnf_ip_address
is None:
739 self
._log
.warning("No IP address obtained "
740 "for VNF: {}, will retry.".format(
741 vnf_status
['vnf_name']))
744 self
._log
.debug("All VMs in VNF are active. Marking as running.")
745 vnfr_msg
.operational_status
= "running"
747 self
._log
.debug("Got VNF ip address: %s, mac-address: %s", vnf_ip_address
, vnf_mac_address
)
748 vnfr_msg
.mgmt_interface
.ip_address
= vnf_ip_address
749 vnfr_msg
.vnf_configuration
.config_access
.mgmt_ip_address
= vnf_ip_address
752 for vm
in vnf_status
["vms"]:
753 if vm
["uuid"] not in self
._vdur
_console
_handler
:
754 vdur_console_handler
= VnfrConsoleOperdataDtsHandler(self
._dts
, self
._log
, self
._loop
,
755 self
, vnfr_msg
.id,vm
["uuid"],vm
["name"])
756 yield from vdur_console_handler
.register()
757 self
._vdur
_console
_handler
[vm
["uuid"]] = vdur_console_handler
759 vdur_msg
= vnfr_msg
.vdur
.add()
760 vdur_msg
.vim_id
= vm
["vim_vm_id"]
761 vdur_msg
.id = vm
["uuid"]
763 # Add connection point information for the config manager
764 cp_info_list
= get_ext_cp_info(vnf_status
)
765 for (cp_name
, cp_ip
, cp_mac_addr
) in cp_info_list
:
766 cp
= vnfr_msg
.connection_point
.add()
768 cp
.short_name
= cp_name
769 cp
.ip_address
= cp_ip
770 cp
.mac_address
= cp_mac_addr
772 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
773 active_vnfs
.append(vnfr
)
775 except Exception as e
:
776 vnfr_msg
.operational_status
= "failed"
777 self
._state
= OpenmanoNSRecordState
.FAILED
778 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
779 self
._log
.exception("Caught exception publishing vnfr info: %s", str(e
))
782 if len(active_vnfs
) == len(self
._vnfrs
):
783 self
._state
= OpenmanoNSRecordState
.RUNNING
784 self
._log
.info("All VNF's are active. Exiting NSR monitoring task")
788 def deploy(self
,nsr_msg
):
789 if self
._nsd
_uuid
is None:
790 raise ValueError("Cannot deploy an uncreated nsd")
792 self
._log
.debug("Deploying openmano instance scenario")
794 name_uuid_map
= yield from self
._loop
.run_in_executor(
796 self
._cli
_api
.ns_instance_list
,
799 if self
._nsr
_config
_msg
.name
in name_uuid_map
:
800 self
._log
.debug("Found existing instance with nsr name: %s", self
._nsr
_config
_msg
.name
)
801 self
._nsr
_uuid
= name_uuid_map
[self
._nsr
_config
_msg
.name
]
803 self
._nsr
_msg
= nsr_msg
804 fpath
= dump_openmano_descriptor(
805 "{}_instance_sce_create".format(self
._nsr
_config
_msg
.name
),
806 self
.openmano_instance_create_yaml
,
808 self
._log
.debug("Dumped Openmano instance Scenario Cretae to: %s", fpath
)
810 self
._nsr
_uuid
= yield from self
._loop
.run_in_executor(
812 self
._cli
_api
.ns_instance_scenario_create
,
813 self
.openmano_instance_create_yaml
)
815 self
._state
= OpenmanoNSRecordState
.INSTANTIATION_PENDING
817 self
._monitor
_task
= asyncio
.ensure_future(
818 self
.instance_monitor_task(), loop
=self
._loop
822 def deploy_scaling(self
, nsr_msg
, rift_vnfd_id
):
823 self
._log
.debug("Deploying Scaling instance scenario")
824 self
._nsr
_msg
= nsr_msg
825 self
._rift
_vnfd
_id
= rift_vnfd_id
826 fpath
= dump_openmano_descriptor(
827 "{}_scale_instance".format(self
._nsr
_config
_msg
.name
),
828 self
.scaling_instance_create_yaml
830 self
._nsr
_uuid
= yield from self
._loop
.run_in_executor(
832 self
._cli
_api
.ns_instance_scenario_create
,
833 self
.scaling_instance_create_yaml
)
835 self
._state
= OpenmanoNSRecordState
.INSTANTIATION_PENDING
837 self
._monitor
_task
= asyncio
.ensure_future(
838 self
.instance_monitor_task(), loop
=self
._loop
841 self
._state
= OpenmanoNSRecordState
.INIT
845 if self
._nsr
_uuid
is None:
846 start_time
= time
.time()
847 while ((time
.time() - start_time
) < OpenmanoNsr
.INSTANCE_TERMINATE_TIMEOUT
) and (self
._nsr
_uuid
is None):
849 self
._log
.warning("Waiting for nsr to get instatiated")
850 if self
._nsr
_uuid
is None:
851 self
._log
.warning("Cannot terminate an un-instantiated nsr")
854 if self
._monitor
_task
is not None:
855 self
._monitor
_task
.cancel()
856 self
._monitor
_task
= None
858 self
._log
.debug("Terminating openmano nsr")
859 self
._cli
_api
.ns_terminate(self
._nsr
_uuid
)
862 def create_vlr(self
,vlr
):
863 self
._log
.debug("Creating openmano vim network VLR name %s, VLR DC: %s",vlr
.vld_msg
.name
,
864 vlr
.om_datacenter_name
)
867 net
['name'] = vlr
.name
869 net
['type'] = 'bridge'
870 self
._log
.debug("Received ip profile is %s",vlr
._ip
_profile
)
871 if vlr
._ip
_profile
and vlr
._ip
_profile
.has_field("ip_profile_params"):
872 ip_profile_params
= vlr
._ip
_profile
.ip_profile_params
874 if ip_profile_params
.ip_version
== "ipv6":
875 ip_profile
['ip_version'] = "IPv6"
877 ip_profile
['ip_version'] = "IPv4"
878 if ip_profile_params
.has_field('subnet_address'):
879 ip_profile
['subnet_address'] = ip_profile_params
.subnet_address
880 if ip_profile_params
.has_field('gateway_address'):
881 ip_profile
['gateway_address'] = ip_profile_params
.gateway_address
882 if ip_profile_params
.has_field('dns_server') and len(ip_profile_params
.dns_server
) > 0:
883 ip_profile
['dns_address'] = ip_profile_params
.dns_server
[0].address
884 if ip_profile_params
.has_field('dhcp_params'):
885 ip_profile
['dhcp_enabled'] = ip_profile_params
.dhcp_params
.enabled
886 ip_profile
['dhcp_start_address'] = ip_profile_params
.dhcp_params
.start_address
887 ip_profile
['dhcp_count'] = ip_profile_params
.dhcp_params
.count
888 net
['ip_profile'] = ip_profile
889 net_create
["network"]= net
891 net_create_msg
= yaml
.safe_dump(net_create
,default_flow_style
=False)
892 fpath
= dump_openmano_descriptor(
893 "{}_vim_net_create_{}".format(self
._nsr
_config
_msg
.name
,vlr
.name
),
895 self
._log
.debug("Dumped Openmano VIM Net create to: %s", fpath
)
897 vim_network_uuid
= yield from self
._loop
.run_in_executor(
899 self
._cli
_api
.ns_vim_network_create
,
901 vlr
.om_datacenter_name
)
902 self
._vlrs
.append(vlr
)
906 class OpenmanoNsPlugin(rwnsmplugin
.NsmPluginBase
):
908 RW Implentation of the NsmPluginBase
910 def __init__(self
, dts
, log
, loop
, publisher
, ro_account
):
914 self
._publisher
= publisher
917 self
._http
_api
= None
918 self
._openmano
_nsrs
= {}
919 self
._vnfr
_uptime
_tasks
= {}
921 self
._set
_ro
_account
(ro_account
)
923 def _set_ro_account(self
, ro_account
):
924 self
._log
.debug("Setting openmano plugin cloud account: %s", ro_account
)
925 self
._cli
_api
= openmano_client
.OpenmanoCliAPI(
927 ro_account
.openmano
.host
,
928 ro_account
.openmano
.port
,
929 ro_account
.openmano
.tenant_id
,
932 self
._http
_api
= openmano_client
.OpenmanoHttpAPI(
934 ro_account
.openmano
.host
,
935 ro_account
.openmano
.port
,
936 ro_account
.openmano
.tenant_id
,
939 def set_state(self
, nsr_id
, state
):
940 # Currently we update only during terminate to
941 # decide how to handle VL terminate
942 if state
.value
== OpenmanoNSRecordState
.TERMINATE
.value
:
943 self
._openmano
_nsrs
[nsr_id
]._state
= \
944 [member
.value
for name
, member
in \
945 OpenmanoNSRecordState
.__members
__.items() \
946 if member
.value
== state
.value
]
948 def create_nsr(self
, nsr_config_msg
, nsd_msg
, key_pairs
=None):
950 Create Network service record
952 openmano_nsr
= OpenmanoNsr(
963 self
._openmano
_nsrs
[nsr_config_msg
.id] = openmano_nsr
966 def deploy(self
, nsr_msg
):
967 self
._log
.debug("Received NSR Deploy msg : %s", nsr_msg
)
968 openmano_nsr
= self
._openmano
_nsrs
[nsr_msg
.ns_instance_config_ref
]
969 yield from openmano_nsr
.create()
970 yield from openmano_nsr
.deploy(nsr_msg
)
973 def instantiate_ns(self
, nsr
, xact
):
975 Instantiate NSR with the passed nsr id
977 yield from nsr
.instantiate(xact
)
980 def instantiate_vnf(self
, nsr
, vnfr
, scaleout
=False):
982 Instantiate NSR with the passed nsr id
984 openmano_nsr
= self
._openmano
_nsrs
[nsr
.id]
986 openmano_vnf_nsr
= OpenmanoNsr(
993 openmano_nsr
.nsd_msg
,
994 openmano_nsr
.nsr_config_msg
,
995 openmano_nsr
.key_pairs
,
996 #openmano_nsr.nsr_msg,
999 for vlr
in openmano_nsr
.vlrs
:
1000 yield from openmano_vnf_nsr
.add_vlr(vlr
)
1002 yield from openmano_nsr
.add_nsr(openmano_vnf_nsr
, vnfr
)
1003 except Exception as e
:
1004 self
.log
.exception(str(e
))
1006 yield from openmano_vnf_nsr
.add_vnfr(vnfr
)
1007 except Exception as e
:
1008 self
.log
.exception(str(e
))
1010 yield from openmano_vnf_nsr
.scaling_scenario_create()
1011 except Exception as e
:
1012 self
.log
.exception(str(e
))
1014 yield from openmano_vnf_nsr
.deploy_scaling(openmano_vnf_nsr
.nsr_msg
, vnfr
.id)
1015 except Exception as e
:
1016 self
.log
.exception(str(e
))
1018 yield from openmano_nsr
.add_vnfr(vnfr
)
1020 # Mark the VNFR as running
1021 # TODO: Create a task to monitor nsr/vnfr status
1022 vnfr_msg
= vnfr
.vnfr_msg
.deep_copy()
1023 vnfr_msg
.operational_status
= "init"
1025 self
._log
.debug("Attempting to publish openmano vnf: %s", vnfr_msg
)
1026 with self
._dts
.transaction() as xact
:
1027 yield from self
._publisher
.publish_vnfr(xact
, vnfr_msg
)
1028 self
._log
.debug("Creating a task to update uptime for vnfr: %s", vnfr
.id)
1029 self
._vnfr
_uptime
_tasks
[vnfr
.id] = self
._loop
.create_task(self
.vnfr_uptime_update(vnfr
))
1031 def vnfr_uptime_update(self
, vnfr
):
1033 vnfr_
= RwVnfrYang
.YangData_Vnfr_VnfrCatalog_Vnfr
.from_dict({'id': vnfr
.id})
1035 vnfr_
.uptime
= int(time
.time()) - vnfr
._create
_time
1036 yield from self
._publisher
.publish_vnfr(None, vnfr_
)
1037 yield from asyncio
.sleep(2, loop
=self
._loop
)
1038 except asyncio
.CancelledError
:
1039 self
._log
.debug("Received cancellation request for vnfr_uptime_update task")
1042 def instantiate_vl(self
, nsr
, vlr
):
1044 Instantiate NSR with the passed nsr id
1046 self
._log
.debug("Received instantiate VL for NSR {}; VLR {}".format(nsr
.id,vlr
))
1047 openmano_nsr
= self
._openmano
_nsrs
[nsr
.id]
1048 if openmano_nsr
._state
== OpenmanoNSRecordState
.RUNNING
:
1049 yield from openmano_nsr
.create_vlr(vlr
)
1050 yield from self
._publisher
.publish_vlr(None, vlr
.vlr_msg
)
1052 yield from openmano_nsr
.add_vlr(vlr
)
1055 def terminate_ns(self
, nsr
):
1057 Terminate the network service
1060 openmano_nsr
= self
._openmano
_nsrs
[nsr_id
]
1062 for _
,handler
in openmano_nsr
._vdur
_console
_handler
.items():
1063 handler
._regh
.deregister()
1065 yield from self
._loop
.run_in_executor(
1071 with self
._dts
.transaction() as xact
:
1072 for vnfr
in openmano_nsr
.vnfrs
:
1073 self
._log
.debug("Unpublishing VNFR: %s", vnfr
.vnfr
.vnfr_msg
)
1074 yield from self
._publisher
.unpublish_vnfr(xact
, vnfr
.vnfr
.vnfr_msg
)
1076 del self
._openmano
_nsrs
[nsr_id
]
1078 def terminate(self
, openmano_nsr
):
1079 openmano_nsr
.terminate()
1080 openmano_nsr
.delete()
1083 def terminate_vnf(self
, vnfr
):
1085 Terminate the network service
1087 if vnfr
.id in self
._vnfr
_uptime
_tasks
:
1088 self
._vnfr
_uptime
_tasks
[vnfr
.id].cancel()
1091 def terminate_vl(self
, vlr
):
1093 Terminate the virtual link
1095 self
._log
.debug("Received terminate VL for VLR {}".format(vlr
))
1096 openmano_nsr
= self
._openmano
_nsrs
[vlr
._nsr
_id
]
1097 if openmano_nsr
._state
== OpenmanoNSRecordState
.RUNNING
:
1098 yield from openmano_nsr
.delete_vlr(vlr
)
1100 yield from openmano_nsr
.remove_vlr(vlr
)