Avoid duplicate processing code with events 35/74335/6
authorStephen Kitt <skitt@redhat.com>
Mon, 23 Jul 2018 12:43:50 +0000 (14:43 +0200)
committerSam Hague <shague@redhat.com>
Sat, 28 Jul 2018 03:31:27 +0000 (03:31 +0000)
When we use event callbacks to react to the appearance of
currently-missing data, we might as well use that as the nominal flow
too: if the data is present, the event callback's underlying listener
will fire immediately, and the callback will be handled straight
away. This avoids duplicating code whenever an event callback is
used.

If performance testing reveals that fast paths are needed, we can
handle that centrally in the event callback mechanism, instead of
repeating it at every call site.

Change-Id: I49791d810946988c704a9cfe24297c977c1c8c84
Signed-off-by: Stephen Kitt <skitt@redhat.com>
natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/AbstractSnatService.java

index fe0da69a665043697ac2406199cf04c1023bfa9b..3615ccb4a693f548ce6321aab91656090da7df22 100644 (file)
@@ -76,7 +76,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.G
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.GetTunnelInterfaceNameOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.Adjacencies;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AdjacenciesBuilder;
@@ -360,27 +359,16 @@ public abstract class AbstractSnatService implements SnatServiceListener {
         LOG.debug("installing the PSNAT to NAPTSwitch GroupEntity:{} with GroupId: {}", groupEntity, groupId);
         mdsalManager.addGroup(confTx, groupEntity);
 
-        //Add the flow to send the packet to the group only after group is available in Config datastore
-        InstanceIdentifier<Group> groupIid = NatUtil.getGroupInstanceId(dpnId, groupId);
-        try {
-            if (confTx.read(groupIid).get().isPresent()) {
-                LOG.info("group {} is present in the config hence adding the flow", groupId);
-                addSnatMissFlowForGroup(confTx, dpnId, routerId, groupId);
-                return;
-            }
-            eventCallbacks.onAddOrUpdate(LogicalDatastoreType.CONFIGURATION,
-                    NatUtil.getGroupInstanceId(dpnId, groupId), (unused, newGroupId) -> {
-                    LOG.info("group {} is created in the config", groupId);
-                    ListenableFutures.addErrorLogging(
-                            txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
-                                innerConfTx -> addSnatMissFlowForGroup(innerConfTx, dpnId, routerId, groupId)),
-                            LOG, "Error adding flow for the group {}",groupId);
-                    return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
-                }, Duration.ofSeconds(5), iid -> LOG.error("groupId {} not found in config datastore",
-                        groupId));
-        } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Error programming SNAT miss entryfor dpn {}", dpnId);
-        }
+        // Add the flow to send the packet to the group only after group is available in Config datastore
+        eventCallbacks.onAddOrUpdate(LogicalDatastoreType.CONFIGURATION,
+                NatUtil.getGroupInstanceId(dpnId, groupId), (unused, newGroupId) -> {
+                LOG.info("group {} is created in the config", groupId);
+                ListenableFutures.addErrorLogging(
+                        txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
+                            innerConfTx -> addSnatMissFlowForGroup(innerConfTx, dpnId, routerId, groupId)),
+                        LOG, "Error adding flow for the group {}",groupId);
+                return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
+            }, Duration.ofSeconds(5), iid -> LOG.error("groupId {} not found in config datastore", groupId));
     }
 
     private void addSnatMissFlowForGroup(TypedReadWriteTransaction<Configuration> confTx,