Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / provider / DecisionProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2020, 2022 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.rest.provider;
23
24 import com.att.research.xacml.api.Request;
25 import com.att.research.xacml.api.Response;
26 import com.att.research.xacml.api.Result;
27 import java.util.Map;
28 import org.apache.commons.lang3.tuple.Pair;
29 import org.onap.policy.models.decisions.concepts.DecisionException;
30 import org.onap.policy.models.decisions.concepts.DecisionRequest;
31 import org.onap.policy.models.decisions.concepts.DecisionResponse;
32 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
33 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
34 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
35 import org.onap.policy.xacml.pdp.application.nativ.NativePdpApplication;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40 public class DecisionProvider {
41     private static final Logger LOGGER = LoggerFactory.getLogger(DecisionProvider.class);
42
43     /**
44      * Retrieves the policy decision for the specified parameters.
45      *
46      * @param request DecisionRequest
47      * @param queryParams Map of parameters
48      * @return DecisionResponse
49      */
50     public DecisionResponse fetchDecision(DecisionRequest request, Map<String, String[]> queryParams) {
51         LOGGER.debug("Fetching decision {}", request);
52         //
53         // Find application for this decision
54         //
55         XacmlApplicationServiceProvider application = findApplication(request);
56         //
57         // Found application for action
58         //
59         Pair<DecisionResponse, Response> decision = application.makeDecision(request, queryParams);
60         //
61         // Calculate statistics
62         //
63         this.calculateStatistic(decision.getValue(), application.applicationName());
64         //
65         // Return the decision
66         //
67         return decision.getKey();
68     }
69
70     /**
71      * Retrieves the policy decision for the native xacml request.
72      *
73      * @param request the xacml request
74      * @return the xacml response
75      */
76     public Response fetchNativeDecision(Request request) {
77         LOGGER.debug("Fetching decision {}", request);
78         //
79         // Assign native request to native application directly
80         //
81         XacmlApplicationServiceProvider nativeApp = findNativeApplication();
82         //
83         // Make xacml decision
84         //
85         Response decision = ((NativePdpApplication) nativeApp).makeNativeDecision(request);
86         LOGGER.debug("Xacml decision {}", decision);
87         //
88         // Calculate statistics
89         //
90         this.calculateStatistic(decision, nativeApp.applicationName());
91         //
92         // Return the string decision
93         //
94         return decision;
95     }
96
97     private XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
98         XacmlApplicationServiceProvider application = XacmlPdpApplicationManager.getCurrent().findApplication(request);
99         if (application != null) {
100             return application;
101         }
102         throw new DecisionException(jakarta.ws.rs.core.Response.Status.BAD_REQUEST,
103                 "No application for action " + request.getAction());
104     }
105
106     private XacmlApplicationServiceProvider findNativeApplication() {
107         XacmlApplicationServiceProvider application = XacmlPdpApplicationManager.getCurrent().findNativeApplication();
108         if (application instanceof NativePdpApplication) {
109             return application;
110         }
111         throw new DecisionException(jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR,
112                 "Native PDP application cannot be found");
113     }
114
115     private void calculateStatistic(Response xacmlResponse, String appName) {
116         if (xacmlResponse == null) {
117             XacmlPdpStatisticsManager.getCurrent().updateErrorCount();
118             return;
119         }
120         for (Result result : xacmlResponse.getResults()) {
121             switch (result.getDecision()) {
122                 case PERMIT:
123                     XacmlPdpStatisticsManager.getCurrent().updatePermitDecisionsCount(appName);
124                     break;
125
126                 case DENY:
127                     XacmlPdpStatisticsManager.getCurrent().updateDenyDecisionsCount(appName);
128                     break;
129
130                 case INDETERMINATE, INDETERMINATE_DENY, INDETERMINATE_DENYPERMIT, INDETERMINATE_PERMIT:
131                     XacmlPdpStatisticsManager.getCurrent().updateIndeterminantDecisionsCount(appName);
132                     break;
133
134                 case NOTAPPLICABLE:
135                     XacmlPdpStatisticsManager.getCurrent().updateNotApplicableDecisionsCount(appName);
136                     break;
137
138                 default:
139                     break;
140             }
141         }
142     }
143 }