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