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