a7f5ff34d69c334b743bdc731e43aea8b8ada77f
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.clamp.controlloop.runtime.main.parameters;
22
23 import java.io.File;
24 import javax.ws.rs.core.Response;
25 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
26 import org.onap.policy.clamp.controlloop.runtime.main.startstop.ClRuntimeCommandLineArguments;
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
32 /**
33  * This class handles reading, parsing and validating of control loop runtime parameters from JSON files.
34  */
35 public class ClRuntimeParameterHandler {
36
37     private static final Coder CODER = new StandardCoder();
38
39     /**
40      * Read the parameters from the parameter file.
41      *
42      * @param arguments the arguments passed to control loop runtime
43      * @return the parameters read from the configuration file
44      * @throws ControlLoopException on parameter exceptions
45      */
46     public ClRuntimeParameterGroup getParameters(final ClRuntimeCommandLineArguments arguments)
47             throws ControlLoopException {
48         ClRuntimeParameterGroup clRuntimeParameterGroup = null;
49
50         // Read the parameters
51         try {
52             // Read the parameters from JSON
53             File file = new File(arguments.getFullConfigurationFilePath());
54             clRuntimeParameterGroup = CODER.decode(file, ClRuntimeParameterGroup.class);
55         } catch (final CoderException e) {
56             throw new ControlLoopException(
57                     Response.Status.NOT_ACCEPTABLE, "error reading parameters from \""
58                             + arguments.getConfigurationFilePath() + "\"\n" + "(" + e.getClass().getSimpleName() + ")",
59                     e);
60         }
61
62         // The JSON processing returns null if there is an empty file
63         if (clRuntimeParameterGroup == null) {
64             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
65                     "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"");
66         }
67
68         // validate the parameters
69         final GroupValidationResult validationResult = clRuntimeParameterGroup.validate();
70         if (!validationResult.isValid()) {
71             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, "validation error(s) on parameters from \""
72                     + arguments.getConfigurationFilePath() + "\"\n" + validationResult.getResult());
73         }
74
75         return clRuntimeParameterGroup;
76     }
77 }