Datastore-constrained txes: vpnmanager
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / InterfaceStateChangeListener.java
index 792c8ba79705598f3d24989775121296cd11fb7f..1516de6e91e73eeee64d57327efb06dbd05dec3e 100644 (file)
@@ -7,7 +7,12 @@
  */
 package org.opendaylight.netvirt.vpnmanager;
 
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
+
 import com.google.common.base.Optional;
+import com.google.common.collect.HashBasedTable;
+import com.google.common.collect.Table;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
@@ -41,20 +46,44 @@ public class InterfaceStateChangeListener
 
     private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
     private static final short DJC_MAX_RETRIES = 3;
-
     private final DataBroker dataBroker;
     private final ManagedNewTransactionRunner txRunner;
     private final VpnInterfaceManager vpnInterfaceManager;
+    private final VpnUtil vpnUtil;
     private final JobCoordinator jobCoordinator;
 
+    Table<OperStatus, OperStatus, IntfTransitionState> stateTable = HashBasedTable.create();
+
+    enum IntfTransitionState {
+        STATE_UP,
+        STATE_DOWN,
+        STATE_IGNORE
+    }
+
+    private void initialize() {
+        //  Interface State Transition Table
+        //               Up                Down            Unknown
+        // ---------------------------------------------------------------
+        /* Up       { STATE_IGNORE,   STATE_DOWN,     STATE_IGNORE }, */
+        /* Down     { STATE_UP,       STATE_IGNORE,   STATE_IGNORE }, */
+        /* Unknown  { STATE_UP,       STATE_DOWN,     STATE_IGNORE }, */
+
+        stateTable.put(Interface.OperStatus.Up, Interface.OperStatus.Down, IntfTransitionState.STATE_DOWN);
+        stateTable.put(Interface.OperStatus.Down, Interface.OperStatus.Up, IntfTransitionState.STATE_UP);
+        stateTable.put(Interface.OperStatus.Unknown, Interface.OperStatus.Up, IntfTransitionState.STATE_UP);
+        stateTable.put(Interface.OperStatus.Unknown, Interface.OperStatus.Down, IntfTransitionState.STATE_DOWN);
+    }
+
     @Inject
     public InterfaceStateChangeListener(final DataBroker dataBroker, final VpnInterfaceManager vpnInterfaceManager,
-            final JobCoordinator jobCoordinator) {
+            final VpnUtil vpnUtil, final JobCoordinator jobCoordinator) {
         super(Interface.class, InterfaceStateChangeListener.class);
         this.dataBroker = dataBroker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.vpnInterfaceManager = vpnInterfaceManager;
+        this.vpnUtil = vpnUtil;
         this.jobCoordinator = jobCoordinator;
+        initialize();
     }
 
     @PostConstruct
@@ -85,25 +114,24 @@ public class InterfaceStateChangeListener
                                 intrf.getName());
                 jobCoordinator.enqueueJob("VPNINTERFACE-" + intrf.getName(), () -> {
                     List<ListenableFuture<Void>> futures = new ArrayList<>(3);
-                    futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
+                    futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, writeInvTxn -> {
                         ListenableFuture<Void> configFuture
-                            = txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> {
+                            = txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, writeConfigTxn -> {
                                 ListenableFuture<Void> operFuture
-                                    = txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeOperTxn -> {
+                                    = txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeOperTxn -> {
                                         final String interfaceName = intrf.getName();
                                         LOG.info("Detected interface add event for interface {}", interfaceName);
-                                        final VpnInterface vpnIf =
-                                                VpnUtil.getConfiguredVpnInterface(dataBroker, interfaceName);
+                                        final VpnInterface vpnIf = vpnUtil.getConfiguredVpnInterface(interfaceName);
                                         if (vpnIf != null) {
                                             for (VpnInstanceNames vpnInterfaceVpnInstance :
                                                     vpnIf.getVpnInstanceNames()) {
                                                 String vpnName = vpnInterfaceVpnInstance.getVpnName();
-                                                String primaryRd = VpnUtil.getPrimaryRd(dataBroker, vpnName);
+                                                String primaryRd = vpnUtil.getPrimaryRd(vpnName);
                                                 if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
                                                     LOG.info("VPN Interface add event - intfName {} onto vpnName {} "
                                                             + "running oper-driven, VpnInstance not ready, holding"
                                                             + " on", vpnIf.getName(), vpnName);
-                                                } else if (VpnUtil.isVpnPendingDelete(dataBroker, primaryRd)) {
+                                                } else if (vpnUtil.isVpnPendingDelete(primaryRd)) {
                                                     LOG.error("add: Ignoring addition of vpnInterface {}, as"
                                                             + " vpnInstance {} with primaryRd {} is already marked for"
                                                             + " deletion", interfaceName, vpnName, primaryRd);
@@ -165,38 +193,39 @@ public class InterfaceStateChangeListener
                 final BigInteger inputDpId = dpId;
                 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
                     List<ListenableFuture<Void>> futures = new ArrayList<>(3);
-                    ListenableFuture<Void> configFuture = txRunner.callWithNewWriteOnlyTransactionAndSubmit(
-                        writeConfigTxn -> futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
-                            writeOperTxn -> futures.add(
-                                    txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
+                    ListenableFuture<Void> configFuture =
+                        txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                            writeConfigTxn -> futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
+                                writeOperTxn -> futures.add(
+                                    txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, writeInvTxn -> {
                                         VpnInterface cfgVpnInterface =
-                                                VpnUtil.getConfiguredVpnInterface(dataBroker, ifName);
+                                            vpnUtil.getConfiguredVpnInterface(ifName);
                                         if (cfgVpnInterface == null) {
                                             LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
                                             return;
                                         }
                                         for (VpnInstanceNames vpnInterfaceVpnInstance :
-                                                cfgVpnInterface.getVpnInstanceNames()) {
+                                            cfgVpnInterface.getVpnInstanceNames()) {
                                             String vpnName = vpnInterfaceVpnInstance.getVpnName();
                                             Optional<VpnInterfaceOpDataEntry> optVpnInterface =
-                                                    VpnUtil.getVpnInterfaceOpDataEntry(dataBroker, ifName, vpnName);
+                                                vpnUtil.getVpnInterfaceOpDataEntry(ifName, vpnName);
                                             if (!optVpnInterface.isPresent()) {
                                                 LOG.debug("Interface {} vpn {} is not a vpninterface, or deletion"
-                                                        + " triggered by northbound agent. ignoring.", ifName, vpnName);
+                                                    + " triggered by northbound agent. ignoring.", ifName, vpnName);
                                                 continue;
                                             }
                                             final VpnInterfaceOpDataEntry vpnInterface = optVpnInterface.get();
                                             String gwMac = intrf.getPhysAddress() != null ? intrf.getPhysAddress()
-                                                    .getValue() : vpnInterface.getGatewayMacAddress();
+                                                .getValue() : vpnInterface.getGatewayMacAddress();
                                             BigInteger dpnId = inputDpId;
                                             if (dpnId == null || dpnId.equals(BigInteger.ZERO)) {
                                                 dpnId = vpnInterface.getDpnId();
                                             }
                                             final int ifIndex = intrf.getIfIndex();
                                             LOG.info("VPN Interface remove event - intfName {} onto vpnName {}"
-                                                    + " running oper-driver", vpnInterface.getName(), vpnName);
+                                                + " running oper-driver", vpnInterface.getName(), vpnName);
                                             vpnInterfaceManager.processVpnInterfaceDown(dpnId, ifName, ifIndex, gwMac,
-                                                    vpnInterface, false, writeConfigTxn, writeOperTxn, writeInvTxn);
+                                                vpnInterface, false, writeConfigTxn, writeOperTxn, writeInvTxn);
                                         }
                                     })))));
                     futures.add(configFuture);
@@ -217,15 +246,6 @@ public class InterfaceStateChangeListener
                     Interface original, Interface update) {
         final String ifName = update.getName();
         try {
-            OperStatus originalOperStatus = original.getOperStatus();
-            OperStatus updateOperStatus = update.getOperStatus();
-            if (originalOperStatus.equals(Interface.OperStatus.Unknown)
-                  || updateOperStatus.equals(Interface.OperStatus.Unknown)) {
-                LOG.debug("Interface {} state change is from/to null/UNKNOWN. Ignoring the update event.",
-                        ifName);
-                return;
-            }
-
             if (update.getIfIndex() == null) {
                 return;
             }
@@ -234,66 +254,78 @@ public class InterfaceStateChangeListener
                         update.getName());
                 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
                     List<ListenableFuture<Void>> futures = new ArrayList<>(3);
-                    futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeOperTxn -> futures.add(
-                            txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeConfigTxn -> futures.add(
-                                txRunner.callWithNewWriteOnlyTransactionAndSubmit(writeInvTxn -> {
-                                    final VpnInterface vpnIf =
-                                            VpnUtil.getConfiguredVpnInterface(dataBroker, ifName);
-                                    if (vpnIf != null) {
-                                        final int ifIndex = update.getIfIndex();
-                                        BigInteger dpnId = BigInteger.ZERO;
-                                        try {
-                                            dpnId = InterfaceUtils.getDpIdFromInterface(update);
-                                        } catch (Exception e) {
-                                            LOG.error("remove: Unable to retrieve dpnId for interface {}", ifName, e);
-                                            return;
-                                        }
-                                        if (update.getOperStatus().equals(OperStatus.Up)) {
-                                            for (VpnInstanceNames vpnInterfaceVpnInstance :
+                    futures.add(
+                        txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeOperTxn -> futures.add(
+                            txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                                writeConfigTxn -> futures.add(
+                                    txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, writeInvTxn -> {
+                                        final VpnInterface vpnIf =
+                                            vpnUtil.getConfiguredVpnInterface(ifName);
+                                        if (vpnIf != null) {
+                                            final int ifIndex = update.getIfIndex();
+                                            BigInteger dpnId;
+                                            try {
+                                                dpnId = InterfaceUtils.getDpIdFromInterface(update);
+                                            } catch (Exception e) {
+                                                LOG.error("remove: Unable to retrieve dpnId for interface {}", ifName,
+                                                    e);
+                                                return;
+                                            }
+                                            IntfTransitionState state = getTransitionState(original.getOperStatus(),
+                                                update.getOperStatus());
+                                            if (state.equals(IntfTransitionState.STATE_IGNORE)) {
+                                                LOG.info("InterfaceStateChangeListener: Interface {} state original {}"
+                                                        + "updated {} not handled", ifName, original.getOperStatus(),
+                                                    update.getOperStatus());
+                                                return;
+                                            }
+                                            if (state.equals(IntfTransitionState.STATE_UP)) {
+                                                for (VpnInstanceNames vpnInterfaceVpnInstance :
                                                     vpnIf.getVpnInstanceNames()) {
-                                                String vpnName = vpnInterfaceVpnInstance.getVpnName();
-                                                String primaryRd = VpnUtil.getPrimaryRd(dataBroker, vpnName);
-                                                if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
-                                                    LOG.error(
+                                                    String vpnName = vpnInterfaceVpnInstance.getVpnName();
+                                                    String primaryRd = vpnUtil.getPrimaryRd(vpnName);
+                                                    if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
+                                                        LOG.error(
                                                             "VPN Interface update event - intfName {} onto vpnName {} "
-                                                                    + "running oper-driven UP, VpnInstance not ready,"
-                                                            + " holding on", vpnIf.getName(), vpnName);
-                                                } else if (VpnUtil.isVpnPendingDelete(dataBroker, primaryRd)) {
-                                                    LOG.error("update: Ignoring UP event for vpnInterface {}, as "
+                                                                + "running oper-driven UP, VpnInstance not ready,"
+                                                                + " holding on", vpnIf.getName(), vpnName);
+                                                    } else if (vpnUtil.isVpnPendingDelete(primaryRd)) {
+                                                        LOG.error("update: Ignoring UP event for vpnInterface {}, as "
                                                             + "vpnInstance {} with primaryRd {} is already marked for"
                                                             + " deletion", vpnIf.getName(), vpnName, primaryRd);
-                                                } else {
-                                                    vpnInterfaceManager.processVpnInterfaceUp(dpnId, vpnIf, primaryRd,
+                                                    } else {
+                                                        vpnInterfaceManager.processVpnInterfaceUp(dpnId, vpnIf,
+                                                            primaryRd,
                                                             ifIndex, true, writeConfigTxn, writeOperTxn, writeInvTxn,
                                                             update, vpnName);
+                                                    }
                                                 }
-                                            }
-                                        } else if (update.getOperStatus().equals(OperStatus.Down)) {
-                                            for (VpnInstanceNames vpnInterfaceVpnInstance :
+                                            } else if (state.equals(IntfTransitionState.STATE_DOWN)) {
+                                                for (VpnInstanceNames vpnInterfaceVpnInstance :
                                                     vpnIf.getVpnInstanceNames()) {
-                                                String vpnName = vpnInterfaceVpnInstance.getVpnName();
-                                                LOG.info("VPN Interface update event - intfName {} onto vpnName {}"
-                                                       + " running oper-driven DOWN", vpnIf.getName(), vpnName);
-                                                Optional<VpnInterfaceOpDataEntry> optVpnInterface =
-                                                     VpnUtil.getVpnInterfaceOpDataEntry(dataBroker,
-                                                                         vpnIf.getName(), vpnName);
-                                                if (optVpnInterface.isPresent()) {
-                                                    VpnInterfaceOpDataEntry vpnOpInterface = optVpnInterface.get();
-                                                    vpnInterfaceManager.processVpnInterfaceDown(dpnId, vpnIf.getName(),
+                                                    String vpnName = vpnInterfaceVpnInstance.getVpnName();
+                                                    LOG.info("VPN Interface update event - intfName {} onto vpnName {}"
+                                                        + " running oper-driven DOWN", vpnIf.getName(), vpnName);
+                                                    Optional<VpnInterfaceOpDataEntry> optVpnInterface =
+                                                        vpnUtil.getVpnInterfaceOpDataEntry(vpnIf.getName(), vpnName);
+                                                    if (optVpnInterface.isPresent()) {
+                                                        VpnInterfaceOpDataEntry vpnOpInterface = optVpnInterface.get();
+                                                        vpnInterfaceManager.processVpnInterfaceDown(dpnId,
+                                                            vpnIf.getName(),
                                                             ifIndex, update.getPhysAddress().getValue(), vpnOpInterface,
                                                             true, writeConfigTxn, writeOperTxn, writeInvTxn);
-                                                } else {
-                                                    LOG.error(
+                                                    } else {
+                                                        LOG.error(
                                                             "InterfaceStateChangeListener Update DOWN - vpnInterface {}"
-                                                            + " not available, ignoring event", vpnIf.getName());
-                                                    continue;
+                                                                + " not available, ignoring event", vpnIf.getName());
+                                                        continue;
+                                                    }
                                                 }
                                             }
+                                        } else {
+                                            LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
                                         }
-                                    } else {
-                                        LOG.debug("Interface {} is not a vpninterface, ignoring.", ifName);
-                                    }
-                                }))))));
+                                    }))))));
                     return futures;
                 });
             }
@@ -330,8 +362,17 @@ public class InterfaceStateChangeListener
                         interfaceName, txnDestination, throwable);
             } else {
                 LOG.error("InterfaceStateChangeListener: VrfEntries for {} removal failed", interfaceName, throwable);
-                VpnUtil.unsetScheduledToRemoveForVpnInterface(txRunner, interfaceName);
+                vpnUtil.unsetScheduledToRemoveForVpnInterface(interfaceName);
             }
         }
     }
+
+    private IntfTransitionState getTransitionState(Interface.OperStatus original , Interface.OperStatus updated) {
+        IntfTransitionState transitionState = stateTable.get(original, updated);
+
+        if (transitionState == null) {
+            return IntfTransitionState.STATE_IGNORE;
+        }
+        return transitionState;
+    }
 }