2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Copyright (C) 2019-2020 Intel. All rights reserved.
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.distribution.main.parameters;
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 java.lang.reflect.Type;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * This class deserialises policy forwarder parameters from JSON.
36 public class ArtifactForwarderConfigurationParametersJsonAdapter
37 implements JsonDeserializer<ArtifactForwarderConfigurationParameterGroup> {
38 private static final XLogger LOGGER =
39 XLoggerFactory.getXLogger(ArtifactForwarderConfigurationParametersJsonAdapter.class);
41 private static final String PARAMETER_CLASS_NAME = "parameterClassName";
42 private static final String Artifact_FORWARDER_PARAMETERS = "parameters";
45 public ArtifactForwarderConfigurationParameterGroup deserialize(final JsonElement json, final Type typeOfT,
46 final JsonDeserializationContext context) {
47 final JsonObject jsonObject = json.getAsJsonObject();
49 final String artifactForwarderParameterClassName = getParameterGroupClassName(jsonObject);
50 Class<?> artifactForwarderParameterClass = getParameterGroupClass(artifactForwarderParameterClassName);
52 return context.deserialize(jsonObject.get(Artifact_FORWARDER_PARAMETERS), artifactForwarderParameterClass);
55 private String getParameterGroupClassName(final JsonObject jsonObject) {
56 final JsonPrimitive classNameJsonPrimitive = ((JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME));
58 if (classNameJsonPrimitive == null || classNameJsonPrimitive.getAsString().length() == 0) {
59 final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
60 + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
61 + "\" invalid in JSON file";
62 LOGGER.warn(errorMessage);
63 throw new IllegalArgumentException(errorMessage);
65 return classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
68 private Class<?> getParameterGroupClass(final String artifactForwarderParameterClassName) {
69 Class<?> artifactForwarderParameterClass = null;
71 artifactForwarderParameterClass = Class.forName(artifactForwarderParameterClassName);
72 } catch (final ClassNotFoundException e) {
73 final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
74 + artifactForwarderParameterClassName + "\", could not find class";
75 LOGGER.warn(errorMessage, e);
76 throw new IllegalArgumentException(errorMessage, e);
78 return artifactForwarderParameterClass;