Activation status handling mechanism for ForwardingConstruct provisioning
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / mef / nrp / impl / ActivationTransaction.java
index 1d0c6af471f1732c030cacd22d716969418a7a93..9cef7c021d53d56d404d644cc6ed7c8d21f51136 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.unimgr.mef.nrp.impl;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
 
 import org.opendaylight.unimgr.mef.nrp.api.ActivationDriver;
 import org.slf4j.Logger;
@@ -19,11 +20,12 @@ import org.slf4j.LoggerFactory;
  * Runs activation over multiple @ drivers.
  *
  * @author bartosz.michalik@amartus.com
+ * @author krzysztof.bijakowski@amartus.com [modifications]
  */
 public class ActivationTransaction {
     private static final Logger LOG = LoggerFactory.getLogger(ActivationTransaction.class);
-    private List<ActivationDriver> drivers = new ArrayList<>();
 
+    private List<ActivationDriver> drivers = new ArrayList<>();
 
     public void addDriver(ActivationDriver driver) {
         drivers.add(driver);
@@ -32,7 +34,7 @@ public class ActivationTransaction {
     /**
      * Activate the contents of this transaction.
      */
-    public void activate() {
+    public Result activate() {
         sortDrivers();
         try {
             for (ActivationDriver d: drivers) {
@@ -40,17 +42,21 @@ public class ActivationTransaction {
             }
             commit();
             LOG.info("Activate transaction successful");
+
+            return Result.success();
         } catch (Exception e) {
             //XXX add transaction identification ???
             LOG.warn("Rolling back activate transaction ", e);
             rollback();
+
+            return Result.fail(e.getMessage(), e);
         }
     }
 
     /**
      * Deactivate the contents of this transaction.
      */
-    public void deactivate() {
+    public Result deactivate() {
         sortDrivers();
         try {
             for (ActivationDriver d: drivers) {
@@ -58,10 +64,14 @@ public class ActivationTransaction {
             }
             LOG.info("Deactivate transaction successful");
             commit();
+
+            return Result.success();
         } catch (Exception e) {
             //XXX add transaction identification ???
             LOG.warn("Rolling back deactivate transaction ", e);
             rollback();
+
+            return Result.fail(e.getMessage(), e);
         }
     }
 
@@ -77,4 +87,38 @@ public class ActivationTransaction {
         drivers.sort((driverA, driverB) -> driverA.priority() - driverB.priority());
     }
 
+    public static class Result {
+        private boolean successful;
+
+        private Optional<String> message;
+
+        private Optional<Throwable> cause;
+
+        private Result(boolean successful, Optional<String> message, Optional<Throwable> cause) {
+            this.successful = successful;
+            this.message = message;
+            this.cause = cause;
+        }
+
+        public Optional<Throwable> getCause() {
+            return cause;
+        }
+
+        public boolean isSuccessful() {
+            return successful;
+        }
+
+        public Optional<String> getMessage() {
+            return message;
+        }
+
+        public static Result success(){
+            return new Result(true, Optional.empty(), Optional.empty());
+        }
+
+        public static Result fail(String message, Throwable cause){
+            return new Result(false, Optional.of(message), Optional.of(cause));
+        }
+    }
+
 }