aboutsummaryrefslogtreecommitdiffstats
path: root/feature-pooling-messages/src/main/java/org/onap/policy/drools/pooling/Serializer.java
blob: 2894b1da4de573dca55e47678867dec392f6e0f6 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2024 Nordix Foundation.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.pooling;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.util.HashMap;
import java.util.Map;
import org.onap.policy.drools.pooling.message.Heartbeat;
import org.onap.policy.drools.pooling.message.Identification;
import org.onap.policy.drools.pooling.message.Leader;
import org.onap.policy.drools.pooling.message.Message;
import org.onap.policy.drools.pooling.message.Offline;
import org.onap.policy.drools.pooling.message.Query;

/**
 * Serialization helper functions.
 */
public class Serializer {

    /**
     * The message type is stored in fields of this name within the JSON.
     */
    private static final String TYPE_FIELD = "type";

    /**
     * Used to encode & decode JSON messages sent & received, respectively, on the
     * internal topic.
     */
    private final Gson gson = new Gson();

    /**
     * Maps a message subclass to its type.
     */
    private static final Map<Class<? extends Message>, String> class2type = new HashMap<>();

    /**
     * Maps a message type to the appropriate subclass.
     */
    private static final Map<String, Class<? extends Message>> type2class = new HashMap<>();

    static {
        class2type.put(Heartbeat.class, "heartbeat");
        class2type.put(Identification.class, "identification");
        class2type.put(Leader.class, "leader");
        class2type.put(Offline.class, "offline");
        class2type.put(Query.class, "query");

        class2type.forEach((clazz, type) -> type2class.put(type, clazz));
    }

    /**
     * Encodes a filter.
     *
     * @param filter filter to be encoded
     * @return the filter, serialized as a JSON string
     */
    public String encodeFilter(Map<String, Object> filter) {
        return gson.toJson(filter);
    }

    /**
     * Encodes a message.
     *
     * @param msg message to be encoded
     * @return the message, serialized as a JSON string
     */
    public String encodeMsg(Message msg) {
        JsonElement jsonEl = gson.toJsonTree(msg);

        String type = class2type.get(msg.getClass());
        if (type == null) {
            throw new JsonParseException("cannot serialize " + msg.getClass());
        }

        jsonEl.getAsJsonObject().addProperty(TYPE_FIELD, type);

        return gson.toJson(jsonEl);
    }

    /**
     * Decodes a JSON string into a Message.
     *
     * @param msg JSON string representing the message
     * @return the message
     */
    public Message decodeMsg(String msg) {
        JsonElement jsonEl = gson.fromJson(msg, JsonElement.class);

        JsonElement typeEl = jsonEl.getAsJsonObject().get(TYPE_FIELD);
        if (typeEl == null) {
            throw new JsonParseException("cannot deserialize " + Message.class
                            + " because it does not contain a field named " + TYPE_FIELD);

        }

        Class<? extends Message> clazz = type2class.get(typeEl.getAsString());
        if (clazz == null) {
            throw new JsonParseException("cannot deserialize " + typeEl);
        }

        return gson.fromJson(jsonEl, clazz);
    }
}