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.apex.service.parameters;
 
  23 import java.io.FileReader;
 
  25 import org.onap.policy.apex.core.engine.EngineParameters;
 
  26 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
 
  27 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
 
  28 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParametersJSONAdapter;
 
  29 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParametersJSONAdapter;
 
  30 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
 
  31 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParametersJSONAdapter;
 
  32 import org.slf4j.ext.XLogger;
 
  33 import org.slf4j.ext.XLoggerFactory;
 
  35 import com.google.gson.Gson;
 
  36 import com.google.gson.GsonBuilder;
 
  39  * This class handles reading, parsing and validating of Apex parameters from JSON files.
 
  41  * @author Liam Fallon (liam.fallon@ericsson.com)
 
  43 public class ApexParameterHandler {
 
  44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class);
 
  47      * Read the parameters from the parameter file.
 
  49      * @param arguments the arguments passed to Apex
 
  50      * @return the parameters read from the configuration file
 
  51      * @throws ApexParameterException on parameter exceptions
 
  53     public ApexParameters getParameters(final ApexCommandLineArguments arguments) throws ApexParameterException {
 
  54         ApexParameters parameters = null;
 
  56         // Read the parameters
 
  58             // Register the adapters for our carrier technologies and event protocols with GSON
 
  60             final Gson gson = new GsonBuilder()
 
  61                     .registerTypeAdapter(EngineParameters           .class, new EngineServiceParametersJSONAdapter())
 
  62                     .registerTypeAdapter(CarrierTechnologyParameters.class, new CarrierTechnologyParametersJSONAdapter())
 
  63                     .registerTypeAdapter(EventProtocolParameters    .class, new EventProtocolParametersJSONAdapter())
 
  66             parameters = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), ApexParameters.class);
 
  67         } catch (final Exception e) {
 
  68             final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
 
  69                     + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
 
  70             LOGGER.error(errorMessage, e);
 
  71             throw new ApexParameterException(errorMessage, e);
 
  74         // The JSON processing returns null if there is an empty file
 
  75         if (parameters == null) {
 
  76             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
 
  77             LOGGER.error(errorMessage);
 
  78             throw new ApexParameterException(errorMessage);
 
  81         // Check if we should override the model file parameter
 
  82         final String modelFilePath = arguments.getModelFilePath();
 
  83         if (modelFilePath != null && modelFilePath.replaceAll("\\s+", "").length() > 0) {
 
  84             parameters.getEngineServiceParameters().setPolicyModelFileName(modelFilePath);
 
  87         // validate the parameters
 
  88         final String validationResult = parameters.validate();
 
  89         if (!validationResult.isEmpty()) {
 
  90             String returnMessage =
 
  91                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
 
  92             returnMessage += validationResult;
 
  94             LOGGER.error(returnMessage);
 
  95             throw new ApexParameterException(returnMessage);