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