Migrate mistral to using the serialization code in mistral-lib
We previously ported the code to mistral-lib, but Mistral has been using the original copy. Closes-Bug: #1782765 Change-Id: Ifb518d821097fdf2ec76161ae00f312ced19c272
This commit is contained in:
parent
8b0171ba25
commit
fe6f0c5c34
|
@ -16,6 +16,7 @@
|
|||
import base64
|
||||
|
||||
from mistral_lib.actions import context as lib_ctx
|
||||
from mistral_lib import serialization
|
||||
from oslo_config import cfg
|
||||
from oslo_context import context as oslo_context
|
||||
import oslo_messaging as messaging
|
||||
|
@ -26,7 +27,6 @@ from pecan import hooks
|
|||
|
||||
from mistral import auth
|
||||
from mistral import exceptions as exc
|
||||
from mistral import serialization
|
||||
from mistral import utils
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
|
|
@ -15,12 +15,9 @@
|
|||
import abc
|
||||
import six
|
||||
|
||||
from mistral import serialization
|
||||
from mistral_lib.actions import types
|
||||
from stevedore import driver
|
||||
|
||||
_EXECUTORS = {}
|
||||
serialization.register_serializer(types.Result, types.ResultSerializer())
|
||||
|
||||
|
||||
def cleanup():
|
||||
|
|
|
@ -14,11 +14,11 @@
|
|||
|
||||
import kombu
|
||||
|
||||
from mistral_lib import serialization as mistral_serialization
|
||||
import oslo_messaging as messaging
|
||||
|
||||
from mistral import config as cfg
|
||||
from mistral import exceptions as exc
|
||||
from mistral import serialization as mistral_serialization
|
||||
from mistral.utils import rpc_utils
|
||||
|
||||
IS_RECEIVED = 'kombu_rpc_is_received'
|
||||
|
|
|
@ -1,143 +0,0 @@
|
|||
# Copyright 2017 Nokia Networks.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from mistral_lib import serialization as ml_serialization
|
||||
from oslo_serialization import jsonutils
|
||||
|
||||
_SERIALIZER = None
|
||||
|
||||
# Backwards compatibility
|
||||
Serializer = ml_serialization.Serializer
|
||||
DictBasedSerializer = ml_serialization.DictBasedSerializer
|
||||
MistralSerializable = ml_serialization.MistralSerializable
|
||||
|
||||
|
||||
# PolymorphicSerializer cannot be used from mistral-lib yet
|
||||
# as mistral-lib does not have the unregister method.
|
||||
# Once it does this will be removed also in favor of mistral-lib
|
||||
|
||||
|
||||
class PolymorphicSerializer(Serializer):
|
||||
"""Polymorphic serializer.
|
||||
|
||||
The purpose of this class is to server as a serialization router
|
||||
between serializers that can work with entities of particular type.
|
||||
All concrete serializers associated with concrete entity classes
|
||||
should be registered via method 'register', after that an instance
|
||||
of polymorphic serializer can be used as a universal serializer
|
||||
for an RPC system or something else.
|
||||
When converting an object into a string this serializer also writes
|
||||
a special key into the result string sequence so that it's possible
|
||||
to find a proper serializer when deserializing this object.
|
||||
If a primitive value is given as an entity this serializer doesn't
|
||||
do anything special and simply converts a value into a string using
|
||||
jsonutils. Similar when it converts a string into a primitive value.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
# {serialization key: serializer}
|
||||
self.serializers = {}
|
||||
|
||||
@staticmethod
|
||||
def _get_serialization_key(entity_cls):
|
||||
if issubclass(entity_cls, MistralSerializable):
|
||||
return entity_cls.get_serialization_key()
|
||||
|
||||
return None
|
||||
|
||||
def register(self, entity_cls, serializer):
|
||||
key = self._get_serialization_key(entity_cls)
|
||||
|
||||
if not key:
|
||||
return
|
||||
|
||||
if key in self.serializers:
|
||||
raise RuntimeError(
|
||||
"A serializer for the entity class has already been"
|
||||
" registered: %s" % entity_cls
|
||||
)
|
||||
|
||||
self.serializers[key] = serializer
|
||||
|
||||
def unregister(self, entity_cls):
|
||||
key = self._get_serialization_key(entity_cls)
|
||||
|
||||
if not key:
|
||||
return
|
||||
|
||||
if key in self.serializers:
|
||||
del self.serializers[key]
|
||||
|
||||
def cleanup(self):
|
||||
self.serializers.clear()
|
||||
|
||||
def serialize(self, entity):
|
||||
if entity is None:
|
||||
return None
|
||||
|
||||
key = self._get_serialization_key(type(entity))
|
||||
|
||||
# Primitive or not registered type.
|
||||
if not key:
|
||||
return jsonutils.dumps(
|
||||
jsonutils.to_primitive(entity, convert_instances=True)
|
||||
)
|
||||
|
||||
serializer = self.serializers.get(key)
|
||||
|
||||
if not serializer:
|
||||
raise RuntimeError(
|
||||
"Failed to find a serializer for the key: %s" % key
|
||||
)
|
||||
|
||||
result = {
|
||||
'__serial_key': key,
|
||||
'__serial_data': serializer.serialize(entity)
|
||||
}
|
||||
|
||||
return jsonutils.dumps(result)
|
||||
|
||||
def deserialize(self, data_str):
|
||||
if data_str is None:
|
||||
return None
|
||||
|
||||
data = jsonutils.loads(data_str)
|
||||
|
||||
if isinstance(data, dict) and '__serial_key' in data:
|
||||
serializer = self.serializers.get(data['__serial_key'])
|
||||
|
||||
return serializer.deserialize(data['__serial_data'])
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def get_polymorphic_serializer():
|
||||
global _SERIALIZER
|
||||
|
||||
if _SERIALIZER is None:
|
||||
_SERIALIZER = PolymorphicSerializer()
|
||||
|
||||
return _SERIALIZER
|
||||
|
||||
|
||||
def register_serializer(entity_cls, serializer):
|
||||
get_polymorphic_serializer().register(entity_cls, serializer)
|
||||
|
||||
|
||||
def unregister_serializer(entity_cls):
|
||||
get_polymorphic_serializer().unregister(entity_cls)
|
||||
|
||||
|
||||
def cleanup():
|
||||
get_polymorphic_serializer().cleanup()
|
|
@ -1,108 +0,0 @@
|
|||
# Copyright 2017 - Nokia Networks.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from mistral import serialization
|
||||
from mistral.tests.unit import base
|
||||
|
||||
|
||||
class MyClass(serialization.MistralSerializable):
|
||||
def __init__(self, a, b):
|
||||
self.a = a
|
||||
self.b = b
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, MyClass):
|
||||
return False
|
||||
|
||||
return other.a == self.a and other.b == self.b
|
||||
|
||||
|
||||
class MyClassSerializer(serialization.DictBasedSerializer):
|
||||
def serialize_to_dict(self, entity):
|
||||
return {'a': entity.a, 'b': entity.b}
|
||||
|
||||
def deserialize_from_dict(self, entity_dict):
|
||||
return MyClass(entity_dict['a'], entity_dict['b'])
|
||||
|
||||
|
||||
class SerializationTest(base.BaseTest):
|
||||
def setUp(self):
|
||||
super(SerializationTest, self).setUp()
|
||||
|
||||
serialization.register_serializer(MyClass, MyClassSerializer())
|
||||
|
||||
self.addCleanup(serialization.unregister_serializer, MyClass)
|
||||
|
||||
def test_dict_based_serializer(self):
|
||||
obj = MyClass('a', 'b')
|
||||
|
||||
serializer = MyClassSerializer()
|
||||
|
||||
s = serializer.serialize(obj)
|
||||
|
||||
self.assertEqual(obj, serializer.deserialize(s))
|
||||
|
||||
self.assertIsNone(serializer.serialize(None))
|
||||
self.assertIsNone(serializer.deserialize(None))
|
||||
|
||||
def test_polymorphic_serializer_primitive_types(self):
|
||||
serializer = serialization.get_polymorphic_serializer()
|
||||
|
||||
self.assertEqual(17, serializer.deserialize(serializer.serialize(17)))
|
||||
self.assertEqual(
|
||||
0.34,
|
||||
serializer.deserialize(serializer.serialize(0.34))
|
||||
)
|
||||
self.assertEqual(-5, serializer.deserialize(serializer.serialize(-5)))
|
||||
self.assertEqual(
|
||||
-6.3,
|
||||
serializer.deserialize(serializer.serialize(-6.3))
|
||||
)
|
||||
self.assertFalse(serializer.deserialize(serializer.serialize(False)))
|
||||
self.assertTrue(serializer.deserialize(serializer.serialize(True)))
|
||||
self.assertEqual(
|
||||
'abc',
|
||||
serializer.deserialize(serializer.serialize('abc'))
|
||||
)
|
||||
self.assertEqual(
|
||||
{'a': 'b', 'c': 'd'},
|
||||
serializer.deserialize(serializer.serialize({'a': 'b', 'c': 'd'}))
|
||||
)
|
||||
self.assertEqual(
|
||||
['a', 'b', 'c'],
|
||||
serializer.deserialize(serializer.serialize(['a', 'b', 'c']))
|
||||
)
|
||||
|
||||
def test_polymorphic_serializer_custom_object(self):
|
||||
serializer = serialization.get_polymorphic_serializer()
|
||||
|
||||
obj = MyClass('a', 'b')
|
||||
|
||||
s = serializer.serialize(obj)
|
||||
|
||||
self.assertIn('__serial_key', s)
|
||||
self.assertIn('__serial_data', s)
|
||||
|
||||
self.assertEqual(obj, serializer.deserialize(s))
|
||||
|
||||
self.assertIsNone(serializer.serialize(None))
|
||||
self.assertIsNone(serializer.deserialize(None))
|
||||
|
||||
def test_register_twice(self):
|
||||
self.assertRaises(
|
||||
RuntimeError,
|
||||
serialization.register_serializer,
|
||||
MyClass,
|
||||
MyClassSerializer()
|
||||
)
|
Loading…
Reference in New Issue