Add basic main structure for policy-api
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / parameters / ApiParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Samsung Electronics Co., Ltd. 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.api.main.parameters;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.onap.policy.common.logging.flexlogger.FlexLogger;
26 import org.onap.policy.common.logging.flexlogger.Logger;
27 import org.onap.policy.common.parameters.GroupValidationResult;
28 import org.onap.policy.api.main.PolicyApiException;
29 import org.onap.policy.api.main.startstop.ApiCommandLineArguments;
30
31 import java.io.FileReader;
32
33 /**
34  * This class handles reading, parsing and validating of policy api parameters from JSON files.
35  */
36 public class ApiParameterHandler {
37     private static final Logger LOGGER = FlexLogger.getLogger(ApiParameterHandler.class);
38
39     /**
40      * Read the parameters from the parameter file.
41      *
42      * @param arguments the arguments passed to policy api
43      * @return the parameters read from the configuration file
44      * @throws PolicyApiException on parameter exceptions
45      */
46     public ApiParameterGroup getParameters(final ApiCommandLineArguments arguments)
47             throws PolicyApiException {
48         ApiParameterGroup apiParameterGroup = null;
49
50         // Read the parameters
51         try {
52             // Read the parameters from JSON using Gson
53             final Gson gson = new GsonBuilder().create();
54             apiParameterGroup = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()),
55                     ApiParameterGroup.class);
56         } catch (final Exception e) {
57             final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
58                     + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
59             LOGGER.error(errorMessage, e);
60             throw new PolicyApiException(errorMessage, e);
61         }
62
63         // The JSON processing returns null if there is an empty file
64         if (apiParameterGroup == null) {
65             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
66             LOGGER.error(errorMessage);
67             throw new PolicyApiException(errorMessage);
68         }
69
70         // validate the parameters
71         final GroupValidationResult validationResult = apiParameterGroup.validate();
72         if (!validationResult.isValid()) {
73             String returnMessage =
74                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
75             returnMessage += validationResult.getResult();
76
77             LOGGER.error(returnMessage);
78             throw new PolicyApiException(returnMessage);
79         }
80
81         return apiParameterGroup;
82     }
83 }