dae4b833414a2ad70e3e15f1d1999105e997b31c
[policy/apex-pdp.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.apex.starter.parameters;
22
23 import java.io.File;
24
25 import org.onap.policy.apex.starter.ApexStarterCommandLineArguments;
26 import org.onap.policy.apex.starter.exception.ApexStarterException;
27 import org.onap.policy.common.parameters.GroupValidationResult;
28 import org.onap.policy.common.utils.coder.Coder;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class handles reading, parsing and validating of apex starter parameters from JSON files.
36  *
37  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
38  */
39 public class ApexStarterParameterHandler {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterParameterHandler.class);
42     private static final Coder CODER = new StandardCoder();
43
44     /**
45      * Read the parameters from the parameter file.
46      *
47      * @param arguments the arguments passed to apex starter
48      * @return the parameters read from the configuration file
49      * @throws ApexStarterException on parameter exceptions
50      */
51     public ApexStarterParameterGroup getParameters(final ApexStarterCommandLineArguments arguments)
52             throws ApexStarterException {
53         ApexStarterParameterGroup apexStarterParameterGroup = null;
54
55         // Read the parameters
56         try {
57             // Read the parameters from JSON
58             final File file = new File(arguments.getFullConfigurationFilePath());
59             apexStarterParameterGroup = CODER.decode(file, ApexStarterParameterGroup.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, e);
64             throw new ApexStarterException(errorMessage, e);
65         }
66
67         // The JSON processing returns null if there is an empty file
68         if (apexStarterParameterGroup == null) {
69             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
70             LOGGER.error(errorMessage);
71             throw new ApexStarterException(errorMessage);
72         }
73
74         // validate the parameters
75         final GroupValidationResult validationResult = apexStarterParameterGroup.validate();
76         if (!validationResult.isValid()) {
77             String returnMessage =
78                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
79             returnMessage += validationResult.getResult();
80
81             LOGGER.error(returnMessage);
82             throw new ApexStarterException(returnMessage);
83         }
84
85         return apexStarterParameterGroup;
86     }
87
88 }