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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.distribution.main.parameters;
 
  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 java.lang.reflect.Type;
 
  29 import org.slf4j.ext.XLogger;
 
  30 import org.slf4j.ext.XLoggerFactory;
 
  33  * This class deserialises policy forwarder parameters from JSON.
 
  35 public class PolicyForwarderConfigurationParametersJsonAdapter
 
  36         implements JsonDeserializer<PolicyForwarderConfigurationParameterGroup> {
 
  37     private static final XLogger LOGGER =
 
  38             XLoggerFactory.getXLogger(PolicyForwarderConfigurationParametersJsonAdapter.class);
 
  40     private static final String PARAMETER_CLASS_NAME = "parameterClassName";
 
  41     private static final String POLICY_FORWARDER_PARAMETERS = "parameters";
 
  44     public PolicyForwarderConfigurationParameterGroup deserialize(final JsonElement json, final Type typeOfT,
 
  45             final JsonDeserializationContext context) {
 
  46         final JsonObject jsonObject = json.getAsJsonObject();
 
  48         final String policyForwarderParameterClassName = getParameterGroupClassName(jsonObject);
 
  49         Class<?> policyForwarderParameterClass = getParameterGroupClass(policyForwarderParameterClassName);
 
  51         return context.deserialize(jsonObject.get(POLICY_FORWARDER_PARAMETERS), policyForwarderParameterClass);
 
  54     private String getParameterGroupClassName(final JsonObject jsonObject) {
 
  55         final JsonPrimitive classNameJsonPrimitive = ((JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME));
 
  57         if (classNameJsonPrimitive == null || classNameJsonPrimitive.getAsString().length() == 0) {
 
  58             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
 
  59                     + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
 
  60                     + "\" invalid in JSON file";
 
  61             LOGGER.warn(errorMessage);
 
  62             throw new IllegalArgumentException(errorMessage);
 
  64         return classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
 
  67     private Class<?> getParameterGroupClass(final String policyForwarderParameterClassName) {
 
  68         Class<?> policyForwarderParameterClass = null;
 
  70             policyForwarderParameterClass = Class.forName(policyForwarderParameterClassName);
 
  71         } catch (final ClassNotFoundException e) {
 
  72             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
 
  73                     + policyForwarderParameterClassName + "\", could not find class";
 
  74             LOGGER.warn(errorMessage, e);
 
  75             throw new IllegalArgumentException(errorMessage, e);
 
  77         return policyForwarderParameterClass;