689bb40c2d7cf2125c3a9c4a0c0670774c65c593
[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.participant.dcae.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.BeanValidationResult;
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 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This class handles reading, parsing and validating of control loop runtime parameters from JSON files.
35  */
36 public class ParticipantDcaeParameterHandler {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantDcaeParameterHandler.class);
39
40     private static final Coder CODER = new StandardCoder();
41
42     /**
43      * Read the parameters from the path of the file.
44      *
45      * @param path path of the config file.
46      * @return the parameters read from the configuration file
47      * @throws ControlLoopException on parameter exceptions
48      */
49     public ParticipantDcaeParameters toParticipantDcaeParameters(String path) throws ControlLoopException {
50         ParticipantDcaeParameters parameters = null;
51         // Read the parameters
52         try {
53             // Read the parameters from JSON
54             File file = new File(path);
55             parameters = CODER.decode(file, ParticipantDcaeParameters.class);
56         } catch (final CoderException e) {
57             final String errorMessage =
58                     "error reading parameters from \"" + path + "\"\n" + "(" + e.getClass().getSimpleName() + ")";
59             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, errorMessage, e);
60         }
61
62         // The JSON processing returns null if there is an empty file
63         if (parameters == null) {
64             final String errorMessage = "no parameters found in \"" + path + "\"";
65             LOGGER.error(errorMessage);
66             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, errorMessage);
67         }
68
69         // validate the parameters
70         final BeanValidationResult validationResult = parameters.validate();
71         if (!validationResult.isValid()) {
72             final String returnMessage =
73                     "validation error(s) on parameters from \"" + path + "\"\n" + validationResult.getResult();
74
75             LOGGER.error(returnMessage);
76             throw new ControlLoopException(Response.Status.NOT_ACCEPTABLE, returnMessage);
77         }
78
79         return parameters;
80     }
81
82 }