35f64830798592692c33f4942013a394501f7600
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.parameters.carriertechnology;
22
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;
30
31 import java.lang.reflect.Type;
32 import java.util.HashMap;
33 import java.util.Map;
34
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;
40
41 /**
42  * This class deserialises various type of carrier technology parameters from JSON.
43  *
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class CarrierTechnologyParametersJsonAdapter
47         implements JsonSerializer<CarrierTechnologyParameters>, JsonDeserializer<CarrierTechnologyParameters> {
48
49     private static final XLogger LOGGER = XLoggerFactory.getXLogger(CarrierTechnologyParametersJsonAdapter.class);
50
51     // Recurring string constants
52     private static final String VALUE_TAG = "\" value \"";
53     private static final String CARRIER_TECHNOLOGY_PREAMBLE = "carrier technology \"";
54
55
56     private static final String PARAMETER_CLASS_NAME = "parameterClassName";
57
58     private static final String CARRIER_TECHNOLOGY_TOKEN = "carrierTechnology";
59     private static final String CARRIER_TECHNOLOGY_PARAMETERS = "parameters";
60
61     // Built in technology parameters
62     private static final Map<String, String> BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP = new HashMap<>();
63
64     static {
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());
69     }
70
71     /**
72      * {@inheritDoc}.
73      */
74     @Override
75     public JsonElement serialize(final CarrierTechnologyParameters src, final Type typeOfSrc,
76             final JsonSerializationContext context) {
77         final String returnMessage = "serialization of Apex carrier technology parameters to Json is not supported";
78         LOGGER.error(returnMessage);
79         throw new ParameterRuntimeException(returnMessage);
80     }
81
82     /**
83      * {@inheritDoc}.
84      */
85     @Override
86     public CarrierTechnologyParameters deserialize(final JsonElement json, final Type typeOfT,
87             final JsonDeserializationContext context) {
88         final JsonObject jsonObject = json.getAsJsonObject();
89
90         // Get the carrier technology label primitive
91         final JsonPrimitive labelJsonPrimitive = (JsonPrimitive) jsonObject.get(CARRIER_TECHNOLOGY_TOKEN);
92
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");
96             return null;
97         }
98
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_TAG
103                     + labelJsonPrimitive.getAsString() + "\" invalid in JSON file";
104             LOGGER.warn(errorMessage);
105             throw new ParameterRuntimeException(errorMessage);
106         }
107
108         // We now get the technology carrier parameter class
109         String carrierTechnologyParameterClassName = null;
110
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);
114
115         // If no technology carrier parameter class was specified, we try to use a built in carrier
116         // technology
117         if (classNameJsonPrimitive == null) {
118             carrierTechnologyParameterClassName =
119                     BUILT_IN_CARRIER_TECHNOLOGY_PARMETER_CLASS_MAP.get(carrierTechnologyLabel);
120         } else {
121             // We use the specified one
122             carrierTechnologyParameterClassName = classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
123         }
124
125         // Check the carrier technology parameter class
126         if (carrierTechnologyParameterClassName == null || carrierTechnologyParameterClassName.length() == 0) {
127             final String errorMessage = CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel + "\" parameter \""
128                     + PARAMETER_CLASS_NAME + VALUE_TAG
129                     + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
130                     + "\" invalid in JSON file";
131             LOGGER.warn(errorMessage);
132             throw new ParameterRuntimeException(errorMessage);
133         }
134
135         // Get the class for the carrier technology
136         Class<?> carrierTechnologyParameterClass = null;
137         try {
138             carrierTechnologyParameterClass = Class.forName(carrierTechnologyParameterClassName);
139         } catch (final ClassNotFoundException e) {
140             final String errorMessage =
141                     CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel + "\" parameter \"" + PARAMETER_CLASS_NAME
142                             + VALUE_TAG + carrierTechnologyParameterClassName + "\", could not find class";
143             LOGGER.warn(errorMessage, e);
144             throw new ParameterRuntimeException(errorMessage, e);
145         }
146
147         // Deserialise the class
148         CarrierTechnologyParameters carrierTechnologyParameters =
149                 context.deserialize(jsonObject.get(CARRIER_TECHNOLOGY_PARAMETERS), carrierTechnologyParameterClass);
150         if (carrierTechnologyParameters == null) {
151             // OK no parameters for the carrier technology have been specified, just instantiate the
152             // default parameters
153             try {
154                 carrierTechnologyParameters =
155                         (CarrierTechnologyParameters) carrierTechnologyParameterClass.newInstance();
156             } catch (final Exception e) {
157                 final String errorMessage = "could not create default parameters for carrier technology \""
158                         + carrierTechnologyLabel + "\"\n" + e.getMessage();
159                 LOGGER.warn(errorMessage, e);
160                 throw new ParameterRuntimeException(errorMessage, e);
161             }
162         }
163
164         // Check that the carrier technology label matches the label in the carrier technology
165         // parameters object
166         if (!carrierTechnologyParameters.getLabel().equals(carrierTechnologyLabel)) {
167             final String errorMessage = CARRIER_TECHNOLOGY_PREAMBLE + carrierTechnologyLabel
168                             + "\" does not match plugin \""
169                     + carrierTechnologyParameters.getLabel() + "\" in \"" + carrierTechnologyParameterClassName
170                     + "\", specify correct carrier technology parameter plugin in parameter \"" + PARAMETER_CLASS_NAME
171                     + "\"";
172             LOGGER.warn(errorMessage);
173             throw new ParameterRuntimeException(errorMessage);
174         }
175
176         return carrierTechnologyParameters;
177     }
178 }