bcf1124d216ddafad1dd2d16a893a3617250eac8
[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.common.parameters.ValidationResult;
27 import org.onap.policy.common.utils.coder.Coder;
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30
31 /**
32  * This class handles reading, parsing and validating of control loop runtime parameters from JSON files.
33  */
34 public class ClRuntimeParameterHandler {
35
36     private static final Coder CODER = new StandardCoder();
37
38     /**
39      * Read the parameters from the parameter file.
40      *
41      * @param path the path passed to control loop runtime
42      * @return the parameters read from the configuration file
43      * @throws ControlLoopException on parameter exceptions
44      */
45     public ClRuntimeParameterGroup getParameters(final String path) throws ControlLoopException {
46         ClRuntimeParameterGroup clRuntimeParameterGroup = null;
47
48         // Read the parameters
49         try {
50             // Read the parameters from JSON
51             File file = new File(path);
52             clRuntimeParameterGroup = CODER.decode(file, ClRuntimeParameterGroup.class);
53         } catch (final CoderException e) {
54             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
55                     "error reading parameters from \"" + path + "\"\n" + "(" + e.getClass().getSimpleName() + ")", e);
56         }
57
58         // The JSON processing returns null if there is an empty file
59         if (clRuntimeParameterGroup == null) {
60             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, "no parameters found in \"" + path + "\"");
61         }
62
63         // validate the parameters
64         final ValidationResult validationResult = clRuntimeParameterGroup.validate();
65         if (!validationResult.isValid()) {
66             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE,
67                     "validation error(s) on parameters from \"" + path + "\"\n" + validationResult.getResult());
68         }
69
70         return clRuntimeParameterGroup;
71     }
72 }