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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.parameters.eventprotocol;
23 import java.lang.reflect.Type;
24 import java.util.HashMap;
27 import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters;
28 import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JSONEventProtocolParameters;
29 import org.onap.policy.apex.service.parameters.ApexParameterRuntimeException;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
33 import com.google.gson.JsonDeserializationContext;
34 import com.google.gson.JsonDeserializer;
35 import com.google.gson.JsonElement;
36 import com.google.gson.JsonObject;
37 import com.google.gson.JsonParseException;
38 import com.google.gson.JsonPrimitive;
39 import com.google.gson.JsonSerializationContext;
40 import com.google.gson.JsonSerializer;
43 * This class serialises and deserialises various type of event protocol parameters to and from
46 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public class EventProtocolParametersJSONAdapter
49 implements JsonSerializer<EventProtocolParameters>, JsonDeserializer<EventProtocolParameters> {
50 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventProtocolParametersJSONAdapter.class);
52 private static final String PARAMETER_CLASS_NAME = "parameterClassName";
54 private static final String EVENT_PROTOCOL_TOKEN = "eventProtocol";
55 private static final String EVENT_PROTOCOL_PARAMETERS = "parameters";
57 // Built in event protocol parameters
58 private static final Map<String, String> BUILT_IN_EVENT_RPOTOCOL_PARMETER_CLASS_MAP = new HashMap<>();
61 BUILT_IN_EVENT_RPOTOCOL_PARMETER_CLASS_MAP.put("JSON", JSONEventProtocolParameters.class.getCanonicalName());
62 BUILT_IN_EVENT_RPOTOCOL_PARMETER_CLASS_MAP.put("APEX", ApexEventProtocolParameters.class.getCanonicalName());
68 * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type,
69 * com.google.gson.JsonSerializationContext)
72 public JsonElement serialize(final EventProtocolParameters src, final Type typeOfSrc,
73 final JsonSerializationContext context) {
74 final String returnMessage = "serialization of Apex event protocol parameters to Json is not supported";
75 LOGGER.error(returnMessage);
76 throw new ApexParameterRuntimeException(returnMessage);
82 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
83 * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
86 public EventProtocolParameters deserialize(final JsonElement json, final Type typeOfT,
87 final JsonDeserializationContext context) throws JsonParseException {
88 final JsonObject jsonObject = json.getAsJsonObject();
90 // Get the event protocol label primitive
91 final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(EVENT_PROTOCOL_TOKEN);
93 // Check if we found our event protocol
94 if (labelJsonPrimitive == null) {
95 LOGGER.warn("event protocol parameter \"" + EVENT_PROTOCOL_TOKEN + "\" not found in JSON file");
99 // Get and check the event protocol label
100 final String eventProtocolLabel = labelJsonPrimitive.getAsString().replaceAll("\\s+", "");
101 if (eventProtocolLabel == null || eventProtocolLabel.length() == 0) {
102 final String errorMessage = "event protocol parameter \"" + EVENT_PROTOCOL_TOKEN + "\" value \""
103 + labelJsonPrimitive.getAsString() + "\" invalid in JSON file";
104 LOGGER.warn(errorMessage);
105 throw new ApexParameterRuntimeException(errorMessage);
108 // We now get the event protocol parameter class
109 String eventProtocolParameterClassName = null;
111 // Get the event protocol parameter class for the event protocol plugin class from the
112 // configuration parameters
113 final JsonPrimitive classNameJsonPrimitive = (JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME);
115 // If no event protocol parameter class was specified, we use the default
116 if (classNameJsonPrimitive == null) {
117 eventProtocolParameterClassName = BUILT_IN_EVENT_RPOTOCOL_PARMETER_CLASS_MAP.get(eventProtocolLabel);
119 // We use the specified one
120 eventProtocolParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
123 // Check the event protocol parameter class
124 if (eventProtocolParameterClassName == null || eventProtocolParameterClassName.length() == 0) {
125 final String errorMessage =
126 "event protocol \"" + eventProtocolLabel + "\" parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
127 + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
128 + "\" invalid in JSON file";
129 LOGGER.warn(errorMessage);
130 throw new ApexParameterRuntimeException(errorMessage);
133 // Get the class for the event protocol
134 Class<?> eventProtocolParameterClass = null;
136 eventProtocolParameterClass = Class.forName(eventProtocolParameterClassName);
137 } catch (final ClassNotFoundException e) {
138 final String errorMessage =
139 "event protocol \"" + eventProtocolLabel + "\" parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
140 + eventProtocolParameterClassName + "\", could not find class";
141 LOGGER.warn(errorMessage, e);
142 throw new ApexParameterRuntimeException(errorMessage, e);
145 // Deserialise the class
146 EventProtocolParameters eventProtocolParameters =
147 context.deserialize(jsonObject.get(EVENT_PROTOCOL_PARAMETERS), eventProtocolParameterClass);
148 if (eventProtocolParameters == null) {
149 // OK no parameters for the event protocol have been specified, just instantiate the
150 // default parameters
152 eventProtocolParameters = (EventProtocolParameters) eventProtocolParameterClass.newInstance();
153 } catch (final Exception e) {
154 final String errorMessage = "could not create default parameters for event protocol \""
155 + eventProtocolLabel + "\"\n" + e.getMessage();
156 LOGGER.warn(errorMessage, e);
157 throw new ApexParameterRuntimeException(errorMessage, e);
161 // Check that the event protocol label matches the label in the event protocol parameters
163 if (!eventProtocolParameters.getLabel().equals(eventProtocolLabel)) {
164 final String errorMessage = "event protocol \"" + eventProtocolLabel + "\" does not match plugin \""
165 + eventProtocolParameters.getLabel() + "\" in \"" + eventProtocolParameterClassName
166 + "\", specify correct event protocol parameter plugin in parameter \"" + PARAMETER_CLASS_NAME
168 LOGGER.warn(errorMessage);
169 throw new ApexParameterRuntimeException(errorMessage);
172 return eventProtocolParameters;