Fix sonars in policy-api
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / parameters / ApiParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.parameters;
25
26 import com.google.gson.GsonBuilder;
27 import java.io.FileReader;
28 import org.onap.policy.api.main.exception.PolicyApiException;
29 import org.onap.policy.api.main.startstop.ApiCommandLineArguments;
30 import org.onap.policy.common.parameters.ValidationResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class handles reading, parsing and validating of policy api parameters from JSON files.
36  */
37 public class ApiParameterHandler {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(ApiParameterHandler.class);
40
41     /**
42      * Read the parameters from the parameter file.
43      *
44      * @param arguments the arguments passed to policy api
45      * @return the parameters read from the configuration file
46      * @throws PolicyApiException on parameter exceptions
47      */
48     public ApiParameterGroup getParameters(final ApiCommandLineArguments arguments)
49             throws PolicyApiException {
50         ApiParameterGroup apiParameterGroup = null;
51
52         // Read the parameters
53         try {
54             // Read the parameters from JSON using Gson
55             final var gson = new GsonBuilder().create();
56             apiParameterGroup = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()),
57                     ApiParameterGroup.class);
58         } catch (final Exception e) {
59             final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
60                     + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
61             throw new PolicyApiException(errorMessage, e);
62         }
63
64         // The JSON processing returns null if there is an empty file
65         if (apiParameterGroup == null) {
66             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
67             LOGGER.error(errorMessage);
68             throw new PolicyApiException(errorMessage);
69         }
70
71         // validate the parameters
72         final ValidationResult validationResult = apiParameterGroup.validate();
73         if (!validationResult.isValid()) {
74             String returnMessage =
75                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
76             returnMessage += validationResult.getResult();
77
78             LOGGER.error(returnMessage);
79             throw new PolicyApiException(returnMessage);
80         }
81
82         return apiParameterGroup;
83     }
84 }