79f10bdead9d7c223e1b9dc759c4b8099ec2a9ab
[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  * ================================================================================
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.parameters;
22
23 import java.io.FileReader;
24
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;
34
35 import com.google.gson.Gson;
36 import com.google.gson.GsonBuilder;
37
38 /**
39  * This class handles reading, parsing and validating of Apex parameters from JSON files.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class ApexParameterHandler {
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class);
45
46     /**
47      * Read the parameters from the parameter file.
48      *
49      * @param arguments the arguments passed to Apex
50      * @return the parameters read from the configuration file
51      * @throws ApexParameterException on parameter exceptions
52      */
53     public ApexParameters getParameters(final ApexCommandLineArguments arguments) throws ApexParameterException {
54         ApexParameters parameters = null;
55
56         // Read the parameters
57         try {
58             // Register the adapters for our carrier technologies and event protocols with GSON
59             // @formatter:off
60             final Gson gson = new GsonBuilder()
61                     .registerTypeAdapter(EngineParameters           .class, new EngineServiceParametersJSONAdapter())
62                     .registerTypeAdapter(CarrierTechnologyParameters.class, new CarrierTechnologyParametersJSONAdapter())
63                     .registerTypeAdapter(EventProtocolParameters    .class, new EventProtocolParametersJSONAdapter())
64                     .create();
65             // @formatter:on
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);
72         }
73
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);
79         }
80
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);
85         }
86
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;
93
94             LOGGER.error(returnMessage);
95             throw new ApexParameterException(returnMessage);
96         }
97
98         return parameters;
99     }
100 }