Ignore unnecessary update() processing in NVPN MD-SAL listeners
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronTrunkChangeListener.java
index 6e337932d6820e0fc4571cc95879f8591c4fae09..f41d8ad3ece0618ab5e6ec0880256eb97ec30e09 100644 (file)
@@ -7,23 +7,27 @@
  */
 package org.opendaylight.netvirt.neutronvpn;
 
-import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
 
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import javax.annotation.PostConstruct;
+import java.util.Map;
+import java.util.Objects;
+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.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
+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.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.interfaces.Interface;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
@@ -40,6 +44,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev15060
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.NetworkTypeVlan;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.trunks.rev170118.trunk.attributes.SubPorts;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.trunks.rev170118.trunk.attributes.SubPortsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.trunks.rev170118.trunks.attributes.Trunks;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.trunks.rev170118.trunks.attributes.trunks.Trunk;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -47,7 +52,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class NeutronTrunkChangeListener extends AsyncDataTreeChangeListenerBase<Trunk, NeutronTrunkChangeListener> {
+public class NeutronTrunkChangeListener extends AbstractAsyncDataTreeChangeListener<Trunk> {
     private static final Logger LOG = LoggerFactory.getLogger(NeutronTrunkChangeListener.class);
 
     private final DataBroker dataBroker;
@@ -58,56 +63,56 @@ public class NeutronTrunkChangeListener extends AsyncDataTreeChangeListenerBase<
     @Inject
     public NeutronTrunkChangeListener(final DataBroker dataBroker, final IInterfaceManager ifMgr,
             final JobCoordinator jobCoordinator) {
+        super(dataBroker, LogicalDatastoreType.CONFIGURATION,
+                InstanceIdentifier.create(Neutron.class).child(Trunks.class).child(Trunk.class),
+                Executors.newSingleThreadExecutor("NeutronTrunkChangeListener", LOG));
         this.dataBroker = dataBroker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.ifMgr = ifMgr;
         this.jobCoordinator = jobCoordinator;
     }
 
-    @Override
-    @PostConstruct
     public void init() {
         LOG.info("{} init", getClass().getSimpleName());
-        registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
     }
 
     @Override
-    protected InstanceIdentifier<Trunk> getWildCardPath() {
-        return InstanceIdentifier.create(Neutron.class).child(Trunks.class).child(Trunk.class);
+    @PreDestroy
+    public void close() {
+        super.close();
+        Executors.shutdownAndAwaitTermination(getExecutorService());
     }
 
     @Override
-    protected NeutronTrunkChangeListener getDataTreeChangeListener() {
-        return NeutronTrunkChangeListener.this;
-    }
-
-    @Override
-    protected void add(InstanceIdentifier<Trunk> identifier, Trunk input) {
+    public void add(InstanceIdentifier<Trunk> identifier, Trunk input) {
         Preconditions.checkNotNull(input.getPortId());
         LOG.trace("Adding Trunk : key: {}, value={}", identifier, input);
-        List<SubPorts> subPorts = input.getSubPorts();
-        if (subPorts != null) {
-            subPorts.forEach(subPort -> createSubPortInterface(input, subPort));
+        Map<SubPortsKey, SubPorts> keySubPortsMap = input.getSubPorts();
+        if (keySubPortsMap != null) {
+            keySubPortsMap.values().forEach(subPort -> createSubPortInterface(input, subPort));
         }
     }
 
     @Override
-    protected void remove(InstanceIdentifier<Trunk> identifier, Trunk input) {
+    public void remove(InstanceIdentifier<Trunk> identifier, Trunk input) {
         Preconditions.checkNotNull(input.getPortId());
         LOG.trace("Removing Trunk : key: {}, value={}", identifier, input);
-        List<SubPorts> subPorts = input.getSubPorts();
-        if (subPorts != null) {
-            subPorts.forEach(this::deleteSubPortInterface);
+        Map<SubPortsKey, SubPorts> keySubPortsMap = input.getSubPorts();
+        if (keySubPortsMap != null) {
+            keySubPortsMap.values().forEach(this::deleteSubPortInterface);
         }
     }
 
     @Override
-    protected void update(InstanceIdentifier<Trunk> identifier, Trunk original, Trunk update) {
-        List<SubPorts> updatedSubPorts = update.getSubPorts();
+    public void update(InstanceIdentifier<Trunk> identifier, Trunk original, Trunk update) {
+        if (Objects.equals(original, update)) {
+            return;
+        }
+        List<SubPorts> updatedSubPorts = new ArrayList<SubPorts>(update.nonnullSubPorts().values());
         if (updatedSubPorts == null) {
             updatedSubPorts = Collections.emptyList();
         }
-        List<SubPorts> originalSubPorts = original.getSubPorts();
+        List<SubPorts> originalSubPorts = new ArrayList<SubPorts>(original.nonnullSubPorts().values());
         if (originalSubPorts == null) {
             originalSubPorts = Collections.emptyList();
         }
@@ -139,7 +144,7 @@ public class NeutronTrunkChangeListener extends AsyncDataTreeChangeListenerBase<
             Uuid subPortUuid = subPort.getPortId();
             portIdToSubportBuilder.withKey(new PortIdToSubportKey(subPortUuid)).setPortId(subPortUuid)
                     .setTrunkPortId(trunk.getPortId()).setVlanId(subPort.getSegmentationId());
-            List<ListenableFuture<Void>> futures = new ArrayList<>();
+            List<ListenableFuture<?>> futures = new ArrayList<>();
             futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
                 CONFIGURATION, tx -> {
                     tx.merge(NeutronvpnUtils.buildPortIdSubportMappingIdentifier(subPortUuid),
@@ -171,11 +176,11 @@ public class NeutronTrunkChangeListener extends AsyncDataTreeChangeListenerBase<
              * Interface is already created for parent NeutronPort. We're updating parent refs
              * and VLAN Information
              */
-            futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
-                CONFIGURATION, tx -> {
-                    tx.merge(interfaceIdentifier, newIface);
-                    LOG.trace("Creating trunk member interface {}", newIface);
-                }));
+            ListenableFuture<?> future = txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                txn -> txn.merge(interfaceIdentifier, newIface));
+            LoggingFutures.addErrorLogging(future, LOG,
+                    "createSubPortInterface: Failed for portName {}, parentName {}", portName, parentName);
+            futures.add(future);
             return futures;
         });
     }
@@ -186,9 +191,10 @@ public class NeutronTrunkChangeListener extends AsyncDataTreeChangeListenerBase<
                         NeutronvpnUtils.buildVlanInterfaceIdentifier(subPort.getPortId().getValue());
         jobCoordinator.enqueueJob("PORT- " + portName, () -> {
             Interface iface = ifMgr.getInterfaceInfoFromConfigDataStore(portName);
+            List<ListenableFuture<?>> futures = new ArrayList<>();
             if (iface == null) {
                 LOG.warn("Interface not present for SubPort {}", subPort);
-                return Collections.emptyList();
+                return futures;
             }
             /*
              * We'll reset interface back to way it was? Can IFM handle parentRef delete?
@@ -209,7 +215,6 @@ public class NeutronTrunkChangeListener extends AsyncDataTreeChangeListenerBase<
              * and this being subport delete path, don't expect any significant changes to
              * corresponding Neutron Port. Deletion of NeutronPort should follow soon enough.
              */
-            List<ListenableFuture<Void>> futures = new ArrayList<>();
             futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
                 CONFIGURATION, tx -> {
                     tx.put(interfaceIdentifier, newIface);