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 com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import java.io.FileReader;
28 import org.onap.policy.apex.core.engine.EngineParameters;
29 import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments;
30 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
31 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParametersJsonAdapter;
32 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParametersJsonAdapter;
33 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
34 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParametersJsonAdapter;
35 import org.onap.policy.common.parameters.GroupValidationResult;
36 import org.onap.policy.common.parameters.ParameterException;
37 import org.onap.policy.common.parameters.ParameterService;
38 import org.slf4j.ext.XLogger;
39 import org.slf4j.ext.XLoggerFactory;
42 * This class handles reading, parsing and validating of Apex parameters from JSON files.
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class ApexParameterHandler {
47 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class);
50 * Read the parameters from the parameter file.
52 * @param arguments the arguments passed to Apex
53 * @return the parameters read from the configuration file
54 * @throws ParameterException on parameter exceptions
56 public ApexParameters getParameters(final ApexCommandLineArguments arguments) throws ParameterException {
57 // Clear all existing parameters
58 ParameterService.clear();
60 ApexParameters parameters = null;
62 // Read the parameters
64 // Register the adapters for our carrier technologies and event protocols with GSON
66 final Gson gson = new GsonBuilder()
67 .registerTypeAdapter(EngineParameters .class,
68 new EngineServiceParametersJsonAdapter())
69 .registerTypeAdapter(CarrierTechnologyParameters.class,
70 new CarrierTechnologyParametersJsonAdapter())
71 .registerTypeAdapter(EventProtocolParameters .class,
72 new EventProtocolParametersJsonAdapter())
75 parameters = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), ApexParameters.class);
76 } catch (final Exception e) {
77 final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
78 + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
79 LOGGER.error(errorMessage, e);
80 throw new ParameterException(errorMessage, e);
83 // The JSON processing returns null if there is an empty file
84 if (parameters == null) {
85 final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
86 LOGGER.error(errorMessage);
87 throw new ParameterException(errorMessage);
90 // Check if we should override the model file parameter
91 final String modelFilePath = arguments.getModelFilePath();
92 if (modelFilePath != null && modelFilePath.replaceAll("\\s+", "").length() > 0) {
93 parameters.getEngineServiceParameters().setPolicyModelFileName(modelFilePath);
96 // Validate the parameters
97 final GroupValidationResult validationResult = parameters.validate();
98 if (!validationResult.isValid()) {
99 String returnMessage = "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath()
101 returnMessage += validationResult.getResult();
103 LOGGER.error(returnMessage);
104 throw new ParameterException(returnMessage);
107 if (!validationResult.isClean()) {
108 String returnMessage = "validation messages(s) on parameters from \"" + arguments.getConfigurationFilePath()
110 returnMessage += validationResult.getResult();
112 LOGGER.info(returnMessage);
115 // Register the parameters with the parameter service
116 registerParameters(parameters);
122 * Register all the incoming parameters with the parameter service.
124 * @param parameters The parameters to register
126 private void registerParameters(ApexParameters parameters) {
127 ParameterService.register(parameters);
128 ParameterService.register(parameters.getEngineServiceParameters());
129 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters());
130 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters());
131 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
132 .getSchemaParameters());
133 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
134 .getDistributorParameters());
135 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
136 .getLockManagerParameters());
137 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
138 .getPersistorParameters());