Support for kafka within xacml tutorial
[policy/xacml-pdp.git] / applications / monitoring / src / main / java / org / onap / policy / xacml / pdp / application / monitoring / MonitoringPdpApplication.java
index 3c9a4fb..626c916 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2021, 2024 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,9 +23,7 @@
 
 package org.onap.policy.xacml.pdp.application.monitoring;
 
-import com.att.research.xacml.api.Request;
 import com.att.research.xacml.api.Response;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -32,7 +31,7 @@ import java.util.Map.Entry;
 import org.apache.commons.lang3.tuple.Pair;
 import org.onap.policy.models.decisions.concepts.DecisionRequest;
 import org.onap.policy.models.decisions.concepts.DecisionResponse;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
 import org.onap.policy.pdp.xacml.application.common.std.StdCombinedPolicyResultsTranslator;
 import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider;
@@ -51,91 +50,63 @@ import org.slf4j.LoggerFactory;
 public class MonitoringPdpApplication extends StdXacmlApplicationServiceProvider {
     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplication.class);
 
-    private static final String ONAP_MONITORING_BASE_POLICY_TYPE = "onap.Monitoring";
-    private static final String ONAP_MONITORING_CDAP = "onap.policies.monitoring.cdap.tca.hi.lo.app";
-    private static final String ONAP_MONITORING_APPSERVER =
-            "onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server";
-    private static final String ONAP_MONITORING_SONHANDLER = "onap.policies.monitoring.docker.sonhandler.app";
-    private static final String ONAP_MONITORING_DERIVED_POLICY_TYPE = "onap.policies.monitoring";
-    private static final String VERSION_100 = "1.0.0";
+    public static final String ONAP_MONITORING_BASE_POLICY_TYPE = "onap.Monitoring";
+    public static final String ONAP_MONITORING_DERIVED_POLICY_TYPE = "onap.policies.monitoring.";
 
-    private StdCombinedPolicyResultsTranslator translator = new StdCombinedPolicyResultsTranslator();
-    private List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();
+    public static final String VERSION_100 = "1.0.0";
+
+    private final StdCombinedPolicyResultsTranslator translator = new StdCombinedPolicyResultsTranslator();
 
     /**
      * Constructor.
      */
     public MonitoringPdpApplication() {
+        super();
+
+        applicationName = "monitoring";
+        actions = List.of("configure");
         //
-        // By default this supports just Monitoring policy types
+        // By default, this supports just Monitoring policy types
         //
-        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_BASE_POLICY_TYPE, VERSION_100));
-        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_CDAP, VERSION_100));
-        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_APPSERVER, VERSION_100));
-        supportedPolicyTypes.add(new ToscaPolicyTypeIdentifier(ONAP_MONITORING_SONHANDLER, VERSION_100));
-    }
-
-    @Override
-    public String applicationName() {
-        return "monitoring";
-    }
-
-    @Override
-    public List<String> actionDecisionsSupported() {
-        return Arrays.asList("configure");
+        supportedPolicyTypes.add(new ToscaConceptIdentifier(ONAP_MONITORING_BASE_POLICY_TYPE, VERSION_100));
     }
 
     @Override
-    public synchronized List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
-        return supportedPolicyTypes;
-    }
-
-    @Override
-    public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
+    public boolean canSupportPolicyType(ToscaConceptIdentifier policyTypeId) {
         //
         // For Monitoring, we will attempt to support all versions
         // of the policy type. Since we are only packaging a decision
         // back with a JSON payload of the property contents.
         //
-        return (policyTypeId.getName().equals(ONAP_MONITORING_BASE_POLICY_TYPE)
-                || policyTypeId.getName().equals(ONAP_MONITORING_CDAP)
-                || policyTypeId.getName().equals(ONAP_MONITORING_APPSERVER)
-                || policyTypeId.getName().equals(ONAP_MONITORING_SONHANDLER)
-                || policyTypeId.getName().startsWith(ONAP_MONITORING_DERIVED_POLICY_TYPE));
+        return policyTypeId.getName().startsWith(ONAP_MONITORING_DERIVED_POLICY_TYPE);
     }
 
     @Override
     public Pair<DecisionResponse, Response> makeDecision(DecisionRequest request,
             Map<String, String[]> requestQueryParams) {
         //
-        // Convert to a XacmlRequest
-        //
-        Request xacmlRequest = this.getTranslator().convertRequest(request);
-        //
-        // Now get a decision
-        //
-        Response xacmlResponse = this.xacmlDecision(xacmlRequest);
-        //
-        // Convert to a DecisionResponse
+        // Make the decision
         //
-        DecisionResponse decisionResponse = this.getTranslator().convertResponse(xacmlResponse);
+        Pair<DecisionResponse, Response> decisionPair = super.makeDecision(request, requestQueryParams);
+        var decisionResponse = decisionPair.getKey();
         //
         // Abbreviate results if needed
         //
-        if (checkAbbreviateResults(requestQueryParams) && decisionResponse.getPolicies() != null
-                && !decisionResponse.getPolicies().isEmpty()) {
+        if (checkAbbreviateResults(requestQueryParams) && decisionResponse.getPolicies() != null) {
             LOGGER.info("Abbreviating decision results {}", decisionResponse);
             for (Entry<String, Object> entry : decisionResponse.getPolicies().entrySet()) {
-                if (entry.getValue() instanceof Map) {
-                    @SuppressWarnings("unchecked")
-                    Map<String, Object> policy = (Map<String, Object>) entry.getValue();
-                    policy.remove("properties");
-                    policy.remove("name");
-                    policy.remove("version");
-                }
+                //
+                // DecisionResponse policies will always be a map
+                //
+                @SuppressWarnings("unchecked")
+                Map<String, Object> policy = (Map<String, Object>) entry.getValue();
+                policy.remove("type_version");
+                policy.remove("properties");
+                policy.remove("name");
+                policy.remove("version");
             }
         }
-        return Pair.of(decisionResponse, xacmlResponse);
+        return decisionPair;
     }
 
     @Override
@@ -148,7 +119,7 @@ public class MonitoringPdpApplication extends StdXacmlApplicationServiceProvider
      *
      * @param queryParams - http request query parameters
      */
-    private boolean checkAbbreviateResults(Map<String, String[]> queryParams) {
+    protected boolean checkAbbreviateResults(Map<String, String[]> queryParams) {
         if (queryParams != null && !queryParams.isEmpty()) {
             // Check if query params contains "abbrev" flag
             if (queryParams.containsKey("abbrev")) {