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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.parameters;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import java.io.FileReader;
26 import org.onap.policy.common.parameters.ValidationResult;
27 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
28 import org.onap.policy.pdpx.main.startstop.XacmlPdpCommandLineArguments;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
35 * This class handles reading, parsing and validating of policy xacml pdp parameters from JSON
38 public class XacmlPdpParameterHandler {
39 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpParameterHandler.class);
40 private static final Gson gson = new GsonBuilder().create();
43 * Read the parameters from the parameter file.
45 * @param arguments the arguments passed to policy xacml pdp
46 * @return the parameters read from the configuration file
47 * @throws PolicyXacmlPdpException on parameter exceptions
49 public XacmlPdpParameterGroup getParameters(final XacmlPdpCommandLineArguments arguments)
50 throws PolicyXacmlPdpException {
51 XacmlPdpParameterGroup xacmlPdpParameterGroup = null;
54 // Read the parameters from JSON using Gson
55 xacmlPdpParameterGroup = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()),
56 XacmlPdpParameterGroup.class);
57 } catch (final Exception e) {
58 final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
59 + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
60 LOGGER.error(errorMessage);
61 throw new PolicyXacmlPdpException(errorMessage, e);
64 // The JSON processing returns null if there is an empty file
65 if (xacmlPdpParameterGroup == null) {
66 final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
67 LOGGER.error(errorMessage);
68 throw new PolicyXacmlPdpException(errorMessage);
71 // validate the parameters
72 final ValidationResult validationResult = xacmlPdpParameterGroup.validate();
73 if (!validationResult.isValid()) {
74 String returnMessage =
75 "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
76 returnMessage += validationResult.getResult();
78 LOGGER.error(returnMessage);
79 throw new PolicyXacmlPdpException(returnMessage);
82 return xacmlPdpParameterGroup;