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;
31 import java.lang.reflect.Type;
32 import java.util.HashMap;
34 import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorCarrierTechnologyParameters;
35 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
36 import org.onap.policy.common.parameters.ParameterRuntimeException;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
41 * This class deserialises various type of carrier technology parameters from JSON.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class CarrierTechnologyParametersJsonAdapter
46 implements JsonSerializer<CarrierTechnologyParameters>, JsonDeserializer<CarrierTechnologyParameters> {
48 private static final XLogger LOGGER = XLoggerFactory.getXLogger(CarrierTechnologyParametersJsonAdapter.class);
50 // Recurring string constants
51 private static final String VALUE_TAG = "\" value \"";
52 private static final String CARRIER_TECHNOLOGY_PREAMBLE = "carrier technology \"";
54 private static final String PARAMETER_CLASS_NAME = "parameterClassName";
56 private static final String CARRIER_TECHNOLOGY_TOKEN = "carrierTechnology";
57 private static final String CARRIER_TECHNOLOGY_PARAMETERS = "parameters";
59 // Built in technology parameters
60 private static final Map<String, String> BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP = new HashMap<>();
63 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("FILE", FileCarrierTechnologyParameters.class.getName());
64 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.put("EVENT_REQUESTOR",
65 EventRequestorCarrierTechnologyParameters.class.getName());
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);
83 public CarrierTechnologyParameters deserialize(final JsonElement json, final Type typeOfT,
84 final JsonDeserializationContext context) {
85 final JsonObject jsonObject = json.getAsJsonObject();
87 // Get the carrier technology label primitive
88 final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(CARRIER_TECHNOLOGY_TOKEN);
90 // Check if we found our carrier technology
91 if (labelJsonPrimitive == null) {
92 LOGGER.warn("carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + "\" not found in JSON file");
96 // Get and check the carrier technology label
97 final String carrierTechnologyLabel = labelJsonPrimitive.getAsString().replaceAll("\\s+", "");
98 if (carrierTechnologyLabel == null || carrierTechnologyLabel.length() == 0) {
99 final String errorMessage = "carrier technology parameter \"" + CARRIER_TECHNOLOGY_TOKEN + VALUE_TAG
100 + labelJsonPrimitive.getAsString() + "\" invalid in JSON file";
101 LOGGER.warn(errorMessage);
102 throw new ParameterRuntimeException(errorMessage);
105 // We now get the technology carrier parameter class
106 String carrierTechnologyParameterClassName = null;
108 // Get the technology carrier parameter class for the carrier technology plugin class from
109 // the configuration parameters
110 final JsonPrimitive classNameJsonPrimitive = (JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME);
112 // If no technology carrier parameter class was specified, we try to use a built in carrier
114 if (classNameJsonPrimitive == null) {
115 carrierTechnologyParameterClassName =
116 BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.get(carrierTechnologyLabel);
118 // We use the specified one
119 carrierTechnologyParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
122 // Check the carrier technology parameter class
123 if (carrierTechnologyParameterClassName == null || carrierTechnologyParameterClassName.length() == 0) {
124 final String errorMessage = CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel + "\" parameter \""
125 + PARAMETER_CLASS_NAME + VALUE_TAG
126 + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
127 + "\" invalid in JSON file";
128 LOGGER.warn(errorMessage);
129 throw new ParameterRuntimeException(errorMessage);
132 // Get the class for the carrier technology
133 Class<?> carrierTechnologyParameterClass = null;
135 carrierTechnologyParameterClass = Class.forName(carrierTechnologyParameterClassName);
136 } catch (final ClassNotFoundException e) {
137 final String errorMessage =
138 CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel + "\" parameter \"" + PARAMETER_CLASS_NAME
139 + VALUE_TAG + carrierTechnologyParameterClassName + "\", could not find class";
140 LOGGER.warn(errorMessage, e);
141 throw new ParameterRuntimeException(errorMessage, e);
144 // Deserialise the class
145 CarrierTechnologyParameters carrierTechnologyParameters =
146 context.deserialize(jsonObject.get(CARRIER_TECHNOLOGY_PARAMETERS), carrierTechnologyParameterClass);
147 if (carrierTechnologyParameters == null) {
148 // OK no parameters for the carrier technology have been specified, just instantiate the
149 // default parameters
151 carrierTechnologyParameters = (CarrierTechnologyParameters) carrierTechnologyParameterClass
152 .getDeclaredConstructor().newInstance();
153 } catch (final Exception e) {
154 final String errorMessage = "could not create default parameters for carrier technology \""
155 + carrierTechnologyLabel + "\"\n" + e.getMessage();
156 LOGGER.warn(errorMessage, e);
157 throw new ParameterRuntimeException(errorMessage, e);
161 // Check that the carrier technology label matches the label in the carrier technology
163 if (!carrierTechnologyParameters.getLabel().equals(carrierTechnologyLabel)) {
164 final String errorMessage =
165 CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel + "\" does not match plugin \""
166 + carrierTechnologyParameters.getLabel() + "\" in \"" + carrierTechnologyParameterClassName
167 + "\", specify correct carrier technology parameter plugin in parameter \""
168 + PARAMETER_CLASS_NAME + "\"";
169 LOGGER.warn(errorMessage);
170 throw new ParameterRuntimeException(errorMessage);
173 return carrierTechnologyParameters;