2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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
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.models.sim.pdp.parameters;
25 import org.onap.policy.common.parameters.GroupValidationResult;
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.pdp.PdpSimulatorCommandLineArguments;
30 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * This class handles reading, parsing and validating of pdp simulator parameters from JSON files.
37 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39 public class PdpSimulatorParameterHandler {
41 private static final Logger logger = LoggerFactory.getLogger(PdpSimulatorParameterHandler.class);
42 private static final Coder CODER = new StandardCoder();
45 * Read the parameters from the parameter file.
47 * @param arguments the arguments passed to pdp simulator
48 * @return the parameters read from the configuration file
49 * @throws PdpSimulatorException on parameter exceptions
51 public PdpSimulatorParameterGroup getParameters(final PdpSimulatorCommandLineArguments arguments)
52 throws PdpSimulatorException {
53 PdpSimulatorParameterGroup pdpSimulatorParameterGroup = null;
55 // Read the parameters
57 // Read the parameters from JSON
58 final File file = new File(arguments.getFullConfigurationFilePath());
59 pdpSimulatorParameterGroup = CODER.decode(file, PdpSimulatorParameterGroup.class);
60 } catch (final CoderException e) {
61 final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
62 + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
63 logger.error(errorMessage);
64 throw new PdpSimulatorException(errorMessage, e);
67 // The JSON processing returns null if there is an empty file
68 if (pdpSimulatorParameterGroup == null) {
69 final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
70 logger.error(errorMessage);
71 throw new PdpSimulatorException(errorMessage);
74 // validate the parameters
75 final GroupValidationResult validationResult = pdpSimulatorParameterGroup.validate();
76 if (!validationResult.isValid()) {
77 String returnMessage =
78 "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
79 returnMessage += validationResult.getResult();
81 logger.error(returnMessage);
82 throw new PdpSimulatorException(returnMessage);
85 return pdpSimulatorParameterGroup;