2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications 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.apex.service.parameters;
24 import com.google.gson.Gson;
25 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 private boolean keepParameterServiceFlag;
53 * Read the parameters from the parameter file.
55 * @param arguments the arguments passed to Apex
56 * @return the parameters read from the configuration file
57 * @throws ParameterException on parameter exceptions
59 public ApexParameters getParameters(final ApexCommandLineArguments arguments) throws ParameterException {
60 // when populating parameters for multiple policies, do not clear the ParameterService already registered
61 // otherwise clear all existing parameters
62 if (!keepParameterServiceFlag) {
63 ParameterService.clear();
66 ApexParameters parameters = null;
68 // Read the parameters
70 // Register the adapters for our carrier technologies and event protocols with GSON
72 final Gson gson = new GsonBuilder()
73 .registerTypeAdapter(EngineParameters .class,
74 new EngineServiceParametersJsonAdapter())
75 .registerTypeAdapter(CarrierTechnologyParameters.class,
76 new CarrierTechnologyParametersJsonAdapter())
77 .registerTypeAdapter(EventProtocolParameters .class,
78 new EventProtocolParametersJsonAdapter())
81 parameters = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), ApexParameters.class);
82 } catch (final Exception e) {
83 final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
84 + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
85 LOGGER.error(errorMessage, e);
86 throw new ParameterException(errorMessage, e);
89 // The JSON processing returns null if there is an empty file
90 if (parameters == null) {
91 final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
92 LOGGER.error(errorMessage);
93 throw new ParameterException(errorMessage);
96 // Check if we should override the model file parameter
97 final String modelFilePath = arguments.getModelFilePath();
98 if (modelFilePath != null && modelFilePath.replaceAll("\\s+", "").length() > 0) {
99 parameters.getEngineServiceParameters().setPolicyModelFileName(modelFilePath);
102 // Validate the parameters
103 final GroupValidationResult validationResult = parameters.validate();
104 if (!validationResult.isValid()) {
105 String returnMessage = "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath()
107 returnMessage += validationResult.getResult();
109 LOGGER.error(returnMessage);
110 throw new ParameterException(returnMessage);
113 if (!validationResult.isClean()) {
114 String returnMessage = "validation messages(s) on parameters from \"" + arguments.getConfigurationFilePath()
116 returnMessage += validationResult.getResult();
118 LOGGER.info(returnMessage);
121 // engine parameters in multiple policies are expected to be same.
122 // no need to do registration if already registered
123 if (!keepParameterServiceFlag) {
124 // Register the parameters with the parameter service
125 registerParameters(parameters);
132 * Register all the incoming parameters with the parameter service.
134 * @param parameters The parameters to register
136 public void registerParameters(ApexParameters parameters) {
137 ParameterService.register(parameters);
138 ParameterService.register(parameters.getEngineServiceParameters());
139 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters());
140 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters());
141 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
142 .getSchemaParameters());
143 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
144 .getDistributorParameters());
145 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
146 .getLockManagerParameters());
147 ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()
148 .getPersistorParameters());