64eb4ed7be8042e35462e6d89382920b3b2076d5
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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.distribution.reception.parameters;
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
29 import java.lang.reflect.Type;
30
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * This class deserialises policy decoder parameters from JSON.
36  *
37  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
38  */
39 public class PolicyDecoderConfigurationParametersJsonAdapter
40         implements JsonDeserializer<PolicyDecoderConfigurationParameterGroup> {
41     private static final XLogger LOGGER =
42             XLoggerFactory.getXLogger(PolicyDecoderConfigurationParametersJsonAdapter.class);
43
44     private static final String PARAMETER_CLASS_NAME = "parameterClassName";
45     private static final String POLICY_DECODER_PARAMETERS = "parameters";
46
47     @Override
48     public PolicyDecoderConfigurationParameterGroup deserialize(final JsonElement json, final Type typeOfT,
49             final JsonDeserializationContext context) {
50         final JsonObject jsonObject = json.getAsJsonObject();
51
52         final String policyDecoderParameterClassName = getParameterGroupClassName(jsonObject);
53         final Class<?> policyDecoderParameterClass = getParameterGroupClass(policyDecoderParameterClassName);
54
55         return context.deserialize(jsonObject.get(POLICY_DECODER_PARAMETERS), policyDecoderParameterClass);
56     }
57
58     private String getParameterGroupClassName(final JsonObject jsonObject) {
59         final JsonPrimitive classNameJsonPrimitive = ((JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME));
60
61         if (classNameJsonPrimitive == null || classNameJsonPrimitive.getAsString().length() == 0) {
62             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
63                     + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
64                     + "\" invalid in JSON file";
65             LOGGER.warn(errorMessage);
66             throw new IllegalArgumentException(errorMessage);
67         }
68         return classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
69     }
70
71     private Class<?> getParameterGroupClass(final String policyDecoderParameterClassName) {
72         Class<?> policyDecoderParameterClass = null;
73         try {
74             policyDecoderParameterClass = Class.forName(policyDecoderParameterClassName);
75         } catch (final ClassNotFoundException exp) {
76             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
77                     + policyDecoderParameterClassName + "\", could not find class";
78             LOGGER.warn(errorMessage, exp);
79             throw new IllegalArgumentException(errorMessage, exp);
80         }
81         return policyDecoderParameterClass;
82     }
83
84 }