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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.api.main.parameters;
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;
33 * This class handles reading, parsing and validating of policy api parameters from JSON files.
35 public class ApiParameterHandler {
36 private static final Logger LOGGER = FlexLogger.getLogger(ApiParameterHandler.class);
39 * Read the parameters from the parameter file.
41 * @param arguments the arguments passed to policy api
42 * @return the parameters read from the configuration file
43 * @throws PolicyApiException on parameter exceptions
45 public ApiParameterGroup getParameters(final ApiCommandLineArguments arguments)
46 throws PolicyApiException {
47 ApiParameterGroup apiParameterGroup = null;
49 // Read the parameters
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);
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);
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();
76 LOGGER.error(returnMessage);
77 throw new PolicyApiException(returnMessage);
80 return apiParameterGroup;