Add obj_make_compatible()

This adds a method to CyborgObject that allows it to convert itself
to and older version, within a compatibility window. So, if an object
had a revision that added or changed the formatting of an attribute,
the obj_make_compatible() method can fix up a primitive representation
before it is sent to a client expecting the older version.

Partial-Implements: blueprint add-description-field-to-device-profiles

Change-Id: I196629059bc32165f161fe9c071a339d63d71c10
This commit is contained in:
zhangbailin 2020-01-19 20:50:29 +08:00 committed by Brin Zhang
parent 3c7e0868e6
commit 9831730208

View File

@ -16,6 +16,7 @@
"""Cyborg common internal object model"""
import netaddr
from oslo_log import log as logging
from oslo_utils import versionutils
from oslo_versionedobjects import base as object_base
@ -23,6 +24,9 @@ from cyborg import objects
from cyborg.objects import fields as object_fields
LOG = logging.getLogger(__name__)
class CyborgObjectRegistry(object_base.VersionedObjectRegistry):
def registration_hook(self, cls, index):
# NOTE(jroll): blatantly stolen from nova
@ -95,6 +99,22 @@ class CyborgObject(object_base.VersionedObject):
objs.append(cls._from_db_object(cls(context), db_obj))
return objs
def obj_make_compatible(self, primitive, target_version):
"""Make an object representation compatible with a target version.
This is responsible for taking the primitive representation of
an object and making it suitable for the given target_version.
This may mean converting the format of object attributes, removing
attributes that have been added since the target version, etc.
:param:primitive: The result of self.obj_to_primitive()
:param:target_version: The version string requested by the recipient
of the object.
"""
_log_backport(self, target_version)
super(CyborgObject, self).obj_make_compatible(primitive,
target_version)
class CyborgObjectSerializer(object_base.VersionedObjectSerializer):
# Base class to use for object hydration
@ -199,3 +219,13 @@ class DriverObjectBase(CyborgObject):
obj.obj_reset_changes()
return obj
def _log_backport(ovo, target_version):
"""Log backported versioned objects."""
if target_version and target_version != ovo.VERSION:
LOG.debug('Backporting %(obj_name)s from version %(src_vers)s '
'to version %(dst_vers)s',
{'obj_name': ovo.obj_name(),
'src_vers': ovo.VERSION,
'dst_vers': target_version})