2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
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.pap.main.parameters;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import java.io.FileReader;
28 import org.onap.policy.common.parameters.GroupValidationResult;
29 import org.onap.policy.pap.main.PolicyPapException;
30 import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * This class handles reading, parsing and validating of policy pap parameters from JSON files.
37 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39 public class PapParameterHandler {
40 private static final XLogger LOGGER = XLoggerFactory.getXLogger(PapParameterHandler.class);
43 * Read the parameters from the parameter file.
45 * @param arguments the arguments passed to policy pap
46 * @return the parameters read from the configuration file
47 * @throws PolicyPapException on parameter exceptions
49 public PapParameterGroup getParameters(final PapCommandLineArguments arguments) throws PolicyPapException {
50 PapParameterGroup papParameterGroup = null;
52 // Read the parameters
54 // Read the parameters from JSON using Gson
55 final Gson gson = new GsonBuilder().create();
57 gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), PapParameterGroup.class);
58 } catch (final Exception e) {
59 final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
60 + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
61 LOGGER.error(errorMessage, e);
62 throw new PolicyPapException(errorMessage, e);
65 // The JSON processing returns null if there is an empty file
66 if (papParameterGroup == null) {
67 final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
68 LOGGER.error(errorMessage);
69 throw new PolicyPapException(errorMessage);
72 // validate the parameters
73 final GroupValidationResult validationResult = papParameterGroup.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 PolicyPapException(returnMessage);
83 return papParameterGroup;