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
)
230 if not self
._created
:
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.")
238 yield from self
._loop
.run_in_executor(
240 self
._cli
_api
.vnf_delete
,
245 class OpenmanoNSRecordState(Enum
):
246 """ Network Service Record State """
248 INSTANTIATION_PENDING
= 102
260 class OpenmanoNsr(object):
263 def __init__(self
, dts
, log
, loop
, publisher
, cli_api
, http_api
, nsd_msg
, nsr_config_msg
,key_pairs
):
267 self
._publisher
= publisher
268 self
._cli
_api
= cli_api
269 self
._http
_api
= http_api
271 self
._nsd
_msg
= nsd_msg
272 self
._nsr
_config
_msg
= nsr_config_msg
276 self
._vdur
_console
_handler
= {}
277 self
._key
_pairs
= key_pairs
279 self
._nsd
_uuid
= None
280 self
._nsr
_uuid
= None
284 self
._created
= False
286 self
._monitor
_task
= None
287 self
._state
= OpenmanoNSRecordState
.INIT
291 return rift2openmano
.RiftNSD(self
._nsd
_msg
)
295 return {v
.rift_vnfd_id
: v
.vnfd
for v
in self
._vnfrs
}
299 return {v
.rift_vnfd_id
: v
.openmano_vnfd_id
for v
in self
._vnfrs
}
306 def openmano_nsd_yaml(self
):
307 self
._log
.debug("Converting nsd %s from rift to openmano", self
.nsd
.id)
308 openmano_nsd
= rift2openmano
.rift2openmano_nsd(self
.nsd
, self
.vnfds
,self
.vnfr_ids
)
309 return yaml
.safe_dump(openmano_nsd
, default_flow_style
=False)
311 def get_ssh_key_pairs(self
):
314 for authorized_key
in self
._nsr
_config
_msg
.ssh_authorized_key
:
315 self
._log
.debug("Key pair ref present is %s",authorized_key
.key_pair_ref
)
316 if authorized_key
.key_pair_ref
in self
._key
_pairs
:
317 key_pairs
.append(self
._key
_pairs
[authorized_key
.key_pair_ref
].key
)
319 for authorized_key
in self
._nsd
_msg
.key_pair
:
320 self
._log
.debug("Key pair NSD is %s",authorized_key
)
321 key_pairs
.append(authorized_key
.key
)
324 cloud_config
["key-pairs"] = key_pairs
327 for user_entry
in self
._nsr
_config
_msg
.user
:
328 self
._log
.debug("User present is %s",user_entry
)
330 user
["name"] = user_entry
.name
331 user
["key-pairs"] = list()
332 for ssh_key
in user_entry
.ssh_authorized_key
:
333 if ssh_key
.key_pair_ref
in self
._key
_pairs
:
334 user
["key-pairs"].append(self
._key
_pairs
[ssh_key
.key_pair_ref
].key
)
337 for user_entry
in self
._nsd
_msg
.user
:
338 self
._log
.debug("User present in NSD is %s",user_entry
)
340 user
["name"] = user_entry
.name
341 user
["key-pairs"] = list()
342 for ssh_key
in user_entry
.key_pair
:
343 user
["key-pairs"].append(ssh_key
.key
)
347 cloud_config
["users"] = users
349 self
._log
.debug("Cloud config formed is %s",cloud_config
)
354 def openmano_instance_create_yaml(self
):
355 self
._log
.debug("Creating instance-scenario-create input file for nsd %s with name %s", self
.nsd
.id, self
._nsr
_config
_msg
.name
)
356 openmano_instance_create
= {}
357 openmano_instance_create
["name"] = self
._nsr
_config
_msg
.name
358 openmano_instance_create
["description"] = self
._nsr
_config
_msg
.description
359 openmano_instance_create
["scenario"] = self
._nsd
_uuid
361 cloud_config
= self
.get_ssh_key_pairs()
363 openmano_instance_create
["cloud-config"] = cloud_config
364 if self
._nsr
_config
_msg
.has_field("om_datacenter"):
365 openmano_instance_create
["datacenter"] = self
._nsr
_config
_msg
.om_datacenter
366 openmano_instance_create
["vnfs"] = {}
367 for vnfr
in self
._vnfrs
:
368 if "om_datacenter" in vnfr
.vnfr
.vnfr_msg
:
369 vnfr_name
= vnfr
.vnfr
.vnfd
.name
+ "__" + str(vnfr
.vnfr
.vnfr_msg
.member_vnf_index_ref
)
370 openmano_instance_create
["vnfs"][vnfr_name
] = {"datacenter": vnfr
.vnfr
.vnfr_msg
.om_datacenter
}
371 openmano_instance_create
["networks"] = {}
372 for vld_msg
in self
._nsd
_msg
.vld
:
373 openmano_instance_create
["networks"][vld_msg
.name
] = {}
374 openmano_instance_create
["networks"][vld_msg
.name
]["sites"] = list()
375 for vlr
in self
._vlrs
:
376 if vlr
.vld_msg
.name
== vld_msg
.name
:
377 self
._log
.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr
.vld_msg
.name
,
378 vlr
.om_datacenter_name
,vld_msg
.name
)
379 #network["vim-network-name"] = vld_msg.name
382 if vld_msg
.vim_network_name
:
383 network
["netmap-use"] = vld_msg
.vim_network_name
384 elif vlr
._ip
_profile
and vlr
._ip
_profile
.has_field("ip_profile_params"):
385 ip_profile_params
= vlr
._ip
_profile
.ip_profile_params
386 if ip_profile_params
.ip_version
== "ipv6":
387 ip_profile
['ip-version'] = "IPv6"
389 ip_profile
['ip-version'] = "IPv4"
390 if ip_profile_params
.has_field('subnet_address'):
391 ip_profile
['subnet-address'] = ip_profile_params
.subnet_address
392 if ip_profile_params
.has_field('gateway_address'):
393 ip_profile
['gateway-address'] = ip_profile_params
.gateway_address
394 if ip_profile_params
.has_field('dns_server') and len(ip_profile_params
.dns_server
) > 0:
395 ip_profile
['dns-address'] = ip_profile_params
.dns_server
[0].address
396 if ip_profile_params
.has_field('dhcp_params'):
397 ip_profile
['dhcp'] = {}
398 ip_profile
['dhcp']['enabled'] = ip_profile_params
.dhcp_params
.enabled
399 ip_profile
['dhcp']['start-address'] = ip_profile_params
.dhcp_params
.start_address
400 ip_profile
['dhcp']['count'] = ip_profile_params
.dhcp_params
.count
402 network
["netmap-create"] = vlr
.name
403 if vlr
.om_datacenter_name
:
404 network
["datacenter"] = vlr
.om_datacenter_name
405 elif vld_msg
.has_field("om_datacenter"):
406 network
["datacenter"] = vld_msg
.om_datacenter
407 elif "datacenter" in openmano_instance_create
:
408 network
["datacenter"] = openmano_instance_create
["datacenter"]
410 openmano_instance_create
["networks"][vld_msg
.name
]["sites"].append(network
)
412 openmano_instance_create
["networks"][vld_msg
.name
]['ip-profile'] = ip_profile
414 return yaml
.safe_dump(openmano_instance_create
, default_flow_style
=False,width
=1000)
417 def add_vlr(self
, vlr
):
418 self
._vlrs
.append(vlr
)
419 yield from self
._publisher
.publish_vlr(None, vlr
.vlr_msg
)
420 yield from asyncio
.sleep(1, loop
=self
._loop
)
423 def remove_vlr(self
, vlr
):
424 if vlr
in self
._vlrs
:
425 self
._vlrs
.remove(vlr
)
426 if not vlr
.vld_msg
.vim_network_name
:
427 yield from self
._loop
.run_in_executor(
429 self
._cli
_api
.ns_vim_network_delete
,
431 vlr
.om_datacenter_name
)
432 yield from self
._publisher
.unpublish_vlr(None, vlr
.vlr_msg
)
433 yield from asyncio
.sleep(1, loop
=self
._loop
)
436 def add_vnfr(self
, vnfr
):
437 vnfr
= OpenmanoVnfr(self
._log
, self
._loop
, self
._cli
_api
, vnfr
, nsd
=self
.nsd
)
438 yield from vnfr
.create()
439 self
._vnfrs
.append(vnfr
)
443 if not self
._created
:
444 self
._log
.debug("NSD wasn't created. Skipping delete.")
447 self
._log
.debug("Deleting openmano nsr")
449 yield from self
._loop
.run_in_executor(
451 self
._cli
_api
.ns_delete
,
455 self
._log
.debug("Deleting openmano vnfrs")
456 for vnfr
in self
._vnfrs
:
457 yield from vnfr
.delete()
462 self
._log
.debug("Creating openmano scenario")
463 name_uuid_map
= yield from self
._loop
.run_in_executor(
465 self
._cli
_api
.ns_list
,
468 if self
._nsd
_msg
.name
in name_uuid_map
:
469 self
._log
.debug("Found existing openmano scenario")
470 self
._nsd
_uuid
= name_uuid_map
[self
._nsd
_msg
.name
]
474 # Use the nsd uuid as the scenario name to rebind to existing
475 # scenario on reload or to support muliple instances of the name
477 self
._nsd
_uuid
, _
= yield from self
._loop
.run_in_executor(
479 self
._cli
_api
.ns_create
,
480 self
.openmano_nsd_yaml
,
483 fpath
= dump_openmano_descriptor(
484 "{}_nsd".format(self
._nsd
_msg
.name
),
485 self
.openmano_nsd_yaml
,
488 self
._log
.debug("Dumped Openmano NS descriptor to: %s", fpath
)
493 def instance_monitor_task(self
):
494 self
._log
.debug("Starting Instance monitoring task")
496 start_time
= time
.time()
500 yield from asyncio
.sleep(1, loop
=self
._loop
)
503 instance_resp_json
= yield from self
._loop
.run_in_executor(
505 self
._http
_api
.get_instance
,
509 self
._log
.debug("Got instance response: %s for NSR ID %s",
513 except openmano_client
.InstanceStatusError
as e
:
514 self
._log
.error("Could not get NS instance status: %s", str(e
))
517 def all_vms_active(vnf
):
518 for vm
in vnf
["vms"]:
519 vm_status
= vm
["status"]
521 if vm_status
!= "ACTIVE":
522 self
._log
.debug("VM is not yet active: %s (status: %s)", vm_uuid
, vm_status
)
527 def any_vm_active_nomgmtip(vnf
):
528 for vm
in vnf
["vms"]:
529 vm_status
= vm
["status"]
531 if vm_status
!= "ACTIVE":
532 self
._log
.debug("VM is not yet active: %s (status: %s)", vm_uuid
, vm_status
)
537 def any_vms_error(vnf
):
538 for vm
in vnf
["vms"]:
539 vm_status
= vm
["status"]
540 vm_vim_info
= vm
["vim_info"]
542 if vm_status
== "ERROR":
543 self
._log
.error("VM Error: %s (vim_info: %s)", vm_uuid
, vm_vim_info
)
548 def get_vnf_ip_address(vnf
):
549 if "ip_address" in vnf
:
550 return vnf
["ip_address"].strip()
553 def get_vnf_mac_address(vnf
):
554 if "mac_address" in vnf
:
555 return vnf
["mac_address"].strip()
558 def get_ext_cp_info(vnf
):
560 for vm
in vnf
["vms"]:
561 if "interfaces" not in vm
:
564 for intf
in vm
["interfaces"]:
565 if "external_name" not in intf
:
568 if not intf
["external_name"]:
571 ip_address
= intf
["ip_address"]
572 if ip_address
is None:
573 ip_address
= "0.0.0.0"
575 mac_address
= intf
["mac_address"]
576 if mac_address
is None:
577 mac_address
="00:00:00:00:00:00"
579 cp_info_list
.append((intf
["external_name"], ip_address
, mac_address
))
583 def get_vnf_status(vnfr
):
584 # When we create an openmano descriptor we use <name>__<idx>
585 # to come up with openmano constituent VNF name. Use this
586 # knowledge to map the vnfr back.
587 openmano_vnfr_suffix
= "__{}".format(
588 vnfr
.vnfr
.vnfr_msg
.member_vnf_index_ref
591 for vnf
in instance_resp_json
["vnfs"]:
592 if vnf
["vnf_name"].endswith(openmano_vnfr_suffix
):
595 self
._log
.warning("Could not find vnf status with name that ends with: %s",
596 openmano_vnfr_suffix
)
599 for vnfr
in self
._vnfrs
:
600 if vnfr
in active_vnfs
:
601 # Skipping, so we don't re-publish the same VNF message.
604 vnfr_msg
= vnfr
.vnfr
.vnfr_msg
.deep_copy()
605 vnfr_msg
.operational_status
= "init"
608 vnf_status
= get_vnf_status(vnfr
)
609 self
._log
.debug("Found VNF status: %s", vnf_status
)
610 if vnf_status
is None:
611 self
._log
.error("Could not find VNF status from openmano")
612 self
._state
= OpenmanoNSRecordState
.FAILED
613 vnfr_msg
.operational_status
= "failed"
614 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
617 # If there was a VNF that has a errored VM, then just fail the VNF and stop monitoring.
618 if any_vms_error(vnf_status
):
619 self
._log
.debug("VM was found to be in error state. Marking as failed.")
620 self
._state
= OpenmanoNSRecordState
.FAILED
621 vnfr_msg
.operational_status
= "failed"
622 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
625 if all_vms_active(vnf_status
):
626 vnf_ip_address
= get_vnf_ip_address(vnf_status
)
627 vnf_mac_address
= get_vnf_mac_address(vnf_status
)
629 if vnf_ip_address
is None:
630 self
._log
.warning("No IP address obtained "
631 "for VNF: {}, will retry.".format(
632 vnf_status
['vnf_name']))
635 self
._log
.debug("All VMs in VNF are active. Marking as running.")
636 vnfr_msg
.operational_status
= "running"
638 self
._log
.debug("Got VNF ip address: %s, mac-address: %s", vnf_ip_address
, vnf_mac_address
)
639 vnfr_msg
.mgmt_interface
.ip_address
= vnf_ip_address
640 vnfr_msg
.vnf_configuration
.config_access
.mgmt_ip_address
= vnf_ip_address
643 for vm
in vnf_status
["vms"]:
644 if vm
["uuid"] not in self
._vdur
_console
_handler
:
645 vdur_console_handler
= VnfrConsoleOperdataDtsHandler(self
._dts
, self
._log
, self
._loop
,
646 self
, vnfr_msg
.id,vm
["uuid"],vm
["name"])
647 yield from vdur_console_handler
.register()
648 self
._vdur
_console
_handler
[vm
["uuid"]] = vdur_console_handler
650 vdur_msg
= vnfr_msg
.vdur
.add()
651 vdur_msg
.vim_id
= vm
["vim_vm_id"]
652 vdur_msg
.id = vm
["uuid"]
654 # Add connection point information for the config manager
655 cp_info_list
= get_ext_cp_info(vnf_status
)
656 for (cp_name
, cp_ip
, cp_mac_addr
) in cp_info_list
:
657 cp
= vnfr_msg
.connection_point
.add()
659 cp
.short_name
= cp_name
660 cp
.ip_address
= cp_ip
661 cp
.mac_address
= cp_mac_addr
663 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
664 active_vnfs
.append(vnfr
)
666 if (time
.time() - start_time
) > OpenmanoNsr
.TIMEOUT_SECS
:
667 self
._log
.error("NSR timed out before reaching running state")
668 self
._state
= OpenmanoNSRecordState
.FAILED
669 vnfr_msg
.operational_status
= "failed"
670 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
673 except Exception as e
:
674 vnfr_msg
.operational_status
= "failed"
675 self
._state
= OpenmanoNSRecordState
.FAILED
676 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
677 self
._log
.exception("Caught exception publishing vnfr info: %s", str(e
))
680 if len(active_vnfs
) == len(self
._vnfrs
):
681 self
._state
= OpenmanoNSRecordState
.RUNNING
682 self
._log
.info("All VNF's are active. Exiting NSR monitoring task")
686 def deploy(self
,nsr_msg
):
687 if self
._nsd
_uuid
is None:
688 raise ValueError("Cannot deploy an uncreated nsd")
690 self
._log
.debug("Deploying openmano scenario")
692 name_uuid_map
= yield from self
._loop
.run_in_executor(
694 self
._cli
_api
.ns_instance_list
,
697 if self
._nsr
_config
_msg
.name
in name_uuid_map
:
698 self
._log
.debug("Found existing instance with nsr name: %s", self
._nsr
_config
_msg
.name
)
699 self
._nsr
_uuid
= name_uuid_map
[self
._nsr
_config
_msg
.name
]
701 self
._nsr
_msg
= nsr_msg
702 fpath
= dump_openmano_descriptor(
703 "{}_instance_sce_create".format(self
._nsr
_config
_msg
.name
),
704 self
.openmano_instance_create_yaml
,
706 self
._log
.debug("Dumped Openmano NS Scenario Cretae to: %s", fpath
)
708 self
._nsr
_uuid
= yield from self
._loop
.run_in_executor(
710 self
._cli
_api
.ns_instance_scenario_create
,
711 self
.openmano_instance_create_yaml
)
713 self
._state
= OpenmanoNSRecordState
.INSTANTIATION_PENDING
715 self
._monitor
_task
= asyncio
.ensure_future(
716 self
.instance_monitor_task(), loop
=self
._loop
722 for _
,handler
in self
._vdur
_console
_handler
.items():
723 handler
._regh
.deregister()
725 if self
._nsr
_uuid
is None:
726 self
._log
.warning("Cannot terminate an un-instantiated nsr")
729 if self
._monitor
_task
is not None:
730 self
._monitor
_task
.cancel()
731 self
._monitor
_task
= None
733 self
._log
.debug("Terminating openmano nsr")
734 yield from self
._loop
.run_in_executor(
736 self
._cli
_api
.ns_terminate
,
741 def create_vlr(self
,vlr
):
742 self
._log
.debug("Creating openmano vim network VLR name %s, VLR DC: %s",vlr
.vld_msg
.name
,
743 vlr
.om_datacenter_name
)
746 net
['name'] = vlr
.name
748 net
['type'] = 'bridge'
749 self
._log
.debug("Received ip profile is %s",vlr
._ip
_profile
)
750 if vlr
._ip
_profile
and vlr
._ip
_profile
.has_field("ip_profile_params"):
751 ip_profile_params
= vlr
._ip
_profile
.ip_profile_params
753 if ip_profile_params
.ip_version
== "ipv6":
754 ip_profile
['ip_version'] = "IPv6"
756 ip_profile
['ip_version'] = "IPv4"
757 if ip_profile_params
.has_field('subnet_address'):
758 ip_profile
['subnet_address'] = ip_profile_params
.subnet_address
759 if ip_profile_params
.has_field('gateway_address'):
760 ip_profile
['gateway_address'] = ip_profile_params
.gateway_address
761 if ip_profile_params
.has_field('dns_server') and len(ip_profile_params
.dns_server
) > 0:
762 ip_profile
['dns_address'] = ip_profile_params
.dns_server
[0].address
763 if ip_profile_params
.has_field('dhcp_params'):
764 ip_profile
['dhcp_enabled'] = ip_profile_params
.dhcp_params
.enabled
765 ip_profile
['dhcp_start_address'] = ip_profile_params
.dhcp_params
.start_address
766 ip_profile
['dhcp_count'] = ip_profile_params
.dhcp_params
.count
767 net
['ip_profile'] = ip_profile
768 net_create
["network"]= net
770 net_create_msg
= yaml
.safe_dump(net_create
,default_flow_style
=False)
771 fpath
= dump_openmano_descriptor(
772 "{}_vim_net_create_{}".format(self
._nsr
_config
_msg
.name
,vlr
.name
),
774 self
._log
.debug("Dumped Openmano VIM Net create to: %s", fpath
)
776 vim_network_uuid
= yield from self
._loop
.run_in_executor(
778 self
._cli
_api
.ns_vim_network_create
,
780 vlr
.om_datacenter_name
)
781 self
._vlrs
.append(vlr
)
785 class OpenmanoNsPlugin(rwnsmplugin
.NsmPluginBase
):
787 RW Implentation of the NsmPluginBase
789 def __init__(self
, dts
, log
, loop
, publisher
, ro_account
):
793 self
._publisher
= publisher
796 self
._http
_api
= None
797 self
._openmano
_nsrs
= {}
798 self
._vnfr
_uptime
_tasks
= {}
800 self
._set
_ro
_account
(ro_account
)
802 def _set_ro_account(self
, ro_account
):
803 self
._log
.debug("Setting openmano plugin cloud account: %s", ro_account
)
804 self
._cli
_api
= openmano_client
.OpenmanoCliAPI(
806 ro_account
.openmano
.host
,
807 ro_account
.openmano
.port
,
808 ro_account
.openmano
.tenant_id
,
811 self
._http
_api
= openmano_client
.OpenmanoHttpAPI(
813 ro_account
.openmano
.host
,
814 ro_account
.openmano
.port
,
815 ro_account
.openmano
.tenant_id
,
818 def create_nsr(self
, nsr_config_msg
, nsd_msg
, key_pairs
=None):
820 Create Network service record
822 openmano_nsr
= OpenmanoNsr(
833 self
._openmano
_nsrs
[nsr_config_msg
.id] = openmano_nsr
836 def deploy(self
, nsr_msg
):
837 self
._log
.debug("Received NSR Deploy msg : %s", nsr_msg
)
838 openmano_nsr
= self
._openmano
_nsrs
[nsr_msg
.ns_instance_config_ref
]
839 yield from openmano_nsr
.create()
840 yield from openmano_nsr
.deploy(nsr_msg
)
843 def instantiate_ns(self
, nsr
, xact
):
845 Instantiate NSR with the passed nsr id
847 yield from nsr
.instantiate(xact
)
850 def instantiate_vnf(self
, nsr
, vnfr
):
852 Instantiate NSR with the passed nsr id
854 openmano_nsr
= self
._openmano
_nsrs
[nsr
.id]
855 yield from openmano_nsr
.add_vnfr(vnfr
)
857 # Mark the VNFR as running
858 # TODO: Create a task to monitor nsr/vnfr status
859 vnfr_msg
= vnfr
.vnfr_msg
.deep_copy()
860 vnfr_msg
.operational_status
= "init"
862 self
._log
.debug("Attempting to publish openmano vnf: %s", vnfr_msg
)
863 with self
._dts
.transaction() as xact
:
864 yield from self
._publisher
.publish_vnfr(xact
, vnfr_msg
)
865 self
._log
.debug("Creating a task to update uptime for vnfr: %s", vnfr
.id)
866 self
._vnfr
_uptime
_tasks
[vnfr
.id] = self
._loop
.create_task(self
.vnfr_uptime_update(vnfr
))
868 def vnfr_uptime_update(self
, vnfr
):
870 vnfr_
= RwVnfrYang
.YangData_Vnfr_VnfrCatalog_Vnfr
.from_dict({'id': vnfr
.id})
872 vnfr_
.uptime
= int(time
.time()) - vnfr
._create
_time
873 yield from self
._publisher
.publish_vnfr(None, vnfr_
)
874 yield from asyncio
.sleep(2, loop
=self
._loop
)
875 except asyncio
.CancelledError
:
876 self
._log
.debug("Received cancellation request for vnfr_uptime_update task")
879 def instantiate_vl(self
, nsr
, vlr
):
881 Instantiate NSR with the passed nsr id
883 self
._log
.debug("Received instantiate VL for NSR {}; VLR {}".format(nsr
.id,vlr
))
884 openmano_nsr
= self
._openmano
_nsrs
[nsr
.id]
885 if openmano_nsr
._state
== OpenmanoNSRecordState
.RUNNING
:
886 yield from openmano_nsr
.create_vlr(vlr
)
888 yield from openmano_nsr
.add_vlr(vlr
)
891 def terminate_ns(self
, nsr
):
893 Terminate the network service
896 openmano_nsr
= self
._openmano
_nsrs
[nsr_id
]
898 yield from openmano_nsr
.terminate()
899 yield from openmano_nsr
.delete()
901 with self
._dts
.transaction() as xact
:
902 for vnfr
in openmano_nsr
.vnfrs
:
903 self
._log
.debug("Unpublishing VNFR: %s", vnfr
.vnfr
.vnfr_msg
)
904 yield from self
._publisher
.unpublish_vnfr(xact
, vnfr
.vnfr
.vnfr_msg
)
906 del self
._openmano
_nsrs
[nsr_id
]
909 def terminate_vnf(self
, vnfr
):
911 Terminate the network service
913 if vnfr
.id in self
._vnfr
_uptime
_tasks
:
914 self
._vnfr
_uptime
_tasks
[vnfr
.id].cancel()
917 def terminate_vl(self
, vlr
):
919 Terminate the virtual link
921 self
._log
.debug("Received terminate VL for VLR {}".format(vlr
))
922 openmano_nsr
= self
._openmano
_nsrs
[vlr
._nsr
_id
]
923 yield from openmano_nsr
.remove_vlr(vlr
)