NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / LocalUcastMacListener.java
index 00a77653c92e7227ef81fa26fbb5b0f9d7be54b1..817dfb4f6e3c37946b3de8df5c63da8581fdaf02 100644 (file)
@@ -17,27 +17,31 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.function.Predicate;
-import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
-import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
+import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
+import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayServiceRecoveryHandler;
 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
+import org.opendaylight.serviceutils.srm.RecoverableListener;
+import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
@@ -51,7 +55,7 @@ import org.slf4j.LoggerFactory;
 
 @Singleton
 public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, String>
-        implements ClusteredDataTreeChangeListener<Node> {
+        implements ClusteredDataTreeChangeListener<Node>, RecoverableListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(LocalUcastMacListener.class);
     public static final String NODE_CHECK = "physical";
@@ -72,7 +76,9 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
                                  final ElanL2GatewayUtils elanL2GatewayUtils,
                                  final JobCoordinator jobCoordinator,
                                  final ElanInstanceCache elanInstanceCache,
-                                 final HwvtepNodeHACache hwvtepNodeHACache) {
+                                 final HwvtepNodeHACache hwvtepNodeHACache,
+                                 final L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler,
+                                 final ServiceRecoveryRegistry serviceRecoveryRegistry) {
         super(dataBroker, false);
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.elanL2GatewayUtils = elanL2GatewayUtils;
@@ -80,6 +86,7 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
         this.jobCoordinator = jobCoordinator;
         this.elanInstanceCache = elanInstanceCache;
         this.hwvtepNodeHACache = hwvtepNodeHACache;
+        serviceRecoveryRegistry.addRecoverableListener(l2GatewayServiceRecoveryHandler.buildServiceRegistryKey(), this);
     }
 
     @Override
@@ -87,6 +94,23 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
     public void init() throws Exception {
         ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.dataBroker);
         super.init();
+        registerListener();
+    }
+
+    @Override
+    @SuppressWarnings("all")
+    public void registerListener() {
+        try {
+            LOG.info("Registering LocalUcastMacListener");
+            registerListener(LogicalDatastoreType.OPERATIONAL, getParentWildCardPath());
+        } catch (Exception e) {
+            LOG.error("Local Ucast Mac register listener error");
+        }
+    }
+
+    public void deregisterListener() {
+        LOG.info("Deregistering LocalUcastMacListener");
+        super.close();
     }
 
     @Override
@@ -132,7 +156,7 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
                 }
 
                 elanL2GwDevice.removeUcastLocalMac(macRemoved);
-                ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
+                ElanInstance elanInstance = elanInstanceCache.get(elanName).orElse(null);
                 elanL2GatewayUtils.unInstallL2GwUcastMacFromL2gwDevices(elanName, elanL2GwDevice,
                         Collections.singletonList(macAddress));
                 elanL2GatewayUtils.unInstallL2GwUcastMacFromElanDpns(elanInstance, elanL2GwDevice,
@@ -151,7 +175,7 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
 
         LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
 
-        ElanInstance elan = elanInstanceCache.get(elanName).orNull();
+        ElanInstance elan = elanInstanceCache.get(elanName).orElse(null);
         if (elan == null) {
             LOG.warn("Could not find ELAN for mac {} being added", macAddress);
             return;
@@ -203,14 +227,14 @@ public class LocalUcastMacListener extends ChildListener<Node, LocalUcastMacs, S
             return;
         }
         // TODO skitt we're only using read transactions here
-        ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
+        LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
             tx -> haOpClusteredListener.onGlobalNodeAdd(nodeIid, modification.getRootNode().getDataAfter(), tx)), LOG,
             "Error processing added parent");
         if (!isHAChild(nodeIid)) {
-            ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
+            LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
                 LOG.trace("On parent add {}", nodeIid);
                 Node operNode = modification.getRootNode().getDataAfter();
-                Set<LocalUcastMacs> configMacs = getMacs(tx.read(nodeIid).get().orNull());
+                Set<LocalUcastMacs> configMacs = getMacs(tx.read(nodeIid).get().orElse(null));
                 Set<LocalUcastMacs> operMacs = getMacs(operNode);
                 Set<LocalUcastMacs> staleMacs = Sets.difference(configMacs, operMacs);
                 staleMacs.forEach(staleMac -> removed(getMacIid(nodeIid, staleMac), staleMac));