dc7297856f89894cb37fe86d66ef8171b828da7c
[multicloud/framework.git] / artifactbroker / main / src / main / java / org / onap / policy / distribution / main / parameters / DistributionParameterHandler.java
1 /*-
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
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.distribution.main.parameters;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.io.FileReader;
27
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
34 /**
35  * This class handles reading, parsing and validating of policy distribution parameters from JSON files.
36  */
37 public class DistributionParameterHandler {
38     private static final Logger LOGGER = FlexLogger.getLogger(DistributionParameterHandler.class);
39
40     /**
41      * Read the parameters from the parameter file.
42      *
43      * @param arguments the arguments passed to policy distribution
44      * @return the parameters read from the configuration file
45      * @throws PolicyDistributionException on parameter exceptions
46      */
47     public DistributionParameterGroup getParameters(final DistributionCommandLineArguments arguments)
48             throws PolicyDistributionException {
49         DistributionParameterGroup distributionParameterGroup = null;
50
51         // Read the parameters
52         try {
53             // Read the parameters from JSON using Gson
54             final Gson gson = new GsonBuilder()
55                     .create();
56             distributionParameterGroup = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()),
57                     DistributionParameterGroup.class);
58         } catch (final Exception e) {
59             final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
60                     + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
61             LOGGER.error(errorMessage, e);
62             throw new PolicyDistributionException(errorMessage, e);
63         }
64
65         // The JSON processing returns null if there is an empty file
66         if (distributionParameterGroup == null) {
67             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
68             LOGGER.error(errorMessage);
69             throw new PolicyDistributionException(errorMessage);
70         }
71
72         // validate the parameters
73         final GroupValidationResult validationResult = distributionParameterGroup.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 PolicyDistributionException(returnMessage);
81         }
82
83         return distributionParameterGroup;
84     }
85 }