2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.main.parameters;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import java.io.FileReader;
28 import org.onap.policy.common.logging.flexlogger.FlexLogger;
29 import org.onap.policy.common.logging.flexlogger.Logger;
30 import org.onap.policy.common.parameters.GroupValidationResult;
31 import org.onap.policy.distribution.main.PolicyDistributionException;
32 import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments;
33 import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup;
34 import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParametersJsonAdapter;
37 * This class handles reading, parsing and validating of policy distribution parameters from JSON files.
39 public class DistributionParameterHandler {
40 private static final Logger LOGGER = FlexLogger.getLogger(DistributionParameterHandler.class);
43 * Read the parameters from the parameter file.
45 * @param arguments the arguments passed to policy distribution
46 * @return the parameters read from the configuration file
47 * @throws PolicyDistributionException on parameter exceptions
49 public DistributionParameterGroup getParameters(final DistributionCommandLineArguments arguments)
50 throws PolicyDistributionException {
51 DistributionParameterGroup distributionParameterGroup = null;
53 // Read the parameters
55 // Read the parameters from JSON using Gson
56 final Gson gson = new GsonBuilder()
57 .registerTypeAdapter(ArtifactForwarderConfigurationParameterGroup.class,
58 new ArtifactForwarderConfigurationParametersJsonAdapter())
59 .registerTypeAdapter(ReceptionHandlerConfigurationParameterGroup.class,
60 new ReceptionHandlerConfigurationParametersJsonAdapter())
62 distributionParameterGroup = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()),
63 DistributionParameterGroup.class);
64 } catch (final Exception e) {
65 final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
66 + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
67 LOGGER.error(errorMessage, e);
68 throw new PolicyDistributionException(errorMessage, e);
71 // The JSON processing returns null if there is an empty file
72 if (distributionParameterGroup == null) {
73 final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
74 LOGGER.error(errorMessage);
75 throw new PolicyDistributionException(errorMessage);
78 // validate the parameters
79 final GroupValidationResult validationResult = distributionParameterGroup.validate();
80 if (!validationResult.isValid()) {
81 String returnMessage =
82 "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
83 returnMessage += validationResult.getResult();
85 LOGGER.error(returnMessage);
86 throw new PolicyDistributionException(returnMessage);
89 return distributionParameterGroup;