Fix sonars in policy-models impls & simulators
[policy/models.git] / models-sim / models-sim-dmaap / src / main / java / org / onap / policy / models / sim / dmaap / parameters / DmaapSimParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
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.models.sim.dmaap.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.models.sim.dmaap.DmaapSimException;
30 import org.onap.policy.models.sim.dmaap.startstop.DmaapSimCommandLineArguments;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class handles reading, parsing and validating of DMaaP simulator parameters from JSON files.
36  */
37 public class DmaapSimParameterHandler {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapSimParameterHandler.class);
40
41     private final Coder coder = new StandardCoder();
42
43     /**
44      * Read the parameters from the parameter file.
45      *
46      * @param arguments the arguments passed to DMaaP simulator
47      * @return the parameters read from the configuration file
48      * @throws DmaapSimException on parameter exceptions
49      */
50     public DmaapSimParameterGroup getParameters(final DmaapSimCommandLineArguments arguments) throws DmaapSimException {
51         DmaapSimParameterGroup dmaapSimParameterGroup = null;
52
53         // Read the parameters
54         try {
55             // Read the parameters from JSON
56             var file = new File(arguments.getFullConfigurationFilePath());
57             dmaapSimParameterGroup = coder.decode(file, DmaapSimParameterGroup.class);
58         } catch (final CoderException e) {
59             final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
60                     + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
61             LOGGER.error(errorMessage);
62             throw new DmaapSimException(errorMessage, e);
63         }
64
65         // The JSON processing returns null if there is an empty file
66         if (dmaapSimParameterGroup == null) {
67             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
68             LOGGER.error(errorMessage);
69             throw new DmaapSimException(errorMessage);
70         }
71
72         // validate the parameters
73         final ValidationResult validationResult = dmaapSimParameterGroup.validate();
74         if (!validationResult.isValid()) {
75             String returnMessage =
76                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
77             returnMessage += validationResult.getResult();
78
79             LOGGER.error(returnMessage);
80             throw new DmaapSimException(returnMessage);
81         }
82
83         return dmaapSimParameterGroup;
84     }
85 }