Support for kafka within xacml tutorial
[policy/xacml-pdp.git] / applications / monitoring / src / main / java / org / onap / policy / xacml / pdp / application / monitoring / MonitoringPdpApplication.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021, 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.xacml.pdp.application.monitoring;
25
26 import com.att.research.xacml.api.Response;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import org.apache.commons.lang3.tuple.Pair;
32 import org.onap.policy.models.decisions.concepts.DecisionRequest;
33 import org.onap.policy.models.decisions.concepts.DecisionResponse;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
35 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
36 import org.onap.policy.pdp.xacml.application.common.std.StdCombinedPolicyResultsTranslator;
37 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * This is the engine class that manages the instance of the XACML PDP engine.
43  *
44  * <p>It is responsible for initializing it and shutting it down properly in a thread-safe manner.
45  *
46  *
47  * @author pameladragosh
48  *
49  */
50 public class MonitoringPdpApplication extends StdXacmlApplicationServiceProvider {
51     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplication.class);
52
53     public static final String ONAP_MONITORING_BASE_POLICY_TYPE = "onap.Monitoring";
54     public static final String ONAP_MONITORING_DERIVED_POLICY_TYPE = "onap.policies.monitoring.";
55
56     public static final String VERSION_100 = "1.0.0";
57
58     private final StdCombinedPolicyResultsTranslator translator = new StdCombinedPolicyResultsTranslator();
59
60     /**
61      * Constructor.
62      */
63     public MonitoringPdpApplication() {
64         super();
65
66         applicationName = "monitoring";
67         actions = List.of("configure");
68         //
69         // By default, this supports just Monitoring policy types
70         //
71         supportedPolicyTypes.add(new ToscaConceptIdentifier(ONAP_MONITORING_BASE_POLICY_TYPE, VERSION_100));
72     }
73
74     @Override
75     public boolean canSupportPolicyType(ToscaConceptIdentifier policyTypeId) {
76         //
77         // For Monitoring, we will attempt to support all versions
78         // of the policy type. Since we are only packaging a decision
79         // back with a JSON payload of the property contents.
80         //
81         return policyTypeId.getName().startsWith(ONAP_MONITORING_DERIVED_POLICY_TYPE);
82     }
83
84     @Override
85     public Pair<DecisionResponse, Response> makeDecision(DecisionRequest request,
86             Map<String, String[]> requestQueryParams) {
87         //
88         // Make the decision
89         //
90         Pair<DecisionResponse, Response> decisionPair = super.makeDecision(request, requestQueryParams);
91         var decisionResponse = decisionPair.getKey();
92         //
93         // Abbreviate results if needed
94         //
95         if (checkAbbreviateResults(requestQueryParams) && decisionResponse.getPolicies() != null) {
96             LOGGER.info("Abbreviating decision results {}", decisionResponse);
97             for (Entry<String, Object> entry : decisionResponse.getPolicies().entrySet()) {
98                 //
99                 // DecisionResponse policies will always be a map
100                 //
101                 @SuppressWarnings("unchecked")
102                 Map<String, Object> policy = (Map<String, Object>) entry.getValue();
103                 policy.remove("type_version");
104                 policy.remove("properties");
105                 policy.remove("name");
106                 policy.remove("version");
107             }
108         }
109         return decisionPair;
110     }
111
112     @Override
113     protected ToscaPolicyTranslator getTranslator(String type) {
114         return translator;
115     }
116
117     /**
118      * Checks the query parameters to determine whether the decision results should be abbreviated.
119      *
120      * @param queryParams - http request query parameters
121      */
122     protected boolean checkAbbreviateResults(Map<String, String[]> queryParams) {
123         if (queryParams != null && !queryParams.isEmpty()) {
124             // Check if query params contains "abbrev" flag
125             if (queryParams.containsKey("abbrev")) {
126                 return Arrays.asList(queryParams.get("abbrev")).contains("true");
127             } else {
128                 LOGGER.info("Unsupported query param for Monitoring application: {}", queryParams);
129                 return false;
130             }
131         }
132         LOGGER.info("Query parameters empty");
133         return false;
134     }
135 }