b1c8053b5daea9789c205f11cf65f6fdebb6df2e
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
5  *  Modifications Copyright (C) 2020 AT&T Inc.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.reception.parameters;
24
25 import com.google.gson.JsonDeserializationContext;
26 import com.google.gson.JsonDeserializer;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonPrimitive;
30
31 import java.lang.reflect.Type;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * This class deserialises policy decoder parameters from JSON.
38  *
39  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
40  */
41 public class PolicyDecoderConfigurationParametersJsonAdapter
42         implements JsonDeserializer<PolicyDecoderConfigurationParameterGroup> {
43     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyDecoderConfigurationParametersJsonAdapter.class);
44
45     private static final String PARAMETER_CLASS_NAME = "parameterClassName";
46     private static final String POLICY_DECODER_PARAMETERS = "parameters";
47
48     @Override
49     public PolicyDecoderConfigurationParameterGroup deserialize(final JsonElement json, final Type typeOfT,
50             final JsonDeserializationContext context) {
51         final JsonObject jsonObject = json.getAsJsonObject();
52
53         final String policyDecoderParameterClassName = getParameterGroupClassName(jsonObject);
54         final Class<?> policyDecoderParameterClass = getParameterGroupClass(policyDecoderParameterClassName);
55
56         return context.deserialize(jsonObject.get(POLICY_DECODER_PARAMETERS), policyDecoderParameterClass);
57     }
58
59     private String getParameterGroupClassName(final JsonObject jsonObject) {
60         final JsonPrimitive classNameJsonPrimitive = ((JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME));
61
62         if (classNameJsonPrimitive == null || classNameJsonPrimitive.getAsString().length() == 0) {
63             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
64                     + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
65                     + "\" invalid in JSON file";
66             LOGGER.warn(errorMessage);
67             throw new IllegalArgumentException(errorMessage);
68         }
69         return classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
70     }
71
72     private Class<?> getParameterGroupClass(final String policyDecoderParameterClassName) {
73         Class<?> policyDecoderParameterClass = null;
74         try {
75             policyDecoderParameterClass = Class.forName(policyDecoderParameterClassName);
76         } catch (final ClassNotFoundException exp) {
77             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
78                     + policyDecoderParameterClassName + "\", could not find class";
79             throw new IllegalArgumentException(errorMessage, exp);
80         }
81         return policyDecoderParameterClass;
82     }
83
84 }