Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / parameters / ApexParameterHandler.java
1 /*-
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
9  * 
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  * 
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.
17  * 
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.service.parameters;
23
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import java.io.FileReader;
27 import lombok.Setter;
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;
40
41 /**
42  * This class handles reading, parsing and validating of Apex parameters from JSON files.
43  *
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class ApexParameterHandler {
47     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class);
48
49     @Setter
50     private boolean keepParameterServiceFlag;
51
52     /**
53      * Read the parameters from the parameter file.
54      *
55      * @param arguments the arguments passed to Apex
56      * @return the parameters read from the configuration file
57      * @throws ParameterException on parameter exceptions
58      */
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();
64         }
65
66         ApexParameters parameters = null;
67
68         // Read the parameters
69         try {
70             // Register the adapters for our carrier technologies and event protocols with GSON
71             // @formatter:off
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())
79                             .create();
80             // @formatter:on
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);
87         }
88
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);
94         }
95
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);
100         }
101
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()
106                             + "\"\n";
107             returnMessage += validationResult.getResult();
108
109             LOGGER.error(returnMessage);
110             throw new ParameterException(returnMessage);
111         }
112
113         if (!validationResult.isClean()) {
114             String returnMessage = "validation messages(s) on parameters from \"" + arguments.getConfigurationFilePath()
115                             + "\"\n";
116             returnMessage += validationResult.getResult();
117
118             LOGGER.info(returnMessage);
119         }
120
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);
126         }
127
128         return parameters;
129     }
130
131     /**
132      * Register all the incoming parameters with the parameter service.
133      *
134      * @param parameters The parameters to register
135      */
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());
149     }
150 }