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.carriertechnology;
23 import java.lang.reflect.Type;
24 import java.util.HashMap;
27 import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorCarrierTechnologyParameters;
28 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FILECarrierTechnologyParameters;
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 deserialises various type of carrier technology parameters from JSON.
45 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class CarrierTechnologyParametersJSONAdapter
48 implements JsonSerializer<CarrierTechnologyParameters>, JsonDeserializer<CarrierTechnologyParameters> {
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(CarrierTechnologyParametersJSONAdapter.class);
51 private static final String PARAMETER_CLASS_NAME = "parameterClassName";
53 private static final String CARRIER_TECHNOLOGY_TOKEN = "carrierTechnology";
54 private static final String CARRIER_TECHNOLOGY_PARAMETERS = "parameters";
56 // Built in technology parameters
57 private static final Map<String, String> BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP = new HashMap<>();
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());
68 * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type,
69 * com.google.gson.JsonSerializationContext)
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 ApexParameterRuntimeException(returnMessage);
82 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
83 * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
86 public CarrierTechnologyParameters deserialize(final JsonElement json, final Type typeOfT,
87 final JsonDeserializationContext context) throws JsonParseException {
88 final JsonObject jsonObject = json.getAsJsonObject();
90 // Get the carrier technology label primitive
91 final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(CARRIER_TECHNOLOGY_TOKEN);
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");
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 ApexParameterRuntimeException(errorMessage);
108 // We now get the technology carrier parameter class
109 String carrierTechnologyParameterClassName = null;
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);
115 // If no technology carrier parameter class was specified, we try to use a built in carrier
117 if (classNameJsonPrimitive == null) {
118 carrierTechnologyParameterClassName =
119 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.get(carrierTechnologyLabel);
121 // We use the specified one
122 carrierTechnologyParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
125 // Check the carrier technology parameter class
126 if (carrierTechnologyParameterClassName == null || carrierTechnologyParameterClassName.length() == 0) {
127 final String errorMessage =
128 "carrier technology \"" + carrierTechnologyLabel + "\" parameter \"" + PARAMETER_CLASS_NAME
129 + "\" value \"" + classNameJsonPrimitive.getAsString() + "\" invalid in JSON file";
130 LOGGER.warn(errorMessage);
131 throw new ApexParameterRuntimeException(errorMessage);
134 // Get the class for the carrier technology
135 Class<?> carrierTechnologyParameterClass = null;
137 carrierTechnologyParameterClass = Class.forName(carrierTechnologyParameterClassName);
138 } catch (final ClassNotFoundException e) {
139 final String errorMessage =
140 "carrier technology \"" + carrierTechnologyLabel + "\" parameter \"" + PARAMETER_CLASS_NAME
141 + "\" value \"" + carrierTechnologyParameterClassName + "\", could not find class";
142 LOGGER.warn(errorMessage, e);
143 throw new ApexParameterRuntimeException(errorMessage, e);
146 // Deserialise the class
147 CarrierTechnologyParameters carrierTechnologyParameters =
148 context.deserialize(jsonObject.get(CARRIER_TECHNOLOGY_PARAMETERS), carrierTechnologyParameterClass);
149 if (carrierTechnologyParameters == null) {
150 // OK no parameters for the carrier technology have been specified, just instantiate the
151 // default parameters
153 carrierTechnologyParameters =
154 (CarrierTechnologyParameters) carrierTechnologyParameterClass.newInstance();
155 } catch (final Exception e) {
156 final String errorMessage = "could not create default parameters for carrier technology \""
157 + carrierTechnologyLabel + "\"\n" + e.getMessage();
158 LOGGER.warn(errorMessage, e);
159 throw new ApexParameterRuntimeException(errorMessage, e);
163 // Check that the carrier technology label matches the label in the carrier technology
165 if (!carrierTechnologyParameters.getLabel().equals(carrierTechnologyLabel)) {
166 final String errorMessage = "carrier technology \"" + carrierTechnologyLabel + "\" does not match plugin \""
167 + carrierTechnologyParameters.getLabel() + "\" in \"" + carrierTechnologyParameterClassName
168 + "\", specify correct carrier technology parameter plugin in parameter \"" + PARAMETER_CLASS_NAME
170 LOGGER.warn(errorMessage);
171 throw new ApexParameterRuntimeException(errorMessage);
174 return carrierTechnologyParameters;