9aaf3daecfb49397a16121b0332d9abe2dc6733c
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / parameters / XacmlPdpParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 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 com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import java.io.FileReader;
26 import org.onap.policy.common.parameters.GroupValidationResult;
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;
31
32
33
34 /**
35  * This class handles reading, parsing and validating of policy xacml pdp parameters from JSON
36  * files.
37  */
38 public class XacmlPdpParameterHandler {
39     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpParameterHandler.class);
40     private static final Gson gson = new GsonBuilder().create();
41
42     /**
43      * Read the parameters from the parameter file.
44      *
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
48      */
49     public XacmlPdpParameterGroup getParameters(final XacmlPdpCommandLineArguments arguments)
50             throws PolicyXacmlPdpException {
51         XacmlPdpParameterGroup xacmlPdpParameterGroup = null;
52
53         try {
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);
62         }
63
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);
69         }
70
71         // validate the parameters
72         final GroupValidationResult validationResult = xacmlPdpParameterGroup.validate();
73         if (!validationResult.isValid()) {
74             String returnMessage =
75                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
76             returnMessage += validationResult.getResult();
77
78             LOGGER.error(returnMessage);
79             throw new PolicyXacmlPdpException(returnMessage);
80         }
81
82         return xacmlPdpParameterGroup;
83     }
84 }