Remove GENIUS UTIL references in Elanmanager Module
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepTerminationPointListener.java
index 12f8d1762671314d96840147d750771ce323ccac..71f58a194986642725f0016923e39bd775410a07 100644 (file)
@@ -7,33 +7,36 @@
  */
 package org.opendaylight.netvirt.elan.l2gw.listeners;
 
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
+import static java.util.Collections.emptyList;
+import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
+
 import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.MoreExecutors;
-import com.google.common.util.concurrent.SettableFuture;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import javax.annotation.Nonnull;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ExecutionException;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
+import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+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.elan.l2gw.utils.ElanL2GatewayUtils;
 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
-import org.opendaylight.netvirt.elan.l2gw.utils.SettableFutureCallback;
 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.DevicesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
@@ -61,16 +64,25 @@ public class HwvtepTerminationPointListener
     private static final Logger LOG = LoggerFactory.getLogger(HwvtepTerminationPointListener.class);
 
     private final DataBroker broker;
+    private final ManagedNewTransactionRunner txRunner;
     private final ElanL2GatewayUtils elanL2GatewayUtils;
     private final ElanClusterUtils elanClusterUtils;
     private final L2GatewayCache l2GatewayCache;
 
     @Inject
     public HwvtepTerminationPointListener(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
-            ElanClusterUtils elanClusterUtils, L2GatewayCache l2GatewayCache) {
-        super(TerminationPoint.class, HwvtepTerminationPointListener.class);
+            ElanClusterUtils elanClusterUtils, L2GatewayCache l2GatewayCache,
+            HwvtepNodeHACache hwvtepNodeHACache) {
+        //super(TerminationPoint.class, HwvtepTerminationPointListener.class, hwvtepNodeHACache);
 
+        super(broker, DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
+            InstanceIdentifier.create(NetworkTopology.class)
+                .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class)
+                .child(TerminationPoint.class)),
+            Executors.newListeningSingleThreadExecutor("HwvtepTerminationPointListener", LOG),
+            hwvtepNodeHACache);
         this.broker = broker;
+        this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
         this.elanL2GatewayUtils = elanL2GatewayUtils;
         this.elanClusterUtils = elanClusterUtils;
         this.l2GatewayCache = l2GatewayCache;
@@ -83,13 +95,11 @@ public class HwvtepTerminationPointListener
     protected void removed(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint del) {
         LOG.trace("physical locator removed {}", identifier);
         final HwvtepPhysicalPortAugmentation portAugmentation =
-                del.getAugmentation(HwvtepPhysicalPortAugmentation.class);
+                del.augmentation(HwvtepPhysicalPortAugmentation.class);
         if (portAugmentation != null) {
-            final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
             elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
                 "Handling Physical port delete",
-                () -> handlePortDeleted(identifier, portAugmentation, del, nodeId));
-            return;
+                () -> handlePortDeleted(identifier));
         }
     }
 
@@ -102,34 +112,26 @@ public class HwvtepTerminationPointListener
     @Override
     protected void added(InstanceIdentifier<TerminationPoint> identifier, final TerminationPoint add) {
         final HwvtepPhysicalPortAugmentation portAugmentation =
-                add.getAugmentation(HwvtepPhysicalPortAugmentation.class);
+                add.augmentation(HwvtepPhysicalPortAugmentation.class);
         if (portAugmentation != null) {
             final NodeId nodeId = identifier.firstIdentifierOf(Node.class).firstKeyOf(Node.class).getNodeId();
             elanClusterUtils.runOnlyInOwnerNode(HwvtepSouthboundConstants.ELAN_ENTITY_NAME,
-                () -> handlePortAdded(portAugmentation, add, nodeId));
+                () -> handlePortAdded(add, nodeId));
             return;
         }
 
         LOG.trace("physical locator available {}", identifier);
     }
 
-    @Override
-    protected InstanceIdentifier<TerminationPoint> getWildCardPath() {
-        return InstanceIdentifier.create(NetworkTopology.class)
-                .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class)
-                .child(TerminationPoint.class);
-    }
-
-    @Override
-    protected HwvtepTerminationPointListener getDataTreeChangeListener() {
-        return this;
-    }
-
-    private List<ListenableFuture<Void>> handlePortAdded(HwvtepPhysicalPortAugmentation portAugmentation,
-                                                         TerminationPoint portAdded, NodeId psNodeId) {
-        Node psNode = HwvtepUtils.getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, psNodeId);
+    private List<ListenableFuture<?>> handlePortAdded(TerminationPoint portAdded, NodeId psNodeId) {
+        Node psNode = null;
+        try {
+            psNode = HwvtepUtils.getHwVtepNode(broker, LogicalDatastoreType.OPERATIONAL, psNodeId);
+        } catch (ExecutionException | InterruptedException e) {
+            LOG.error("Exception while retriving HwVtepNode {}", psNodeId.getValue(), e);
+        }
         if (psNode != null) {
-            String psName = psNode.getAugmentation(PhysicalSwitchAugmentation.class).getHwvtepNodeName().getValue();
+            String psName = psNode.augmentation(PhysicalSwitchAugmentation.class).getHwvtepNodeName().getValue();
             L2GatewayDevice l2GwDevice = l2GatewayCache.get(psName);
             if (l2GwDevice != null) {
                 if (isL2GatewayConfigured(l2GwDevice)) {
@@ -148,36 +150,13 @@ public class HwvtepTerminationPointListener
         } else {
             LOG.error("{} entry not in config datastore", psNodeId);
         }
-        return Collections.emptyList();
+        return emptyList();
     }
 
-    private List<ListenableFuture<Void>> handlePortDeleted(InstanceIdentifier<TerminationPoint> identifier,
-                                                           HwvtepPhysicalPortAugmentation portAugmentation,
-                                                           TerminationPoint portDeleted,
-                                                           NodeId psNodeId) throws ReadFailedException {
+    private List<ListenableFuture<?>> handlePortDeleted(InstanceIdentifier<TerminationPoint> identifier) {
         InstanceIdentifier<Node> psNodeIid = identifier.firstIdentifierOf(Node.class);
-        final ReadWriteTransaction tx = broker.newReadWriteTransaction();
-        final SettableFuture<Void> settableFuture = SettableFuture.create();
-        List<ListenableFuture<Void>> futures = Collections.singletonList(settableFuture);
-        Futures.addCallback(tx.read(LogicalDatastoreType.CONFIGURATION, psNodeIid),
-            new FutureCallback<Optional<Node>>() {
-                @Override
-                public void onSuccess(@Nonnull Optional<Node> nodeOptional) {
-                    if (nodeOptional.isPresent()) {
-                        //case of port deleted
-                        tx.delete(LogicalDatastoreType.CONFIGURATION, identifier);
-                        Futures.addCallback(tx.submit(), new SettableFutureCallback<>(settableFuture),
-                                MoreExecutors.directExecutor());
-                    }
-                }
-
-                @Override
-                public void onFailure(Throwable failure) {
-                    LOG.error("Read of {} failed", psNodeIid, failure);
-                    tx.cancel();
-                }
-            }, MoreExecutors.directExecutor());
-        return futures;
+        return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
+            tx -> tx.read(psNodeIid).get().ifPresent(node -> tx.delete(identifier))));
     }
 
     private List<VlanBindings> getVlanBindings(List<L2gatewayConnection> l2GwConns, NodeId hwvtepNodeId, String psName,
@@ -190,12 +169,12 @@ public class HwvtepTerminationPointListener
             } else {
                 String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
                         l2GwConn.getNetworkId().getValue());
-                List<Devices> l2Devices = l2Gateway.getDevices();
-                for (Devices l2Device : l2Devices) {
+                Map<DevicesKey, Devices> l2Devices = l2Gateway.nonnullDevices();
+                for (Devices l2Device : l2Devices.values()) {
                     String l2DeviceName = l2Device.getDeviceName();
                     if (l2DeviceName != null && l2DeviceName.equals(psName)) {
-                        for (Interfaces deviceInterface : l2Device.getInterfaces()) {
-                            if (deviceInterface.getInterfaceName().equals(newPortId)) {
+                        for (Interfaces deviceInterface : l2Device.nonnullInterfaces().values()) {
+                            if (Objects.equals(deviceInterface.getInterfaceName(), newPortId)) {
                                 if (deviceInterface.getSegmentationIds() != null
                                         && !deviceInterface.getSegmentationIds().isEmpty()) {
                                     for (Integer vlanId : deviceInterface.getSegmentationIds()) {
@@ -219,7 +198,7 @@ public class HwvtepTerminationPointListener
         return vlanBindings;
     }
 
-    private boolean isL2GatewayConfigured(L2GatewayDevice l2GwDevice) {
+    private static boolean isL2GatewayConfigured(L2GatewayDevice l2GwDevice) {
         return l2GwDevice.getHwvtepNodeId() != null
                 && !l2GwDevice.getL2GatewayIds().isEmpty() && l2GwDevice.getTunnelIp() != null;
     }