6cdbf28bb6d2b96c7d9f7656799313d492184e68
[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 import java.lang.reflect.Type;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class deserialises reception handler parameters from JSON.
36  */
37 public class ReceptionHandlerConfigurationParametersJsonAdapter
38         implements JsonDeserializer<ReceptionHandlerConfigurationParameterGroup> {
39     private static final Logger LOGGER =
40             LoggerFactory.getLogger(ReceptionHandlerConfigurationParametersJsonAdapter.class);
41
42     private static final String PARAMETER_CLASS_NAME = "parameterClassName";
43     private static final String RECEPTION_HANDLER_PARAMETERS = "parameters";
44
45     @Override
46     public ReceptionHandlerConfigurationParameterGroup deserialize(final JsonElement json, final Type typeOfT,
47             final JsonDeserializationContext context) {
48         final JsonObject jsonObject = json.getAsJsonObject();
49
50         final String receptionHandlerParameterClassName = getParameterGroupClassName(jsonObject);
51         final Class<?> receptionHandlerParameterClass = getParameterGroupClass(receptionHandlerParameterClassName);
52
53         return context.deserialize(jsonObject.get(RECEPTION_HANDLER_PARAMETERS), receptionHandlerParameterClass);
54     }
55
56     private String getParameterGroupClassName(final JsonObject jsonObject) {
57         final JsonPrimitive classNameJsonPrimitive = ((JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME));
58
59         if (classNameJsonPrimitive == null || classNameJsonPrimitive.getAsString().length() == 0) {
60             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
61                     + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
62                     + "\" invalid in JSON file";
63             LOGGER.warn(errorMessage);
64             throw new IllegalArgumentException(errorMessage);
65         }
66         return classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
67     }
68
69     private Class<?> getParameterGroupClass(final String receptionHAndlerParameterClassName) {
70         Class<?> receptionHandlerParameterClass = null;
71         try {
72             receptionHandlerParameterClass = Class.forName(receptionHAndlerParameterClassName);
73         } catch (final ClassNotFoundException e) {
74             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
75                     + receptionHAndlerParameterClassName + "\", could not find class";
76             throw new IllegalArgumentException(errorMessage, e);
77         }
78         return receptionHandlerParameterClass;
79     }
80
81 }