9d087bf31c3d1d5e34c5bde6a1e54037126e544b
[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 java.lang.reflect.Type;
24 import java.util.HashMap;
25 import java.util.Map;
26
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;
32
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;
41
42 /**
43  * This class serialises and deserialises various type of event protocol parameters to and from
44  * JSON.
45  *
46  * @author Liam Fallon (liam.fallon@ericsson.com)
47  */
48 public class EventProtocolParametersJSONAdapter
49         implements JsonSerializer<EventProtocolParameters>, JsonDeserializer<EventProtocolParameters> {
50     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventProtocolParametersJSONAdapter.class);
51
52     private static final String PARAMETER_CLASS_NAME = "parameterClassName";
53
54     private static final String EVENT_PROTOCOL_TOKEN = "eventProtocol";
55     private static final String EVENT_PROTOCOL_PARAMETERS = "parameters";
56
57     // Built in event protocol parameters
58     private static final Map<String, String> BUILT_IN_EVENT_RPOTOCOL_PARMETER_CLASS_MAP = new HashMap<>();
59     
60     static {
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());
63     }
64
65     /*
66      * (non-Javadoc)
67      *
68      * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type,
69      * com.google.gson.JsonSerializationContext)
70      */
71     @Override
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);
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
83      * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
84      */
85     @Override
86     public EventProtocolParameters deserialize(final JsonElement json, final Type typeOfT,
87             final JsonDeserializationContext context) throws JsonParseException {
88         final JsonObject jsonObject = json.getAsJsonObject();
89
90         // Get the event protocol label primitive
91         final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(EVENT_PROTOCOL_TOKEN);
92
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");
96             return null;
97         }
98
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);
106         }
107
108         // We now get the event protocol parameter class
109         String eventProtocolParameterClassName = null;
110
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);
114
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);
118         } else {
119             // We use the specified one
120             eventProtocolParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
121         }
122
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);
131         }
132
133         // Get the class for the event protocol
134         Class<?> eventProtocolParameterClass = null;
135         try {
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);
143         }
144
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
151             try {
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);
158             }
159         }
160
161         // Check that the event protocol label matches the label in the event protocol parameters
162         // object
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
167                     + "\"";
168             LOGGER.warn(errorMessage);
169             throw new ApexParameterRuntimeException(errorMessage);
170         }
171
172         return eventProtocolParameters;
173     }
174 }