class TerminateVnfRequest(base.TackerObject, base.TackerPersistentObject): # Version 1.0: Initial version VERSION = '1.0' fields = { 'termination_type': fields.VnfInstanceTerminationTypeField(nullable=False), 'graceful_termination_timeout': fields.IntegerField(nullable=True, default=0), 'additional_params': fields.DictOfStringsField(nullable=True, default={}), } @classmethod def obj_from_primitive(cls, primitive, context): if 'tacker_object.name' in primitive: obj_terminate_vnf_req = super(TerminateVnfRequest, cls).obj_from_primitive( primitive, context) else: obj_terminate_vnf_req = TerminateVnfRequest._from_dict(primitive) return obj_terminate_vnf_req @classmethod def _from_dict(cls, data_dict): termination_type = data_dict.get('termination_type') graceful_termination_timeout = \ data_dict.get('graceful_termination_timeout', 0) additional_params = data_dict.get('additional_params', {}) return cls(termination_type=termination_type, graceful_termination_timeout=graceful_termination_timeout, additional_params=additional_params)
class ScaleVnfRequest(base.TackerObject): # Version 1.0: Initial version VERSION = '1.0' fields = { 'type': fields.StringField(nullable=False), 'aspect_id': fields.StringField(nullable=False), 'number_of_steps': fields.IntegerField(nullable=True, default=1), 'additional_params': fields.DictOfStringsField(nullable=True, default={}), } @classmethod def obj_from_primitive(cls, primitive, context): if 'tacker_object.name' in primitive: obj_scle_vnf_req = super(ScaleVnfRequest, cls).obj_from_primitive( primitive, context) else: obj_scle_vnf_req = ScaleVnfRequest._from_dict(primitive) return obj_scle_vnf_req @classmethod def _from_dict(cls, data_dict): type = data_dict.get('type') aspect_id = data_dict.get('aspect_id') number_of_steps = data_dict.get('number_of_steps') additional_params = data_dict.get('additional_params') obj = cls(type=type, aspect_id=aspect_id, number_of_steps=number_of_steps, additional_params=additional_params) return obj
class ScaleInfo(base.TackerObject, base.TackerObjectDictCompat, base.TackerPersistentObject): # Version 1.0: Initial version VERSION = '1.0' fields = { 'aspect_id': fields.StringField(nullable=False), 'scale_level': fields.IntegerField(nullable=False), } @classmethod def obj_from_primitive(cls, primitive, context): if 'tacker_object.name' in primitive: obj_scale_status = super(ScaleInfo, cls).obj_from_primitive( primitive, context) else: obj_scale_status = ScaleInfo._from_dict(primitive) return obj_scale_status @classmethod def _from_dict(cls, data_dict): aspect_id = data_dict.get('aspect_id') scale_level = data_dict.get('scale_level') obj = cls(aspect_id=aspect_id, scale_level=scale_level) return obj def to_dict(self): return {'aspect_id': self.aspect_id, 'scale_level': self.scale_level}
class ProblemDetails(base.TackerObject, base.TackerPersistentObject): # Version 1.0: Initial version VERSION = '1.0' fields = { 'title': fields.StringField(nullable=True, default=''), 'status': fields.IntegerField(nullable=False), 'detail': fields.StringField(nullable=False) } @classmethod def obj_from_primitive(cls, primitive, context): if 'tacker_object.name' in primitive: problem_detail = super(ProblemDetails, cls).obj_from_primitive(primitive, context) else: p_dict = jsonutils.loads(primitive) problem_detail = ProblemDetails._from_dict(p_dict) return problem_detail @classmethod def _from_dict(cls, data_dict): title = data_dict.get('title') status = data_dict.get('status') detail = data_dict.get('detail') obj = cls(title=title, status=status, detail=detail) return obj def to_dict(self): return { 'title': self.title, 'status': self.status, 'detail': self.detail }
class VnfLcmOpOcc(base.TackerObject, base.TackerObjectDictCompat, base.TackerPersistentObject): # Version 1.0: Initial version VERSION = '1.0' fields = { 'id': fields.UUIDField(nullable=False), 'operation_state': fields.StringField(nullable=False), 'state_entered_time': fields.DateTimeField(nullable=False), 'start_time': fields.DateTimeField(nullable=False), 'vnf_instance_id': fields.StringField(nullable=False), 'operation': fields.StringField(nullable=False), 'is_automatic_invocation': fields.BooleanField(default=False), 'operation_params': fields.StringField(nullable=True), 'is_cancel_pending': fields.BooleanField(default=False), 'error': fields.ObjectField('ProblemDetails', nullable=True, default=None), 'resource_changes': fields.ObjectField('ResourceChanges', nullable=True, default=None), 'changed_info': fields.ObjectField('VnfInfoModifications', nullable=True, default=None), 'error_point': fields.IntegerField(nullable=True, default=0) } @base.remotable def create(self): updates = self.obj_clone() _vnf_lcm_op_occ_create(self._context, updates) @base.remotable def save(self): updates = self.obj_clone() _vnf_lcm_op_occ_update(self._context, updates) @staticmethod def _from_db_object(context, vnf_lcm_op_occ_obj, db_vnf_lcm_op_occ): special_fields = ['error', 'resource_changes', 'changed_info'] for key in vnf_lcm_op_occ_obj.fields: if key in special_fields: continue setattr(vnf_lcm_op_occ_obj, key, db_vnf_lcm_op_occ.get(key)) if db_vnf_lcm_op_occ['error']: error = ProblemDetails.obj_from_primitive( db_vnf_lcm_op_occ['error'], context) vnf_lcm_op_occ_obj.error = error if db_vnf_lcm_op_occ['resource_changes']: resource_changes = ResourceChanges.obj_from_primitive( db_vnf_lcm_op_occ['resource_changes'], context) vnf_lcm_op_occ_obj.resource_changes = resource_changes if db_vnf_lcm_op_occ['changed_info']: changed_info = VnfInfoModifications.obj_from_primitive( db_vnf_lcm_op_occ['changed_info'], context) vnf_lcm_op_occ_obj.changed_info = changed_info vnf_lcm_op_occ_obj._context = context vnf_lcm_op_occ_obj.obj_reset_changes() return vnf_lcm_op_occ_obj @classmethod def obj_from_primitive(cls, primitive, context): if 'tacker_object.name' in primitive: vnf_lcm_op_occ = super(VnfLcmOpOcc, cls).obj_from_primitive(primitive, context) else: if 'error' in primitive.keys(): obj_data = ProblemDetails._from_dict(primitive.get('error')) primitive.update({'error': obj_data}) if 'resource_changes' in primitive.keys(): obj_data = ResourceChanges._from_dict( primitive.get('resource_changes')) primitive.update({'resource_changes': obj_data}) if 'changed_info' in primitive.keys(): obj_data = VnfInfoModifications._from_dict( primitive.get('changed_info')) primitive.update({'changed_info': obj_data}) vnf_lcm_op_occ = VnfLcmOpOcc._from_dict(primitive) return vnf_lcm_op_occ @classmethod def obj_from_db_obj(cls, context, db_obj): return cls._from_db_object(context, cls(), db_obj) @classmethod def _from_dict(cls, data_dict): operation_state = data_dict.get('operation_state') state_entered_time = data_dict.get('state_entered_time') start_time = data_dict.get('start_time') vnf_instance_id = data_dict.get('vnf_instance_id') operation = data_dict.get('operation') is_automatic_invocation = data_dict.get('is_automatic_invocation') operation_params = data_dict.get('operation_params') is_cancel_pending = data_dict.get('is_cancel_pending') error = data_dict.get('error') resource_changes = data_dict.get('resource_changes') changed_info = data_dict.get('changed_info') error_point = data_dict.get('error_point') obj = cls(operation_state=operation_state, state_entered_time=state_entered_time, start_time=start_time, vnf_instance_id=vnf_instance_id, operation=operation, is_automatic_invocation=is_automatic_invocation, operation_params=operation_params, is_cancel_pending=is_cancel_pending, error=error, resource_changes=resource_changes, changed_info=changed_info, error_point=error_point) return obj def to_dict(self): data = { 'id': self.id, 'operation_state': self.operation_state, 'state_entered_time': self.state_entered_time, 'start_time': self.start_time, 'vnf_instance_id': self.vnf_instance_id, 'operation': self.operation, 'is_automatic_invocation': self.is_automatic_invocation, 'operation_params': self.operation_params, 'is_cancel_pending': self.is_cancel_pending, 'error_point': self.error_point } if self.error: data.update({'error': self.error.to_dict()}) if self.resource_changes: data.update({'resource_changes': self.resource_changes.to_dict()}) if self.changed_info: data.update({'changed_info': self.changed_info.to_dict()}) return data @base.remotable_classmethod def get_by_id(cls, context, id): db_vnf_lcm_op_occs = _vnf_lcm_op_occs_get_by_id(context, id) return cls._from_db_object(context, cls(), db_vnf_lcm_op_occs)
class VnfPackage(base.TackerObject, base.TackerPersistentObject, base.TackerObjectDictCompat): # Key corresponds to the name of the parameter as defined # in type VnfPkgInfo of SOL003 document and value will contain tuple # of following values:- # 1. Parameter that is mapped to version object # 2. Data type of the field as per the data types supported by # attribute-based filtering # 3. DB model that's mapped to the version object. # 4. Valid values for a given data type if any. This value is set # especially for 'enum' data type. ALL_ATTRIBUTES = { 'id': ('id', "string", 'VnfPackage'), 'onboardingState': ('onboarding_state', "enum", 'VnfPackage', fields.PackageOnboardingStateTypeField().valid_values), 'operationalState': ('operational_state', 'enum', 'VnfPackage', fields.PackageOperationalStateTypeField().valid_values), 'usageState': ('usage_state', 'enum', 'VnfPackage', fields.PackageUsageStateTypeField().valid_values), 'vnfProvider': ('vnfd.vnf_provider', 'string', 'VnfPackageVnfd'), 'vnfProductName': ('vnfd.vnf_product_name', 'string', 'VnfPackageVnfd'), 'vnfdId': ('vnfd.vnfd_id', 'string', 'VnfPackageVnfd'), 'vnfSoftwareVersion': ('vnfd.vnf_software_version', 'string', 'VnfPackageVnfd'), 'vnfdVersion': ('vnfd.vnfd_version', 'string', 'VnfPackageVnfd'), 'userDefinedData/*': ('user_data', 'key_value_pair', { "key_column": "key", "value_column": "value", "model": "VnfPackageUserData" }), "checksum": { 'algorithm': ('algorithm', 'string', 'VnfPackage'), 'hash': ('hash', 'string', 'VnfPackage'), } } ALL_ATTRIBUTES.update(vnf_software_image.VnfSoftwareImage.ALL_ATTRIBUTES) FLATTEN_ATTRIBUTES = utils.flatten_dict(ALL_ATTRIBUTES.copy()) simple_attributes = [ 'id', 'onboardingState', 'operationalState', 'usageState' ] simple_instantiated_attributes = [ 'vnfProvider', 'vnfProductName', 'vnfdId', 'vnfSoftwareVersion', 'vnfdVersion' ] COMPLEX_ATTRIBUTES = ["checksum", "userDefinedData"] COMPLEX_ATTRIBUTES.extend( vnf_software_image.VnfSoftwareImage.COMPLEX_ATTRIBUTES) # Version 1.1: Added 'size' to persist size of VnfPackage. VERSION = '1.1' fields = { 'id': fields.UUIDField(nullable=False), 'onboarding_state': fields.PackageOnboardingStateTypeField(nullable=False), 'operational_state': fields.PackageOperationalStateTypeField(nullable=False), 'usage_state': fields.PackageUsageStateTypeField(nullable=False), 'user_data': fields.DictOfStringsField(), 'tenant_id': fields.StringField(nullable=False), 'algorithm': fields.StringField(nullable=True), 'hash': fields.StringField(nullable=True), 'location_glance_store': fields.StringField(nullable=True), 'vnf_deployment_flavours': fields.ObjectField('VnfDeploymentFlavoursList', nullable=True), 'vnfd': fields.ObjectField('VnfPackageVnfd', nullable=True), 'size': fields.IntegerField(nullable=False, default=0), } def __init__(self, context=None, **kwargs): super(VnfPackage, self).__init__(context, **kwargs) self.obj_set_defaults() def obj_make_compatible(self, primitive, target_version): super(VnfPackage, self).obj_make_compatible(primitive, target_version) target_version = versionutils.convert_version_to_tuple(target_version) if target_version < (1, 1) and 'size' in primitive: del primitive['size'] @staticmethod def _from_db_object(context, vnf_package, db_vnf_package, expected_attrs=None): expected_attrs = expected_attrs or [] vnf_package._context = context for key in vnf_package.fields: if key in VNF_PACKAGE_OPTIONAL_ATTRS: continue if key == 'user_data': db_key = 'metadetails' else: db_key = key setattr(vnf_package, key, db_vnf_package[db_key]) vnf_package._context = context vnf_package._extra_attributes_from_db_object(vnf_package, db_vnf_package, expected_attrs) vnf_package.obj_reset_changes() return vnf_package @staticmethod def _extra_attributes_from_db_object(vnf_package, db_vnf_package, expected_attrs=None): """Method to help with migration of extra attributes to objects.""" if expected_attrs is None: expected_attrs = [] if 'vnf_deployment_flavours' in expected_attrs: vnf_package._load_vnf_deployment_flavours( db_vnf_package.get('vnf_deployment_flavours')) if 'vnfd' in expected_attrs: vnf_package._load_vnfd(db_vnf_package.get('vnfd')) def _load_vnf_deployment_flavours(self, db_flavours=_NO_DATA_SENTINEL): if db_flavours is _NO_DATA_SENTINEL: vnf_package = self.get_by_id( self._context, self.id, expected_attrs=['vnf_deployment_flavours']) if 'vnf_deployment_flavours' in vnf_package: self.vnf_deployment_flavours = \ vnf_package.vnf_deployment_flavours self.vnf_deployment_flavours.obj_reset_changes(recursive=True) self.obj_reset_changes(['vnf_deployment_flavours']) else: self.vnf_deployment_flavours = \ objects.VnfDeploymentFlavoursList(objects=[]) elif db_flavours: self.vnf_deployment_flavours = base.obj_make_list( self._context, objects.VnfDeploymentFlavoursList(self._context), objects.VnfDeploymentFlavour, db_flavours) self.obj_reset_changes(['vnf_deployment_flavours']) def _load_vnfd(self, db_vnfd=_NO_DATA_SENTINEL): if db_vnfd is None: self.vnfd = None elif db_vnfd is _NO_DATA_SENTINEL: vnf_package = self.get_by_id(self._context, self.id, expected_attrs=['vnfd']) if 'vnfd' in vnf_package and vnf_package.vnfd is not None: self.vnfd = vnf_package.vnfd self.vnfd.obj_reset_changes(recursive=True) self.obj_reset_changes(['vnfd']) else: self.vnfd = None elif db_vnfd: self.vnfd = objects.VnfPackageVnfd.obj_from_db_obj( self._context, db_vnfd) self.obj_reset_changes(['vnfd']) def _load_generic(self, attrname): vnf_package = self.__class__.get_by_id(self._context, id=self.id, expected_attrs=None) if attrname not in vnf_package: raise exceptions.ObjectActionError( action='obj_load_attr', reason=_('loading %s requires recursion') % attrname) for field in self.fields: if field in vnf_package and field not in self: setattr(self, field, getattr(vnf_package, field)) def obj_load_attr(self, attrname): if not self._context: raise exceptions.OrphanedObjectError(method='obj_load_attr', objtype=self.obj_name()) if 'id' not in self: raise exceptions.ObjectActionError( action='obj_load_attr', reason=_('attribute %s not lazy-loadable') % attrname) LOG.debug("Lazy-loading '%(attr)s' on %(name)s id %(id)s", { 'attr': attrname, 'name': self.obj_name(), 'id': self.id, }) self._obj_load_attr(attrname) def _obj_load_attr(self, attrname): """Internal method for loading attributes from vnf package.""" if attrname == 'vnf_deployment_flavours': self._load_vnf_deployment_flavours() elif attrname == 'vnfd': self._load_vnfd() elif attrname in self.fields and attrname != 'id': self._load_generic(attrname) else: # NOTE(nirajsingh): Raise error if non existing field is # requested. raise exceptions.ObjectActionError( action='obj_load_attr', reason=_('attribute %s not lazy-loadable') % attrname) self.obj_reset_changes([attrname]) @base.remotable def create(self): if self.obj_attr_is_set('id'): raise exceptions.ObjectActionError(action='create', reason=_('already created')) updates = self.obj_get_changes() if 'id' not in updates: updates['id'] = uuidutils.generate_uuid() self.id = updates['id'] for key in ['vnf_deployment_flavours']: if key in updates: updates.pop(key) user_data = updates.pop('user_data', None) db_vnf_package = _vnf_package_create(self._context, updates, user_data=user_data) self._from_db_object(self._context, self, db_vnf_package) @base.remotable_classmethod def get_by_id(cls, context, id, expected_attrs=None): db_vnf_package = _vnf_package_get_by_id(context, id, columns_to_join=expected_attrs) return cls._from_db_object(context, cls(), db_vnf_package, expected_attrs=expected_attrs) @base.remotable def destroy(self, context): if not self.obj_attr_is_set('id'): raise exceptions.ObjectActionError(action='destroy', reason='no uuid') _destroy_vnf_package(context, self.id) @base.remotable def save(self): updates = self.tacker_obj_get_changes() for key in ['vnf_deployment_flavours']: if key in updates: updates.pop(key) db_vnf_package = _vnf_package_update(self._context, self.id, updates) self._from_db_object(self._context, self, db_vnf_package) @base.remotable def is_package_in_use(self, context): if self.onboarding_state == \ fields.PackageOnboardingStateType.ONBOARDED: # check if vnf package is used by any vnf instances. query = context.session.query( func.count(models.VnfInstance.id)).\ filter_by( instantiation_state=fields.VnfInstanceState.INSTANTIATED).\ filter_by(tenant_id=self.tenant_id).\ filter_by(vnfd_id=self.vnfd.vnfd_id).\ filter_by(deleted=False) result = query.scalar() return True if result > 0 else False else: return False def _get_vnfd(self, include_fields=None): response = dict() to_fields = set( self.simple_instantiated_attributes).intersection(include_fields) for field in to_fields: response[field] = utils.deepgetattr( self, self.FLATTEN_ATTRIBUTES[field][0]) return response def _get_checksum(self, include_fields=None): response = dict() to_fields = set([ key for key in self.FLATTEN_ATTRIBUTES.keys() if key.startswith('checksum') ]) to_fields = to_fields.intersection(include_fields) for field in to_fields: display_field = field.split("/")[-1] response[display_field] = getattr( self, self.FLATTEN_ATTRIBUTES[field][0]) return {'checksum': response} if response else None def _get_user_defined_data(self, include_fields=None): # Need special handling for field containing key-value pair. # If user requests userDefined/key1 and if userDefineData contains # key1=value1, key2-value2, it should return only keys that are # requested in include_fields. If user requests only userDefinedData, # then in that case,it should return all key/value pairs. In case, # if any of the requested key is not present, then it should # siliently ignore it. key = 'userDefinedData' if key in include_fields or 'userDefinedData/*' in include_fields: return {key: self.user_data} else: # Check if user has requested specified keys from # userDefinedData. data_resp = dict() key_list = [] special_key = 'userDefinedData/' for field in include_fields: if field.startswith(special_key): key_list.append(field[len(special_key):]) for key_req in key_list: if key_req in self.user_data: data_resp[key_req] = self.user_data[key_req] if data_resp: return {key: data_resp} def _basic_vnf_package_info(self, include_fields=None): response = dict() to_fields = set(self.simple_attributes).intersection(include_fields) for field in to_fields: response[field] = getattr(self, self.FLATTEN_ATTRIBUTES[field][0]) return response def to_dict(self, include_fields=None): if not include_fields: include_fields = set(self.FLATTEN_ATTRIBUTES.keys()) vnf_package_response = self._basic_vnf_package_info( include_fields=include_fields) user_defined_data = self._get_user_defined_data( include_fields=include_fields) if user_defined_data: vnf_package_response.update(user_defined_data) if (self.onboarding_state == fields.PackageOnboardingStateType.ONBOARDED): software_images = self.vnf_deployment_flavours.to_dict( include_fields=include_fields) if software_images: vnf_package_response.update( {'softwareImages': software_images}) vnf_package_response.update( self._get_vnfd(include_fields=include_fields)) checksum = self._get_checksum(include_fields=include_fields) if checksum: vnf_package_response.update(checksum) return vnf_package_response
class VnfSoftwareImage(base.TackerObject, base.TackerPersistentObject): # Version 1.0: Initial version VERSION = '1.0' fields = { 'id': fields.UUIDField(nullable=False), 'software_image_id': fields.StringField(nullable=False), 'flavour_uuid': fields.UUIDField(nullable=False), 'name': fields.StringField(nullable=True), 'provider': fields.StringField(nullable=True), 'version': fields.StringField(nullable=True), 'algorithm': fields.StringField(nullable=True), 'hash': fields.StringField(nullable=True), 'container_format': fields.StringField(nullable=True), 'disk_format': fields.StringField(nullable=True), 'min_disk': fields.IntegerField(), 'min_ram': fields.IntegerField(default=0), 'size': fields.IntegerField(), 'image_path': fields.StringField(), 'metadata': fields.DictOfStringsField(nullable=True) } @staticmethod def _from_db_object(context, vnf_sw_image, db_sw_image, expected_attrs=None): vnf_sw_image._context = context for key in vnf_sw_image.fields: if key in VNF_SOFTWARE_IMAGE_OPTIONAL_ATTRS: continue else: db_key = key setattr(vnf_sw_image, key, db_sw_image[db_key]) vnf_sw_image._extra_attributes_from_db_object(vnf_sw_image, db_sw_image, expected_attrs) vnf_sw_image.obj_reset_changes() return vnf_sw_image @staticmethod def _extra_attributes_from_db_object(vnf_sw_image, db_sw_image, expected_attrs=None): """Method to help with migration of extra attributes to objects. """ if expected_attrs is None: expected_attrs = [] if 'metadata' in expected_attrs: setattr(vnf_sw_image, 'metadata', db_sw_image['metadetails']) def obj_load_attr(self, attrname): if not self._context: raise exceptions.OrphanedObjectError(method='obj_load_attr', objtype=self.obj_name()) if 'id' not in self: raise exceptions.ObjectActionError( action='obj_load_attr', reason=_('attribute %s not lazy-loadable') % attrname) LOG.debug("Lazy-loading '%(attr)s' on %(name)s id %(id)s", { 'attr': attrname, 'name': self.obj_name(), 'id': self.id, }) self._obj_load_attr(attrname) def _obj_load_attr(self, attrname): """Internal method for loading attributes from vnf flavour.""" if attrname in self.fields and attrname != 'id': self._load_generic(attrname) else: # NOTE(nirajsingh): Raise error if non existing field is # requested. raise exceptions.ObjectActionError( action='obj_load_attr', reason=_('attribute %s not lazy-loadable') % attrname) self.obj_reset_changes([attrname]) def _load_generic(self, attrname): software_image = self.__class__.get_by_id(self._context, id=self.id, expected_attrs=attrname) if attrname not in software_image: raise exceptions.ObjectActionError( action='obj_load_attr', reason=_('loading %s requires recursion') % attrname) for field in self.fields: if field in software_image and field not in self: setattr(self, field, getattr(software_image, field)) @base.remotable def create(self): if self.obj_attr_is_set('id'): raise exceptions.ObjectActionError(action='create', reason=_('already created')) updates = self.obj_get_changes() if 'id' not in updates: updates['id'] = uuidutils.generate_uuid() self.id = updates['id'] metadata = updates.pop('metadata', None) db_sw_image = _vnf_sw_image_create(self._context, updates, metadata=metadata) self._from_db_object(self._context, self, db_sw_image) @base.remotable_classmethod def get_by_id(cls, context, id, expected_attrs=None): db_sw_image = _vnf_sw_image_get_by_id(context, id) return cls._from_db_object(context, cls(), db_sw_image, expected_attrs=expected_attrs)
class VnfSoftwareImage(base.TackerObject, base.TackerPersistentObject): ALL_ATTRIBUTES = { "softwareImages": { 'id': ('software_image_id', 'string', 'VnfSoftwareImage'), 'imagePath': ('image_path', 'string', 'VnfSoftwareImage'), 'diskFormat': ('disk_format', 'string', 'VnfSoftwareImage'), 'userMetadata/*': ('metadata', 'key_value_pair', { "key_column": "key", "value_column": "value", "model": "VnfSoftwareImageMetadata" }), 'size': ('size', 'number', 'VnfSoftwareImage'), 'createdAt': ('created_at', 'datetime', 'VnfSoftwareImage'), 'name': ('name', 'string', 'VnfSoftwareImage'), 'minDisk': ('min_disk', 'number', 'VnfSoftwareImage'), 'version': ('version', 'string', 'VnfSoftwareImage'), 'provider': ('provider', 'string', 'VnfSoftwareImage'), 'minRam': ('min_ram', 'number', 'VnfSoftwareImage'), 'containerFormat': ('container_format', 'string', 'VnfSoftwareImage'), "checksum": { 'hash': ('hash', 'string', 'VnfSoftwareImage'), 'algorithm': ('algorithm', 'string', 'VnfSoftwareImage') } } } FLATTEN_ATTRIBUTES = utils.flatten_dict(ALL_ATTRIBUTES.copy()) SIMPLE_ATTRIBUTES = [ 'id', 'imagePath', 'diskFormat', 'size', 'createdAt', 'name', 'minDisk', 'version', 'provider', 'minRam', 'containerFormat' ] COMPLEX_ATTRIBUTES = [ 'softwareImages', 'softwareImages/userMetadata', 'softwareImages/checksum' ] # Version 1.0: Initial version VERSION = '1.0' fields = { 'id': fields.UUIDField(nullable=False), 'software_image_id': fields.StringField(nullable=False), 'flavour_uuid': fields.UUIDField(nullable=False), 'name': fields.StringField(nullable=True), 'provider': fields.StringField(nullable=True), 'version': fields.StringField(nullable=True), 'algorithm': fields.StringField(nullable=True), 'hash': fields.StringField(nullable=True), 'container_format': fields.StringField(nullable=True), 'disk_format': fields.StringField(nullable=True), 'min_disk': fields.IntegerField(), 'min_ram': fields.IntegerField(default=0), 'size': fields.IntegerField(), 'image_path': fields.StringField(), 'metadata': fields.DictOfStringsField(nullable=True) } @staticmethod def _from_db_object(context, vnf_sw_image, db_sw_image, expected_attrs=None): vnf_sw_image._context = context for key in vnf_sw_image.fields: if key in VNF_SOFTWARE_IMAGE_OPTIONAL_ATTRS: continue else: db_key = key setattr(vnf_sw_image, key, db_sw_image[db_key]) vnf_sw_image._extra_attributes_from_db_object(vnf_sw_image, db_sw_image, expected_attrs) vnf_sw_image.obj_reset_changes() return vnf_sw_image @staticmethod def _extra_attributes_from_db_object(vnf_sw_image, db_sw_image, expected_attrs=None): """Method to help with migration of extra attributes to objects. """ if expected_attrs is None: expected_attrs = [] if 'metadata' in expected_attrs: setattr(vnf_sw_image, 'metadata', db_sw_image['metadetails']) def obj_load_attr(self, attrname): if not self._context: raise exceptions.OrphanedObjectError(method='obj_load_attr', objtype=self.obj_name()) if 'id' not in self: raise exceptions.ObjectActionError( action='obj_load_attr', reason=_('attribute %s not lazy-loadable') % attrname) LOG.debug("Lazy-loading '%(attr)s' on %(name)s id %(id)s", { 'attr': attrname, 'name': self.obj_name(), 'id': self.id, }) self._obj_load_attr(attrname) def _obj_load_attr(self, attrname): """Internal method for loading attributes from vnf flavour.""" if attrname in self.fields and attrname != 'id': self._load_generic(attrname) else: # NOTE(nirajsingh): Raise error if non existing field is # requested. raise exceptions.ObjectActionError( action='obj_load_attr', reason=_('attribute %s not lazy-loadable') % attrname) self.obj_reset_changes([attrname]) def _load_generic(self, attrname): software_image = self.__class__.get_by_id(self._context, id=self.id, expected_attrs=attrname) if attrname not in software_image: raise exceptions.ObjectActionError( action='obj_load_attr', reason=_('loading %s requires recursion') % attrname) for field in self.fields: if field in software_image and field not in self: setattr(self, field, getattr(software_image, field)) @base.remotable def create(self): if self.obj_attr_is_set('id'): raise exceptions.ObjectActionError(action='create', reason=_('already created')) updates = self.obj_get_changes() if 'id' not in updates: updates['id'] = uuidutils.generate_uuid() self.id = updates['id'] metadata = updates.pop('metadata', None) db_sw_image = _vnf_sw_image_create(self._context, updates, metadata=metadata) self._from_db_object(self._context, self, db_sw_image) @base.remotable_classmethod def get_by_id(cls, context, id, expected_attrs=None): db_sw_image = _vnf_sw_image_get_by_id(context, id) return cls._from_db_object(context, cls(), db_sw_image, expected_attrs=expected_attrs) def _get_user_metadata(self, include_fields=None): # Need special handling for field containing key-value pair. # If user requests softwareImages/userMetadata/key1 and if # softwareImages/userMetadata contains key1=value1, key2=value2, # it should return only keys that are requested in include_fields. # If user requests only softwareImages/userMetadata, then in that # case, it should return all key/value pairs. If any of the requested # key is not present, then it will siliently ignore it. key = 'softwareImages/userMetadata' if key in include_fields or '%s/*' % key in \ include_fields: return self.metadata else: # Check if user has requested specified keys from # softwareImages/userMetadata. key_list = [] special_key = '%s/' % key for field in include_fields: if field.startswith(special_key): key_list.append(field[len(special_key):]) data_resp = dict() for key_req in key_list: if key_req in self.metadata: data_resp[key_req] = self.metadata[key_req] if len(key_list) > 0: return data_resp def to_dict(self, include_fields=None): response = dict() fields = [ 'softwareImages/%s' % attribute for attribute in self.SIMPLE_ATTRIBUTES ] to_fields = set(fields).intersection(include_fields) for field in to_fields: display_field = field.split("/")[-1] response[display_field] = getattr( self, self.FLATTEN_ATTRIBUTES[field][0]) # add checksum to_fields = set([ key for key in self.FLATTEN_ATTRIBUTES.keys() if key.startswith('softwareImages/checksum') ]) checksum = dict() to_fields = to_fields.intersection(include_fields) for field in to_fields: display_field = field.split("/")[-1] checksum[display_field] = getattr( self, self.FLATTEN_ATTRIBUTES[field][0]) if checksum: response.update({"checksum": checksum}) user_metadata = self._get_user_metadata(include_fields) if user_metadata is not None: response.update({"userMetadata": user_metadata}) return response