Update snapshot and/or references of policy/pap to latest snapshots
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / parameters / PapParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.parameters;
23
24 import java.io.File;
25 import org.onap.policy.common.parameters.ValidationResult;
26 import org.onap.policy.common.utils.coder.Coder;
27 import org.onap.policy.common.utils.coder.CoderException;
28 import org.onap.policy.common.utils.coder.StandardCoder;
29 import org.onap.policy.pap.main.PolicyPapException;
30 import org.onap.policy.pap.main.startstop.PapCommandLineArguments;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class handles reading, parsing and validating of policy pap parameters from JSON files.
36  *
37  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
38  */
39 public class PapParameterHandler {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(PapParameterHandler.class);
42
43     private static final Coder CODER = new StandardCoder();
44
45     /**
46      * Read the parameters from the parameter file.
47      *
48      * @param arguments the arguments passed to policy pap
49      * @return the parameters read from the configuration file
50      * @throws PolicyPapException on parameter exceptions
51      */
52     public PapParameterGroup getParameters(final PapCommandLineArguments arguments) throws PolicyPapException {
53         PapParameterGroup papParameterGroup = null;
54
55         // Read the parameters
56         try {
57             // Read the parameters from JSON
58             var file = new File(arguments.getFullConfigurationFilePath());
59             papParameterGroup = CODER.decode(file, PapParameterGroup.class);
60         } catch (final CoderException e) {
61             final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
62                     + "\"\n" + "(" + e.getClass().getSimpleName() + ")";
63             throw new PolicyPapException(errorMessage, e);
64         }
65
66         // The JSON processing returns null if there is an empty file
67         if (papParameterGroup == null) {
68             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
69             LOGGER.error(errorMessage);
70             throw new PolicyPapException(errorMessage);
71         }
72
73         // validate the parameters
74         final ValidationResult validationResult = papParameterGroup.validate();
75         if (!validationResult.isValid()) {
76             String returnMessage =
77                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
78             returnMessage += validationResult.getResult();
79
80             LOGGER.error(returnMessage);
81             throw new PolicyPapException(returnMessage);
82         }
83
84         return papParameterGroup;
85     }
86 }