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=6ff32f3089242bcef2f60886aa850a0f4d5dd90a;hb=0267562f3c2be39373f289008a367776dd325ff8;hp=7abbaad6530e57b6829b21d12babf16423e9eeee;hpb=e4da8e6ea130a5103508c55503fca6fcfa3fc168;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 7abbaad653..6ff32f3089 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,14 +7,14 @@ */ 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; -import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdFromNodeIdentifier; +import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.getNodeIdValueFromNodeIdentifier; 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; @@ -26,7 +26,6 @@ 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.DataTreeIdentifier; import org.opendaylight.mdsal.binding.api.ReadTransaction; import org.opendaylight.mdsal.binding.api.WriteTransaction; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; @@ -57,14 +56,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.Add import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef; import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; 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; @@ -79,43 +76,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; - - public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db) { - super(manager, db); - } - - @Override - @SuppressWarnings("IllegalCatch") - public void registerListener() { - final DataTreeIdentifier treeId = DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, - getWildCardPath()); - try { - listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, FlowForwarder.this); - } catch (final Exception e) { - LOG.warn("FRM Flow DataTreeChange listener registration fail!"); - LOG.debug("FRM Flow DataTreeChange listener registration fail ..", e); - throw new IllegalStateException("FlowForwarder startup fail! System needs restart.", e); - } - } - - - @Override - public void deregisterListener() { - close(); - } - - @Override - public void close() { - if (listenerRegistration != null) { - listenerRegistration.close(); - listenerRegistration = null; - } + public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db, + final ListenerRegistrationHelper registrationHelper) { + super(manager, db, registrationHelper); } @Override @@ -128,8 +94,8 @@ public class FlowForwarder extends AbstractListeningCommiter { if (bundleId != null) { provider.getBundleFlowListener().remove(identifier, removeDataObj, nodeIdent, bundleId); } else { - final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent); - nodeConfigurator.enqueueJob(nodeId.getValue(), () -> { + final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent); + nodeConfigurator.enqueueJob(nodeId, () -> { final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj); builder.setFlowRef(new FlowRef(identifier)); builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))); @@ -184,8 +150,8 @@ public class FlowForwarder extends AbstractListeningCommiter { if (bundleId != null) { provider.getBundleFlowListener().update(identifier, original, update, nodeIdent, bundleId); } else { - final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent); - nodeConfigurator.enqueueJob(nodeId.getValue(), () -> { + final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent); + nodeConfigurator.enqueueJob(nodeId, () -> { final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder(); builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))); builder.setFlowRef(new FlowRef(identifier)); @@ -201,10 +167,10 @@ public class FlowForwarder extends AbstractListeningCommiter { Uint32 groupId = isFlowDependentOnGroup(update); if (groupId != null) { LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present", - getFlowId(new FlowRef(identifier)), groupId); + getFlowId(identifier), groupId); if (isGroupExistsOnDevice(nodeIdent, groupId, provider)) { LOG.trace("The dependent group {} is already programmed. Updating the flow {}", groupId, - getFlowId(new FlowRef(identifier))); + getFlowId(identifier)); return provider.getSalFlowService().updateFlow(builder.build()); } else { LOG.trace("The dependent group {} isn't programmed yet. Pushing the group", groupId); @@ -219,7 +185,7 @@ public class FlowForwarder extends AbstractListeningCommiter { } LOG.trace("The flow {} is not dependent on any group. Updating the flow", - getFlowId(new FlowRef(identifier))); + getFlowId(identifier)); return provider.getSalFlowService().updateFlow(builder.build()); }); } @@ -236,8 +202,8 @@ public class FlowForwarder extends AbstractListeningCommiter { if (bundleId != null) { return provider.getBundleFlowListener().add(identifier, addDataObj, nodeIdent, bundleId); } else { - final NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent); - nodeConfigurator.enqueueJob(nodeId.getValue(), () -> { + final String nodeId = getNodeIdValueFromNodeIdentifier(nodeIdent); + nodeConfigurator.enqueueJob(nodeId, () -> { final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj); builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))); @@ -273,8 +239,8 @@ public class FlowForwarder extends AbstractListeningCommiter { } @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); @@ -287,8 +253,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()); @@ -297,39 +263,39 @@ public class FlowForwarder extends AbstractListeningCommiter { return true; } - private 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, false); + staleFlow); FluentFuture submitFuture = writeTransaction.commit(); handleStaleFlowResultFuture(submitFuture); } - private void handleStaleFlowResultFuture(FluentFuture submitFuture) { + private static void handleStaleFlowResultFuture(final FluentFuture submitFuture) { submitFuture.addCallback(new FutureCallback() { @Override - public void onSuccess(Object result) { + public void onSuccess(final Object 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 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()))); @@ -348,34 +314,34 @@ 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; } private final class AddFlowCallBack implements FutureCallback> { private final AddFlowInput addFlowInput; - private final NodeId nodeId; + private final String nodeId; private final Uint32 groupId; private final SettableFuture> resultFuture; - private AddFlowCallBack(final AddFlowInput addFlowInput, final NodeId 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; @@ -383,35 +349,35 @@ 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()); LOG.debug("Flow add with id {} finished without error for node {}", - getFlowId(addFlowInput.getFlowRef()), nodeId.getValue()); + getFlowId(addFlowInput.getFlowRef()), nodeId); } else { LOG.error("Flow add with id {} failed for node {} with error {}", getFlowId(addFlowInput.getFlowRef()), - nodeId.getValue(), rpcResult.getErrors()); + nodeId, rpcResult.getErrors()); resultFuture.set(RpcResultBuilder.failed() .withRpcErrors(rpcResult.getErrors()).build()); } } @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); @@ -420,12 +386,12 @@ public class FlowForwarder extends AbstractListeningCommiter { private final class UpdateFlowCallBack implements FutureCallback> { private final UpdateFlowInput updateFlowInput; - private final NodeId nodeId; + private final String nodeId; private final Uint32 groupId; private final SettableFuture> resultFuture; - private UpdateFlowCallBack(final UpdateFlowInput updateFlowInput, final NodeId nodeId, - SettableFuture> resultFuture, Uint32 groupId) { + private UpdateFlowCallBack(final UpdateFlowInput updateFlowInput, final String nodeId, + final SettableFuture> resultFuture, final Uint32 groupId) { this.updateFlowInput = updateFlowInput; this.nodeId = nodeId; this.groupId = groupId; @@ -433,19 +399,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()); @@ -461,10 +427,10 @@ 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); } } -} +} \ No newline at end of file