b03c365f160b48fd0bd269146a8cbe93d5ffba7b
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.parameters.eventprotocol;
22
23 import com.google.gson.JsonDeserializationContext;
24 import com.google.gson.JsonDeserializer;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonPrimitive;
28 import com.google.gson.JsonSerializationContext;
29 import com.google.gson.JsonSerializer;
30
31 import java.lang.reflect.Type;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters;
36 import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters;
37 import org.onap.policy.common.parameters.ParameterRuntimeException;
38 import org.slf4j.ext.XLogger;
39 import org.slf4j.ext.XLoggerFactory;
40
41 /**
42  * This class serialises and deserialises various type of event protocol parameters to and from
43  * JSON.
44  *
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  */
47 public class EventProtocolParametersJsonAdapter
48         implements JsonSerializer<EventProtocolParameters>, JsonDeserializer<EventProtocolParameters> {
49     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventProtocolParametersJsonAdapter.class);
50
51     // Recurring string constants
52     private static final String EVENT_PROTOCOL_PREFIX = "event protocol \"";
53     private static final String VALUE_TAG = "\" value \"";
54
55     private static final String PARAMETER_CLASS_NAME = "parameterClassName";
56
57     private static final String EVENT_PROTOCOL_TOKEN = "eventProtocol";
58     private static final String EVENT_PROTOCOL_PARAMETERS = "parameters";
59
60     // Built in event protocol parameters
61     private static final Map<String, String> BUILT_IN_EVENT_PROTOCOL_PARMETER_CLASS_MAP = new HashMap<>();
62
63     static {
64         BUILT_IN_EVENT_PROTOCOL_PARMETER_CLASS_MAP.put("JSON", JsonEventProtocolParameters.class.getName());
65         BUILT_IN_EVENT_PROTOCOL_PARMETER_CLASS_MAP.put("APEX", ApexEventProtocolParameters.class.getName());
66     }
67
68     /*
69      * (non-Javadoc)
70      *
71      * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type,
72      * com.google.gson.JsonSerializationContext)
73      */
74     @Override
75     public JsonElement serialize(final EventProtocolParameters src, final Type typeOfSrc,
76             final JsonSerializationContext context) {
77         final String returnMessage = "serialization of Apex event protocol parameters to Json is not supported";
78         LOGGER.error(returnMessage);
79         throw new ParameterRuntimeException(returnMessage);
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
86      * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
87      */
88     @Override
89     public EventProtocolParameters deserialize(final JsonElement json, final Type typeOfT,
90             final JsonDeserializationContext context) {
91         final JsonObject jsonObject = json.getAsJsonObject();
92
93         // Get the event protocol label primitive
94         final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(EVENT_PROTOCOL_TOKEN);
95
96         // Check if we found our event protocol
97         if (labelJsonPrimitive == null) {
98             LOGGER.warn("event protocol parameter \"" + EVENT_PROTOCOL_TOKEN + "\" not found in JSON file");
99             return null;
100         }
101
102         // Get and check the event protocol label
103         final String eventProtocolLabel = labelJsonPrimitive.getAsString().replaceAll("\\s+", "");
104         if (eventProtocolLabel == null || eventProtocolLabel.length() == 0) {
105             final String errorMessage = "event protocol parameter \"" + EVENT_PROTOCOL_TOKEN + VALUE_TAG
106                     + labelJsonPrimitive.getAsString() + "\" invalid in JSON file";
107             LOGGER.warn(errorMessage);
108             throw new ParameterRuntimeException(errorMessage);
109         }
110
111         // We now get the event protocol parameter class
112         String eventProtocolParameterClassName = null;
113
114         // Get the event protocol parameter class for the event protocol plugin class from the
115         // configuration parameters
116         final JsonPrimitive classNameJsonPrimitive = (JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME);
117
118         // If no event protocol parameter class was specified, we use the default
119         if (classNameJsonPrimitive == null) {
120             eventProtocolParameterClassName = BUILT_IN_EVENT_PROTOCOL_PARMETER_CLASS_MAP.get(eventProtocolLabel);
121         } else {
122             // We use the specified one
123             eventProtocolParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
124         }
125
126         // Check the event protocol parameter class
127         if (eventProtocolParameterClassName == null || eventProtocolParameterClassName.length() == 0) {
128             final String errorMessage =
129                     EVENT_PROTOCOL_PREFIX + eventProtocolLabel + "\" parameter \"" + PARAMETER_CLASS_NAME + VALUE_TAG
130                             + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
131                             + "\" invalid in JSON file";
132             LOGGER.warn(errorMessage);
133             throw new ParameterRuntimeException(errorMessage);
134         }
135
136         // Get the class for the event protocol
137         Class<?> eventProtocolParameterClass = null;
138         try {
139             eventProtocolParameterClass = Class.forName(eventProtocolParameterClassName);
140         } catch (final ClassNotFoundException e) {
141             final String errorMessage =
142                     EVENT_PROTOCOL_PREFIX + eventProtocolLabel + "\" parameter \"" + PARAMETER_CLASS_NAME + VALUE_TAG
143                             + eventProtocolParameterClassName + "\", could not find class";
144             LOGGER.warn(errorMessage, e);
145             throw new ParameterRuntimeException(errorMessage, e);
146         }
147
148         // Deserialise the class
149         EventProtocolParameters eventProtocolParameters =
150                 context.deserialize(jsonObject.get(EVENT_PROTOCOL_PARAMETERS), eventProtocolParameterClass);
151         if (eventProtocolParameters == null) {
152             // OK no parameters for the event protocol have been specified, just instantiate the
153             // default parameters
154             try {
155                 eventProtocolParameters = (EventProtocolParameters) eventProtocolParameterClass.newInstance();
156             } catch (final Exception e) {
157                 final String errorMessage = "could not create default parameters for event protocol \""
158                         + eventProtocolLabel + "\"\n" + e.getMessage();
159                 LOGGER.warn(errorMessage, e);
160                 throw new ParameterRuntimeException(errorMessage, e);
161             }
162         }
163
164         // Check that the event protocol label matches the label in the event protocol parameters
165         // object
166         if (!eventProtocolParameters.getLabel().equals(eventProtocolLabel)) {
167             final String errorMessage = EVENT_PROTOCOL_PREFIX + eventProtocolLabel + "\" does not match plugin \""
168                     + eventProtocolParameters.getLabel() + "\" in \"" + eventProtocolParameterClassName
169                     + "\", specify correct event protocol parameter plugin in parameter \"" + PARAMETER_CLASS_NAME
170                     + "\"";
171             LOGGER.warn(errorMessage);
172             throw new ParameterRuntimeException(errorMessage);
173         }
174
175         return eventProtocolParameters;
176     }
177 }