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