2c5ccebf992e8e2ec2d7bea4a1965c4dcc263bd6
[policy/models.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.sim.pdp.parameters;
22
23 import java.io.File;
24
25
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.models.sim.pdp.PdpSimulatorCommandLineArguments;
31 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This class handles reading, parsing and validating of pdp simulator parameters from JSON files.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class PdpSimulatorParameterHandler {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(PdpSimulatorParameterHandler.class);
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 pdp simulator
49      * @return the parameters read from the configuration file
50      * @throws PdpSimulatorException on parameter exceptions
51      */
52     public PdpSimulatorParameterGroup getParameters(final PdpSimulatorCommandLineArguments arguments)
53             throws PdpSimulatorException {
54         PdpSimulatorParameterGroup pdpSimulatorParameterGroup = null;
55
56         // Read the parameters
57         try {
58             // Read the parameters from JSON
59             final File file = new File(arguments.getFullConfigurationFilePath());
60             pdpSimulatorParameterGroup = CODER.decode(file, PdpSimulatorParameterGroup.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 PdpSimulatorException(errorMessage, e);
66         }
67
68         // The JSON processing returns null if there is an empty file
69         if (pdpSimulatorParameterGroup == null) {
70             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
71             LOGGER.error(errorMessage);
72             throw new PdpSimulatorException(errorMessage);
73         }
74
75         // validate the parameters
76         final GroupValidationResult validationResult = pdpSimulatorParameterGroup.validate();
77         if (!validationResult.isValid()) {
78             String returnMessage =
79                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
80             returnMessage += validationResult.getResult();
81
82             LOGGER.error(returnMessage);
83             throw new PdpSimulatorException(returnMessage);
84         }
85
86         return pdpSimulatorParameterGroup;
87     }
88
89 }