4312793e461e96ce07c49a3a6c71767a9879019f
[policy/apex-pdp.git] /
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 com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.io.FileReader;
27
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     /**
50      * Read the parameters from the parameter file.
51      *
52      * @param arguments the arguments passed to Apex
53      * @return the parameters read from the configuration file
54      * @throws ParameterException on parameter exceptions
55      */
56     public ApexParameters getParameters(final ApexCommandLineArguments arguments) throws ParameterException {
57         // Clear all existing parameters
58         ParameterService.clear();
59
60         ApexParameters parameters = null;
61
62         // Read the parameters
63         try {
64             // Register the adapters for our carrier technologies and event protocols with GSON
65             // @formatter:off
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())
73                             .create();
74             // @formatter:on
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);
81         }
82
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);
88         }
89
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);
94         }
95
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()
100                             + "\"\n";
101             returnMessage += validationResult.getResult();
102
103             LOGGER.error(returnMessage);
104             throw new ParameterException(returnMessage);
105         }
106
107         if (!validationResult.isClean()) {
108             String returnMessage = "validation messages(s) on parameters from \"" + arguments.getConfigurationFilePath()
109                             + "\"\n";
110             returnMessage += validationResult.getResult();
111
112             LOGGER.info(returnMessage);
113         }
114
115         // Register the parameters with the parameter service
116         registerParameters(parameters);
117
118         return parameters;
119     }
120
121     /**
122      * Register all the incoming parameters with the parameter service.
123      * 
124      * @param parameters The parameters to register
125      */
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());
139     }
140 }