2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.parameters;
26 import org.onap.policy.common.parameters.GroupValidationResult;
27 import org.onap.policy.common.utils.coder.Coder;
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30 import org.onap.policy.pap.main.PolicyPapException;
31 import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * This class handles reading, parsing and validating of policy pap parameters from JSON files.
38 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
40 public class PapParameterHandler {
42 private static final Logger LOGGER = LoggerFactory.getLogger(PapParameterHandler.class);
44 private static final Coder CODER = new StandardCoder();
47 * Read the parameters from the parameter file.
49 * @param arguments the arguments passed to policy pap
50 * @return the parameters read from the configuration file
51 * @throws PolicyPapException on parameter exceptions
53 public PapParameterGroup getParameters(final PapCommandLineArguments arguments) throws PolicyPapException {
54 PapParameterGroup papParameterGroup = null;
56 // Read the parameters
58 // Read the parameters from JSON
59 File file = new File(arguments.getFullConfigurationFilePath());
60 papParameterGroup = CODER.decode(file, PapParameterGroup.class);
61 } catch (final CoderException e) {
62 final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
63 + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
64 LOGGER.error(errorMessage, e);
65 throw new PolicyPapException(errorMessage, e);
68 // The JSON processing returns null if there is an empty file
69 if (papParameterGroup == null) {
70 final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
71 LOGGER.error(errorMessage);
72 throw new PolicyPapException(errorMessage);
75 // validate the parameters
76 final GroupValidationResult validationResult = papParameterGroup.validate();
77 if (!validationResult.isValid()) {
78 String returnMessage =
79 "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
80 returnMessage += validationResult.getResult();
82 LOGGER.error(returnMessage);
83 throw new PolicyPapException(returnMessage);
86 return papParameterGroup;