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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.rest.provider;
24 import com.att.research.xacml.api.Request;
25 import com.att.research.xacml.api.Response;
26 import com.att.research.xacml.api.Result;
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;
40 public class DecisionProvider {
41 private static final Logger LOGGER = LoggerFactory.getLogger(DecisionProvider.class);
44 * Retrieves the policy decision for the specified parameters.
46 * @param request DecisionRequest
47 * @param queryParams Map of parameters
48 * @return DecisionResponse
50 public DecisionResponse fetchDecision(DecisionRequest request, Map<String, String[]> queryParams) {
51 LOGGER.debug("Fetching decision {}", request);
53 // Find application for this decision
55 XacmlApplicationServiceProvider application = findApplication(request);
57 // Found application for action
59 Pair<DecisionResponse, Response> decision = application.makeDecision(request, queryParams);
61 // Calculate statistics
63 this.calculateStatistic(decision.getValue(), application.applicationName());
65 // Return the decision
67 return decision.getKey();
71 * Retrieves the policy decision for the native xacml request.
73 * @param request the xacml request
74 * @return the xacml response
76 public Response fetchNativeDecision(Request request) {
77 LOGGER.debug("Fetching decision {}", request);
79 // Assign native request to native application directly
81 XacmlApplicationServiceProvider nativeApp = findNativeApplication();
83 // Make xacml decision
85 Response decision = ((NativePdpApplication) nativeApp).makeNativeDecision(request);
86 LOGGER.debug("Xacml decision {}", decision);
88 // Calculate statistics
90 this.calculateStatistic(decision, nativeApp.applicationName());
92 // Return the string decision
97 private XacmlApplicationServiceProvider findApplication(DecisionRequest request) {
98 XacmlApplicationServiceProvider application = XacmlPdpApplicationManager.getCurrent().findApplication(request);
99 if (application != null) {
102 throw new DecisionException(jakarta.ws.rs.core.Response.Status.BAD_REQUEST,
103 "No application for action " + request.getAction());
106 private XacmlApplicationServiceProvider findNativeApplication() {
107 XacmlApplicationServiceProvider application = XacmlPdpApplicationManager.getCurrent().findNativeApplication();
108 if (application instanceof NativePdpApplication) {
111 throw new DecisionException(jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR,
112 "Native PDP application cannot be found");
115 private void calculateStatistic(Response xacmlResponse, String appName) {
116 if (xacmlResponse == null) {
117 XacmlPdpStatisticsManager.getCurrent().updateErrorCount();
120 for (Result result : xacmlResponse.getResults()) {
121 switch (result.getDecision()) {
123 XacmlPdpStatisticsManager.getCurrent().updatePermitDecisionsCount(appName);
127 XacmlPdpStatisticsManager.getCurrent().updateDenyDecisionsCount(appName);
130 case INDETERMINATE, INDETERMINATE_DENY, INDETERMINATE_DENYPERMIT, INDETERMINATE_PERMIT:
131 XacmlPdpStatisticsManager.getCurrent().updateIndeterminantDecisionsCount(appName);
135 XacmlPdpStatisticsManager.getCurrent().updateNotApplicableDecisionsCount(appName);