Use new RestClientParameters class in xacml-pdp
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / parameters / XacmlPdpParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019, 2021 AT&T Intellectual Property. 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.pdpx.main.parameters;
22
23 import java.io.File;
24 import org.onap.policy.common.endpoints.parameters.RestClientParameters;
25 import org.onap.policy.common.parameters.ValidationResult;
26 import org.onap.policy.common.utils.coder.Coder;
27 import org.onap.policy.common.utils.coder.StandardCoder;
28 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
29 import org.onap.policy.pdpx.main.startstop.XacmlPdpCommandLineArguments;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35 /**
36  * This class handles reading, parsing and validating of policy xacml pdp parameters from JSON
37  * files.
38  */
39 public class XacmlPdpParameterHandler {
40     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpParameterHandler.class);
41     private static final Coder CODER = new StandardCoder();
42
43     /**
44      * Read the parameters from the parameter file.
45      *
46      * @param arguments the arguments passed to policy xacml pdp
47      * @return the parameters read from the configuration file
48      * @throws PolicyXacmlPdpException on parameter exceptions
49      */
50     public XacmlPdpParameterGroup getParameters(final XacmlPdpCommandLineArguments arguments)
51             throws PolicyXacmlPdpException {
52         XacmlPdpParameterGroup xacmlPdpParameterGroup = null;
53
54         try {
55             // Read the parameters from JSON
56             xacmlPdpParameterGroup = CODER.decode(new File(arguments.getFullConfigurationFilePath()),
57                     XacmlPdpParameterGroup.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);
62             throw new PolicyXacmlPdpException(errorMessage, e);
63         }
64
65         // The JSON processing returns null if there is an empty file
66         if (xacmlPdpParameterGroup == null) {
67             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
68             LOGGER.error(errorMessage);
69             throw new PolicyXacmlPdpException(errorMessage);
70         }
71
72         RestClientParameters apiClientParams = xacmlPdpParameterGroup.getPolicyApiParameters();
73         apiClientParams.setName(XacmlPdpParameterGroup.PARAM_POLICY_API);
74         apiClientParams.setManaged(false);
75
76         // validate the parameters
77         final ValidationResult validationResult = xacmlPdpParameterGroup.validate();
78         if (!validationResult.isValid()) {
79             String returnMessage =
80                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
81             returnMessage += validationResult.getResult();
82
83             LOGGER.error(returnMessage);
84             throw new PolicyXacmlPdpException(returnMessage);
85         }
86
87         return xacmlPdpParameterGroup;
88     }
89 }