summaryrefslogtreecommitdiff
path: root/trove/common/rpc/serializer.py
blob: 2bc2cdb8f06efb7c50913c3544da1505b6c2e373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright 2016 Tesora, Inc.
# 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.

import oslo_messaging as messaging
from osprofiler import profiler

from trove.common.context import TroveContext


class TroveSerializer(messaging.Serializer):
    """The Trove serializer class that handles class inheritance and base
       serializers.
    """

    def __init__(self, base):
        self._base = base

    def _serialize_entity(self, context, entity):
        return entity

    def serialize_entity(self, context, entity):
        if self._base:
            entity = self._base.serialize_entity(context, entity)

        return self._serialize_entity(context, entity)

    def _deserialize_entity(self, context, entity):
        return entity

    def deserialize_entity(self, context, entity):
        entity = self._deserialize_entity(context, entity)

        if self._base:
            entity = self._base.deserialize_entity(context, entity)

        return entity

    def _serialize_context(self, context):
        return context

    def serialize_context(self, context):
        if self._base:
            context = self._base.serialize_context(context)

        return self._serialize_context(context)

    def _deserialize_context(self, context):
        return context

    def deserialize_context(self, context):
        context = self._deserialize_context(context)

        if self._base:
            context = self._base.deserialize_context(context)

        return context


class TroveRequestContextSerializer(TroveSerializer):
    def _serialize_context(self, context):
        _context = context.to_dict()
        prof = profiler.get()
        if prof:
            trace_info = {
                "hmac_key": prof.hmac_key,
                "base_id": prof.get_base_id(),
                "parent_id": prof.get_id()
            }
            _context.update({"trace_info": trace_info})
        return _context

    def _deserialize_context(self, context):
        trace_info = context.pop("trace_info", None)
        if trace_info:
            profiler.init(**trace_info)
        return TroveContext.from_dict(context)