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=28fe152d2fe04048653f82c7f130c30168bbc6f7;hpb=5ca86fd4d46550e0667d9063986000e3eabe6472;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 28fe152d2f..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 @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. +/* + * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -7,20 +7,28 @@ */ package org.opendaylight.openflowplugin.applications.frm.impl; -import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.CheckedFuture; +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.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.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; -import java.util.concurrent.Callable; -import java.util.concurrent.Future; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; -import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +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.openflowplugin.common.wait.SimpleTaskRetryLooper; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId; @@ -30,108 +38,90 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.ta import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder; +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.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.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +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; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * FlowForwarder - * It implements {@link org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener} - * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for methods: - * add, update and remove {@link Flow} processing for - * {@link org.opendaylight.controller.md.sal.binding.api.DataTreeModification}. + * FlowForwarder It implements + * {@link org.opendaylight.mdsal.binding.api.DataTreeChangeListener} + * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for + * methods: add, update and remove {@link Flow} processing for + * {@link org.opendaylight.mdsal.binding.api.DataTreeModification}. */ public class FlowForwarder extends AbstractListeningCommiter { - private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class); - private final DataBroker dataBroker; - private ListenerRegistration listenerRegistration; + private static final String GROUP_EXISTS_IN_DEVICE_ERROR = "GROUPEXISTS"; - public FlowForwarder (final ForwardingRulesManager manager, final DataBroker db) { - super(manager, Flow.class); - dataBroker = Preconditions.checkNotNull(db, "DataBroker can not be null!"); - registrationListener(db); - } - - private void registrationListener(final DataBroker db) { - final DataTreeIdentifier treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath()); - try { - SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK, - ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES); - listenerRegistration = looper.loopUntilNoException(new Callable>() { - @Override - public ListenerRegistration call() throws Exception { - return db.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); - } + public FlowForwarder(final ForwardingRulesManager manager, final DataBroker db, + final ListenerRegistrationHelper registrationHelper) { + super(manager, db, registrationHelper); } @Override - public void close() { - if (listenerRegistration != null) { - try { - listenerRegistration.close(); - } catch (final Exception e) { - LOG.warn("Error by stop FRM FlowChangeListener: {}", e.getMessage()); - LOG.debug("Error by stop FRM FlowChangeListener..", e); - } - listenerRegistration = null; - } - } - - @Override - public void remove(final InstanceIdentifier identifier, - final Flow removeDataObj, - final InstanceIdentifier nodeIdent) { + public void remove(final InstanceIdentifier identifier, final Flow removeDataObj, + final InstanceIdentifier nodeIdent) { - final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class); + final TableKey tableKey = identifier.firstKeyOf(Table.class); if (tableIdValidationPrecondition(tableKey, removeDataObj)) { - final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj); - builder.setFlowRef(new FlowRef(identifier)); - builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))); - builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey))); - - // This method is called only when a given flow object has been - // removed from datastore. So FRM always needs to set strict flag - // into remove-flow input so that only a flow entry associated with - // a given flow object is removed. - builder.setTransactionUri(new Uri(provider.getNewTransactionId())). - setStrict(Boolean.TRUE); - provider.getSalFlowService().removeFlow(builder.build()); + BundleId bundleId = getActiveBundle(nodeIdent, provider); + if (bundleId != null) { + provider.getBundleFlowListener().remove(identifier, removeDataObj, nodeIdent, bundleId); + } else { + 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))); + builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey))); + + // This method is called only when a given flow object has been + // removed from datastore. So FRM always needs to set strict flag + // into remove-flow input so that only a flow entry associated with + // a given flow object is removed. + builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE); + final ListenableFuture> resultFuture = + provider.getSalFlowService().removeFlow(builder.build()); + LoggingFutures.addErrorLogging(resultFuture, LOG, "removeFlow"); + return resultFuture; + }); + } } } - - - - //TODO: Pull this into ForwardingRulesCommiter and override it here + // TODO: Pull this into ForwardingRulesCommiter and override it here @Override - public Future> removeWithResult(final InstanceIdentifier identifier, - final Flow removeDataObj, - final InstanceIdentifier nodeIdent) { + public ListenableFuture> removeWithResult(final InstanceIdentifier identifier, + final Flow removeDataObj, final InstanceIdentifier nodeIdent) { - Future> resultFuture = SettableFuture.create(); - final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class); + ListenableFuture> resultFuture = SettableFuture.create(); + final TableKey tableKey = identifier.firstKeyOf(Table.class); if (tableIdValidationPrecondition(tableKey, removeDataObj)) { final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj); builder.setFlowRef(new FlowRef(identifier)); @@ -142,123 +132,295 @@ public class FlowForwarder extends AbstractListeningCommiter { // removed from datastore. So FRM always needs to set strict flag // into remove-flow input so that only a flow entry associated with // a given flow object is removed. - builder.setTransactionUri(new Uri(provider.getNewTransactionId())). - setStrict(Boolean.TRUE); + builder.setTransactionUri(new Uri(provider.getNewTransactionId())).setStrict(Boolean.TRUE); resultFuture = provider.getSalFlowService().removeFlow(builder.build()); } return resultFuture; } - - @Override - public void update(final InstanceIdentifier identifier, - final Flow original, final Flow update, - final InstanceIdentifier nodeIdent) { + public void update(final InstanceIdentifier identifier, final Flow original, final Flow update, + final InstanceIdentifier nodeIdent) { - final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class); + final TableKey tableKey = identifier.firstKeyOf(Table.class); if (tableIdValidationPrecondition(tableKey, update)) { - final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder(); - - builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class))); - builder.setFlowRef(new FlowRef(identifier)); - builder.setTransactionUri(new Uri(provider.getNewTransactionId())); - - // This method is called only when a given flow object in datastore - // has been updated. So FRM always needs to set strict flag into - // update-flow input so that only a flow entry associated with - // a given flow object is updated. - builder.setUpdatedFlow((new UpdatedFlowBuilder(update)).setStrict(Boolean.TRUE).build()); - builder.setOriginalFlow((new OriginalFlowBuilder(original)).setStrict(Boolean.TRUE).build()); - - provider.getSalFlowService().updateFlow(builder.build()); + BundleId bundleId = getActiveBundle(nodeIdent, provider); + if (bundleId != null) { + provider.getBundleFlowListener().update(identifier, original, update, nodeIdent, bundleId); + } else { + 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)); + builder.setTransactionUri(new Uri(provider.getNewTransactionId())); + + // This method is called only when a given flow object in datastore + // has been updated. So FRM always needs to set strict flag into + // update-flow input so that only a flow entry associated with + // a given flow object is updated. + builder.setUpdatedFlow(new UpdatedFlowBuilder(update).setStrict(Boolean.TRUE).build()); + builder.setOriginalFlow(new OriginalFlowBuilder(original).setStrict(Boolean.TRUE).build()); + + Uint32 groupId = isFlowDependentOnGroup(update); + if (groupId != null) { + LOG.trace("The flow {} is dependent on group {}. Checking if the group is already present", + getFlowId(identifier), groupId); + if (isGroupExistsOnDevice(nodeIdent, groupId, provider)) { + LOG.trace("The dependent group {} is already programmed. Updating the flow {}", groupId, + getFlowId(identifier)); + return provider.getSalFlowService().updateFlow(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 UpdateFlowCallBack(builder.build(), nodeId, resultFuture, groupId), + MoreExecutors.directExecutor()); + return resultFuture; + } + } + + LOG.trace("The flow {} is not dependent on any group. Updating the flow", + getFlowId(identifier)); + return provider.getSalFlowService().updateFlow(builder.build()); + }); + } } } @Override - public Future> add( - final InstanceIdentifier identifier, final Flow addDataObj, - final InstanceIdentifier nodeIdent) { + 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); + } - Future> future; - final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class); - if (tableIdValidationPrecondition(tableKey, addDataObj)) { - final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj); + 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()); + } - 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())); - future = provider.getSalFlowService().addFlow(builder.build()); - } else { - future = Futures.>immediateFuture(null); - } + 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 future; + 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) { - LOG.debug("Creating Stale-Mark entry for the switch {} for flow {} ", nodeIdent.toString(), del.toString()); - + 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); - } - - @Override protected InstanceIdentifier getWildCardPath() { - return InstanceIdentifier.create(Nodes.class).child(Node.class) - .augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class); + return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class) + .child(Table.class).child(Flow.class); } - 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!"); - if (! tableKey.getId().equals(flow.getTableId())) { - LOG.warn("TableID in URI tableId={} and in palyload tableId={} is not same.", - flow.getTableId(), tableKey.getId()); + private static boolean tableIdValidationPrecondition(final TableKey tableKey, final Flow flow) { + 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()); return false; } return true; } - private StaleFlow makeStaleFlow(InstanceIdentifier identifier, Flow del, InstanceIdentifier nodeIdent){ - StaleFlowBuilder staleFlowBuilder = new StaleFlowBuilder(del); + 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); - - CheckedFuture submitFuture = writeTransaction.submit(); - handleStaleFlowResultFuture(submitFuture); - } + writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleFlowInstanceIdentifier(staleFlow, nodeIdent), + staleFlow); - private void handleStaleFlowResultFuture(CheckedFuture submitFuture) { - Futures.addCallback(submitFuture, new FutureCallback() { + writeTransaction.commit().addCallback(new FutureCallback() { @Override - public void onSuccess(Void result) { + public void onSuccess(final CommitInfo result) { LOG.debug("Stale Flow creation success"); } @Override - public void onFailure(Throwable t) { - LOG.error("Stale Flow creation failed {}", t); + public void onFailure(final Throwable throwable) { + LOG.error("Stale Flow creation failed", throwable); } - }); + }, MoreExecutors.directExecutor()); + } + + private static InstanceIdentifier getStaleFlowInstanceIdentifier( + 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()))); + } + private ListenableFuture> pushDependentGroup( + final InstanceIdentifier nodeIdent, final Uint32 groupId) { + + //TODO This read to the DS might have a performance impact. + //if the dependent group is not installed than we should just cache the parent group, + //till we receive the dependent group DTCN and then push it. + + InstanceIdentifier groupIdent = buildGroupInstanceIdentifier(nodeIdent, groupId); + ListenableFuture> resultFuture; + LOG.info("Reading the group from config inventory: {}", groupId); + try (ReadTransaction readTransaction = provider.getReadTransaction()) { + Optional group = readTransaction.read(LogicalDatastoreType.CONFIGURATION, groupIdent).get(); + if (group.isPresent()) { + 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 = provider.getSalGroupService().addGroup(addGroupInput); + } else { + 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 = RpcResultBuilder.failed() + .withError(ErrorType.APPLICATION, + "Error while reading group " + groupId + " from inventory").buildFuture(); + } + return resultFuture; } - private InstanceIdentifier getStaleFlowInstanceIdentifier(StaleFlow staleFlow, 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()))); + private final class AddFlowCallBack implements FutureCallback> { + private final AddFlowInput addFlowInput; + private final String nodeId; + private final Uint32 groupId; + private final 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; + this.resultFuture = resultFuture; + } + + @Override + 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(final RpcResult result) { + resultFuture.set(result); + } + + @Override + 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); + } else { + LOG.error("Flow add with id {} failed for node {} with error {}", getFlowId(addFlowInput.getFlowRef()), + nodeId, rpcResult.getErrors()); + resultFuture.set(RpcResultBuilder.failed() + .withRpcErrors(rpcResult.getErrors()).build()); + } + } + + @Override + 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); + } } -} + private final class UpdateFlowCallBack implements FutureCallback> { + private final UpdateFlowInput updateFlowInput; + private final String nodeId; + private final Uint32 groupId; + private final SettableFuture> resultFuture; + + private UpdateFlowCallBack(final UpdateFlowInput updateFlowInput, final String nodeId, + final SettableFuture> resultFuture, final Uint32 groupId) { + this.updateFlowInput = updateFlowInput; + this.nodeId = nodeId; + this.groupId = groupId; + this.resultFuture = resultFuture; + } + + @Override + 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(final RpcResult result) { + resultFuture.set(result); + } + + @Override + public void onFailure(final Throwable failure) { + resultFuture.setException(failure); + } + }, MoreExecutors.directExecutor()); + + LOG.debug("Flow update with id {} finished without error for node {}", + getFlowId(updateFlowInput.getFlowRef()), nodeId); + } else { + LOG.error("Flow update with id {} failed for node {} with error {}", + getFlowId(updateFlowInput.getFlowRef()), nodeId, rpcResult.getErrors()); + resultFuture.set(RpcResultBuilder.failed() + .withRpcErrors(rpcResult.getErrors()).build()); + } + } + + @Override + 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