2 # Copyright 2016 RIFT.IO Inc
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
29 gi
.require_version('RwDts', '1.0')
30 gi
.require_version('RwVnfrYang', '1.0')
31 gi
.require_version('RwVnfmYang', '1.0')
32 gi
.require_version('RwVlrYang', '1.0')
33 gi
.require_version('RwManifestYang', '1.0')
34 gi
.require_version('RwBaseYang', '1.0')
35 gi
.require_version('RwResourceMgrYang', '1.0')
37 from gi
.repository
import (
50 import rift
.package
.store
51 import rift
.package
.cloud_init
52 import rift
.package
.script
53 import rift
.mano
.dts
as mano_dts
54 from rift
.mano
.utils
.project
import (
58 import rift
.mano
.utils
.short_name
as mano_short_name
61 class VMResourceError(Exception):
62 """ VM resource Error"""
66 class VnfRecordError(Exception):
67 """ VNF record instatiation failed"""
71 class VduRecordError(Exception):
72 """ VDU record instatiation failed"""
76 class NotImplemented(Exception):
77 """Not implemented """
81 class VnfrRecordExistsError(Exception):
82 """VNFR record already exist with the same VNFR id"""
86 class InternalVirtualLinkRecordError(Exception):
87 """Internal virtual link record error"""
91 class VDUImageNotFound(Exception):
92 """VDU Image not found error"""
96 class VirtualDeploymentUnitRecordError(Exception):
97 """VDU Instantiation failed"""
101 class VMNotReadyError(Exception):
102 """ VM Not yet received from resource manager """
106 class VDURecordNotFound(Exception):
107 """ Could not find a VDU record """
111 class VirtualNetworkFunctionRecordDescNotFound(Exception):
112 """ Cannot find Virtual Network Function Record Descriptor """
116 class VirtualNetworkFunctionDescriptorError(Exception):
117 """ Virtual Network Function Record Descriptor Error """
121 class VirtualNetworkFunctionDescriptorNotFound(Exception):
122 """ Virtual Network Function Record Descriptor Not Found """
126 class VirtualNetworkFunctionRecordNotFound(Exception):
127 """ Virtual Network Function Record Not Found """
131 class VirtualNetworkFunctionDescriptorRefCountExists(Exception):
132 """ Virtual Network Funtion Descriptor reference count exists """
136 class VnfrInstantiationFailed(Exception):
137 """ Virtual Network Funtion Instantiation failed"""
141 class VNFMPlacementGroupError(Exception):
144 class VirtualNetworkFunctionRecordState(enum
.Enum
):
151 VL_TERMINATE_PHASE
= 6
152 VDU_TERMINATE_PHASE
= 7
157 class VDURecordState(enum
.Enum
):
158 """VDU record state """
161 RESOURCE_ALLOC_PENDING
= 3
168 class VcsComponent(object):
169 """ VCS Component within the VNF descriptor """
170 def __init__(self
, dts
, log
, loop
, cluster_name
,
171 vcs_handler
, component
, mangled_name
):
175 self
._component
= component
176 self
._cluster
_name
= cluster_name
177 self
._vcs
_handler
= vcs_handler
178 self
._mangled
_name
= mangled_name
181 def mangle_name(component_name
, vnf_name
, vnfd_id
):
182 """ mangled component name """
183 return vnf_name
+ ":" + component_name
+ ":" + vnfd_id
187 """ name of this component"""
188 return self
._mangled
_name
192 """ The path for this object """
193 return ("D,/rw-manifest:manifest" +
194 "/rw-manifest:operational-inventory" +
195 "/rw-manifest:component" +
196 "[rw-manifest:component-name = '{}']").format(self
.name
)
199 def instance_xpath(self
):
200 """ The path for this object """
201 return("D,/rw-base:vcs" +
204 "[instance-name = '{}']".format(self
._cluster
_name
))
207 def start_comp_xpath(self
):
208 """ start component xpath """
209 return (self
.instance_xpath
+
210 "/child-n[instance-name = 'START-REQ']")
212 def get_start_comp_msg(self
, ip_address
):
213 """ start this component """
214 start_msg
= RwBaseYang
.VcsInstance_Instance_ChildN()
215 start_msg
.instance_name
= 'START-REQ'
216 start_msg
.component_name
= self
.name
217 start_msg
.admin_command
= "START"
218 start_msg
.ip_address
= ip_address
224 """ Returns the message for this vcs component"""
226 vcs_comp_dict
= self
._component
.as_dict()
228 def mangle_comp_names(comp_dict
):
229 """ mangle component name with VNF name, id"""
230 for key
, val
in comp_dict
.items():
231 if isinstance(val
, dict):
232 comp_dict
[key
] = mangle_comp_names(val
)
233 elif isinstance(val
, list):
236 if isinstance(ent
, dict):
237 val
[i
] = mangle_comp_names(ent
)
241 elif key
== "component_name":
242 comp_dict
[key
] = VcsComponent
.mangle_name(val
,
247 mangled_dict
= mangle_comp_names(vcs_comp_dict
)
248 msg
= RwManifestYang
.OpInventory_Component
.from_dict(mangled_dict
)
252 def publish(self
, xact
):
253 """ Publishes the VCS component """
254 self
._log
.debug("Publishing the VcsComponent %s, path = %s comp = %s",
255 self
.name
, self
.path
, self
.msg
)
256 yield from self
._vcs
_handler
.publish(xact
, self
.path
, self
.msg
)
259 def start(self
, xact
, parent
, ip_addr
=None):
260 """ Starts this VCS component """
261 # ATTN RV - replace with block add
262 start_msg
= self
.get_start_comp_msg(ip_addr
)
263 self
._log
.debug("starting component %s %s",
264 self
.start_comp_xpath
, start_msg
)
265 yield from self
._dts
.query_create(self
.start_comp_xpath
,
268 self
._log
.debug("started component %s, %s",
269 self
.start_comp_xpath
, start_msg
)
272 class VirtualDeploymentUnitRecord(object):
273 """ Virtual Deployment Unit Record """
287 placement_groups
=[]):
291 self
._project
= project
294 self
._nsr
_config
= nsr_config
295 self
._mgmt
_intf
= mgmt_intf
296 self
._cloud
_account
_name
= cloud_account_name
297 self
._vnfd
_package
_store
= vnfd_package_store
298 self
._mgmt
_network
= mgmt_network
300 self
._vdur
_id
= vdur_id
or str(uuid
.uuid4())
303 self
._state
= VDURecordState
.INIT
304 self
._state
_failed
_reason
= None
305 self
._request
_id
= str(uuid
.uuid4())
306 self
._name
= vnfr
.name
+ "__" + vdud
.id
307 self
._placement
_groups
= placement_groups
310 self
._vdud
_cloud
_init
= None
311 self
._vdur
_console
_handler
= VnfrConsoleOperdataDtsHandler(
312 dts
, log
, loop
, self
._vnfr
._vnfm
, self
._vnfr
.vnfr_id
, self
._vdur
_id
,self
.vdu_id
)
315 def vdu_opdata_register(self
):
316 yield from self
._vdur
_console
_handler
.register()
318 def cp_ip_addr(self
, cp_name
):
319 """ Find ip address by connection point name """
320 if self
._vm
_resp
is not None:
321 for conn_point
in self
._vm
_resp
.connection_points
:
322 if conn_point
.name
== cp_name
:
323 return conn_point
.ip_address
326 def cp_mac_addr(self
, cp_name
):
327 """ Find mac address by connection point name """
328 if self
._vm
_resp
is not None:
329 for conn_point
in self
._vm
_resp
.connection_points
:
330 if conn_point
.name
== cp_name
:
331 return conn_point
.mac_addr
332 return "00:00:00:00:00:00"
334 def cp_id(self
, cp_name
):
335 """ Find connection point id by connection point name """
336 if self
._vm
_resp
is not None:
337 for conn_point
in self
._vm
_resp
.connection_points
:
338 if conn_point
.name
== cp_name
:
339 return conn_point
.connection_point_id
352 """ Return this VDUR's name """
355 # Truncated name confirming to RFC 1123
357 def unique_short_name(self
):
358 """ Return this VDUR's unique short name """
359 # Impose these restrictions on Unique name
361 # - Max 10 of NSR name (remove all specialcharacters, only numbers and alphabets)
362 # - 6 chars of shortened name
363 # - Max 10 of VDU name (remove all specialcharacters, only numbers and alphabets)
365 def _restrict_tag(input_str
):
366 # Exclude all characters except a-zA-Z0-9
367 outstr
= re
.sub('[^a-zA-Z0-9]', '', input_str
)
368 # Take max of 10 chars
371 # Use NSR name for part1
372 part1
= _restrict_tag(self
._nsr
_config
.name
)
373 # Get unique short string (6 chars)
374 part2
= mano_short_name
.StringShortner(self
._name
)
375 # Use VDU ID for part3
376 part3
= _restrict_tag(self
._vdud
.id)
377 shortstr
= part1
+ "-" + part2
.short_string
+ "-" + part3
381 def cloud_account_name(self
):
382 """ Cloud account this VDU should be created in """
383 return self
._cloud
_account
_name
386 def image_name(self
):
387 """ name that should be used to lookup the image on the CMP """
388 if 'image' not in self
._vdud
:
390 return os
.path
.basename(self
._vdud
.image
)
393 def image_checksum(self
):
394 """ name that should be used to lookup the image on the CMP """
395 return self
._vdud
.image_checksum
if self
._vdud
.has_field("image_checksum") else None
398 def management_ip(self
):
401 return self
._vm
_resp
.public_ip
if self
._vm
_resp
.has_field('public_ip') else self
._vm
_resp
.management_ip
404 def vm_management_ip(self
):
407 return self
._vm
_resp
.management_ip
410 def operational_status(self
):
411 """ Operational status of this VDU"""
412 op_stats_dict
= {"INIT": "init",
413 "INSTANTIATING": "vm_init_phase",
414 "RESOURCE_ALLOC_PENDING": "vm_alloc_pending",
417 "TERMINATING": "terminated",
418 "TERMINATED": "terminated",
420 return op_stats_dict
[self
._state
.name
]
424 """ Process VDU message from resmgr"""
425 vdu_fields
= ["vm_flavor",
432 vdu_copy_dict
= {k
: v
for k
, v
in
433 self
._vdud
.as_dict().items() if k
in vdu_fields
}
434 vdur_dict
= {"id": self
._vdur
_id
,
435 "vdu_id_ref": self
._vdud
.id,
436 "operational_status": self
.operational_status
,
437 "operational_status_details": self
._state
_failed
_reason
,
439 "unique_short_name": self
.unique_short_name
442 if self
.vm_resp
is not None:
443 vdur_dict
.update({"vim_id": self
.vm_resp
.vdu_id
,
444 "flavor_id": self
.vm_resp
.flavor_id
446 if self
._vm
_resp
.has_field('image_id'):
447 vdur_dict
.update({ "image_id": self
.vm_resp
.image_id
})
449 if self
.management_ip
is not None:
450 vdur_dict
["management_ip"] = self
.management_ip
452 if self
.vm_management_ip
is not None:
453 vdur_dict
["vm_management_ip"] = self
.vm_management_ip
455 vdur_dict
.update(vdu_copy_dict
)
457 if self
.vm_resp
is not None:
458 if self
._vm
_resp
.has_field('volumes'):
459 for opvolume
in self
._vm
_resp
.volumes
:
460 vdurvol_data
= [vduvol
for vduvol
in vdur_dict
['volumes'] if vduvol
['name'] == opvolume
.name
]
461 if len(vdurvol_data
) == 1:
462 vdurvol_data
[0]["volume_id"] = opvolume
.volume_id
463 if opvolume
.has_field('custom_meta_data'):
464 metadata_list
= list()
465 for metadata_item
in opvolume
.custom_meta_data
:
466 metadata_list
.append(metadata_item
.as_dict())
467 vdurvol_data
[0]['custom_meta_data'] = metadata_list
469 if self
._vm
_resp
.has_field('supplemental_boot_data'):
470 vdur_dict
['supplemental_boot_data'] = dict()
471 if self
._vm
_resp
.supplemental_boot_data
.has_field('boot_data_drive'):
472 vdur_dict
['supplemental_boot_data']['boot_data_drive'] = self
._vm
_resp
.supplemental_boot_data
.boot_data_drive
473 if self
._vm
_resp
.supplemental_boot_data
.has_field('custom_meta_data'):
474 metadata_list
= list()
475 for metadata_item
in self
._vm
_resp
.supplemental_boot_data
.custom_meta_data
:
476 metadata_list
.append(metadata_item
.as_dict())
477 vdur_dict
['supplemental_boot_data']['custom_meta_data'] = metadata_list
478 if self
._vm
_resp
.supplemental_boot_data
.has_field('config_file'):
480 for file_item
in self
._vm
_resp
.supplemental_boot_data
.config_file
:
481 file_list
.append(file_item
.as_dict())
482 vdur_dict
['supplemental_boot_data']['config_file'] = file_list
487 for intf
, cp_id
, vlr
in self
._int
_intf
:
488 cp
= self
.find_internal_cp_by_cp_id(cp_id
)
490 icp_list
.append({"name": cp
.name
,
492 "type_yang": "VPORT",
493 "ip_address": self
.cp_ip_addr(cp
.id),
494 "mac_address": self
.cp_mac_addr(cp
.id)})
496 ii_list
.append({"name": intf
.name
,
497 "vdur_internal_connection_point_ref": cp
.id,
498 "virtual_interface": {}})
500 vdur_dict
["internal_connection_point"] = icp_list
501 self
._log
.debug("internal_connection_point:%s", vdur_dict
["internal_connection_point"])
502 vdur_dict
["internal_interface"] = ii_list
505 for intf
, cp
, vlr
in self
._ext
_intf
:
506 ei_list
.append({"name": cp
.name
,
507 "vnfd_connection_point_ref": cp
.name
,
508 "virtual_interface": {}})
509 self
._vnfr
.update_cp(cp
.name
,
510 self
.cp_ip_addr(cp
.name
),
511 self
.cp_mac_addr(cp
.name
),
514 vdur_dict
["external_interface"] = ei_list
516 placement_groups
= []
517 for group
in self
._placement
_groups
:
518 placement_groups
.append(group
.as_dict())
519 vdur_dict
['placement_groups_info'] = placement_groups
521 return RwVnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr_Vdur
.from_dict(vdur_dict
)
524 def resmgr_path(self
):
525 """ path for resource-mgr"""
526 xpath
= self
._project
.add_project("D,/rw-resource-mgr:resource-mgmt" +
528 "/vdu-event-data[event-id='{}']".format(self
._request
_id
))
532 def vm_flavor_msg(self
):
533 """ VM flavor message """
534 flavor
= self
._vdud
.vm_flavor
.__class
__()
535 flavor
.copy_from(self
._vdud
.vm_flavor
)
540 def vdud_cloud_init(self
):
541 """ Return the cloud-init contents for the VDU """
542 if self
._vdud
_cloud
_init
is None:
543 self
._vdud
_cloud
_init
= self
.cloud_init()
545 return self
._vdud
_cloud
_init
547 def cloud_init(self
):
548 """ Populate cloud_init with cloud-config script from
549 either the inline contents or from the file provided
551 if self
._vdud
.cloud_init
is not None:
552 self
._log
.debug("cloud_init script provided inline %s", self
._vdud
.cloud_init
)
553 return self
._vdud
.cloud_init
554 elif self
._vdud
.cloud_init_file
is not None:
555 # Get cloud-init script contents from the file provided in the cloud_init_file param
556 self
._log
.debug("cloud_init script provided in file %s", self
._vdud
.cloud_init_file
)
557 filename
= self
._vdud
.cloud_init_file
558 self
._vnfd
_package
_store
.refresh()
559 stored_package
= self
._vnfd
_package
_store
.get_package(self
._vnfr
.vnfd_id
)
560 cloud_init_extractor
= rift
.package
.cloud_init
.PackageCloudInitExtractor(self
._log
)
562 return cloud_init_extractor
.read_script(stored_package
, filename
)
563 except rift
.package
.cloud_init
.CloudInitExtractionError
as e
:
564 self
.instantiation_failed(str(e
))
565 raise VirtualDeploymentUnitRecordError(e
)
567 self
._log
.debug("VDU Instantiation: cloud-init script not provided")
569 def process_openstack_placement_group_construct(self
, vm_create_msg_dict
):
571 availability_zones
= []
573 for group
in self
._placement
_groups
:
574 if group
.has_field('host_aggregate'):
575 for aggregate
in group
.host_aggregate
:
576 host_aggregates
.append(aggregate
.as_dict())
577 if group
.has_field('availability_zone'):
578 availability_zones
.append(group
.availability_zone
.as_dict())
579 if group
.has_field('server_group'):
580 server_groups
.append(group
.server_group
.as_dict())
582 if availability_zones
:
583 if len(availability_zones
) > 1:
584 self
._log
.error("Can not launch VDU: %s in multiple availability zones. " +
585 "Requested Zones: %s", self
.name
, availability_zones
)
586 raise VNFMPlacementGroupError("Can not launch VDU: {} in multiple availability" +
587 " zones. Requsted Zones".format(self
.name
, availability_zones
))
589 vm_create_msg_dict
['availability_zone'] = availability_zones
[0]
592 if len(server_groups
) > 1:
593 self
._log
.error("Can not launch VDU: %s in multiple Server Group. " +
594 "Requested Groups: %s", self
.name
, server_groups
)
595 raise VNFMPlacementGroupError("Can not launch VDU: {} in multiple " +
596 "Server Groups. Requsted Groups".format(self
.name
, server_groups
))
598 vm_create_msg_dict
['server_group'] = server_groups
[0]
601 vm_create_msg_dict
['host_aggregate'] = host_aggregates
605 def process_placement_groups(self
, vm_create_msg_dict
):
606 """Process the placement_groups and fill resource-mgr request"""
607 if not self
._placement
_groups
:
610 cloud_set
= set([group
.cloud_type
for group
in self
._placement
_groups
])
611 assert len(cloud_set
) == 1
612 cloud_type
= cloud_set
.pop()
614 if cloud_type
== 'openstack':
615 self
.process_openstack_placement_group_construct(vm_create_msg_dict
)
618 self
._log
.info("Ignoring placement group with cloud construct for cloud-type: %s", cloud_type
)
621 def process_custom_bootdata(self
, vm_create_msg_dict
):
622 """Process the custom boot data"""
623 if 'config_file' not in vm_create_msg_dict
['supplemental_boot_data']:
626 self
._vnfd
_package
_store
.refresh()
627 stored_package
= self
._vnfd
_package
_store
.get_package(self
._vnfr
.vnfd_id
)
628 cloud_init_extractor
= rift
.package
.cloud_init
.PackageCloudInitExtractor(self
._log
)
629 for file_item
in vm_create_msg_dict
['supplemental_boot_data']['config_file']:
630 if 'source' not in file_item
or 'dest' not in file_item
:
632 source
= file_item
['source']
633 # Find source file in scripts dir of VNFD
634 self
._log
.debug("Checking for source config file at %s", source
)
636 source_file_str
= cloud_init_extractor
.read_script(stored_package
, source
)
637 except rift
.package
.cloud_init
.CloudInitExtractionError
as e
:
638 raise VirtualDeploymentUnitRecordError(e
)
639 # Update source file location with file contents
640 file_item
['source'] = source_file_str
644 def resmgr_msg(self
, config
=None):
645 vdu_fields
= ["vm_flavor",
651 "supplemental_boot_data"]
653 self
._log
.debug("Creating params based on VDUD: %s", self
._vdud
)
654 vdu_copy_dict
= {k
: v
for k
, v
in self
._vdud
.as_dict().items() if k
in vdu_fields
}
656 vm_create_msg_dict
= {
657 "name": self
.unique_short_name
, # Truncated name confirming to RFC 1123
658 "node_id": self
.name
, # Rift assigned Id
661 if self
.image_name
is not None:
662 vm_create_msg_dict
["image_name"] = self
.image_name
664 if self
.image_checksum
is not None:
665 vm_create_msg_dict
["image_checksum"] = self
.image_checksum
667 vm_create_msg_dict
["allocate_public_address"] = self
._mgmt
_intf
668 if self
._vdud
.has_field('mgmt_vpci'):
669 vm_create_msg_dict
["mgmt_vpci"] = self
._vdud
.mgmt_vpci
671 self
._log
.debug("VDUD: %s", self
._vdud
)
672 if config
is not None:
673 vm_create_msg_dict
['vdu_init'] = {'userdata': config
}
675 if self
._mgmt
_network
:
676 vm_create_msg_dict
['mgmt_network'] = self
._mgmt
_network
679 for intf
, cp
, vlr
in self
._ext
_intf
:
680 cp_info
= { "name": cp
.name
,
681 "virtual_link_id": vlr
.network_id
,
682 "type_yang": intf
.virtual_interface
.type_yang
}
684 if cp
.has_field('port_security_enabled'):
685 cp_info
["port_security_enabled"] = cp
.port_security_enabled
687 if (intf
.virtual_interface
.has_field('vpci') and
688 intf
.virtual_interface
.vpci
is not None):
689 cp_info
["vpci"] = intf
.virtual_interface
.vpci
691 if (vlr
.has_field('ip_profile_params')) and (vlr
.ip_profile_params
.has_field('security_group')):
692 cp_info
['security_group'] = vlr
.ip_profile_params
.security_group
694 cp_list
.append(cp_info
)
696 for intf
, cp
, vlr
in self
._int
_intf
:
697 if (intf
.virtual_interface
.has_field('vpci') and
698 intf
.virtual_interface
.vpci
is not None):
699 cp_list
.append({"name": cp
,
700 "virtual_link_id": vlr
.network_id
,
701 "type_yang": intf
.virtual_interface
.type_yang
,
702 "vpci": intf
.virtual_interface
.vpci
})
704 if cp
.has_field('port_security_enabled'):
705 cp_list
.append({"name": cp
,
706 "virtual_link_id": vlr
.network_id
,
707 "type_yang": intf
.virtual_interface
.type_yang
,
708 "port_security_enabled": cp
.port_security_enabled
})
710 cp_list
.append({"name": cp
,
711 "virtual_link_id": vlr
.network_id
,
712 "type_yang": intf
.virtual_interface
.type_yang
})
715 vm_create_msg_dict
["connection_points"] = cp_list
716 vm_create_msg_dict
.update(vdu_copy_dict
)
718 self
.process_placement_groups(vm_create_msg_dict
)
719 if 'supplemental_boot_data' in vm_create_msg_dict
:
720 self
.process_custom_bootdata(vm_create_msg_dict
)
722 msg
= RwResourceMgrYang
.VDUEventData()
723 msg
.event_id
= self
._request
_id
724 msg
.cloud_account
= self
.cloud_account_name
725 msg
.request_info
.from_dict(vm_create_msg_dict
)
730 def terminate(self
, xact
):
731 """ Delete resource in VIM """
732 if self
._state
!= VDURecordState
.READY
and self
._state
!= VDURecordState
.FAILED
:
733 self
._log
.warning("VDU terminate in not ready state - Ignoring request")
736 self
._state
= VDURecordState
.TERMINATING
737 if self
._vm
_resp
is not None:
739 with self
._dts
.transaction() as new_xact
:
740 yield from self
.delete_resource(new_xact
)
742 self
._log
.exception("Caught exception while deleting VDU %s", self
.vdu_id
)
744 if self
._rm
_regh
is not None:
745 self
._log
.debug("Deregistering resource manager registration handle")
746 self
._rm
_regh
.deregister()
749 if self
._vdur
_console
_handler
is not None:
750 self
._log
.debug("Deregistering vnfr vdur registration handle")
751 self
._vdur
_console
_handler
._regh
.deregister()
752 self
._vdur
_console
_handler
._regh
= None
754 self
._state
= VDURecordState
.TERMINATED
756 def find_internal_cp_by_cp_id(self
, cp_id
):
757 """ Find the CP corresponding to the connection point id"""
760 self
._log
.debug("find_internal_cp_by_cp_id(%s) called",
763 for int_cp
in self
._vdud
.internal_connection_point
:
764 self
._log
.debug("Checking for int cp %s in internal connection points",
766 if int_cp
.id == cp_id
:
771 self
._log
.debug("Failed to find cp %s in internal connection points",
773 msg
= "Failed to find cp %s in internal connection points" % cp_id
774 raise VduRecordError(msg
)
776 # return the VLR associated with the connection point
780 def create_resource(self
, xact
, vnfr
, config
=None):
781 """ Request resource from ResourceMgr """
782 def find_cp_by_name(cp_name
):
783 """ Find a connection point by name """
785 self
._log
.debug("find_cp_by_name(%s) called", cp_name
)
786 for ext_cp
in vnfr
._cprs
:
787 self
._log
.debug("Checking ext cp (%s) called", ext_cp
.name
)
788 if ext_cp
.name
== cp_name
:
792 self
._log
.debug("Failed to find cp %s in external connection points",
796 def find_internal_vlr_by_cp_name(cp_name
):
797 """ Find the VLR corresponding to the connection point name"""
800 self
._log
.debug("find_internal_vlr_by_cp_name(%s) called",
803 for int_cp
in self
._vdud
.internal_connection_point
:
804 self
._log
.debug("Checking for int cp %s in internal connection points",
806 if int_cp
.id == cp_name
:
811 self
._log
.debug("Failed to find cp %s in internal connection points",
813 msg
= "Failed to find cp %s in internal connection points" % cp_name
814 raise VduRecordError(msg
)
816 # return the VLR associated with the connection point
817 return vnfr
.find_vlr_by_cp(cp_name
)
819 block
= xact
.block_create()
821 self
._log
.debug("Executing vm request id: %s, action: create",
824 # Resolve the networks associated external interfaces
825 for ext_intf
in self
._vdud
.external_interface
:
826 self
._log
.debug("Resolving external interface name [%s], cp[%s]",
827 ext_intf
.name
, ext_intf
.vnfd_connection_point_ref
)
828 cp
= find_cp_by_name(ext_intf
.vnfd_connection_point_ref
)
830 self
._log
.debug("Failed to find connection point - %s",
831 ext_intf
.vnfd_connection_point_ref
)
833 self
._log
.debug("Connection point name [%s], type[%s]",
834 cp
.name
, cp
.type_yang
)
836 vlr
= vnfr
.ext_vlr_by_id(cp
.vlr_ref
)
838 etuple
= (ext_intf
, cp
, vlr
)
839 self
._ext
_intf
.append(etuple
)
841 self
._log
.debug("Created external interface tuple : %s", etuple
)
843 # Resolve the networks associated internal interfaces
844 for intf
in self
._vdud
.internal_interface
:
845 cp_id
= intf
.vdu_internal_connection_point_ref
846 self
._log
.debug("Resolving internal interface name [%s], cp[%s]",
850 vlr
= find_internal_vlr_by_cp_name(cp_id
)
851 except Exception as e
:
852 self
._log
.debug("Failed to find cp %s in internal VLR list", cp_id
)
853 msg
= "Failed to find cp %s in internal VLR list, e = %s" % (cp_id
, e
)
854 raise VduRecordError(msg
)
856 ituple
= (intf
, cp_id
, vlr
)
857 self
._int
_intf
.append(ituple
)
859 self
._log
.debug("Created internal interface tuple : %s", ituple
)
861 resmgr_path
= self
.resmgr_path
862 resmgr_msg
= self
.resmgr_msg(config
)
864 self
._log
.debug("Creating new VM request at: %s, params: %s", resmgr_path
, resmgr_msg
)
865 block
.add_query_create(resmgr_path
, resmgr_msg
)
867 res_iter
= yield from block
.execute(now
=True)
875 if resp
is None or not (resp
.has_field('resource_info') and resp
.resource_info
.has_field('resource_state')):
876 raise VMResourceError("Did not get a vm resource response (resp: %s)", resp
)
877 self
._log
.debug("Got vm request response: %s", resp
.resource_info
)
878 return resp
.resource_info
881 def delete_resource(self
, xact
):
882 block
= xact
.block_create()
884 self
._log
.debug("Executing vm request id: %s, action: delete",
887 block
.add_query_delete(self
.resmgr_path
)
889 yield from block
.execute(flags
=0, now
=True)
892 def read_resource(self
, xact
):
893 block
= xact
.block_create()
895 self
._log
.debug("Executing vm request id: %s, action: delete",
898 block
.add_query_read(self
.resmgr_path
)
900 res_iter
= yield from block
.execute(flags
=0, now
=True)
905 if resp
is None or not (resp
.has_field('resource_info') and resp
.resource_info
.has_field('resource_state')):
906 raise VMResourceError("Did not get a vm resource response (resp: %s)", resp
)
907 self
._log
.debug("Got vm request response: %s", resp
.resource_info
)
908 #self._vm_resp = resp.resource_info
909 return resp
.resource_info
913 def start_component(self
):
914 """ This VDUR is active """
915 self
._log
.debug("Starting component %s for vdud %s vdur %s",
916 self
._vdud
.vcs_component_ref
,
919 yield from self
._vnfr
.start_component(self
._vdud
.vcs_component_ref
,
920 self
.vm_resp
.management_ip
)
924 """ Is this VDU active """
925 return True if self
._state
is VDURecordState
.READY
else False
928 def instantiation_failed(self
, failed_reason
=None):
929 """ VDU instantiation failed """
930 self
._log
.debug("VDU %s instantiation failed ", self
._vdur
_id
)
931 self
._state
= VDURecordState
.FAILED
932 self
._state
_failed
_reason
= failed_reason
933 yield from self
._vnfr
.instantiation_failed(failed_reason
)
936 def vdu_is_active(self
):
937 """ This VDU is active"""
939 self
._log
.warning("VDU %s was already marked as active", self
._vdur
_id
)
942 self
._log
.debug("VDUR id %s in VNFR %s is active", self
._vdur
_id
, self
._vnfr
.vnfr_id
)
944 if self
._vdud
.vcs_component_ref
is not None:
945 yield from self
.start_component()
947 self
._state
= VDURecordState
.READY
949 if self
._vnfr
.all_vdus_active():
950 self
._log
.debug("Inside vdu_is_active. VNFR is READY. Info: %s", self
._vnfr
)
951 yield from self
._vnfr
.is_ready()
954 def instantiate(self
, xact
, vnfr
, config
=None):
955 """ Instantiate this VDU """
956 self
._state
= VDURecordState
.INSTANTIATING
959 def on_prepare(xact_info
, query_action
, ks_path
, msg
):
960 """ This VDUR is active """
961 self
._log
.debug("Received VDUR instantiate on_prepare (%s:%s:%s)",
966 if (query_action
== rwdts
.QueryAction
.UPDATE
or
967 query_action
== rwdts
.QueryAction
.CREATE
):
970 if msg
.resource_state
== "active":
971 # Move this VDU to ready state
972 yield from self
.vdu_is_active()
973 elif msg
.resource_state
== "failed":
974 yield from self
.instantiation_failed(msg
.resource_errors
)
975 elif query_action
== rwdts
.QueryAction
.DELETE
:
976 self
._log
.debug("DELETE action in on_prepare for VDUR instantiation, ignoring")
978 raise NotImplementedError(
979 "%s action on VirtualDeployementUnitRecord not supported",
982 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
985 reg_event
= asyncio
.Event(loop
=self
._loop
)
988 def on_ready(regh
, status
):
991 handler
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
, on_ready
=on_ready
)
992 self
._rm
_regh
= yield from self
._dts
.register(self
.resmgr_path
+ '/resource-info',
993 flags
=rwdts
.Flag
.SUBSCRIBER
,
995 yield from reg_event
.wait()
997 vm_resp
= yield from self
.create_resource(xact
, vnfr
, config
)
998 self
._vm
_resp
= vm_resp
999 self
._state
= VDURecordState
.RESOURCE_ALLOC_PENDING
1001 self
._log
.debug("Requested VM from resource manager response %s",
1003 if vm_resp
.resource_state
== "active":
1004 self
._log
.debug("Resourcemgr responded wih an active vm resp %s",
1006 yield from self
.vdu_is_active()
1007 self
._state
= VDURecordState
.READY
1008 elif (vm_resp
.resource_state
== "pending" or
1009 vm_resp
.resource_state
== "inactive"):
1010 self
._log
.debug("Resourcemgr responded wih a pending vm resp %s",
1012 # handler = rift.tasklets.DTS.RegistrationHandler(on_prepare=on_prepare)
1013 # self._rm_regh = yield from self._dts.register(self.resmgr_path + '/resource-info',
1014 # flags=rwdts.Flag.SUBSCRIBER,
1017 self
._log
.debug("Resourcemgr responded wih an error vm resp %s",
1019 raise VirtualDeploymentUnitRecordError(
1020 "Failed VDUR instantiation %s " % vm_resp
)
1022 except Exception as e
:
1024 traceback
.print_exc()
1025 self
._log
.exception(e
)
1026 self
._log
.error("Instantiation of VDU record failed: %s", str(e
))
1027 self
._state
= VDURecordState
.FAILED
1028 yield from self
.instantiation_failed(str(e
))
1031 class VlRecordState(enum
.Enum
):
1032 """ VL Record State """
1034 INSTANTIATION_PENDING
= 102
1036 TERMINATE_PENDING
= 104
1041 class InternalVirtualLinkRecord(object):
1042 """ Internal Virtual Link record """
1043 def __init__(self
, dts
, log
, loop
, project
,
1044 ivld_msg
, vnfr_name
, cloud_account_name
, ip_profile
=None):
1048 self
._project
= project
1049 self
._ivld
_msg
= ivld_msg
1050 self
._vnfr
_name
= vnfr_name
1051 self
._cloud
_account
_name
= cloud_account_name
1052 self
._ip
_profile
= ip_profile
1054 self
._vlr
_req
= self
.create_vlr()
1056 self
._state
= VlRecordState
.INIT
1060 """ Find VLR by id """
1061 return self
._vlr
_req
.id
1065 """ Name of this VL """
1066 if self
._ivld
_msg
.vim_network_name
:
1067 return self
._ivld
_msg
.vim_network_name
1069 return self
._vnfr
_name
+ "." + self
._ivld
_msg
.name
1072 def network_id(self
):
1073 """ Find VLR by id """
1074 return self
._vlr
.network_id
if self
._vlr
else None
1077 """ VLR path for this VLR instance"""
1078 return self
._project
.add_project("D,/vlr:vlr-catalog/vlr:vlr[vlr:id = '{}']".
1079 format(self
.vlr_id
))
1081 def create_vlr(self
):
1082 """ Create the VLR record which will be instantiated """
1084 vld_fields
= ["short_name",
1092 vld_copy_dict
= {k
: v
for k
, v
in self
._ivld
_msg
.as_dict().items() if k
in vld_fields
}
1094 vlr_dict
= {"id": str(uuid
.uuid4()),
1096 "cloud_account": self
._cloud
_account
_name
,
1099 if self
._ip
_profile
and self
._ip
_profile
.has_field('ip_profile_params'):
1100 vlr_dict
['ip_profile_params' ] = self
._ip
_profile
.ip_profile_params
.as_dict()
1102 vlr_dict
.update(vld_copy_dict
)
1104 vlr
= RwVlrYang
.YangData_RwProject_Project_VlrCatalog_Vlr
.from_dict(vlr_dict
)
1108 def instantiate(self
, xact
, restart_mode
=False):
1109 """ Instantiate VL """
1112 def instantiate_vlr():
1113 """ Instantiate VLR"""
1114 self
._log
.debug("Create VL with xpath %s and vlr %s",
1115 self
.vlr_path(), self
._vlr
_req
)
1117 with self
._dts
.transaction(flags
=0) as xact
:
1118 block
= xact
.block_create()
1119 block
.add_query_create(xpath
=self
.vlr_path(), msg
=self
._vlr
_req
)
1120 self
._log
.debug("Executing VL create path:%s msg:%s",
1121 self
.vlr_path(), self
._vlr
_req
)
1125 res_iter
= yield from block
.execute()
1127 self
._state
= VlRecordState
.FAILED
1128 self
._log
.exception("Caught exception while instantial VL")
1131 for ent
in res_iter
:
1132 res
= yield from ent
1133 self
._vlr
= res
.result
1135 if self
._vlr
.operational_status
== 'failed':
1136 self
._log
.debug("VL creation failed for vlr id %s", self
._vlr
.id)
1137 self
._state
= VlRecordState
.FAILED
1138 raise VnfrInstantiationFailed("instantiation due to VL failure %s" % (self
._vlr
.id))
1140 self
._log
.info("Created VL with xpath %s and vlr %s",
1141 self
.vlr_path(), self
._vlr
)
1145 """ Get the network id """
1146 res_iter
= yield from self
._dts
.query_read(self
.vlr_path(), rwdts
.XactFlag
.MERGE
)
1148 for ent
in res_iter
:
1149 res
= yield from ent
1153 err
= "Failed to get VLR for path %s" % self
.vlr_path()
1155 raise InternalVirtualLinkRecordError(err
)
1158 self
._state
= VlRecordState
.INSTANTIATION_PENDING
1161 vl
= yield from get_vlr()
1163 yield from instantiate_vlr()
1165 yield from instantiate_vlr()
1167 self
._state
= VlRecordState
.ACTIVE
1169 def vlr_in_vns(self
):
1170 """ Is there a VLR record in VNS """
1171 if (self
._state
== VlRecordState
.ACTIVE
or
1172 self
._state
== VlRecordState
.INSTANTIATION_PENDING
or
1173 self
._state
== VlRecordState
.FAILED
):
1179 def terminate(self
, xact
):
1180 """Terminate this VL """
1181 if not self
.vlr_in_vns():
1182 self
._log
.debug("Ignoring terminate request for id %s in state %s",
1183 self
.vlr_id
, self
._state
)
1186 self
._log
.debug("Terminating VL with path %s", self
.vlr_path())
1187 self
._state
= VlRecordState
.TERMINATE_PENDING
1188 block
= xact
.block_create()
1189 block
.add_query_delete(self
.vlr_path())
1190 yield from block
.execute(flags
=0, now
=True)
1191 self
._state
= VlRecordState
.TERMINATED
1192 self
._log
.debug("Terminated VL with path %s", self
.vlr_path())
1195 class VirtualNetworkFunctionRecord(object):
1196 """ Virtual Network Function Record """
1197 def __init__(self
, dts
, log
, loop
, cluster_name
, vnfm
, vcs_handler
, vnfr_msg
, mgmt_network
=None):
1201 self
._project
= vnfm
._project
1202 self
._cluster
_name
= cluster_name
1203 self
._vnfr
_msg
= vnfr_msg
1204 self
._vnfr
_id
= vnfr_msg
.id
1205 self
._vnfd
_id
= vnfr_msg
.vnfd
.id
1207 self
._vcs
_handler
= vcs_handler
1208 self
._vnfr
= vnfr_msg
1209 self
._mgmt
_network
= mgmt_network
1211 self
._vnfd
= vnfr_msg
.vnfd
1212 self
._state
= VirtualNetworkFunctionRecordState
.INIT
1213 self
._state
_failed
_reason
= None
1214 self
._ext
_vlrs
= {} # The list of external virtual links
1215 self
._vlrs
= [] # The list of internal virtual links
1216 self
._vdus
= [] # The list of vdu
1217 self
._vlr
_by
_cp
= {}
1219 self
._inventory
= {}
1220 self
._create
_time
= int(time
.time())
1221 self
._vnf
_mon
= None
1222 self
._config
_status
= vnfr_msg
.config_status
1223 self
._vnfd
_package
_store
= rift
.package
.store
.VnfdPackageFilesystemStore(self
._log
)
1224 self
._rw
_vnfd
= None
1225 self
._vnfd
_ref
_count
= 0
1227 def _get_vdur_from_vdu_id(self
, vdu_id
):
1228 self
._log
.debug("Finding vdur for vdu_id %s", vdu_id
)
1229 self
._log
.debug("Searching through vdus: %s", self
._vdus
)
1230 for vdu
in self
._vdus
:
1231 self
._log
.debug("vdu_id: %s", vdu
.vdu_id
)
1232 if vdu
.vdu_id
== vdu_id
:
1235 raise VDURecordNotFound("Could not find vdu record from id: %s", vdu_id
)
1238 def operational_status(self
):
1239 """ Operational status of this VNFR """
1240 op_status_map
= {"INIT": "init",
1241 "VL_INIT_PHASE": "vl_init_phase",
1242 "VM_INIT_PHASE": "vm_init_phase",
1244 "TERMINATE": "terminate",
1245 "VL_TERMINATE_PHASE": "vl_terminate_phase",
1246 "VDU_TERMINATE_PHASE": "vm_terminate_phase",
1247 "TERMINATED": "terminated",
1248 "FAILED": "failed", }
1249 return op_status_map
[self
._state
.name
]
1252 def vnfd_xpath(vnfd_id
):
1253 """ VNFD xpath associated with this VNFR """
1254 return ("C,/project-vnfd:vnfd-catalog/project-vnfd:vnfd[project-vnfd:id = '{}']".
1258 def vnfd_ref_count(self
):
1259 """ Returns the VNFD reference count associated with this VNFR """
1260 return self
._vnfd
_ref
_count
1262 def vnfd_in_use(self
):
1263 """ Returns whether vnfd is in use or not """
1264 return True if self
._vnfd
_ref
_count
> 0 else False
1267 """ Take a reference on this object """
1268 self
._vnfd
_ref
_count
+= 1
1269 return self
._vnfd
_ref
_count
1271 def vnfd_unref(self
):
1272 """ Release reference on this object """
1273 if self
._vnfd
_ref
_count
< 1:
1274 msg
= ("Unref on a VNFD object - vnfd id %s, vnfd_ref_count = %s" %
1275 (self
.vnfd
.id, self
._vnfd
_ref
_count
))
1276 self
._log
.critical(msg
)
1277 raise VnfRecordError(msg
)
1278 self
._log
.debug("Releasing ref on VNFD %s - curr vnfd_ref_count:%s",
1279 self
.vnfd
.id, self
._vnfd
_ref
_count
)
1280 self
._vnfd
_ref
_count
-= 1
1281 return self
._vnfd
_ref
_count
1285 """ VNFD for this VNFR """
1290 """ VNFD name associated with this VNFR """
1291 return self
.vnfd
.name
1295 """ Name of this VNF in the record """
1296 return self
._vnfr
.name
1299 def cloud_account_name(self
):
1300 """ Name of the cloud account this VNFR is instantiated in """
1301 return self
._vnfr
.cloud_account
1305 """ VNFD Id associated with this VNFR """
1310 """ VNFR Id associated with this VNFR """
1311 return self
._vnfr
_id
1314 def member_vnf_index(self
):
1315 """ Member VNF index associated with this VNFR """
1316 return self
._vnfr
.member_vnf_index_ref
1319 def config_status(self
):
1320 """ Config agent status for this VNFR """
1321 return self
._config
_status
1323 def component_by_name(self
, component_name
):
1324 """ Find a component by name in the inventory list"""
1325 mangled_name
= VcsComponent
.mangle_name(component_name
,
1328 return self
._inventory
[mangled_name
]
1333 def get_nsr_config(self
):
1334 ### Need access to NS instance configuration for runtime resolution.
1335 ### This shall be replaced when deployment flavors are implemented
1336 xpath
= self
._project
.add_project("C,/nsr:ns-instance-config")
1337 results
= yield from self
._dts
.query_read(xpath
, rwdts
.XactFlag
.MERGE
)
1339 for result
in results
:
1340 entry
= yield from result
1341 ns_instance_config
= entry
.result
1342 for nsr
in ns_instance_config
.nsr
:
1343 if nsr
.id == self
._vnfr
_msg
.nsr_id_ref
:
1348 def start_component(self
, component_name
, ip_addr
):
1349 """ Start a component in the VNFR by name """
1350 comp
= self
.component_by_name(component_name
)
1351 yield from comp
.start(None, None, ip_addr
)
1353 def cp_ip_addr(self
, cp_name
):
1354 """ Get ip address for connection point """
1355 self
._log
.debug("cp_ip_addr()")
1356 for cp
in self
._cprs
:
1357 if cp
.name
== cp_name
and cp
.ip_address
is not None:
1358 return cp
.ip_address
1361 def mgmt_intf_info(self
):
1362 """ Get Management interface info for this VNFR """
1363 mgmt_intf_desc
= self
.vnfd
.mgmt_interface
1365 if mgmt_intf_desc
.has_field("cp"):
1366 ip_addr
= self
.cp_ip_addr(mgmt_intf_desc
.cp
)
1367 elif mgmt_intf_desc
.has_field("vdu_id"):
1369 vdur
= self
._get
_vdur
_from
_vdu
_id
(mgmt_intf_desc
.vdu_id
)
1370 ip_addr
= vdur
.management_ip
1371 except VDURecordNotFound
:
1372 self
._log
.debug("Did not find mgmt interface for vnfr id %s", self
._vnfr
_id
)
1375 ip_addr
= mgmt_intf_desc
.ip_address
1376 port
= mgmt_intf_desc
.port
1378 return ip_addr
, port
1382 """ Message associated with this VNFR """
1383 vnfd_fields
= ["short_name", "vendor", "description", "version"]
1384 vnfd_copy_dict
= {k
: v
for k
, v
in self
.vnfd
.as_dict().items() if k
in vnfd_fields
}
1386 mgmt_intf
= VnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr_MgmtInterface()
1387 ip_address
, port
= self
.mgmt_intf_info()
1389 if ip_address
is not None:
1390 mgmt_intf
.ip_address
= ip_address
1391 if port
is not None:
1392 mgmt_intf
.port
= port
1394 vnfr_dict
= {"id": self
._vnfr
_id
,
1395 "nsr_id_ref": self
._vnfr
_msg
.nsr_id_ref
,
1397 "member_vnf_index_ref": self
.member_vnf_index
,
1398 "operational_status": self
.operational_status
,
1399 "operational_status_details": self
._state
_failed
_reason
,
1400 "cloud_account": self
.cloud_account_name
,
1401 "config_status": self
._config
_status
1404 vnfr_dict
.update(vnfd_copy_dict
)
1406 vnfr_msg
= RwVnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr
.from_dict(vnfr_dict
)
1407 vnfr_msg
.vnfd
= VnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr_Vnfd
.from_dict(self
.vnfd
.as_dict())
1409 vnfr_msg
.create_time
= self
._create
_time
1410 vnfr_msg
.uptime
= int(time
.time()) - self
._create
_time
1411 vnfr_msg
.mgmt_interface
= mgmt_intf
1413 # Add all the VLRs to VNFR
1414 for vlr
in self
._vlrs
:
1415 ivlr
= vnfr_msg
.internal_vlr
.add()
1416 ivlr
.vlr_ref
= vlr
.vlr_id
1418 # Add all the VDURs to VDUR
1419 if self
._vdus
is not None:
1420 for vdu
in self
._vdus
:
1421 vdur
= vnfr_msg
.vdur
.add()
1422 vdur
.from_dict(vdu
.msg
.as_dict())
1424 if self
.vnfd
.mgmt_interface
.has_field('dashboard_params'):
1425 vnfr_msg
.dashboard_url
= self
.dashboard_url
1427 for cpr
in self
._cprs
:
1428 new_cp
= VnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr_ConnectionPoint
.from_dict(cpr
.as_dict())
1429 vnfr_msg
.connection_point
.append(new_cp
)
1431 if self
._vnf
_mon
is not None:
1432 for monp
in self
._vnf
_mon
.msg
:
1433 vnfr_msg
.monitoring_param
.append(
1434 VnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr_MonitoringParam
.from_dict(monp
.as_dict()))
1436 if self
._vnfr
.vnf_configuration
is not None:
1437 vnfr_msg
.vnf_configuration
.from_dict(self
._vnfr
.vnf_configuration
.as_dict())
1438 if (ip_address
is not None and
1439 vnfr_msg
.vnf_configuration
.config_access
.mgmt_ip_address
is None):
1440 vnfr_msg
.vnf_configuration
.config_access
.mgmt_ip_address
= ip_address
1442 for group
in self
._vnfr
_msg
.placement_groups_info
:
1443 group_info
= VnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr_PlacementGroupsInfo()
1444 group_info
.from_dict(group
.as_dict())
1445 vnfr_msg
.placement_groups_info
.append(group_info
)
1450 def dashboard_url(self
):
1451 ip
, cfg_port
= self
.mgmt_intf_info()
1454 if self
.vnfd
.mgmt_interface
.dashboard_params
.has_field('https'):
1455 if self
.vnfd
.mgmt_interface
.dashboard_params
.https
is True:
1458 if self
.vnfd
.mgmt_interface
.dashboard_params
.has_field('port'):
1459 http_port
= self
.vnfd
.mgmt_interface
.dashboard_params
.port
1461 url
= "{protocol}://{ip_address}:{port}/{path}".format(
1465 path
=self
.vnfd
.mgmt_interface
.dashboard_params
.path
.lstrip("/"),
1472 """ path for this VNFR """
1473 return self
._project
.add_project("D,/vnfr:vnfr-catalog"
1474 "/vnfr:vnfr[vnfr:id='{}']".format(self
.vnfr_id
))
1477 def publish(self
, xact
):
1478 """ publish this VNFR """
1480 self
._log
.debug("Publishing VNFR path = [%s], record = [%s]",
1481 self
.xpath
, self
.msg
)
1482 vnfr
.create_time
= self
._create
_time
1483 yield from self
._vnfm
.publish_vnfr(xact
, self
.xpath
, self
.msg
)
1484 self
._log
.debug("Published VNFR path = [%s], record = [%s]",
1485 self
.xpath
, self
.msg
)
1487 def resolve_vld_ip_profile(self
, vnfd_msg
, vld
):
1488 self
._log
.debug("Receieved ip profile ref is %s",vld
.ip_profile_ref
)
1489 if not vld
.has_field('ip_profile_ref'):
1491 profile
= [profile
for profile
in vnfd_msg
.ip_profiles
if profile
.name
== vld
.ip_profile_ref
]
1492 return profile
[0] if profile
else None
1495 def create_vls(self
):
1496 """ Publish The VLs associated with this VNF """
1497 self
._log
.debug("Publishing Internal Virtual Links for vnfd id: %s",
1499 for ivld_msg
in self
.vnfd
.internal_vld
:
1500 self
._log
.debug("Creating internal vld:"
1501 " %s, int_cp_ref = %s",
1502 ivld_msg
, ivld_msg
.internal_connection_point
1504 vlr
= InternalVirtualLinkRecord(dts
=self
._dts
,
1508 vnfr_name
=self
.name
,
1509 cloud_account_name
=self
.cloud_account_name
,
1510 ip_profile
=self
.resolve_vld_ip_profile(self
.vnfd
, ivld_msg
)
1512 self
._vlrs
.append(vlr
)
1514 for int_cp
in ivld_msg
.internal_connection_point
:
1515 if int_cp
.id_ref
in self
._vlr
_by
_cp
:
1516 msg
= ("Connection point %s already "
1517 " bound %s" % (int_cp
.id_ref
, self
._vlr
_by
_cp
[int_cp
.id_ref
]))
1518 raise InternalVirtualLinkRecordError(msg
)
1519 self
._log
.debug("Setting vlr %s to internal cp = %s",
1521 self
._vlr
_by
_cp
[int_cp
.id_ref
] = vlr
1524 def instantiate_vls(self
, xact
, restart_mode
=False):
1525 """ Instantiate the VLs associated with this VNF """
1526 self
._log
.debug("Instantiating Internal Virtual Links for vnfd id: %s",
1529 for vlr
in self
._vlrs
:
1530 self
._log
.debug("Instantiating VLR %s", vlr
)
1531 yield from vlr
.instantiate(xact
, restart_mode
)
1533 def find_vlr_by_cp(self
, cp_name
):
1534 """ Find the VLR associated with the cp name """
1535 return self
._vlr
_by
_cp
[cp_name
]
1537 def resolve_placement_group_cloud_construct(self
, input_group
, nsr_config
):
1539 Returns the cloud specific construct for placement group
1541 input_group: VNFD PlacementGroup
1542 nsr_config: Configuration for VNFDGroup MAP in the NSR config
1544 copy_dict
= ['name', 'requirement', 'strategy']
1545 for group_info
in nsr_config
.vnfd_placement_group_maps
:
1546 if group_info
.placement_group_ref
== input_group
.name
and \
1547 group_info
.vnfd_id_ref
== self
.vnfd_id
:
1548 group
= VnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr_Vdur_PlacementGroupsInfo()
1549 group_dict
= {k
:v
for k
,v
in
1550 group_info
.as_dict().items()
1551 if (k
!= 'placement_group_ref' and k
!='vnfd_id_ref')}
1552 for param
in copy_dict
:
1553 group_dict
.update({param
: getattr(input_group
, param
)})
1554 group
.from_dict(group_dict
)
1559 def get_vdu_placement_groups(self
, vdu
, nsr_config
):
1560 placement_groups
= []
1561 ### Step-1: Get VNF level placement groups
1562 for group
in self
._vnfr
_msg
.placement_groups_info
:
1563 #group_info = VnfrYang.YangData_RwProject_Project_VnfrCatalog_Vnfr_Vdur_PlacementGroupsInfo()
1564 #group_info.from_dict(group.as_dict())
1565 placement_groups
.append(group
)
1567 ### Step-2: Get VDU level placement groups
1568 for group
in self
.vnfd
.placement_groups
:
1569 for member_vdu
in group
.member_vdus
:
1570 if member_vdu
.member_vdu_ref
== vdu
.id:
1571 group_info
= self
.resolve_placement_group_cloud_construct(group
,
1573 if group_info
is None:
1574 self
._log
.info("Could not resolve cloud-construct for " +
1575 "placement group: %s", group
.name
)
1577 self
._log
.info("Successfully resolved cloud construct for " +
1578 "placement group: %s for VDU: %s in VNF: %s (Member Index: %s)",
1582 self
.member_vnf_index
)
1583 placement_groups
.append(group_info
)
1585 return placement_groups
1588 def vdu_cloud_init_instantiation(self
):
1589 [vdu
.vdud_cloud_init
for vdu
in self
._vdus
]
1592 def create_vdus(self
, vnfr
, restart_mode
=False):
1593 """ Create the VDUs associated with this VNF """
1595 def get_vdur_id(vdud
):
1596 """Get the corresponding VDUR's id for the VDUD. This is useful in
1599 In restart mode we check for exiting VDUR's ID and use them, if
1600 available. This way we don't end up creating duplicate VDURs
1604 if restart_mode
and vdud
is not None:
1606 vdur
= [vdur
.id for vdur
in vnfr
._vnfr
.vdur
if vdur
.vdu_id_ref
== vdud
.id]
1609 self
._log
.error("Unable to find a VDUR for VDUD {}".format(vdud
))
1614 self
._log
.info("Creating VDU's for vnfd id: %s", self
.vnfd_id
)
1616 # Get NSR config - Needed for placement groups and to derive VDU short-name
1617 nsr_config
= yield from self
.get_nsr_config()
1619 for vdu
in self
._rw
_vnfd
.vdu
:
1620 self
._log
.debug("Creating vdu: %s", vdu
)
1621 vdur_id
= get_vdur_id(vdu
)
1624 placement_groups
= yield from self
.get_vdu_placement_groups(vdu
, nsr_config
)
1625 self
._log
.info("Launching VDU: %s from VNFD :%s (Member Index: %s) with Placement Groups: %s, Existing vdur_id %s",
1628 self
.member_vnf_index
,
1629 [ group
.name
for group
in placement_groups
],
1632 vdur
= VirtualDeploymentUnitRecord(
1636 project
= self
._project
,
1639 nsr_config
=nsr_config
,
1640 mgmt_intf
=self
.has_mgmt_interface(vdu
),
1641 mgmt_network
=self
._mgmt
_network
,
1642 cloud_account_name
=self
.cloud_account_name
,
1643 vnfd_package_store
=self
._vnfd
_package
_store
,
1645 placement_groups
= placement_groups
,
1647 yield from vdur
.vdu_opdata_register()
1649 self
._vdus
.append(vdur
)
1652 def instantiate_vdus(self
, xact
, vnfr
):
1653 """ Instantiate the VDUs associated with this VNF """
1654 self
._log
.debug("Instantiating VDU's for vnfd id %s: %s", self
.vnfd_id
, self
._vdus
)
1656 lookup
= {vdu
.vdu_id
: vdu
for vdu
in self
._vdus
}
1658 # Identify any dependencies among the VDUs
1659 dependencies
= collections
.defaultdict(list)
1660 vdu_id_pattern
= re
.compile(r
"\{\{ vdu\[([^]]+)\]\S* \}\}")
1662 for vdu
in self
._vdus
:
1663 if vdu
._vdud
_cloud
_init
is not None:
1664 for vdu_id
in vdu_id_pattern
.findall(vdu
._vdud
_cloud
_init
):
1665 if vdu_id
!= vdu
.vdu_id
:
1666 # This means that vdu.vdu_id depends upon vdu_id,
1667 # i.e. vdu_id must be instantiated before
1669 dependencies
[vdu
.vdu_id
].append(lookup
[vdu_id
])
1671 # Define the terminal states of VDU instantiation
1673 VDURecordState
.READY
,
1674 VDURecordState
.TERMINATED
,
1675 VDURecordState
.FAILED
,
1678 datastore
= VdurDatastore()
1682 def instantiate_monitor(vdu
):
1683 """Monitor the state of the VDU during instantiation
1686 vdu - a VirtualDeploymentUnitRecord
1689 # wait for the VDUR to enter a terminal state
1690 while vdu
._state
not in terminal
:
1691 yield from asyncio
.sleep(1, loop
=self
._loop
)
1692 # update the datastore
1693 datastore
.update(vdu
)
1695 # add the VDU to the set of processed VDUs
1696 processed
.add(vdu
.vdu_id
)
1699 def instantiate(vdu
):
1700 """Instantiate the specified VDU
1703 vdu - a VirtualDeploymentUnitRecord
1706 if the VDU, or any of the VDUs this VDU depends upon, are
1707 terminated or fail to instantiate properly, a
1708 VirtualDeploymentUnitRecordError is raised.
1711 for dependency
in dependencies
[vdu
.vdu_id
]:
1712 self
._log
.debug("{}: waiting for {}".format(vdu
.vdu_id
, dependency
.vdu_id
))
1714 while dependency
.vdu_id
not in processed
:
1715 yield from asyncio
.sleep(1, loop
=self
._loop
)
1717 if not dependency
.active
:
1718 raise VirtualDeploymentUnitRecordError()
1720 self
._log
.debug('instantiating {}'.format(vdu
.vdu_id
))
1722 # Populate the datastore with the current values of the VDU
1725 # Substitute any variables contained in the cloud config script
1726 config
= str(vdu
.vdud_cloud_init
) if vdu
.vdud_cloud_init
is not None else ""
1728 parts
= re
.split("\{\{ ([^\}]+) \}\}", config
)
1731 # Extract the variable names
1733 for variable
in parts
[1::2]:
1734 variables
.append(variable
.lstrip('{{').rstrip('}}').strip())
1736 # Iterate of the variables and substitute values from the
1738 for variable
in variables
:
1740 # Handle a reference to a VDU by ID
1741 if variable
.startswith('vdu['):
1742 value
= datastore
.get(variable
)
1744 msg
= "Unable to find a substitute for {} in {} cloud-init script"
1745 raise ValueError(msg
.format(variable
, vdu
.vdu_id
))
1747 config
= config
.replace("{{ %s }}" % variable
, value
)
1750 # Handle a reference to the current VDU
1751 if variable
.startswith('vdu'):
1752 value
= datastore
.get('vdu[{}]'.format(vdu
.vdu_id
) + variable
[3:])
1753 config
= config
.replace("{{ %s }}" % variable
, value
)
1756 # Handle unrecognized variables
1757 msg
= 'unrecognized cloud-config variable: {}'
1758 raise ValueError(msg
.format(variable
))
1760 # Instantiate the VDU
1761 with self
._dts
.transaction() as xact
:
1762 self
._log
.debug("Instantiating vdu: %s", vdu
)
1763 yield from vdu
.instantiate(xact
, vnfr
, config
=config
)
1764 if self
._state
== VirtualNetworkFunctionRecordState
.FAILED
:
1765 self
._log
.error("Instatiation of VNF %s failed while instantiating vdu %s",
1768 # First create a set of tasks to monitor the state of the VDUs and
1769 # report when they have entered a terminal state
1770 for vdu
in self
._vdus
:
1771 self
._loop
.create_task(instantiate_monitor(vdu
))
1773 for vdu
in self
._vdus
:
1774 self
._loop
.create_task(instantiate(vdu
))
1776 def has_mgmt_interface(self
, vdu
):
1777 # ## TODO: Support additional mgmt_interface type options
1778 if self
.vnfd
.mgmt_interface
.vdu_id
== vdu
.id:
1782 def vlr_xpath(self
, vlr_id
):
1784 return self
._project
.add_project("D,/vlr:vlr-catalog/"
1785 "vlr:vlr[vlr:id = '{}']".format(vlr_id
))
1787 def ext_vlr_by_id(self
, vlr_id
):
1788 """ find ext vlr by id """
1789 return self
._ext
_vlrs
[vlr_id
]
1792 def publish_inventory(self
, xact
):
1793 """ Publish the inventory associated with this VNF """
1794 self
._log
.debug("Publishing inventory for VNFR id: %s", self
._vnfr
_id
)
1796 for component
in self
._rw
_vnfd
.component
:
1797 self
._log
.debug("Creating inventory component %s", component
)
1798 mangled_name
= VcsComponent
.mangle_name(component
.component_name
,
1802 comp
= VcsComponent(dts
=self
._dts
,
1805 cluster_name
=self
._cluster
_name
,
1806 vcs_handler
=self
._vcs
_handler
,
1807 component
=component
,
1808 mangled_name
=mangled_name
,
1810 if comp
.name
in self
._inventory
:
1811 self
._log
.debug("Duplicate entries in inventory %s for vnfr %s",
1812 component
, self
._vnfd
_id
)
1814 self
._log
.debug("Adding component %s for vnrf %s",
1815 comp
.name
, self
._vnfr
_id
)
1816 self
._inventory
[comp
.name
] = comp
1817 yield from comp
.publish(xact
)
1819 def all_vdus_active(self
):
1820 """ Are all VDUS in this VNFR active? """
1821 for vdu
in self
._vdus
:
1825 self
._log
.debug("Inside all_vdus_active. Returning True")
1829 def instantiation_failed(self
, failed_reason
=None):
1830 """ VNFR instantiation failed """
1831 self
._log
.debug("VNFR %s instantiation failed ", self
.vnfr_id
)
1832 self
.set_state(VirtualNetworkFunctionRecordState
.FAILED
)
1833 self
._state
_failed
_reason
= failed_reason
1835 # Update the VNFR with the changed status
1836 yield from self
.publish(None)
1840 """ This VNF is ready"""
1841 self
._log
.debug("VNFR id %s is ready", self
.vnfr_id
)
1843 if self
._state
!= VirtualNetworkFunctionRecordState
.FAILED
:
1844 self
.set_state(VirtualNetworkFunctionRecordState
.READY
)
1847 self
._log
.debug("VNFR id %s ignoring state change", self
.vnfr_id
)
1849 # Update the VNFR with the changed status
1850 yield from self
.publish(None)
1852 def update_cp(self
, cp_name
, ip_address
, mac_addr
, cp_id
):
1853 """Updated the connection point with ip address"""
1854 for cp
in self
._cprs
:
1855 if cp
.name
== cp_name
:
1856 self
._log
.debug("Setting ip address and id for cp %s, cpr %s with ip %s id %s",
1857 cp_name
, cp
, ip_address
, cp_id
)
1858 cp
.ip_address
= ip_address
1859 cp
.mac_address
= mac_addr
1860 cp
.connection_point_id
= cp_id
1863 err
= "No connection point %s found in VNFR id %s" % (cp
.name
, self
._vnfr
_id
)
1864 self
._log
.debug(err
)
1865 raise VirtualDeploymentUnitRecordError(err
)
1867 def set_state(self
, state
):
1868 """ Set state for this VNFR"""
1872 def instantiate(self
, xact
, restart_mode
=False):
1873 """ instantiate this VNF """
1874 self
._log
.info("Instantiate VNF {}: {}".format(self
._vnfr
_id
, self
._state
))
1875 self
.set_state(VirtualNetworkFunctionRecordState
.VL_INIT_PHASE
)
1876 self
._rw
_vnfd
= yield from self
._vnfm
.fetch_vnfd(self
._vnfd
_id
)
1881 # Iterate over all the connection points in VNFR and fetch the
1884 def cpr_from_cp(cp
):
1885 """ Creates a record level connection point from the desciptor cp"""
1886 cp_fields
= ["name", "image", "vm-flavor", "port_security_enabled"]
1887 cp_copy_dict
= {k
: v
for k
, v
in cp
.as_dict().items() if k
in cp_fields
}
1889 cpr_dict
.update(cp_copy_dict
)
1890 return VnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr_ConnectionPoint
.from_dict(cpr_dict
)
1892 self
._log
.debug("Fetching VLRs for VNFR id = %s, cps = %s",
1893 self
._vnfr
_id
, self
._vnfr
.connection_point
)
1895 for cp
in self
._vnfr
.connection_point
:
1896 cpr
= cpr_from_cp(cp
)
1897 self
._cprs
.append(cpr
)
1898 self
._log
.debug("Adding Connection point record %s ", cp
)
1900 vlr_path
= self
.vlr_xpath(cp
.vlr_ref
)
1901 self
._log
.debug("Fetching VLR with path = %s", vlr_path
)
1902 res_iter
= yield from self
._dts
.query_read(vlr_path
,
1903 rwdts
.XactFlag
.MERGE
)
1907 self
._ext
_vlrs
[cp
.vlr_ref
] = d
1908 cpr
.vlr_ref
= cp
.vlr_ref
1909 self
._log
.debug("Fetched VLR [%s] with path = [%s]", d
, vlr_path
)
1911 # Increase the VNFD reference count
1916 # Fetch External VLRs
1917 self
._log
.debug("VNFR-ID %s: Fetching vlrs", self
._vnfr
_id
)
1918 yield from fetch_vlrs()
1921 self
._log
.debug("VNFR-ID %s: Publishing Inventory", self
._vnfr
_id
)
1922 yield from self
.publish_inventory(xact
)
1925 self
._log
.debug("Create VLs {}: {}".format(self
._vnfr
_id
, self
._state
))
1926 yield from self
.create_vls()
1929 self
._log
.debug("Publish VNFR {}: {}".format(self
._vnfr
_id
, self
._state
))
1930 yield from self
.publish(xact
)
1934 self
._log
.debug("VNFR-ID %s: Instantiate VLs, restart mode %s", self
._vnfr
_id
, restart_mode
)
1936 yield from self
.instantiate_vls(xact
, restart_mode
)
1937 except Exception as e
:
1938 self
._log
.exception("VL instantiation failed (%s)", str(e
))
1939 yield from self
.instantiation_failed(str(e
))
1942 self
.set_state(VirtualNetworkFunctionRecordState
.VM_INIT_PHASE
)
1945 self
._log
.debug("VNFR-ID %s: Create VDUs, restart mode %s", self
._vnfr
_id
, restart_mode
)
1946 yield from self
.create_vdus(self
, restart_mode
)
1949 yield from self
.vdu_cloud_init_instantiation()
1950 except Exception as e
:
1951 self
.set_state(VirtualNetworkFunctionRecordState
.FAILED
)
1952 self
._state
_failed
_reason
= str(e
)
1953 yield from self
.publish(xact
)
1956 self
._log
.debug("VNFR {}: Publish VNFR with state {}".
1957 format(self
._vnfr
_id
, self
._state
))
1958 yield from self
.publish(xact
)
1961 # ToDo: Check if this should be prevented during restart
1962 self
._log
.debug("Instantiate VDUs {}: {}".format(self
._vnfr
_id
, self
._state
))
1963 _
= self
._loop
.create_task(self
.instantiate_vdus(xact
, self
))
1966 self
._log
.debug("VNFR-ID %s: Publish VNFR", self
._vnfr
_id
)
1967 yield from self
.publish(xact
)
1969 self
._log
.debug("VNFR-ID %s: Instantiation Done", self
._vnfr
_id
)
1971 # create task updating uptime for this vnfr
1972 self
._log
.debug("VNFR-ID %s: Starting task to update uptime", self
._vnfr
_id
)
1973 self
._loop
.create_task(self
.vnfr_uptime_update(xact
))
1976 def terminate(self
, xact
):
1977 """ Terminate this virtual network function """
1979 self
._log
.debug("Terminatng VNF id %s", self
.vnfr_id
)
1981 self
.set_state(VirtualNetworkFunctionRecordState
.TERMINATE
)
1984 if self
._vnf
_mon
is not None:
1985 self
._vnf
_mon
.stop()
1986 self
._vnf
_mon
.deregister()
1987 self
._vnf
_mon
= None
1990 def terminate_vls():
1991 """ Terminate VLs in this VNF """
1992 for vl
in self
._vlrs
:
1993 yield from vl
.terminate(xact
)
1996 def terminate_vdus():
1997 """ Terminate VDUS in this VNF """
1998 for vdu
in self
._vdus
:
1999 yield from vdu
.terminate(xact
)
2001 self
._log
.debug("Terminatng VLs in VNF id %s", self
.vnfr_id
)
2002 self
.set_state(VirtualNetworkFunctionRecordState
.VL_TERMINATE_PHASE
)
2003 yield from terminate_vls()
2005 self
._log
.debug("Terminatng VDUs in VNF id %s", self
.vnfr_id
)
2006 self
.set_state(VirtualNetworkFunctionRecordState
.VDU_TERMINATE_PHASE
)
2007 yield from terminate_vdus()
2009 self
._log
.debug("Terminated VNF id %s", self
.vnfr_id
)
2010 self
.set_state(VirtualNetworkFunctionRecordState
.TERMINATED
)
2013 def vnfr_uptime_update(self
, xact
):
2015 # Return when vnfr state is FAILED or TERMINATED etc
2016 if self
._state
not in [VirtualNetworkFunctionRecordState
.INIT
,
2017 VirtualNetworkFunctionRecordState
.VL_INIT_PHASE
,
2018 VirtualNetworkFunctionRecordState
.VM_INIT_PHASE
,
2019 VirtualNetworkFunctionRecordState
.READY
]:
2021 yield from self
.publish(xact
)
2022 yield from asyncio
.sleep(2, loop
=self
._loop
)
2026 class VnfdDtsHandler(object):
2027 """ DTS handler for VNFD config changes """
2028 XPATH
= "C,/project-vnfd:vnfd-catalog/project-vnfd:vnfd"
2030 def __init__(self
, dts
, log
, loop
, vnfm
):
2039 """ DTS registration handle """
2042 def deregister(self
):
2043 '''De-register from DTS'''
2044 self
._log
.debug("De-register VNFD DTS handler for project {}".
2045 format(self
._vnfm
._project
.name
))
2047 self
._regh
.deregister()
2052 """ Register for VNFD configuration"""
2054 def on_apply(dts
, acg
, xact
, action
, scratch
):
2055 """Apply the configuration"""
2056 self
._log
.debug("Got VNFM VNFD apply (xact: %s) (action: %s)(scr: %s)",
2057 xact
, action
, scratch
)
2059 is_recovery
= xact
.xact
is None and action
== rwdts
.AppconfAction
.INSTALL
2062 def on_prepare(dts
, acg
, xact
, xact_info
, ks_path
, msg
, scratch
):
2063 """ on prepare callback """
2064 self
._log
.debug("Got on prepare for VNFD (path: %s) (action: %s) (msg: %s)",
2065 ks_path
.to_xpath(RwVnfmYang
.get_schema()),
2066 xact_info
.query_action
, msg
)
2067 fref
= ProtobufC
.FieldReference
.alloc()
2068 fref
.goto_whole_message(msg
.to_pbcm())
2070 # Handle deletes in prepare_callback
2071 if fref
.is_field_deleted():
2072 # Delete an VNFD record
2073 self
._log
.debug("Deleting VNFD with id %s", msg
.id)
2074 if self
._vnfm
.vnfd_in_use(msg
.id):
2075 self
._log
.debug("Cannot delete VNFD in use - %s", msg
)
2076 err
= "Cannot delete a VNFD in use - %s" % msg
2077 raise VirtualNetworkFunctionDescriptorRefCountExists(err
)
2078 # Delete a VNFD record
2079 yield from self
._vnfm
.delete_vnfd(msg
.id)
2081 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
2083 xpath
= self
._vnfm
._project
.add_project(VnfdDtsHandler
.XPATH
)
2084 self
._log
.debug("Registering for VNFD config using xpath: {}".
2087 acg_hdl
= rift
.tasklets
.AppConfGroup
.Handler(on_apply
=on_apply
)
2088 with self
._dts
.appconf_group_create(handler
=acg_hdl
) as acg
:
2089 self
._regh
= acg
.register(
2091 flags
=rwdts
.Flag
.SUBSCRIBER | rwdts
.Flag
.DELTA_READY
,
2092 on_prepare
=on_prepare
)
2095 class VcsComponentDtsHandler(object):
2096 """ Vcs Component DTS handler """
2097 XPATH
= ("D,/rw-manifest:manifest" +
2098 "/rw-manifest:operational-inventory" +
2099 "/rw-manifest:component")
2101 def __init__(self
, dts
, log
, loop
, vnfm
):
2110 """ DTS registration handle """
2113 def deregister(self
):
2114 '''De-register from DTS'''
2115 self
._log
.debug("De-register VCS DTS handler for project {}".
2116 format(self
._vnfm
._project
))
2118 self
._regh
.deregister()
2123 """ Registers VCS component dts publisher registration"""
2124 self
._log
.debug("VCS Comp publisher DTS handler registering path %s",
2125 VcsComponentDtsHandler
.XPATH
)
2127 hdl
= rift
.tasklets
.DTS
.RegistrationHandler()
2128 handlers
= rift
.tasklets
.Group
.Handler()
2129 with self
._dts
.group_create(handler
=handlers
) as group
:
2130 self
._regh
= group
.register(xpath
=VcsComponentDtsHandler
.XPATH
,
2132 flags
=(rwdts
.Flag
.PUBLISHER |
2133 rwdts
.Flag
.NO_PREP_READ |
2134 rwdts
.Flag
.DATASTORE
),)
2137 def publish(self
, xact
, path
, msg
):
2138 """ Publishes the VCS component """
2139 self
._log
.debug("Publishing the VcsComponent xact = %s, %s:%s",
2141 self
.regh
.create_element(path
, msg
)
2142 self
._log
.debug("Published the VcsComponent to %s xact = %s, %s:%s",
2143 VcsComponentDtsHandler
.XPATH
, xact
, path
, msg
)
2145 class VnfrConsoleOperdataDtsHandler(object):
2147 Registers 'D,/rw-project:project/vnfr:vnfr-console/vnfr:vnfr[id]/vdur[id]'
2148 and handles CRUD from DTS
2152 def vnfr_vdu_console_xpath(self
):
2153 """ path for resource-mgr"""
2154 return self
._project
.add_project("D,/rw-vnfr:vnfr-console/rw-vnfr:vnfr[rw-vnfr:id='{}']" +
2155 "/rw-vnfr:vdur[vnfr:id='{}']".format(self
._vnfr
_id
,self
._vdur
_id
))
2157 def __init__(self
, dts
, log
, loop
, vnfm
, vnfr_id
, vdur_id
, vdu_id
):
2164 self
._vnfr
_id
= vnfr_id
2165 self
._vdur
_id
= vdur_id
2166 self
._vdu
_id
= vdu_id
2168 self
._project
= vnfm
._project
2170 def deregister(self
):
2171 '''De-register from DTS'''
2172 self
._log
.debug("De-register VNFR console DTS handler for project {}".
2173 format(self
._project
))
2175 self
._regh
.deregister()
2180 """ Register for VNFR VDU Operational Data read from dts """
2183 def on_prepare(xact_info
, action
, ks_path
, msg
):
2184 """ prepare callback from dts """
2185 xpath
= ks_path
.to_xpath(RwVnfrYang
.get_schema())
2187 "Got VNFR VDU Opdata xact_info: %s, action: %s): %s:%s",
2188 xact_info
, action
, xpath
, msg
2191 if action
== rwdts
.QueryAction
.READ
:
2192 schema
= RwVnfrYang
.YangData_RwProject_Project_VnfrConsole_Vnfr_Vdur
.schema()
2193 path_entry
= schema
.keyspec_to_entry(ks_path
)
2194 self
._log
.debug("VDU Opdata path is {}".format(path_entry
.key00
.id))
2196 vnfr
= self
._vnfm
.get_vnfr(self
._vnfr
_id
)
2197 except VnfRecordError
as e
:
2198 self
._log
.error("VNFR id %s not found", self
._vnfr
_id
)
2199 xact_info
.respond_xpath(rsp_code
=rwdts
.XactRspCode
.ACK
)
2202 vdur
= vnfr
._get
_vdur
_from
_vdu
_id
(self
._vdu
_id
)
2203 if not vdur
._state
== VDURecordState
.READY
:
2204 self
._log
.debug("VDUR state is not READY. current state is {}".format(vdur
._state
))
2205 xact_info
.respond_xpath(rsp_code
=rwdts
.XactRspCode
.ACK
)
2207 with self
._dts
.transaction() as new_xact
:
2208 resp
= yield from vdur
.read_resource(new_xact
)
2209 vdur_console
= RwVnfrYang
.YangData_RwProject_Project_VnfrConsole_Vnfr_Vdur()
2210 vdur_console
.id = self
._vdur
_id
2211 if resp
.console_url
:
2212 vdur_console
.console_url
= resp
.console_url
2214 vdur_console
.console_url
= 'none'
2215 self
._log
.debug("Recevied console URL for vdu {} is {}".format(self
._vdu
_id
,vdur_console
))
2217 self
._log
.exception("Caught exception while reading VDU %s", self
._vdu
_id
)
2218 vdur_console
= RwVnfrYang
.YangData_RwProject_Project_VnfrConsole_Vnfr_Vdur()
2219 vdur_console
.id = self
._vdur
_id
2220 vdur_console
.console_url
= 'none'
2222 xact_info
.respond_xpath(rsp_code
=rwdts
.XactRspCode
.ACK
,
2223 xpath
=self
.vnfr_vdu_console_xpath
,
2226 #raise VnfRecordError("Not supported operation %s" % action)
2227 self
._log
.error("Not supported operation %s" % action
)
2228 xact_info
.respond_xpath(rsp_code
=rwdts
.XactRspCode
.ACK
)
2232 self
._log
.debug("Registering for VNFR VDU using xpath: %s",
2233 self
.vnfr_vdu_console_xpath
)
2234 hdl
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
,)
2235 with self
._dts
.group_create() as group
:
2236 self
._regh
= group
.register(xpath
=self
.vnfr_vdu_console_xpath
,
2238 flags
=rwdts
.Flag
.PUBLISHER
,
2242 class VnfrDtsHandler(object):
2243 """ registers 'D,/rw-project:project/vnfr:vnfr-catalog/vnfr:vnfr' and handles CRUD from DTS"""
2244 XPATH
= "D,/vnfr:vnfr-catalog/vnfr:vnfr"
2246 def __init__(self
, dts
, log
, loop
, vnfm
):
2253 self
._project
= vnfm
._project
2257 """ Return registration handle"""
2262 """ Return VNF manager instance """
2265 def deregister(self
):
2266 '''De-register from DTS'''
2267 self
._log
.debug("De-register VNFR DTS handler for project {}".
2268 format(self
._project
))
2270 self
._regh
.deregister()
2275 """ Register for vnfr create/update/delete/read requests from dts """
2276 def on_commit(xact_info
):
2277 """ The transaction has been committed """
2278 self
._log
.debug("Got vnfr commit (xact_info: %s)", xact_info
)
2279 return rwdts
.MemberRspCode
.ACTION_OK
2281 def on_abort(*args
):
2282 """ Abort callback """
2283 self
._log
.debug("VNF transaction got aborted")
2286 def on_event(dts
, g_reg
, xact
, xact_event
, scratch_data
):
2289 def instantiate_realloc_vnfr(vnfr
):
2290 """Re-populate the vnfm after restart
2297 yield from vnfr
.instantiate(None, restart_mode
=True)
2299 if xact_event
== rwdts
.MemberEvent
.INSTALL
:
2300 curr_cfg
= self
.regh
.elements
2301 for cfg
in curr_cfg
:
2302 vnfr
= self
.vnfm
.create_vnfr(cfg
)
2303 self
._loop
.create_task(instantiate_realloc_vnfr(vnfr
))
2305 self
._log
.debug("Got on_event in vnfm")
2307 return rwdts
.MemberRspCode
.ACTION_OK
2310 def on_prepare(xact_info
, action
, ks_path
, msg
):
2311 """ prepare callback from dts """
2313 "Got vnfr on_prepare callback (xact_info: %s, action: %s): %s",
2314 xact_info
, action
, msg
2317 if action
== rwdts
.QueryAction
.CREATE
:
2318 if not msg
.has_field("vnfd"):
2319 err
= "Vnfd not provided"
2320 self
._log
.error(err
)
2321 raise VnfRecordError(err
)
2323 vnfr
= self
.vnfm
.create_vnfr(msg
)
2325 # RIFT-9105: Unable to add a READ query under an existing transaction
2326 # xact = xact_info.xact
2327 yield from vnfr
.instantiate(None)
2328 except Exception as e
:
2329 self
._log
.exception(e
)
2330 self
._log
.error("Error while instantiating vnfr:%s", vnfr
.vnfr_id
)
2331 vnfr
.set_state(VirtualNetworkFunctionRecordState
.FAILED
)
2332 yield from vnfr
.publish(None)
2333 elif action
== rwdts
.QueryAction
.DELETE
:
2334 schema
= RwVnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr
.schema()
2335 path_entry
= schema
.keyspec_to_entry(ks_path
)
2336 vnfr
= self
._vnfm
.get_vnfr(path_entry
.key00
.id)
2339 self
._log
.debug("VNFR id %s not found for delete", path_entry
.key00
.id)
2340 raise VirtualNetworkFunctionRecordNotFound(
2341 "VNFR id %s", path_entry
.key00
.id)
2344 yield from vnfr
.terminate(xact_info
.xact
)
2347 yield from self
._vnfm
.delete_vnfr(xact_info
.xact
, vnfr
)
2348 except Exception as e
:
2349 self
._log
.exception(e
)
2350 self
._log
.error("Caught exception while deleting vnfr %s", path_entry
.key00
.id)
2352 elif action
== rwdts
.QueryAction
.UPDATE
:
2353 schema
= RwVnfrYang
.YangData_RwProject_Project_VnfrCatalog_Vnfr
.schema()
2354 path_entry
= schema
.keyspec_to_entry(ks_path
)
2357 vnfr
= self
._vnfm
.get_vnfr(path_entry
.key00
.id)
2358 except Exception as e
:
2359 self
._log
.debug("No vnfr found with id %s", path_entry
.key00
.id)
2360 xact_info
.respond_xpath(rwdts
.XactRspCode
.NA
)
2364 self
._log
.debug("VNFR id %s not found for update", path_entry
.key00
.id)
2365 xact_info
.respond_xpath(rwdts
.XactRspCode
.NA
)
2368 self
._log
.debug("VNFR {} update config status {} (current {})".
2369 format(vnfr
.name
, msg
.config_status
, vnfr
.config_status
))
2370 # Update the config status and publish
2371 vnfr
._config
_status
= msg
.config_status
2372 yield from vnfr
.publish(None)
2375 raise NotImplementedError(
2376 "%s action on VirtualNetworkFunctionRecord not supported",
2379 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
2381 xpath
= self
._project
.add_project(VnfrDtsHandler
.XPATH
)
2382 self
._log
.debug("Registering for VNFR using xpath: {}".
2385 hdl
= rift
.tasklets
.DTS
.RegistrationHandler(on_commit
=on_commit
,
2386 on_prepare
=on_prepare
,)
2387 handlers
= rift
.tasklets
.Group
.Handler(on_event
=on_event
,)
2388 with self
._dts
.group_create(handler
=handlers
) as group
:
2389 self
._regh
= group
.register(xpath
=xpath
,
2391 flags
=(rwdts
.Flag
.PUBLISHER |
2392 rwdts
.Flag
.NO_PREP_READ |
2394 rwdts
.Flag
.DATASTORE
),)
2397 def create(self
, xact
, xpath
, msg
):
2399 Create a VNFR record in DTS with path and message
2401 path
= self
._project
.add_project(xpath
)
2402 self
._log
.debug("Creating VNFR xact = %s, %s:%s",
2405 self
.regh
.create_element(path
, msg
)
2406 self
._log
.debug("Created VNFR xact = %s, %s:%s",
2410 def update(self
, xact
, xpath
, msg
):
2412 Update a VNFR record in DTS with path and message
2414 path
= self
._project
.add_project(xpath
)
2415 self
._log
.debug("Updating VNFR xact = %s, %s:%s",
2417 self
.regh
.update_element(path
, msg
)
2418 self
._log
.debug("Updated VNFR xact = %s, %s:%s",
2422 def delete(self
, xact
, xpath
):
2424 Delete a VNFR record in DTS with path and message
2426 path
= self
._project
.add_project(xpath
)
2427 self
._log
.debug("Deleting VNFR xact = %s, %s", xact
, path
)
2428 self
.regh
.delete_element(path
)
2429 self
._log
.debug("Deleted VNFR xact = %s, %s", xact
, path
)
2432 class VnfdRefCountDtsHandler(object):
2433 """ The VNFD Ref Count DTS handler """
2434 XPATH
= "D,/vnfr:vnfr-catalog/rw-vnfr:vnfd-ref-count"
2436 def __init__(self
, dts
, log
, loop
, vnfm
):
2446 """ Return registration handle """
2451 """ Return the NS manager instance """
2454 def deregister(self
):
2455 '''De-register from DTS'''
2456 self
._log
.debug("De-register VNFD Ref DTS handler for project {}".
2457 format(self
._vnfm
._project
))
2459 self
._regh
.deregister()
2464 """ Register for VNFD ref count read from dts """
2467 def on_prepare(xact_info
, action
, ks_path
, msg
):
2468 """ prepare callback from dts """
2469 xpath
= ks_path
.to_xpath(RwVnfrYang
.get_schema())
2471 "Got VNFD ref count get xact_info: %s, action: %s): %s:%s",
2472 xact_info
, action
, xpath
, msg
2475 if action
== rwdts
.QueryAction
.READ
:
2476 schema
= RwVnfrYang
.YangData_RwProject_Project_VnfrCatalog_VnfdRefCount
.schema()
2477 path_entry
= schema
.keyspec_to_entry(ks_path
)
2478 vnfd_list
= yield from self
._vnfm
.get_vnfd_refcount(path_entry
.key00
.vnfd_id_ref
)
2479 for xpath
, msg
in vnfd_list
:
2480 self
._log
.debug("Responding to ref count query path:%s, msg:%s",
2482 xact_info
.respond_xpath(rsp_code
=rwdts
.XactRspCode
.MORE
,
2485 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
2487 raise VnfRecordError("Not supported operation %s" % action
)
2489 hdl
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
,)
2490 with self
._dts
.group_create() as group
:
2491 self
._regh
= group
.register(xpath
=self
._vnfm
._project
.add_project(
2492 VnfdRefCountDtsHandler
.XPATH
),
2494 flags
=rwdts
.Flag
.PUBLISHER
,
2498 class VdurDatastore(object):
2500 This VdurDatastore is intended to expose select information about a VDUR
2501 such that it can be referenced in a cloud config file. The data that is
2502 exposed does not necessarily follow the structure of the data in the yang
2503 model. This is intentional. The data that are exposed are intended to be
2504 agnostic of the yang model so that changes in the model do not necessarily
2505 require changes to the interface provided to the user. It also means that
2506 the user does not need to be familiar with the RIFT.ware yang models.
2510 """Create an instance of VdurDatastore"""
2511 self
._vdur
_data
= dict()
2512 self
._pattern
= re
.compile("vdu\[([^]]+)\]\.(.+)")
2514 def add(self
, vdur
):
2515 """Add a new VDUR to the datastore
2518 vdur - a VirtualDeploymentUnitRecord instance
2521 A ValueError is raised if the VDUR is (1) None or (2) already in
2525 if vdur
.vdu_id
is None:
2526 raise ValueError('VDURs are required to have an ID')
2528 if vdur
.vdu_id
in self
._vdur
_data
:
2529 raise ValueError('cannot add a VDUR more than once')
2531 self
._vdur
_data
[vdur
.vdu_id
] = dict()
2533 def set_if_not_none(key
, attr
):
2534 if attr
is not None:
2535 self
._vdur
_data
[vdur
.vdu_id
][key
] = attr
2537 set_if_not_none('name', vdur
._vdud
.name
)
2538 set_if_not_none('mgmt.ip', vdur
.vm_management_ip
)
2539 # The below can be used for hostname
2540 set_if_not_none('vdur_name', vdur
.unique_short_name
)
2542 def update(self
, vdur
):
2543 """Update the VDUR information in the datastore
2546 vdur - a GI representation of a VDUR
2549 A ValueError is raised if the VDUR is (1) None or (2) already in
2553 if vdur
.vdu_id
is None:
2554 raise ValueError('VNFDs are required to have an ID')
2556 if vdur
.vdu_id
not in self
._vdur
_data
:
2557 raise ValueError('VNF is not recognized')
2559 def set_or_delete(key
, attr
):
2561 if key
in self
._vdur
_data
[vdur
.vdu_id
]:
2562 del self
._vdur
_data
[vdur
.vdu_id
][key
]
2565 self
._vdur
_data
[vdur
.vdu_id
][key
] = attr
2567 set_or_delete('name', vdur
._vdud
.name
)
2568 set_or_delete('mgmt.ip', vdur
.vm_management_ip
)
2569 # The below can be used for hostname
2570 set_or_delete('vdur_name', vdur
.unique_short_name
)
2572 def remove(self
, vdur_id
):
2573 """Remove all of the data associated with specified VDUR
2576 vdur_id - the identifier of a VNFD in the datastore
2579 A ValueError is raised if the VDUR is not contained in the
2583 if vdur_id
not in self
._vdur
_data
:
2584 raise ValueError('VNF is not recognized')
2586 del self
._vdur
_data
[vdur_id
]
2588 def get(self
, expr
):
2589 """Retrieve VDUR information from the datastore
2591 An expression should be of the form,
2595 where <id> is the VDUR ID (an unquoted UUID), and <attr> is the name of
2596 the exposed attribute that the user wishes to retrieve.
2598 If the requested data is not available, None is returned.
2601 expr - a string that specifies the data to return
2604 A ValueError is raised if the provided expression cannot be parsed.
2607 The requested data or None
2610 result
= self
._pattern
.match(expr
)
2612 raise ValueError('data expression not recognized ({})'.format(expr
))
2614 vdur_id
, key
= result
.groups()
2616 if vdur_id
not in self
._vdur
_data
:
2619 return self
._vdur
_data
[vdur_id
].get(key
, None)
2622 class VnfManager(object):
2623 """ The virtual network function manager class """
2624 def __init__(self
, dts
, log
, loop
, project
, cluster_name
):
2628 self
._project
= project
2629 self
._cluster
_name
= cluster_name
2631 self
._vcs
_handler
= VcsComponentDtsHandler(dts
, log
, loop
, self
)
2632 self
._vnfr
_handler
= VnfrDtsHandler(dts
, log
, loop
, self
)
2633 self
._vnfr
_ref
_handler
= VnfdRefCountDtsHandler(dts
, log
, loop
, self
)
2634 self
._nsr
_handler
= mano_dts
.NsInstanceConfigSubscriber(
2635 log
, dts
, loop
, project
, callback
=self
.handle_nsr
)
2637 self
._dts
_handlers
= [VnfdDtsHandler(dts
, log
, loop
, self
),
2640 self
._vnfr
_ref
_handler
,
2643 self
._vnfds
_to
_vnfr
= {}
2647 def vnfr_handler(self
):
2648 """ VNFR dts handler """
2649 return self
._vnfr
_handler
2652 def vcs_handler(self
):
2653 """ VCS dts handler """
2654 return self
._vcs
_handler
2658 """ Register all static DTS handlers """
2659 for hdl
in self
._dts
_handlers
:
2660 yield from hdl
.register()
2662 def deregister(self
):
2663 self
._log
.debug("De-register VNFM project {}".format(self
._project
.name
))
2664 for hdl
in self
._dts
_handlers
:
2669 """ Run this VNFM instance """
2670 self
._log
.debug("Run VNFManager - registering static DTS handlers""")
2671 yield from self.register()
2673 def handle_nsr(self, nsr, action):
2674 if action in [rwdts.QueryAction.CREATE]:
2675 self._nsrs[nsr.id] = nsr
2676 elif action == rwdts.QueryAction.DELETE:
2677 if nsr.id in self._nsrs:
2678 del self._nsrs[nsr.id]
2680 def get_linked_mgmt_network(self, vnfr):
2681 """For the given VNFR get the related mgmt network from the NSD, if
2684 vnfd_id = vnfr.vnfd.id
2685 nsr_id = vnfr.nsr_id_ref
2687 # for the given related VNFR, get the corresponding NSR-config
2690 nsr_obj = self._nsrs[nsr_id]
2692 raise("Unable to find the NS with the ID
: {}".format(nsr_id))
2694 # for the related NSD check if a VLD exists such that it's a mgmt
2696 for vld in nsr_obj.nsd.vld:
2697 if vld.mgmt_network:
2702 def get_vnfr(self, vnfr_id):
2703 """ get VNFR by vnfr id """
2705 if vnfr_id not in self._vnfrs:
2706 raise VnfRecordError("VNFR
id %s not found
", vnfr_id)
2708 return self._vnfrs[vnfr_id]
2710 def create_vnfr(self, vnfr):
2711 """ Create a VNFR instance """
2712 if vnfr.id in self._vnfrs:
2713 msg = "Vnfr
id %s already exists
" % vnfr.id
2714 self._log.error(msg)
2715 raise VnfRecordError(msg)
2717 self._log.info("Create VirtualNetworkFunctionRecord
%s from vnfd_id
: %s",
2721 mgmt_network = self.get_linked_mgmt_network(vnfr)
2723 self._vnfrs[vnfr.id] = VirtualNetworkFunctionRecord(
2724 self._dts, self._log, self._loop, self._cluster_name, self, self.vcs_handler, vnfr,
2725 mgmt_network=mgmt_network
2729 if vnfr.vnfd.id in self._vnfds_to_vnfr:
2730 self._vnfds_to_vnfr[vnfr.vnfd.id] += 1
2732 self._vnfds_to_vnfr[vnfr.vnfd.id] = 1
2734 return self._vnfrs[vnfr.id]
2737 def delete_vnfr(self, xact, vnfr):
2738 """ Create a VNFR instance """
2739 if vnfr.vnfr_id in self._vnfrs:
2740 self._log.debug("Deleting VNFR
id %s", vnfr.vnfr_id)
2741 yield from self._vnfr_handler.delete(xact, vnfr.xpath)
2743 if vnfr.vnfd.id in self._vnfds_to_vnfr:
2744 if self._vnfds_to_vnfr[vnfr.vnfd.id]:
2745 self._vnfds_to_vnfr[vnfr.vnfd.id] -= 1
2747 del self._vnfrs[vnfr.vnfr_id]
2750 def fetch_vnfd(self, vnfd_id):
2751 """ Fetch VNFDs based with the vnfd id"""
2752 vnfd_path = self._project.add_project(
2753 VirtualNetworkFunctionRecord.vnfd_xpath(vnfd_id))
2754 self._log.debug("Fetch vnfd with path
%s", vnfd_path)
2757 res_iter = yield from self._dts.query_read(vnfd_path,
2758 rwdts.XactFlag.MERGE)
2760 for ent in res_iter:
2761 res = yield from ent
2765 err = "Failed to get Vnfd
%s" % vnfd_id
2766 self._log.error(err)
2767 raise VnfRecordError(err)
2769 self._log.debug("Fetched vnfd
for path
%s, vnfd
- %s", vnfd_path, vnfd)
2773 def vnfd_in_use(self, vnfd_id):
2774 """ Is this VNFD in use """
2775 self._log.debug("Is this VNFD
in use
- msg
:%s", vnfd_id)
2776 if vnfd_id in self._vnfds_to_vnfr:
2777 return (self._vnfds_to_vnfr[vnfd_id] > 0)
2781 def publish_vnfr(self, xact, path, msg):
2782 """ Publish a VNFR """
2783 self._log.debug("publish_vnfr called with path
%s, msg
%s",
2785 yield from self.vnfr_handler.update(xact, path, msg)
2788 def delete_vnfd(self, vnfd_id):
2789 """ Delete the Virtual Network Function descriptor with the passed id """
2790 self._log.debug("Deleting the virtual network function descriptor
- %s", vnfd_id)
2791 if vnfd_id in self._vnfds_to_vnfr:
2792 if self._vnfds_to_vnfr[vnfd_id]:
2793 self._log.debug("Cannot delete VNFD
id %s reference exists
%s",
2795 self._vnfds_to_vnfr[vnfd_id].vnfd_ref_count)
2796 raise VirtualNetworkFunctionDescriptorRefCountExists(
2797 "Cannot delete
:%s, ref_count
:%s",
2799 self._vnfds_to_vnfr[vnfd_id].vnfd_ref_count)
2801 del self._vnfds_to_vnfr[vnfd_id]
2803 # Remove any files uploaded with VNFD and stored under $RIFT_ARTIFACTS/libs/<id>
2805 rift_artifacts_dir = os.environ['RIFT_ARTIFACTS']
2806 vnfd_dir = os.path.join(rift_artifacts_dir, 'launchpad/libs', vnfd_id)
2807 if os.path.exists(vnfd_dir):
2808 shutil.rmtree(vnfd_dir, ignore_errors=True)
2809 except Exception as e:
2810 self._log.error("Exception in cleaning up VNFD
{}: {}".
2811 format(self._vnfds_to_vnfr[vnfd_id].vnfd.name, e))
2812 self._log.exception(e)
2815 def vnfd_refcount_xpath(self, vnfd_id):
2816 """ xpath for ref count entry """
2817 return self._project.add_project(VnfdRefCountDtsHandler.XPATH +
2818 "[rw
-vnfr
:vnfd
-id-ref
= '{}']").format(vnfd_id)
2821 def get_vnfd_refcount(self, vnfd_id):
2822 """ Get the vnfd_list from this VNFM"""
2824 if vnfd_id is None or vnfd_id == "":
2825 for vnfd in self._vnfds_to_vnfr.keys():
2826 vnfd_msg = RwVnfrYang.YangData_RwProject_Project_VnfrCatalog_VnfdRefCount()
2827 vnfd_msg.vnfd_id_ref = vnfd
2828 vnfd_msg.instance_ref_count = self._vnfds_to_vnfr[vnfd]
2829 vnfd_list.append((self.vnfd_refcount_xpath(vnfd), vnfd_msg))
2830 elif vnfd_id in self._vnfds_to_vnfr:
2831 vnfd_msg = RwVnfrYang.YangData_RwProject_Project_VnfrCatalog_VnfdRefCount()
2832 vnfd_msg.vnfd_id_ref = vnfd_id
2833 vnfd_msg.instance_ref_count = self._vnfds_to_vnfr[vnfd_id]
2834 vnfd_list.append((self.vnfd_refcount_xpath(vnfd_id), vnfd_msg))
2839 class VnfmProject(ManoProject):
2841 def __init__(self, name, tasklet, **kw):
2842 super(VnfmProject, self).__init__(tasklet.log, name)
2843 self.update(tasklet)
2848 def register (self):
2850 vm_parent_name = self._tasklet.tasklet_info.get_parent_vm_parent_instance_name()
2851 assert vm_parent_name is not None
2852 self._vnfm = VnfManager(self._dts, self.log, self.loop, self, vm_parent_name)
2853 yield from self._vnfm.run()
2855 print("Caught
Exception in VNFM init
:", sys.exc_info()[0])
2858 def deregister(self):
2859 self._log.debug("De
-register project
{} for VnfmProject
".
2861 self._vnfm.deregister()
2864 class VnfmTasklet(rift.tasklets.Tasklet):
2865 """ VNF Manager tasklet class """
2866 def __init__(self, *args, **kwargs):
2867 super(VnfmTasklet, self).__init__(*args, **kwargs)
2868 self.rwlog.set_category("rw
-mano
-log
")
2869 self.rwlog.set_subcategory("vnfm
")
2872 self._project_handler = None
2881 super(VnfmTasklet, self).start()
2882 self.log.info("Starting VnfmTasklet
")
2884 self.log.setLevel(logging.DEBUG)
2886 self.log.debug("Registering with dts
")
2887 self._dts = rift.tasklets.DTS(self.tasklet_info,
2888 RwVnfmYang.get_schema(),
2890 self.on_dts_state_change)
2892 self.log.debug("Created DTS Api GI Object
: %s", self._dts)
2894 print("Caught
Exception in VNFM start
:", sys.exc_info()[0])
2897 def on_instance_started(self):
2898 """ Task insance started callback """
2899 self.log.debug("Got instance started callback
")
2905 print("Caught
Exception in VNFM stop
:", sys.exc_info()[0])
2910 """ Task init callback """
2911 self.log.debug("creating project handler
")
2912 self.project_handler = ProjectHandler(self, VnfmProject)
2913 self.project_handler.register()
2917 """ Task run callback """
2921 def on_dts_state_change(self, state):
2922 """Take action according to current dts state to transition
2923 application into the corresponding application state
2926 state - current dts state
2929 rwdts.State.INIT: rwdts.State.REGN_COMPLETE,
2930 rwdts.State.CONFIG: rwdts.State.RUN,
2934 rwdts.State.INIT: self.init,
2935 rwdts.State.RUN: self.run,
2938 # Transition application to next state
2939 handler = handlers.get(state, None)
2940 if handler is not None:
2941 yield from handler()
2943 # Transition dts to next state
2944 next_state = switch.get(state, None)
2945 if next_state is not None:
2946 self._dts.handle.set_state(next_state)