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,
62 new EngineServiceParametersJSONAdapter())
63 .registerTypeAdapter(CarrierTechnologyParameters.class,
64 new CarrierTechnologyParametersJSONAdapter())
65 .registerTypeAdapter(EventProtocolParameters .class,
66 new EventProtocolParametersJSONAdapter())
69 parameters = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), ApexParameters.class);
70 } catch (final Exception e) {
71 final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
72 + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
73 LOGGER.error(errorMessage, e);
74 throw new ApexParameterException(errorMessage, e);
77 // The JSON processing returns null if there is an empty file
78 if (parameters == null) {
79 final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
80 LOGGER.error(errorMessage);
81 throw new ApexParameterException(errorMessage);
84 // Check if we should override the model file parameter
85 final String modelFilePath = arguments.getModelFilePath();
86 if (modelFilePath != null && modelFilePath.replaceAll("\\s+", "").length() > 0) {
87 parameters.getEngineServiceParameters().setPolicyModelFileName(modelFilePath);
90 // validate the parameters
91 final String validationResult = parameters.validate();
92 if (!validationResult.isEmpty()) {
93 String returnMessage =
94 "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
95 returnMessage += validationResult;
97 LOGGER.error(returnMessage);
98 throw new ApexParameterException(returnMessage);