b4e342f17789b58372fe97b742a8a0ac9a738ae7
[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.carriertechnology;
22
23 import java.lang.reflect.Type;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import com.google.gson.JsonDeserializationContext;
28 import com.google.gson.JsonDeserializer;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonPrimitive;
32 import com.google.gson.JsonSerializationContext;
33 import com.google.gson.JsonSerializer;
34
35 import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorCarrierTechnologyParameters;
36 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FILECarrierTechnologyParameters;
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 deserialises various type of carrier technology parameters from JSON.
43  *
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class CarrierTechnologyParametersJSONAdapter
47         implements JsonSerializer<CarrierTechnologyParameters>, JsonDeserializer<CarrierTechnologyParameters> {
48     private static final XLogger LOGGER = XLoggerFactory.getXLogger(CarrierTechnologyParametersJSONAdapter.class);
49
50     private static final String PARAMETER_CLASS_NAME = "parameterClassName";
51
52     private static final String CARRIER_TECHNOLOGY_TOKEN = "carrierTechnology";
53     private static final String CARRIER_TECHNOLOGY_PARAMETERS = "parameters";
54
55     // Built in technology parameters
56     private static final Map<String, String> BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP = new HashMap<>();
57     
58     static {
59         BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("FILE",
60                 FILECarrierTechnologyParameters.class.getCanonicalName());
61         BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("EVENT_REQUESTOR",
62                 EventRequestorCarrierTechnologyParameters.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 CarrierTechnologyParameters src, final Type typeOfSrc,
73             final JsonSerializationContext context) {
74         final String returnMessage = "serialization of Apex carrier technology parameters to Json is not supported";
75         LOGGER.error(returnMessage);
76         throw new ParameterRuntimeException(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 CarrierTechnologyParameters deserialize(final JsonElement json, final Type typeOfT,
87             final JsonDeserializationContext context) {
88         final JsonObject jsonObject = json.getAsJsonObject();
89
90         // Get the carrier technology label primitive
91         final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(CARRIER_TECHNOLOGY_TOKEN);
92
93         // Check if we found our carrier technology
94         if (labelJsonPrimitive == null) {
95             LOGGER.warn("carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + "\" not found in JSON file");
96             return null;
97         }
98
99         // Get and check the carrier technology label
100         final String carrierTechnologyLabel = labelJsonPrimitive.getAsString().replaceAll("\\s+", "");
101         if (carrierTechnologyLabel == null || carrierTechnologyLabel.length() == 0) {
102             final String errorMessage = "carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + "\" value \""
103                     + labelJsonPrimitive.getAsString() + "\" invalid in JSON file";
104             LOGGER.warn(errorMessage);
105             throw new ParameterRuntimeException(errorMessage);
106         }
107
108         // We now get the technology carrier parameter class
109         String carrierTechnologyParameterClassName = null;
110
111         // Get the technology carrier parameter class for the carrier technology plugin class from
112         // the configuration parameters
113         final JsonPrimitive classNameJsonPrimitive = (JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME);
114
115         // If no technology carrier parameter class was specified, we try to use a built in carrier
116         // technology
117         if (classNameJsonPrimitive == null) {
118             carrierTechnologyParameterClassName =
119                     BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.get(carrierTechnologyLabel);
120         } else {
121             // We use the specified one
122             carrierTechnologyParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
123         }
124
125         // Check the carrier technology parameter class
126         if (carrierTechnologyParameterClassName == null || carrierTechnologyParameterClassName.length() == 0) {
127             final String errorMessage = "carrier technology \"" + carrierTechnologyLabel + "\" parameter \""
128                     + PARAMETER_CLASS_NAME + "\" value \""
129                     + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
130                     + "\" invalid in JSON file";
131             LOGGER.warn(errorMessage);
132             throw new ParameterRuntimeException(errorMessage);
133         }
134
135         // Get the class for the carrier technology
136         Class<?> carrierTechnologyParameterClass = null;
137         try {
138             carrierTechnologyParameterClass = Class.forName(carrierTechnologyParameterClassName);
139         } catch (final ClassNotFoundException e) {
140             final String errorMessage =
141                     "carrier technology \"" + carrierTechnologyLabel + "\" parameter \"" + PARAMETER_CLASS_NAME
142                             + "\" value \"" + carrierTechnologyParameterClassName + "\", could not find class";
143             LOGGER.warn(errorMessage, e);
144             throw new ParameterRuntimeException(errorMessage, e);
145         }
146
147         // Deserialise the class
148         CarrierTechnologyParameters carrierTechnologyParameters =
149                 context.deserialize(jsonObject.get(CARRIER_TECHNOLOGY_PARAMETERS), carrierTechnologyParameterClass);
150         if (carrierTechnologyParameters == null) {
151             // OK no parameters for the carrier technology have been specified, just instantiate the
152             // default parameters
153             try {
154                 carrierTechnologyParameters =
155                         (CarrierTechnologyParameters) carrierTechnologyParameterClass.newInstance();
156             } catch (final Exception e) {
157                 final String errorMessage = "could not create default parameters for carrier technology \""
158                         + carrierTechnologyLabel + "\"\n" + e.getMessage();
159                 LOGGER.warn(errorMessage, e);
160                 throw new ParameterRuntimeException(errorMessage, e);
161             }
162         }
163
164         // Check that the carrier technology label matches the label in the carrier technology
165         // parameters object
166         if (!carrierTechnologyParameters.getLabel().equals(carrierTechnologyLabel)) {
167             final String errorMessage = "carrier technology \"" + carrierTechnologyLabel + "\" does not match plugin \""
168                     + carrierTechnologyParameters.getLabel() + "\" in \"" + carrierTechnologyParameterClassName
169                     + "\", specify correct carrier technology parameter plugin in parameter \"" + PARAMETER_CLASS_NAME
170                     + "\"";
171             LOGGER.warn(errorMessage);
172             throw new ParameterRuntimeException(errorMessage);
173         }
174
175         return carrierTechnologyParameters;
176     }
177 }