Remove GENIUS UTIL References in VpnManager Module
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / InterfaceStateChangeListener.java
index 1c017946d6cac4609608f068efb2aae54beab5e2..1d5a6500b63eb7a5a21514a7845d9d0f4017efe2 100644 (file)
@@ -7,10 +7,9 @@
  */
 package org.opendaylight.netvirt.vpnmanager;
 
-import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
-import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
+import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
+import static org.opendaylight.mdsal.binding.util.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;
@@ -22,18 +21,20 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
-import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
-import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
-import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
+import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
@@ -42,6 +43,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.lea
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.Adjacencies;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.Adjacency;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.adjacency.list.AdjacencyKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.VpnInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.interfaces.vpn._interface.VpnInstanceNames;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -50,8 +52,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class InterfaceStateChangeListener
-    extends AsyncDataTreeChangeListenerBase<Interface, InterfaceStateChangeListener> {
+public class InterfaceStateChangeListener extends AbstractAsyncDataTreeChangeListener<Interface> {
 
     private static final Logger LOG = LoggerFactory.getLogger(InterfaceStateChangeListener.class);
     private static final short DJC_MAX_RETRIES = 3;
@@ -87,7 +88,9 @@ public class InterfaceStateChangeListener
     @Inject
     public InterfaceStateChangeListener(final DataBroker dataBroker, final VpnInterfaceManager vpnInterfaceManager,
             final VpnUtil vpnUtil, final JobCoordinator jobCoordinator, final IFibManager fibManager) {
-        super(Interface.class, InterfaceStateChangeListener.class);
+        super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(InterfacesState.class)
+                .child(Interface.class),
+                Executors.newListeningSingleThreadExecutor("InterfaceStateChangeListener", LOG));
         this.dataBroker = dataBroker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.vpnInterfaceManager = vpnInterfaceManager;
@@ -97,49 +100,43 @@ public class InterfaceStateChangeListener
         initialize();
     }
 
-    @PostConstruct
     public void start() {
         LOG.info("{} start", getClass().getSimpleName());
-        registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
-    }
-
-
-    @Override
-    protected InstanceIdentifier<Interface> getWildCardPath() {
-        return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
     }
 
     @Override
-    protected InterfaceStateChangeListener getDataTreeChangeListener() {
-        return InterfaceStateChangeListener.this;
+    @PreDestroy
+    public void close() {
+        super.close();
+        Executors.shutdownAndAwaitTermination(getExecutorService());
     }
 
 
     @Override
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
+    public void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
         try {
             if (L2vlan.class.equals(intrf.getType())) {
                 LOG.info("VPN Interface add event - intfName {} from InterfaceStateChangeListener",
                                 intrf.getName());
                 jobCoordinator.enqueueJob("VPNINTERFACE-" + intrf.getName(), () -> {
-                    List<ListenableFuture<Void>> futures = new ArrayList<>(3);
+                    List<ListenableFuture<?>> futures = new ArrayList<>(3);
                     futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, writeInvTxn -> {
                         //map of prefix and vpn name used, as entry in prefix-to-interface datastore
                         // is prerequisite for refresh Fib to avoid race condition leading to missing remote next hop
                         // in bucket actions on bgp-vpn delete
                         Map<String, Set<String>> mapOfRdAndPrefixesForRefreshFib = new HashMap<>();
-                        ListenableFuture<Void> configFuture
+                        ListenableFuture<?> configFuture
                             = txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, writeConfigTxn -> {
-                                ListenableFuture<Void> operFuture
+                                ListenableFuture<?> operFuture
                                     = txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeOperTxn -> {
                                         final String interfaceName = intrf.getName();
                                         LOG.info("Detected interface add event for interface {}", interfaceName);
                                         final VpnInterface vpnIf = vpnUtil.getConfiguredVpnInterface(interfaceName);
                                         if (vpnIf != null) {
                                             for (VpnInstanceNames vpnInterfaceVpnInstance :
-                                                    vpnIf.nonnullVpnInstanceNames()) {
+                                                    vpnIf.nonnullVpnInstanceNames().values()) {
                                                 String vpnName = vpnInterfaceVpnInstance.getVpnName();
                                                 String primaryRd = vpnUtil.getPrimaryRd(vpnName);
                                                 if (!vpnInterfaceManager.isVpnInstanceReady(vpnName)) {
@@ -199,7 +196,7 @@ public class InterfaceStateChangeListener
     @Override
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
+    public void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
         final String ifName = intrf.getName();
         Uint64 dpId = Uint64.ZERO;
         try {
@@ -214,8 +211,8 @@ public class InterfaceStateChangeListener
                 }
                 final Uint64 inputDpId = dpId;
                 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
-                    List<ListenableFuture<Void>> futures = new ArrayList<>(3);
-                    ListenableFuture<Void> configFuture =
+                    List<ListenableFuture<?>> futures = new ArrayList<>(3);
+                    ListenableFuture<?> configFuture =
                         txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
                             writeConfigTxn -> futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
                                 writeOperTxn -> futures.add(
@@ -227,7 +224,7 @@ public class InterfaceStateChangeListener
                                             return;
                                         }
                                         for (VpnInstanceNames vpnInterfaceVpnInstance :
-                                                cfgVpnInterface.nonnullVpnInstanceNames()) {
+                                                cfgVpnInterface.nonnullVpnInstanceNames().values()) {
                                             String vpnName = vpnInterfaceVpnInstance.getVpnName();
                                             Optional<VpnInterfaceOpDataEntry> optVpnInterface =
                                                 vpnUtil.getVpnInterfaceOpDataEntry(ifName, vpnName);
@@ -265,7 +262,7 @@ public class InterfaceStateChangeListener
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
-    protected void update(InstanceIdentifier<Interface> identifier,
+    public void update(InstanceIdentifier<Interface> identifier,
                     Interface original, Interface update) {
         final String ifName = update.getName();
         try {
@@ -276,13 +273,13 @@ public class InterfaceStateChangeListener
                 LOG.info("VPN Interface update event - intfName {} from InterfaceStateChangeListener",
                         update.getName());
                 jobCoordinator.enqueueJob("VPNINTERFACE-" + ifName, () -> {
-                    List<ListenableFuture<Void>> futures = new ArrayList<>(3);
+                    List<ListenableFuture<?>> futures = new ArrayList<>(3);
                     futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, writeOperTxn -> {
                         //map of prefix and vpn name used, as entry in prefix-to-interface datastore
                         // is prerequisite for refresh Fib to avoid race condition leading to missing remote
                         // next hop in bucket actions on bgp-vpn delete
                         Map<String, Set<String>> mapOfRdAndPrefixesForRefreshFib = new HashMap<>();
-                        ListenableFuture<Void> configTxFuture =
+                        ListenableFuture<?> configTxFuture =
                                 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, writeConfigTxn ->
                                     futures.add(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
                                         writeInvTxn -> {
@@ -312,7 +309,7 @@ public class InterfaceStateChangeListener
                                                 if (state.equals(IntfTransitionState.STATE_UP)
                                                         && vpnIf.getVpnInstanceNames() != null) {
                                                     for (VpnInstanceNames vpnInterfaceVpnInstance :
-                                                            vpnIf.getVpnInstanceNames()) {
+                                                            vpnIf.getVpnInstanceNames().values()) {
                                                         String vpnName = vpnInterfaceVpnInstance.getVpnName();
                                                         String primaryRd = vpnUtil.getPrimaryRd(vpnName);
                                                         Set<String> prefixes = new HashSet<>();
@@ -336,7 +333,7 @@ public class InterfaceStateChangeListener
                                                 } else if (state.equals(IntfTransitionState.STATE_DOWN)
                                                         && vpnIf.getVpnInstanceNames() != null) {
                                                     for (VpnInstanceNames vpnInterfaceVpnInstance :
-                                                            vpnIf.getVpnInstanceNames()) {
+                                                            vpnIf.getVpnInstanceNames().values()) {
                                                         String vpnName = vpnInterfaceVpnInstance.getVpnName();
                                                         LOG.info("VPN Interface update event - intfName {} "
                                                             + " onto vpnName {} running oper-driven DOWN",
@@ -380,9 +377,9 @@ public class InterfaceStateChangeListener
         String interfaceName = cfgVpnInterface.getName();
         Adjacencies adjacencies = cfgVpnInterface.augmentation(Adjacencies.class);
         if (adjacencies != null) {
-            List<Adjacency> adjacencyList = adjacencies.getAdjacency();
-            if (!adjacencyList.isEmpty()) {
-                for (Adjacency adj : adjacencyList) {
+            Map<AdjacencyKey, Adjacency> adjacencyMap = adjacencies.nonnullAdjacency();
+            if (!adjacencyMap.isEmpty()) {
+                for (Adjacency adj : adjacencyMap.values()) {
                     if (adj.getAdjacencyType() != Adjacency.AdjacencyType.PrimaryAdjacency) {
                         String ipAddress = adj.getIpAddress();
                         String prefix = ipAddress.split("/")[0];
@@ -399,7 +396,7 @@ public class InterfaceStateChangeListener
         }
     }
 
-    private class PostVpnInterfaceThreadWorker implements FutureCallback<Void> {
+    private class PostVpnInterfaceThreadWorker implements FutureCallback<Object> {
         private final String interfaceName;
         private final boolean add;
         private final String txnDestination;
@@ -411,7 +408,7 @@ public class InterfaceStateChangeListener
         }
 
         @Override
-        public void onSuccess(Void voidObj) {
+        public void onSuccess(Object voidObj) {
             if (add) {
                 LOG.debug("InterfaceStateChangeListener: VrfEntries for {} stored into destination {} successfully",
                         interfaceName, txnDestination);
@@ -441,7 +438,7 @@ public class InterfaceStateChangeListener
         return transitionState;
     }
 
-    private class VpnInterfaceCallBackHandler implements FutureCallback<Void> {
+    private class VpnInterfaceCallBackHandler implements FutureCallback<Object> {
         private final Map<String, Set<String>> mapOfRdAndPrefixesForRefreshFib;
 
         VpnInterfaceCallBackHandler(Map<String, Set<String>> mapOfRdAndPrefixesForRefreshFib) {
@@ -449,7 +446,7 @@ public class InterfaceStateChangeListener
         }
 
         @Override
-        public void onSuccess(Void voidObj) {
+        public void onSuccess(Object voidObj) {
             mapOfRdAndPrefixesForRefreshFib.forEach((primaryRd, prefixes) -> {
                 prefixes.forEach(prefix -> {
                     fibManager.refreshVrfEntry(primaryRd, prefix);