2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Ericsson. All rights reserved.
 
   4  *  Copyright (C) 2019 Nordix Foundation.
 
   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.reception.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;
 
  30 import java.lang.reflect.Type;
 
  32 import org.slf4j.Logger;
 
  33 import org.slf4j.LoggerFactory;
 
  36  * This class deserialises reception handler parameters from JSON.
 
  38 public class ReceptionHandlerConfigurationParametersJsonAdapter
 
  39         implements JsonDeserializer<ReceptionHandlerConfigurationParameterGroup> {
 
  40     private static final Logger LOGGER =
 
  41             LoggerFactory.getLogger(ReceptionHandlerConfigurationParametersJsonAdapter.class);
 
  43     private static final String PARAMETER_CLASS_NAME = "parameterClassName";
 
  44     private static final String RECEPTION_HANDLER_PARAMETERS = "parameters";
 
  47     public ReceptionHandlerConfigurationParameterGroup deserialize(final JsonElement json, final Type typeOfT,
 
  48             final JsonDeserializationContext context) {
 
  49         final JsonObject jsonObject = json.getAsJsonObject();
 
  51         final String receptionHandlerParameterClassName = getParameterGroupClassName(jsonObject);
 
  52         final Class<?> receptionHandlerParameterClass = getParameterGroupClass(receptionHandlerParameterClassName);
 
  54         return context.deserialize(jsonObject.get(RECEPTION_HANDLER_PARAMETERS), receptionHandlerParameterClass);
 
  57     private String getParameterGroupClassName(final JsonObject jsonObject) {
 
  58         final JsonPrimitive classNameJsonPrimitive = ((JsonPrimitive) jsonObject.get(PARAMETER_CLASS_NAME));
 
  60         if (classNameJsonPrimitive == null || classNameJsonPrimitive.getAsString().length() == 0) {
 
  61             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
 
  62                     + (classNameJsonPrimitive != null ? classNameJsonPrimitive.getAsString() : "null")
 
  63                     + "\" invalid in JSON file";
 
  64             LOGGER.warn(errorMessage);
 
  65             throw new IllegalArgumentException(errorMessage);
 
  67         return classNameJsonPrimitive.getAsString().replaceAll("\\s+", "");
 
  70     private Class<?> getParameterGroupClass(final String receptionHAndlerParameterClassName) {
 
  71         Class<?> receptionHandlerParameterClass = null;
 
  73             receptionHandlerParameterClass = Class.forName(receptionHAndlerParameterClassName);
 
  74         } catch (final ClassNotFoundException e) {
 
  75             final String errorMessage = "parameter \"" + PARAMETER_CLASS_NAME + "\" value \""
 
  76                     + receptionHAndlerParameterClassName + "\", could not find class";
 
  77             LOGGER.warn(errorMessage, e);
 
  78             throw new IllegalArgumentException(errorMessage, e);
 
  80         return receptionHandlerParameterClass;