2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.api.main.parameters;
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;
35 * This class handles reading, parsing and validating of policy api parameters from JSON files.
37 public class ApiParameterHandler {
39 private static final Logger LOGGER = LoggerFactory.getLogger(ApiParameterHandler.class);
42 * Read the parameters from the parameter file.
44 * @param arguments the arguments passed to policy api
45 * @return the parameters read from the configuration file
46 * @throws PolicyApiException on parameter exceptions
48 public ApiParameterGroup getParameters(final ApiCommandLineArguments arguments)
49 throws PolicyApiException {
50 ApiParameterGroup apiParameterGroup = null;
52 // Read the parameters
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);
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);
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();
78 LOGGER.error(returnMessage);
79 throw new PolicyApiException(returnMessage);
82 return apiParameterGroup;