Added 'onap.monitoring.*' in list of supported policy types
[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 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.Request;
26 import com.att.research.xacml.api.Response;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import org.apache.commons.lang3.tuple.Pair;
33 import org.onap.policy.models.decisions.concepts.DecisionRequest;
34 import org.onap.policy.models.decisions.concepts.DecisionResponse;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
36 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
37 import org.onap.policy.pdp.xacml.application.common.std.StdCombinedPolicyResultsTranslator;
38 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * This is the engine class that manages the instance of the XACML PDP engine.
44  *
45  * <p>It is responsible for initializing it and shutting it down properly in a thread-safe manner.
46  *
47  *
48  * @author pameladragosh
49  *
50  */
51 public class MonitoringPdpApplication extends StdXacmlApplicationServiceProvider {
52     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplication.class);
53
54     private static final String ONAP_MONITORING_BASE_POLICY_TYPE = "onap.Monitoring";
55     private static final String ONAP_MONITORING_CDAP = "onap.policies.monitoring.cdap.tca.hi.lo.app";
56     private static final String ONAP_MONITORING_APPSERVER =
57             "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server";
58     private static final String ONAP_MONITORING_SONHANDLER = "onap.policies.monitoring.docker.sonhandler.app";
59     private static final String ONAP_MONITORING_DERIVED_POLICY_TYPE = "onap.policies.monitoring.";
60     // Note: this requirement is temporary; it will no longer be necessary once the PDPs and PAP
61     // are updated to use the PDP Group name instead of the supported types.
62     private static final String ONAP_MONITORING_ALL_DERIVED_POLICY_TYPE = ONAP_MONITORING_DERIVED_POLICY_TYPE + "*";
63
64     private static final String VERSION_100 = "1.0.0";
65
66     private StdCombinedPolicyResultsTranslator translator = new StdCombinedPolicyResultsTranslator();
67     private List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
68
69     /**
70      * Constructor.
71      */
72     public MonitoringPdpApplication() {
73         //
74         // By default this supports just Monitoring policy types
75         //
76         supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_BASE_POLICY_TYPE, VERSION_100));
77         supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_CDAP, VERSION_100));
78         supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_APPSERVER, VERSION_100));
79         supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_SONHANDLER, VERSION_100));
80
81         // temporary requirement
82         supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_ALL_DERIVED_POLICY_TYPE, VERSION_100));
83     }
84
85     @Override
86     public String applicationName() {
87         return "monitoring";
88     }
89
90     @Override
91     public List<String> actionDecisionsSupported() {
92         return Arrays.asList("configure");
93     }
94
95     @Override
96     public synchronized List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
97         return supportedPolicyTypes;
98     }
99
100     @Override
101     public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
102         //
103         // For Monitoring, we will attempt to support all versions
104         // of the policy type. Since we are only packaging a decision
105         // back with a JSON payload of the property contents.
106         //
107         return (policyTypeId.getName().equals(ONAP_MONITORING_BASE_POLICY_TYPE)
108                 || policyTypeId.getName().equals(ONAP_MONITORING_CDAP)
109                 || policyTypeId.getName().equals(ONAP_MONITORING_APPSERVER)
110                 || policyTypeId.getName().equals(ONAP_MONITORING_SONHANDLER)
111                 || policyTypeId.getName().startsWith(ONAP_MONITORING_DERIVED_POLICY_TYPE));
112     }
113
114     @Override
115     public Pair<DecisionResponse, Response> makeDecision(DecisionRequest request,
116             Map<String, String[]> requestQueryParams) {
117         //
118         // Convert to a XacmlRequest
119         //
120         Request xacmlRequest = this.getTranslator().convertRequest(request);
121         //
122         // Now get a decision
123         //
124         Response xacmlResponse = this.xacmlDecision(xacmlRequest);
125         //
126         // Convert to a DecisionResponse
127         //
128         DecisionResponse decisionResponse = this.getTranslator().convertResponse(xacmlResponse);
129         //
130         // Abbreviate results if needed
131         //
132         if (checkAbbreviateResults(requestQueryParams) && decisionResponse.getPolicies() != null
133                 && !decisionResponse.getPolicies().isEmpty()) {
134             LOGGER.info("Abbreviating decision results {}", decisionResponse);
135             for (Entry<String, Object> entry : decisionResponse.getPolicies().entrySet()) {
136                 if (entry.getValue() instanceof Map) {
137                     @SuppressWarnings("unchecked")
138                     Map<String, Object> policy = (Map<String, Object>) entry.getValue();
139                     policy.remove("type_version");
140                     policy.remove("properties");
141                     policy.remove("name");
142                     policy.remove("version");
143                 }
144             }
145         }
146         return Pair.of(decisionResponse, xacmlResponse);
147     }
148
149     @Override
150     protected ToscaPolicyTranslator getTranslator(String type) {
151         return translator;
152     }
153
154     /**
155      * Checks the query parameters to determine whether the decision results should be abbreviated.
156      *
157      * @param queryParams - http request query parameters
158      */
159     private boolean checkAbbreviateResults(Map<String, String[]> queryParams) {
160         if (queryParams != null && !queryParams.isEmpty()) {
161             // Check if query params contains "abbrev" flag
162             if (queryParams.containsKey("abbrev")) {
163                 return Arrays.asList(queryParams.get("abbrev")).contains("true");
164             } else {
165                 LOGGER.info("Unsupported query param for Monitoring application: {}", queryParams);
166                 return false;
167             }
168         }
169         LOGGER.info("Query parameters empty");
170         return false;
171     }
172 }