5c41a9e28176a8b835dc8808dc953d7d9191d7fa
[policy/xacml-pdp.git] /
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.ArrayList;
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.ToscaPolicyTypeIdentifier;
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_CDAP = "onap.policies.monitoring.cdap.tca.hi.lo.app";
55     public static final String ONAP_MONITORING_APPSERVER =
56             "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server";
57     public static final String ONAP_MONITORING_SONHANDLER = "onap.policies.monitoring.docker.sonhandler.app";
58     public static final String ONAP_MONITORING_DERIVED_POLICY_TYPE = "onap.policies.monitoring.";
59
60     public static final String VERSION_100 = "1.0.0";
61
62     private StdCombinedPolicyResultsTranslator translator = new StdCombinedPolicyResultsTranslator();
63     private List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
64
65     /**
66      * Constructor.
67      */
68     public MonitoringPdpApplication() {
69         //
70         // By default this supports just Monitoring policy types
71         //
72         supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_BASE_POLICY_TYPE, VERSION_100));
73         supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_CDAP, VERSION_100));
74         supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_APPSERVER, VERSION_100));
75         supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_SONHANDLER, VERSION_100));
76     }
77
78     @Override
79     public String applicationName() {
80         return "monitoring";
81     }
82
83     @Override
84     public List<String> actionDecisionsSupported() {
85         return Arrays.asList("configure");
86     }
87
88     @Override
89     public synchronized List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
90         return supportedPolicyTypes;
91     }
92
93     @Override
94     public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
95         //
96         // For Monitoring, we will attempt to support all versions
97         // of the policy type. Since we are only packaging a decision
98         // back with a JSON payload of the property contents.
99         //
100         return (policyTypeId.getName().equals(ONAP_MONITORING_BASE_POLICY_TYPE)
101                 || policyTypeId.getName().equals(ONAP_MONITORING_CDAP)
102                 || policyTypeId.getName().equals(ONAP_MONITORING_APPSERVER)
103                 || policyTypeId.getName().equals(ONAP_MONITORING_SONHANDLER)
104                 || policyTypeId.getName().startsWith(ONAP_MONITORING_DERIVED_POLICY_TYPE));
105     }
106
107     @Override
108     public Pair<DecisionResponse, Response> makeDecision(DecisionRequest request,
109             Map<String, String[]> requestQueryParams) {
110         //
111         // Make the decision
112         //
113         Pair<DecisionResponse, Response> decisionPair = super.makeDecision(request, requestQueryParams);
114         DecisionResponse decisionResponse = decisionPair.getKey();
115         //
116         // Abbreviate results if needed
117         //
118         if (checkAbbreviateResults(requestQueryParams) && decisionResponse.getPolicies() != null) {
119             LOGGER.info("Abbreviating decision results {}", decisionResponse);
120             for (Entry<String, Object> entry : decisionResponse.getPolicies().entrySet()) {
121                 //
122                 // DecisionResponse policies will always be a map
123                 //
124                 @SuppressWarnings("unchecked")
125                 Map<String, Object> policy = (Map<String, Object>) entry.getValue();
126                 policy.remove("type_version");
127                 policy.remove("properties");
128                 policy.remove("name");
129                 policy.remove("version");
130             }
131         }
132         return decisionPair;
133     }
134
135     @Override
136     protected ToscaPolicyTranslator getTranslator(String type) {
137         return translator;
138     }
139
140     /**
141      * Checks the query parameters to determine whether the decision results should be abbreviated.
142      *
143      * @param queryParams - http request query parameters
144      */
145     private boolean checkAbbreviateResults(Map<String, String[]> queryParams) {
146         if (queryParams != null && !queryParams.isEmpty()) {
147             // Check if query params contains "abbrev" flag
148             if (queryParams.containsKey("abbrev")) {
149                 return Arrays.asList(queryParams.get("abbrev")).contains("true");
150             } else {
151                 LOGGER.info("Unsupported query param for Monitoring application: {}", queryParams);
152                 return false;
153             }
154         }
155         LOGGER.info("Query parameters empty");
156         return false;
157     }
158 }