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 com.google.gson.JsonDeserializationContext;
24 import com.google.gson.JsonDeserializer;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonPrimitive;
28 import com.google.gson.JsonSerializationContext;
29 import com.google.gson.JsonSerializer;
31 import java.lang.reflect.Type;
32 import java.util.HashMap;
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;
42 * This class deserialises various type of carrier technology parameters from JSON.
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class CarrierTechnologyParametersJsonAdapter
47 implements JsonSerializer<CarrierTechnologyParameters>, JsonDeserializer<CarrierTechnologyParameters> {
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(CarrierTechnologyParametersJsonAdapter.class);
51 // Recurring string constants
52 private static final String VALUE_TAG = "\" value \"";
53 private static final String CARRIER_TECHNOLOGY_PREAMBLE = "carrier technology \"";
56 private static final String PARAMETER_CLASS_NAME = "parameterClassName";
58 private static final String CARRIER_TECHNOLOGY_TOKEN = "carrierTechnology";
59 private static final String CARRIER_TECHNOLOGY_PARAMETERS = "parameters";
61 // Built in technology parameters
62 private static final Map<String, String> BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP = new HashMap<>();
65 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("FILE",
66 FileCarrierTechnologyParameters.class.getName());
67 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("EVENT_REQUESTOR",
68 EventRequestorCarrierTechnologyParameters.class.getName());
74 * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type,
75 * com.google.gson.JsonSerializationContext)
78 public JsonElement serialize(final CarrierTechnologyParameters src, final Type typeOfSrc,
79 final JsonSerializationContext context) {
80 final String returnMessage = "serialization of Apex carrier technology parameters to Json is not supported";
81 LOGGER.error(returnMessage);
82 throw new ParameterRuntimeException(returnMessage);
88 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
89 * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
92 public CarrierTechnologyParameters deserialize(final JsonElement json, final Type typeOfT,
93 final JsonDeserializationContext context) {
94 final JsonObject jsonObject = json.getAsJsonObject();
96 // Get the carrier technology label primitive
97 final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(CARRIER_TECHNOLOGY_TOKEN);
99 // Check if we found our carrier technology
100 if (labelJsonPrimitive == null) {
101 LOGGER.warn("carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + "\" not found in JSON file");
105 // Get and check the carrier technology label
106 final String carrierTechnologyLabel = labelJsonPrimitive.getAsString().replaceAll("\\s+", "");
107 if (carrierTechnologyLabel == null || carrierTechnologyLabel.length() == 0) {
108 final String errorMessage = "carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + VALUE_TAG
109 + labelJsonPrimitive.getAsString() + "\" invalid in JSON file";
110 LOGGER.warn(errorMessage);
111 throw new ParameterRuntimeException(errorMessage);
114 // We now get the technology carrier parameter class
115 String carrierTechnologyParameterClassName = null;
117 // Get the technology carrier parameter class for the carrier technology plugin class from
118 // the configuration parameters
119 final JsonPrimitive classNameJsonPrimitive = (JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME);
121 // If no technology carrier parameter class was specified, we try to use a built in carrier
123 if (classNameJsonPrimitive == null) {
124 carrierTechnologyParameterClassName =
125 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.get(carrierTechnologyLabel);
127 // We use the specified one
128 carrierTechnologyParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
131 // Check the carrier technology parameter class
132 if (carrierTechnologyParameterClassName == null || carrierTechnologyParameterClassName.length() == 0) {
133 final String errorMessage = CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel + "\" parameter \""
134 + PARAMETER_CLASS_NAME + VALUE_TAG
135 + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
136 + "\" invalid in JSON file";
137 LOGGER.warn(errorMessage);
138 throw new ParameterRuntimeException(errorMessage);
141 // Get the class for the carrier technology
142 Class<?> carrierTechnologyParameterClass = null;
144 carrierTechnologyParameterClass = Class.forName(carrierTechnologyParameterClassName);
145 } catch (final ClassNotFoundException e) {
146 final String errorMessage =
147 CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel + "\" parameter \"" + PARAMETER_CLASS_NAME
148 + VALUE_TAG + carrierTechnologyParameterClassName + "\", could not find class";
149 LOGGER.warn(errorMessage, e);
150 throw new ParameterRuntimeException(errorMessage, e);
153 // Deserialise the class
154 CarrierTechnologyParameters carrierTechnologyParameters =
155 context.deserialize(jsonObject.get(CARRIER_TECHNOLOGY_PARAMETERS), carrierTechnologyParameterClass);
156 if (carrierTechnologyParameters == null) {
157 // OK no parameters for the carrier technology have been specified, just instantiate the
158 // default parameters
160 carrierTechnologyParameters =
161 (CarrierTechnologyParameters) carrierTechnologyParameterClass.newInstance();
162 } catch (final Exception e) {
163 final String errorMessage = "could not create default parameters for carrier technology \""
164 + carrierTechnologyLabel + "\"\n" + e.getMessage();
165 LOGGER.warn(errorMessage, e);
166 throw new ParameterRuntimeException(errorMessage, e);
170 // Check that the carrier technology label matches the label in the carrier technology
172 if (!carrierTechnologyParameters.getLabel().equals(carrierTechnologyLabel)) {
173 final String errorMessage = CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel
174 + "\" does not match plugin \""
175 + carrierTechnologyParameters.getLabel() + "\" in \"" + carrierTechnologyParameterClassName
176 + "\", specify correct carrier technology parameter plugin in parameter \"" + PARAMETER_CLASS_NAME
178 LOGGER.warn(errorMessage);
179 throw new ParameterRuntimeException(errorMessage);
182 return carrierTechnologyParameters;