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<>();
60 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("FILE",
61 FILECarrierTechnologyParameters.class.getCanonicalName());
62 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("EVENT_REQUESTOR",
63 EventRequestorCarrierTechnologyParameters.class.getCanonicalName());
69 * @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type,
70 * com.google.gson.JsonSerializationContext)
73 public JsonElement serialize(final CarrierTechnologyParameters src, final Type typeOfSrc,
74 final JsonSerializationContext context) {
75 final String returnMessage = "serialization of Apex carrier technology parameters to Json is not supported";
76 LOGGER.error(returnMessage);
77 throw new ApexParameterRuntimeException(returnMessage);
83 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
84 * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
87 public CarrierTechnologyParameters deserialize(final JsonElement json, final Type typeOfT,
88 final JsonDeserializationContext context) throws JsonParseException {
89 final JsonObject jsonObject = json.getAsJsonObject();
91 // Get the carrier technology label primitive
92 final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(CARRIER_TECHNOLOGY_TOKEN);
94 // Check if we found our carrier technology
95 if (labelJsonPrimitive == null) {
96 LOGGER.warn("carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + "\" not found in JSON file");
100 // Get and check the carrier technology label
101 final String carrierTechnologyLabel = labelJsonPrimitive.getAsString().replaceAll("\\s+", "");
102 if (carrierTechnologyLabel == null || carrierTechnologyLabel.length() == 0) {
103 final String errorMessage = "carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + "\" value \""
104 + labelJsonPrimitive.getAsString() + "\" invalid in JSON file";
105 LOGGER.warn(errorMessage);
106 throw new ApexParameterRuntimeException(errorMessage);
109 // We now get the technology carrier parameter class
110 String carrierTechnologyParameterClassName = null;
112 // Get the technology carrier parameter class for the carrier technology plugin class from
113 // the configuration parameters
114 final JsonPrimitive classNameJsonPrimitive = (JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME);
116 // If no technology carrier parameter class was specified, we try to use a built in carrier
118 if (classNameJsonPrimitive == null) {
119 carrierTechnologyParameterClassName =
120 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.get(carrierTechnologyLabel);
122 // We use the specified one
123 carrierTechnologyParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
126 // Check the carrier technology parameter class
127 if (carrierTechnologyParameterClassName == null || carrierTechnologyParameterClassName.length() == 0) {
128 final String errorMessage = "carrier technology \"" + carrierTechnologyLabel + "\" parameter \""
129 + PARAMETER_CLASS_NAME + "\" value \""
130 + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
131 + "\" invalid in JSON file";
132 LOGGER.warn(errorMessage);
133 throw new ApexParameterRuntimeException(errorMessage);
136 // Get the class for the carrier technology
137 Class<?> carrierTechnologyParameterClass = null;
139 carrierTechnologyParameterClass = Class.forName(carrierTechnologyParameterClassName);
140 } catch (final ClassNotFoundException e) {
141 final String errorMessage =
142 "carrier technology \"" + carrierTechnologyLabel + "\" parameter \"" + PARAMETER_CLASS_NAME
143 + "\" value \"" + carrierTechnologyParameterClassName + "\", could not find class";
144 LOGGER.warn(errorMessage, e);
145 throw new ApexParameterRuntimeException(errorMessage, e);
148 // Deserialise the class
149 CarrierTechnologyParameters carrierTechnologyParameters =
150 context.deserialize(jsonObject.get(CARRIER_TECHNOLOGY_PARAMETERS), carrierTechnologyParameterClass);
151 if (carrierTechnologyParameters == null) {
152 // OK no parameters for the carrier technology have been specified, just instantiate the
153 // default parameters
155 carrierTechnologyParameters =
156 (CarrierTechnologyParameters) carrierTechnologyParameterClass.newInstance();
157 } catch (final Exception e) {
158 final String errorMessage = "could not create default parameters for carrier technology \""
159 + carrierTechnologyLabel + "\"\n" + e.getMessage();
160 LOGGER.warn(errorMessage, e);
161 throw new ApexParameterRuntimeException(errorMessage, e);
165 // Check that the carrier technology label matches the label in the carrier technology
167 if (!carrierTechnologyParameters.getLabel().equals(carrierTechnologyLabel)) {
168 final String errorMessage = "carrier technology \"" + carrierTechnologyLabel + "\" does not match plugin \""
169 + carrierTechnologyParameters.getLabel() + "\" in \"" + carrierTechnologyParameterClassName
170 + "\", specify correct carrier technology parameter plugin in parameter \"" + PARAMETER_CLASS_NAME
172 LOGGER.warn(errorMessage);
173 throw new ApexParameterRuntimeException(errorMessage);
176 return carrierTechnologyParameters;