Add code to store abatement events. 35/61435/5
authorJoshua Reich <jreich@research.att.com>
Tue, 14 Aug 2018 22:04:03 +0000 (15:04 -0700)
committerJoshua Reich <jreich@research.att.com>
Fri, 31 Aug 2018 18:51:59 +0000 (11:51 -0700)
Stores abatement events in history DB.

Change-Id: Ifca6acd42cd9eea85e27765bd268a491028e305a
Issue-ID: POLICY-975
Signed-off-by: Joshua Reich <jreich@research.att.com>
controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager.java
controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManager.java
controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl

index 4b438a9..930f957 100644 (file)
@@ -30,6 +30,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import java.util.NoSuchElementException;
 
 import org.onap.policy.aai.AaiGetVnfResponse;
 import org.onap.policy.aai.AaiGetVserverResponse;
@@ -99,6 +100,7 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
     private boolean isActivated = false;
     private LinkedList<ControlLoopOperation> controlLoopHistory = new LinkedList<>();
     private ControlLoopOperationManager currentOperation = null;
+    private ControlLoopOperationManager lastOperationManager = null;
     private transient TargetLock targetLock = null;
     private AaiGetVnfResponse vnfResponse = null;
     private AaiGetVserverResponse vserverResponse = null;
@@ -414,6 +416,7 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
         //
         // And setup an operation
         //
+        this.lastOperationManager = this.currentOperation;
         this.currentOperation = new ControlLoopOperationManager(this.onset, policy, this);
         //
         // Return it
@@ -449,6 +452,7 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
                 //
                 // Just null this out
                 //
+                this.lastOperationManager = this.currentOperation;
                 this.currentOperation = null;
                 //
                 // TODO: Release our lock
@@ -594,6 +598,26 @@ public class ControlLoopEventManager implements LockCallback, Serializable {
         return NEW_EVENT_STATUS.SYNTAX_ERROR;
     }
 
+
+    /**
+     * Commit the abatement to the history database.
+     *
+     * @param message the abatement message
+     * @param outcome the abatement outcome
+     */
+    public void commitAbatement(String message, String outcome) {
+        if (this.lastOperationManager == null) {
+            logger.error("{}: commitAbatement: no operation manager", this);
+            return;
+        }
+        try{
+            this.lastOperationManager.commitAbatement(message,outcome);          
+        } catch (NoSuchElementException e) {
+            logger.error("{}: commitAbatement threw an exception ", this, e);
+        }
+    }
+
+    
     /**
      * Set the control loop time out.
      *
index 0eb924d..f1d912e 100644 (file)
@@ -27,6 +27,7 @@ import java.util.AbstractMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Properties;
+import java.util.NoSuchElementException;
 
 import javax.persistence.EntityManager;
 import javax.persistence.Persistence;
@@ -844,7 +845,36 @@ public class ControlLoopOperationManager implements Serializable {
             }
         }
         logger.debug("Could not find associated operation");
-
     }
 
+
+    /**
+     * Commit the abatement to the history database.
+     *
+     * @param message the abatement message
+     * @param outcome the abatement outcome
+     */
+    public void commitAbatement(String message, String outcome) {
+        logger.info("commitAbatement: " + message + ", " + outcome);
+        
+        if (this.currentOperation == null) {
+            try {
+                this.currentOperation = this.operationHistory.getLast();
+            } catch (NoSuchElementException e) {
+                logger.error("{}: commitAbatement threw an exception ", this, e);
+                return;
+            }
+        }
+        this.currentOperation.clOperation.setEnd(Instant.now());
+        this.currentOperation.clOperation.setMessage(message);
+        this.currentOperation.clOperation.setOutcome(outcome);
+        //
+        // Store commit in DB
+        //
+        this.storeOperationInDataBase();
+        //
+        // Clear the current operation field
+        //
+        this.currentOperation = null;
+     }
 }
index 71715c0..b210c79 100644 (file)
@@ -340,6 +340,10 @@ rule "${policyName}.EVENT.MANAGER"
                 logger.info("{}: {}: abatement received for {}.  Closing the control loop", 
                             $params.getClosedLoopControlName(), drools.getRule().getName(), 
                             $event.getRequestId());
+                
+                /// DB Write---end event processing for this RequestId()
+                $manager.commitAbatement("Event Abated","Closed");
+                
                 notification.setFrom("policy");
                 notification.setPolicyName(drools.getRule().getName());
                 notification.setPolicyScope("${policyScope}");