Migrate pap startup & controllers to spring boot
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyStatusProvider.java
index 8c96978..dd133a1 100644 (file)
@@ -4,6 +4,7 @@
  * ================================================================================
  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
+ * Modifications Copyright (C) 2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,7 +22,9 @@
 
 package org.onap.policy.pap.main.rest;
 
+import com.google.re2j.Pattern;
 import java.util.Collection;
+import java.util.List;
 import java.util.stream.Collectors;
 import org.onap.policy.common.utils.services.Registry;
 import org.onap.policy.models.base.PfModelException;
@@ -32,22 +35,26 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierO
 import org.onap.policy.pap.main.PapConstants;
 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
 import org.onap.policy.pap.main.notification.DeploymentTracker;
+import org.springframework.boot.context.event.ApplicationReadyEvent;
+import org.springframework.context.event.EventListener;
+import org.springframework.stereotype.Service;
 
 /**
  * Provider for PAP component to query policy deployment status.
  */
+@Service
 public class PolicyStatusProvider {
 
     /**
      * Factory for PAP DAO.
      */
-    private final PolicyModelsProviderFactoryWrapper daoFactory;
-
+    private PolicyModelsProviderFactoryWrapper daoFactory;
 
     /**
      * Constructs the object. Loads all deployed policies into the internal cache.
      */
-    public PolicyStatusProvider() {
+    @EventListener(ApplicationReadyEvent.class)
+    public void initialize() {
         this.daoFactory = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
     }
 
@@ -76,6 +83,28 @@ public class PolicyStatusProvider {
         }
     }
 
+    /**
+     * Gets the deployment status of a policy, returns only statuses, which matches the regex.
+     *
+     * @param patternString policy of interest
+     * @return the deployment status of all policies
+     * @throws PfModelException if a DB error occurs
+     */
+    public Collection<PolicyStatus> getByRegex(String patternString) throws PfModelException {
+        // try to make pattern out of regex
+        final var pattern = Pattern.compile(patternString);
+        // get all the statuses
+        final List<PdpPolicyStatus> policyStatuses;
+        try (PolicyModelsProvider dao = daoFactory.create()) {
+            policyStatuses = dao.getAllPolicyStatus();
+        }
+        // filter out statuses with the wrong name
+        final Collection<PdpPolicyStatus> pdpPolicyStatuses = filterWithPattern(pattern, policyStatuses);
+
+        return accumulate(pdpPolicyStatuses);
+    }
+
+
     /**
      * Accumulates the deployment status of individual PDP/policy pairs into a status for
      * a policy.
@@ -84,7 +113,7 @@ public class PolicyStatusProvider {
      * @return the deployment status of the policies
      */
     private Collection<PolicyStatus> accumulate(Collection<PdpPolicyStatus> source) {
-        DeploymentTracker tracker = new DeploymentTracker();
+        var tracker = new DeploymentTracker();
 
         for (PdpPolicyStatus status : source) {
             if (status.isDeploy()) {
@@ -125,7 +154,7 @@ public class PolicyStatusProvider {
      * Gets the status of a policy in a PdpGroup.
      *
      * @param pdpGroupName the pdp group
-     * @param policy the policy
+     * @param policy       the policy
      * @return the deployment status of the policy
      * @throws PfModelException if a DB error occurs
      */
@@ -136,4 +165,34 @@ public class PolicyStatusProvider {
                 .collect(Collectors.toList());
         }
     }
+
+    /**
+     * Gets the status of policies in a PdpGroup that match the given regex.
+     *
+     * @param pdpGroupName  the pdp group
+     * @param patternString regex
+     * @return the deployment status of policies
+     * @throws PfModelException if a DB error occurs
+     */
+    public Collection<PdpPolicyStatus> getPolicyStatusByRegex(String pdpGroupName, String patternString)
+        throws PfModelException {
+        final var pattern = Pattern.compile(patternString);
+        // get all the statuses
+        final Collection<PdpPolicyStatus> policyStatuses = getPolicyStatus(pdpGroupName);
+        // filter out statuses with the wrong name
+        return filterWithPattern(pattern, policyStatuses);
+    }
+
+    private Collection<PdpPolicyStatus> filterWithPattern(Pattern pattern, Collection<PdpPolicyStatus> policyStatuses) {
+        return policyStatuses
+            .stream()
+            .filter(policyStatus -> {
+                // Check policy name
+                final String policyName = policyStatus
+                    .getPolicy()
+                    .getName();
+                return pattern.matcher(policyName).matches();
+            })
+            .collect(Collectors.toList());
+    }
 }