X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=applications%2Fforwardingrules-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fapplications%2Ffrm%2Fimpl%2FFlowForwarder.java;h=663b03ebffbf00efde7aeb40ffd273f4cf025214;hb=136910d52e526c9205cc6d3b989f60b81551d87b;hp=99935e4f1213bf28db965f7f18290a6a7c386ae3;hpb=15bd551ab43d2aa93c8aed0baed7526926ef61d2;p=openflowplugin.git diff --git a/applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/FlowForwarder.java b/applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/FlowForwarder.java index 99935e4f12..663b03ebff 100644 --- a/applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/FlowForwarder.java +++ b/applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/FlowForwarder.java @@ -7,6 +7,7 @@ */ package org.opendaylight.openflowplugin.applications.frm.impl; +import static java.util.Objects.requireNonNull; import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.buildGroupInstanceIdentifier; import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getActiveBundle; import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getFlowId; @@ -14,8 +15,6 @@ import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getN import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isFlowDependentOnGroup; import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.isGroupExistsOnDevice; -import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.FluentFuture; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; @@ -23,11 +22,11 @@ import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.util.Optional; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; import org.opendaylight.infrautils.utils.concurrent.LoggingFutures; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.ReadTransaction; import org.opendaylight.mdsal.binding.api.WriteTransaction; +import org.opendaylight.mdsal.common.api.CommitInfo; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri; @@ -60,9 +59,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId; -import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.common.RpcError; +import org.opendaylight.yangtools.yang.common.ErrorType; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.opendaylight.yangtools.yang.common.Uint32; @@ -77,32 +75,12 @@ import org.slf4j.LoggerFactory; * {@link org.opendaylight.mdsal.binding.api.DataTreeModification}. */ public class FlowForwarder extends AbstractListeningCommiter { - private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class); - private static final String GROUP_EXISTS_IN_DEVICE_ERROR = "GROUPEXISTS"; - private ListenerRegistration listenerRegistration; - - private final BundleFlowForwarder bundleFlowForwarder; - public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db, final ListenerRegistrationHelper registrationHelper) { super(manager, db, registrationHelper); - bundleFlowForwarder = new BundleFlowForwarder(manager); - } - - @Override - public void deregisterListener() { - close(); - } - - @Override - public void close() { - if (listenerRegistration != null) { - listenerRegistration.close(); - listenerRegistration = null; - } } @Override @@ -214,54 +192,51 @@ public class FlowForwarder extends AbstractListeningCommiter { } @Override - public Future> add(final InstanceIdentifier identifier, final Flow addDataObj, - final InstanceIdentifier nodeIdent) { - - final TableKey tableKey = identifier.firstKeyOf(Table.class); - if (tableIdValidationPrecondition(tableKey, addDataObj)) { - BundleId bundleId = getActiveBundle(nodeIdent, provider); - if (bundleId != null) { - return provider.getBundleFlowListener().add(identifier, addDataObj, nodeIdent, bundleId); - } else { - final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent); - nodeConfigurator.enqueueJob(nodeId, () -> { - final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj); - - builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))); - builder.setFlowRef(new FlowRef(identifier)); - builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey))); - builder.setTransactionUri(new Uri(provider.getNewTransactionId())); - Uint32 groupId = isFlowDependentOnGroup(addDataObj); - if (groupId != null) { - LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present", - getFlowId(new FlowRef(identifier)), groupId); - if (isGroupExistsOnDevice(nodeIdent, groupId, provider)) { - LOG.trace("The dependent group {} is already programmed. Adding the flow {}", groupId, - getFlowId(new FlowRef(identifier))); - return provider.getSalFlowService().addFlow(builder.build()); - } else { - LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId); - ListenableFuture> groupFuture = pushDependentGroup(nodeIdent, - groupId); - SettableFuture> resultFuture = SettableFuture.create(); - Futures.addCallback(groupFuture, new AddFlowCallBack(builder.build(), nodeId, groupId, - resultFuture), MoreExecutors.directExecutor()); - return resultFuture; - } - } + public ListenableFuture> add(final InstanceIdentifier identifier, + final Flow addDataObj, final InstanceIdentifier nodeIdent) { + final var tableKey = identifier.firstKeyOf(Table.class); + if (!tableIdValidationPrecondition(tableKey, addDataObj)) { + return Futures.immediateFuture(null); + } + final var bundleId = getActiveBundle(nodeIdent, provider); + if (bundleId != null) { + return provider.getBundleFlowListener().add(identifier, addDataObj, nodeIdent, bundleId); + } - LOG.trace("The flow {} is not dependent on any group. Adding the flow", - getFlowId(new FlowRef(identifier))); + final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent); + return nodeConfigurator.enqueueJob(nodeId, () -> { + final var builder = new AddFlowInputBuilder(addDataObj) + .setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))) + .setFlowRef(new FlowRef(identifier)) + .setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey))) + .setTransactionUri(new Uri(provider.getNewTransactionId())); + final var groupId = isFlowDependentOnGroup(addDataObj); + if (groupId != null) { + LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present", + getFlowId(new FlowRef(identifier)), groupId); + if (isGroupExistsOnDevice(nodeIdent, groupId, provider)) { + LOG.trace("The dependent group {} is already programmed. Adding the flow {}", groupId, + getFlowId(new FlowRef(identifier))); return provider.getSalFlowService().addFlow(builder.build()); - }); + } + + LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId); + final var groupFuture = pushDependentGroup(nodeIdent, groupId); + final var resultFuture = SettableFuture.>create(); + Futures.addCallback(groupFuture, new AddFlowCallBack(builder.build(), nodeId, groupId, + resultFuture), MoreExecutors.directExecutor()); + return resultFuture; } - } - return Futures.immediateFuture(null); + + LOG.trace("The flow {} is not dependent on any group. Adding the flow", + getFlowId(new FlowRef(identifier))); + return provider.getSalFlowService().addFlow(builder.build()); + }); } @Override - public void createStaleMarkEntity(InstanceIdentifier identifier, Flow del, - InstanceIdentifier nodeIdent) { + public void createStaleMarkEntity(final InstanceIdentifier identifier, final Flow del, + final InstanceIdentifier nodeIdent) { LOG.debug("Creating Stale-Mark entry for the switch {} for flow {} ", nodeIdent, del); StaleFlow staleFlow = makeStaleFlow(identifier, del, nodeIdent); persistStaleFlow(staleFlow, nodeIdent); @@ -274,8 +249,8 @@ public class FlowForwarder extends AbstractListeningCommiter { } private static boolean tableIdValidationPrecondition(final TableKey tableKey, final Flow flow) { - Preconditions.checkNotNull(tableKey, "TableKey can not be null or empty!"); - Preconditions.checkNotNull(flow, "Flow can not be null or empty!"); + requireNonNull(tableKey, "TableKey can not be null or empty!"); + requireNonNull(flow, "Flow can not be null or empty!"); if (!tableKey.getId().equals(flow.getTableId())) { LOG.warn("TableID in URI tableId={} and in palyload tableId={} is not same.", flow.getTableId(), tableKey.getId()); @@ -284,39 +259,33 @@ public class FlowForwarder extends AbstractListeningCommiter { return true; } - private static StaleFlow makeStaleFlow(InstanceIdentifier identifier, Flow del, - InstanceIdentifier nodeIdent) { + private static StaleFlow makeStaleFlow(final InstanceIdentifier identifier, final Flow del, + final InstanceIdentifier nodeIdent) { StaleFlowBuilder staleFlowBuilder = new StaleFlowBuilder(del); return staleFlowBuilder.setId(del.getId()).build(); } - private void persistStaleFlow(StaleFlow staleFlow, InstanceIdentifier nodeIdent) { + private void persistStaleFlow(final StaleFlow staleFlow, final InstanceIdentifier nodeIdent) { WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction(); writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleFlowInstanceIdentifier(staleFlow, nodeIdent), staleFlow); - FluentFuture submitFuture = writeTransaction.commit(); - handleStaleFlowResultFuture(submitFuture); - } - - private static void handleStaleFlowResultFuture(FluentFuture submitFuture) { - submitFuture.addCallback(new FutureCallback() { + writeTransaction.commit().addCallback(new FutureCallback() { @Override - public void onSuccess(Object result) { + public void onSuccess(final CommitInfo result) { LOG.debug("Stale Flow creation success"); } @Override - public void onFailure(Throwable throwable) { + public void onFailure(final Throwable throwable) { LOG.error("Stale Flow creation failed", throwable); } }, MoreExecutors.directExecutor()); - } private static InstanceIdentifier getStaleFlowInstanceIdentifier( - StaleFlow staleFlow, InstanceIdentifier nodeIdent) { + final StaleFlow staleFlow, final InstanceIdentifier nodeIdent) { return nodeIdent.child(Table.class, new TableKey(staleFlow.getTableId())).child( org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow.class, new StaleFlowKey(new FlowId(staleFlow.getId()))); @@ -335,22 +304,22 @@ public class FlowForwarder extends AbstractListeningCommiter { try (ReadTransaction readTransaction = provider.getReadTransaction()) { Optional group = readTransaction.read(LogicalDatastoreType.CONFIGURATION, groupIdent).get(); if (group.isPresent()) { - final AddGroupInputBuilder builder = new AddGroupInputBuilder(group.get()); + final AddGroupInputBuilder builder = new AddGroupInputBuilder(group.orElseThrow()); builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))); builder.setGroupRef(new GroupRef(nodeIdent)); builder.setTransactionUri(new Uri(provider.getNewTransactionId())); AddGroupInput addGroupInput = builder.build(); - resultFuture = this.provider.getSalGroupService().addGroup(addGroupInput); + resultFuture = provider.getSalGroupService().addGroup(addGroupInput); } else { - resultFuture = Futures.immediateFuture(RpcResultBuilder.failed() - .withError(RpcError.ErrorType.APPLICATION, - "Group " + groupId + " not present in the config inventory").build()); + resultFuture = RpcResultBuilder.failed() + .withError(ErrorType.APPLICATION, + "Group " + groupId + " not present in the config inventory").buildFuture(); } } catch (InterruptedException | ExecutionException e) { LOG.error("Error while reading group from config datastore for the group ID {}", groupId, e); - resultFuture = Futures.immediateFuture(RpcResultBuilder.failed() - .withError(RpcError.ErrorType.APPLICATION, - "Error while reading group " + groupId + " from inventory").build()); + resultFuture = RpcResultBuilder.failed() + .withError(ErrorType.APPLICATION, + "Error while reading group " + groupId + " from inventory").buildFuture(); } return resultFuture; } @@ -361,8 +330,8 @@ public class FlowForwarder extends AbstractListeningCommiter { private final Uint32 groupId; private final SettableFuture> resultFuture; - private AddFlowCallBack(final AddFlowInput addFlowInput, final String nodeId, Uint32 groupId, - SettableFuture> resultFuture) { + private AddFlowCallBack(final AddFlowInput addFlowInput, final String nodeId, final Uint32 groupId, + final SettableFuture> resultFuture) { this.addFlowInput = addFlowInput; this.nodeId = nodeId; this.groupId = groupId; @@ -370,19 +339,19 @@ public class FlowForwarder extends AbstractListeningCommiter { } @Override - public void onSuccess(RpcResult rpcResult) { + public void onSuccess(final RpcResult rpcResult) { if (rpcResult.isSuccessful() || rpcResult.getErrors().size() == 1 && rpcResult.getErrors().iterator().next().getMessage().contains(GROUP_EXISTS_IN_DEVICE_ERROR)) { provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId); Futures.addCallback(provider.getSalFlowService().addFlow(addFlowInput), new FutureCallback>() { @Override - public void onSuccess(RpcResult result) { + public void onSuccess(final RpcResult result) { resultFuture.set(result); } @Override - public void onFailure(Throwable failure) { + public void onFailure(final Throwable failure) { resultFuture.setException(failure); } }, MoreExecutors.directExecutor()); @@ -398,7 +367,7 @@ public class FlowForwarder extends AbstractListeningCommiter { } @Override - public void onFailure(Throwable throwable) { + public void onFailure(final Throwable throwable) { LOG.error("Service call for adding flow with id {} failed for node {}", getFlowId(addFlowInput.getFlowRef()), nodeId, throwable); resultFuture.setException(throwable); @@ -412,7 +381,7 @@ public class FlowForwarder extends AbstractListeningCommiter { private final SettableFuture> resultFuture; private UpdateFlowCallBack(final UpdateFlowInput updateFlowInput, final String nodeId, - SettableFuture> resultFuture, Uint32 groupId) { + final SettableFuture> resultFuture, final Uint32 groupId) { this.updateFlowInput = updateFlowInput; this.nodeId = nodeId; this.groupId = groupId; @@ -420,19 +389,19 @@ public class FlowForwarder extends AbstractListeningCommiter { } @Override - public void onSuccess(RpcResult rpcResult) { + public void onSuccess(final RpcResult rpcResult) { if (rpcResult.isSuccessful() || rpcResult.getErrors().size() == 1 && rpcResult.getErrors().iterator().next().getMessage().contains(GROUP_EXISTS_IN_DEVICE_ERROR)) { provider.getDevicesGroupRegistry().storeGroup(nodeId, groupId); Futures.addCallback(provider.getSalFlowService().updateFlow(updateFlowInput), new FutureCallback>() { @Override - public void onSuccess(RpcResult result) { + public void onSuccess(final RpcResult result) { resultFuture.set(result); } @Override - public void onFailure(Throwable failure) { + public void onFailure(final Throwable failure) { resultFuture.setException(failure); } }, MoreExecutors.directExecutor()); @@ -448,7 +417,7 @@ public class FlowForwarder extends AbstractListeningCommiter { } @Override - public void onFailure(Throwable throwable) { + public void onFailure(final Throwable throwable) { LOG.error("Service call for updating flow with id {} failed for node {}", getFlowId(updateFlowInput.getFlowRef()), nodeId, throwable); resultFuture.setException(throwable);