2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.parameters.carriertechnology;
24 import com.google.gson.JsonDeserializationContext;
25 import com.google.gson.JsonDeserializer;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonPrimitive;
29 import com.google.gson.JsonSerializationContext;
30 import com.google.gson.JsonSerializer;
32 import java.lang.reflect.Type;
33 import java.util.HashMap;
36 import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorCarrierTechnologyParameters;
37 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
38 import org.onap.policy.common.parameters.ParameterRuntimeException;
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
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> {
50 private static final XLogger LOGGER = XLoggerFactory.getXLogger(CarrierTechnologyParametersJsonAdapter.class);
52 // Recurring string constants
53 private static final String VALUE_TAG = "\" value \"";
54 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", FileCarrierTechnologyParameters.class.getName());
66 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("EVENT_REQUESTOR",
67 EventRequestorCarrierTechnologyParameters.class.getName());
74 public JsonElement serialize(final CarrierTechnologyParameters src, final Type typeOfSrc,
75 final JsonSerializationContext context) {
76 final String returnMessage = "serialization of Apex carrier technology parameters to Json is not supported";
77 LOGGER.error(returnMessage);
78 throw new ParameterRuntimeException(returnMessage);
85 public CarrierTechnologyParameters deserialize(final JsonElement json, final Type typeOfT,
86 final JsonDeserializationContext context) {
87 final JsonObject jsonObject = json.getAsJsonObject();
89 // Get the carrier technology label primitive
90 final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(CARRIER_TECHNOLOGY_TOKEN);
92 // Check if we found our carrier technology
93 if (labelJsonPrimitive == null) {
94 LOGGER.warn("carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + "\" not found in JSON file");
98 // Get and check the carrier technology label
99 final String carrierTechnologyLabel = labelJsonPrimitive.getAsString().replaceAll("\\s+", "");
100 if (carrierTechnologyLabel == null || carrierTechnologyLabel.length() == 0) {
101 final String errorMessage = "carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + VALUE_TAG
102 + labelJsonPrimitive.getAsString() + "\" invalid in JSON file";
103 LOGGER.warn(errorMessage);
104 throw new ParameterRuntimeException(errorMessage);
107 // We now get the technology carrier parameter class
108 String carrierTechnologyParameterClassName = null;
110 // Get the technology carrier parameter class for the carrier technology plugin class from
111 // the configuration parameters
112 final JsonPrimitive classNameJsonPrimitive = (JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME);
114 // If no technology carrier parameter class was specified, we try to use a built in carrier
116 if (classNameJsonPrimitive == null) {
117 carrierTechnologyParameterClassName =
118 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.get(carrierTechnologyLabel);
120 // We use the specified one
121 carrierTechnologyParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
124 // Check the carrier technology parameter class
125 if (carrierTechnologyParameterClassName == null || carrierTechnologyParameterClassName.length() == 0) {
126 final String errorMessage = CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel + "\" parameter \""
127 + PARAMETER_CLASS_NAME + VALUE_TAG
128 + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
129 + "\" invalid in JSON file";
130 LOGGER.warn(errorMessage);
131 throw new ParameterRuntimeException(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_PREAMBLE + carrierTechnologyLabel + "\" parameter \"" + PARAMETER_CLASS_NAME
141 + VALUE_TAG + carrierTechnologyParameterClassName + "\", could not find class";
142 LOGGER.warn(errorMessage, e);
143 throw new ParameterRuntimeException(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 = (CarrierTechnologyParameters) carrierTechnologyParameterClass
154 .getDeclaredConstructor().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 ParameterRuntimeException(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 =
167 CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel + "\" does not match plugin \""
168 + carrierTechnologyParameters.getLabel() + "\" in \"" + carrierTechnologyParameterClassName
169 + "\", specify correct carrier technology parameter plugin in parameter \""
170 + PARAMETER_CLASS_NAME + "\"";
171 LOGGER.warn(errorMessage);
172 throw new ParameterRuntimeException(errorMessage);
175 return carrierTechnologyParameters;