/**
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
- * <p/>
+ *
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.openflowplugin.impl.services;
-import com.google.common.base.Function;
+import com.google.common.annotations.VisibleForTesting;
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.SettableFuture;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
-import java.util.concurrent.ExecutionException;
+import java.util.Objects;
import java.util.concurrent.Future;
-import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
+import javax.annotation.Nullable;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
-import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
-import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowHash;
-import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
+import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
+import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
+import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
-import org.opendaylight.openflowplugin.impl.registry.flow.FlowHashFactory;
-import org.opendaylight.openflowplugin.impl.util.FlowUtil;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowConvertor;
+import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
+import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
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.UpdateFlowOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
-import org.opendaylight.yangtools.yang.common.RpcError;
-import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SalFlowServiceImpl implements SalFlowService, ItemLifeCycleSource {
+ private static final Logger LOG = LoggerFactory.getLogger(SalFlowServiceImpl.class);
+ private final FlowService<UpdateFlowOutput> flowUpdate;
+ private final FlowService<AddFlowOutput> flowAdd;
+ private final FlowService<RemoveFlowOutput> flowRemove;
+ private final DeviceContext deviceContext;
+ private ItemLifecycleListener itemLifecycleListener;
+
+ public SalFlowServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
+ this.deviceContext = deviceContext;
+ flowRemove = new FlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class, convertorExecutor);
+ flowAdd = new FlowService<>(requestContextStack, deviceContext, AddFlowOutput.class, convertorExecutor);
+ flowUpdate = new FlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class, convertorExecutor);
+ }
-public class SalFlowServiceImpl extends CommonService implements SalFlowService {
-
- private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalFlowServiceImpl.class);
-
- public SalFlowServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
- super(requestContextStack, deviceContext);
+ @Override
+ public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
+ this.itemLifecycleListener = itemLifecycleListener;
}
@Override
public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
- getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_ENTERED);
-
- final List<FlowModInputBuilder> ofFlowModInputs = FlowConvertor.toFlowModInputs(input, getVersion(), getDatapathId());
- final ListenableFuture<RpcResult<AddFlowOutput>> future = processFlowModInputBuilders(ofFlowModInputs);
- final FlowId flowId;
- if (null != input.getFlowRef()) {
- flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
- } else {
- flowId = FlowUtil.createAlienFlowId(input.getTableId());
- }
-
- final DeviceContext deviceContext = getDeviceContext();
- final FlowHash flowHash = FlowHashFactory.create(input, deviceContext.getPrimaryConnectionContext().getFeatures().getVersion());
- final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
- deviceContext.getDeviceFlowRegistry().store(flowHash, flowDescriptor);
- Futures.addCallback(future, new FutureCallback<RpcResult<AddFlowOutput>>() {
-
-
- @Override
- public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
- if (rpcResult.isSuccessful()) {
- LOG.debug("flow add finished without error, id={}", flowId.getValue());
- } else {
- LOG.debug("flow add failed with error, id={}", flowId.getValue());
- }
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- deviceContext.getDeviceFlowRegistry().markToBeremoved(flowHash);
- LOG.trace("Service call for adding flows failed, id={}.", flowId.getValue(), throwable);
- }
- });
-
+ final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
+ final ListenableFuture<RpcResult<AddFlowOutput>> future =
+ flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
+ Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey));
return future;
}
@Override
public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
- LOG.trace("Calling remove flow for flow with ID ={}.", input.getFlowRef());
- return handleServiceCall(new Function<RequestContext<RemoveFlowOutput>, ListenableFuture<RpcResult<RemoveFlowOutput>>>() {
- @Override
- public ListenableFuture<RpcResult<RemoveFlowOutput>> apply(final RequestContext<RemoveFlowOutput> requestContext) {
- final FlowModInputBuilder ofFlowModInput = FlowConvertor.toFlowModInput(input, getVersion(),
- getDatapathId());
- final ListenableFuture<RpcResult<RemoveFlowOutput>> future = createResultForFlowMod(requestContext, ofFlowModInput);
- Futures.addCallback(future, new FutureCallback<RpcResult<RemoveFlowOutput>>() {
- @Override
- public void onSuccess(final RpcResult<RemoveFlowOutput> o) {
- final DeviceContext deviceContext = getDeviceContext();
- getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
- FlowHash flowHash = FlowHashFactory.create(input, deviceContext.getPrimaryConnectionContext().getFeatures().getVersion());
- deviceContext.getDeviceFlowRegistry().markToBeremoved(flowHash);
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
- LOG.trace("Flow modification failed..", throwable);
- StringBuilder errors = new StringBuilder();
- try {
- RpcResult<RemoveFlowOutput> result = future.get();
- Collection<RpcError> rpcErrors = result.getErrors();
- if (null != rpcErrors && rpcErrors.size() > 0) {
- for (RpcError rpcError : rpcErrors) {
- errors.append(rpcError.getMessage());
- }
- }
- } catch (InterruptedException | ExecutionException e) {
- LOG.trace("Flow modification failed. Can't read errors from RpcResult.");
- } finally {
- LOG.trace("Flow modification failed. Errors : {}", errors.toString());
- }
- }
- });
- return future;
- }
- });
+ final ListenableFuture<RpcResult<RemoveFlowOutput>> future =
+ flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
+ Futures.addCallback(future, new RemoveFlowCallback(input));
+ return future;
}
@Override
public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
- final UpdateFlowInput in = input;
- final UpdatedFlow updated = in.getUpdatedFlow();
- final OriginalFlow original = in.getOriginalFlow();
+ final UpdatedFlow updated = input.getUpdatedFlow();
+ final OriginalFlow original = input.getOriginalFlow();
final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
- List<FlowModInputBuilder> ofFlowModInputs;
+ final List<FlowModInputBuilder> ofFlowModInputs;
- if (!FlowCreatorUtil.canModifyFlow(original, updated, getVersion())) {
+ if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
// We would need to remove original and add updated.
// remove flow
final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
- final List<FlowModInputBuilder> ofFlowRemoveInput = FlowConvertor.toFlowModInputs(removeflow.build(),
- getVersion(), getDatapathId());
+ final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
// remove flow should be the first
allFlowMods.addAll(ofFlowRemoveInput);
final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
- ofFlowModInputs = FlowConvertor.toFlowModInputs(addFlowInputBuilder.build(), getVersion(), getDatapathId());
+ ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
} else {
- ofFlowModInputs = FlowConvertor.toFlowModInputs(updated, getVersion(), getDatapathId());
+ ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
}
allFlowMods.addAll(ofFlowModInputs);
- ListenableFuture<RpcResult<UpdateFlowOutput>> future = processFlowModInputBuilders(allFlowMods);
- Futures.addCallback(future, new FutureCallback<RpcResult<UpdateFlowOutput>>() {
- @Override
- public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
- final DeviceContext deviceContext = getDeviceContext();
- getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
- final short version = deviceContext.getPrimaryConnectionContext().getFeatures().getVersion();
- FlowHash flowHash = FlowHashFactory.create(original, version);
-
- FlowHash updatedflowHash = FlowHashFactory.create(updated, version);
- FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
- FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
- final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
- deviceFlowRegistry.markToBeremoved(flowHash);
- deviceFlowRegistry.store(updatedflowHash, flowDescriptor);
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- getMessageSpy().spyMessage(input.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
- }
- });
+ ListenableFuture<RpcResult<UpdateFlowOutput>> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
+ Futures.addCallback(future, new UpdateFlowCallback(input));
return future;
}
- private <T> ListenableFuture<RpcResult<T>> processFlowModInputBuilders(final List<FlowModInputBuilder> ofFlowModInputs) {
+ @VisibleForTesting
+ private static KeyedInstanceIdentifier<Flow, FlowKey> createFlowPath(FlowDescriptor flowDescriptor,
+ KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
+ return nodePath.augmentation(FlowCapableNode.class)
+ .child(Table.class, flowDescriptor.getTableKey())
+ .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));
+ }
- final List<ListenableFuture<RpcResult<T>>> partialFutures = new ArrayList<>();
+ private class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
+ private final AddFlowInput input;
+ private final FlowRegistryKey flowRegistryKey;
- for (final FlowModInputBuilder flowModInputBuilder : ofFlowModInputs) {
- ListenableFuture<RpcResult<T>> partialFuture = handleServiceCall(
- new Function<RequestContext<T>, ListenableFuture<RpcResult<T>>>() {
- @Override
- public ListenableFuture<RpcResult<T>> apply(final RequestContext<T> requestContext) {
- return createResultForFlowMod(requestContext, flowModInputBuilder);
- }
- });
- partialFutures.add(partialFuture);
+ private AddFlowCallback(final AddFlowInput input,
+ final FlowRegistryKey flowRegistryKey) {
+ this.input = input;
+ this.flowRegistryKey = flowRegistryKey;
}
- final ListenableFuture<List<RpcResult<T>>> allFutures = Futures.successfulAsList(partialFutures);
- final SettableFuture<RpcResult<T>> finalFuture = SettableFuture.create();
- Futures.addCallback(allFutures, new FutureCallback<List<RpcResult<T>>>() {
- @Override
- public void onSuccess(final List<RpcResult<T>> results) {
- RpcResultBuilder<T> rpcResultBuilder = RpcResultBuilder.success();
- finalFuture.set(rpcResultBuilder.build());
- }
+ @Override
+ public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
+ if (rpcResult.isSuccessful()) {
+ final FlowDescriptor flowDescriptor;
- @Override
- public void onFailure(final Throwable t) {
- RpcResultBuilder<T> rpcResultBuilder = RpcResultBuilder.failed();
- finalFuture.set(rpcResultBuilder.build());
+ if (Objects.nonNull(input.getFlowRef())) {
+ final FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
+ flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
+ deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
+ } else {
+ final FlowId flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);
+ flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
+ }
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
+ }
+
+ if (itemLifecycleListener != null) {
+ KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+ final FlowBuilder flowBuilder = new FlowBuilder(input).setId(flowDescriptor.getFlowId());
+ itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
+ }
+ } else {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Flow add failed for flow={}, errors={}", input,
+ ErrorUtil.errorsToString(rpcResult.getErrors()));
+ }
}
- });
+ }
- return finalFuture;
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.warn("Service call for adding flow={} failed, reason: {}", input, throwable);
+ }
}
- protected <T> ListenableFuture<RpcResult<T>> createResultForFlowMod(final RequestContext<T> requestContext, final FlowModInputBuilder flowModInputBuilder) {
- final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
- final long xid = requestContext.getXid().getValue();
- flowModInputBuilder.setXid(xid);
- final FlowModInput flowModInput = flowModInputBuilder.build();
+ private class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
+ private final RemoveFlowInput input;
- outboundQueue.commitEntry(xid, flowModInput, new FutureCallback<OfHeader>() {
- @Override
- public void onSuccess(final OfHeader ofHeader) {
- getMessageSpy().spyMessage(FlowModInput.class, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
+ private RemoveFlowCallback(final RemoveFlowInput input) {
+ this.input = input;
+ }
- RpcResultBuilder<T> rpcResultBuilder = RpcResultBuilder.<T>success();
- requestContext.setResult(rpcResultBuilder.build());
+ @Override
+ public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
+ if (result.isSuccessful()) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Flow remove finished without error for flow={}", input);
+ }
+ FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
+ deviceContext.getDeviceFlowRegistry().removeDescriptor(flowRegistryKey);
+
+ if (itemLifecycleListener != null) {
+ final FlowDescriptor flowDescriptor =
+ deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
+ if (flowDescriptor != null) {
+ KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+ itemLifecycleListener.onRemoved(flowPath);
+ }
+ }
+ } else {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Flow remove failed for flow={}, errors={}", input,
+ ErrorUtil.errorsToString(result.getErrors()));
+ }
+ }
+ }
- RequestContextUtil.closeRequstContext(requestContext);
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.warn("Service call for removing flow={} failed, reason: {}", input, throwable);
+ }
+ }
+
+ private class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
+ private final UpdateFlowInput input;
+
+ private UpdateFlowCallback(UpdateFlowInput input) {
+ this.input = input;
+ }
+
+ @Override
+ public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
+ final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
+
+ final UpdatedFlow updated = input.getUpdatedFlow();
+ final OriginalFlow original = input.getOriginalFlow();
+ final FlowRegistryKey origFlowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), original);
+ final FlowRegistryKey updatedFlowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), updated);
+ final FlowDescriptor origFlowDescriptor = deviceFlowRegistry.retrieveIdForFlow(origFlowRegistryKey);
+
+ final boolean isUpdate = Objects.nonNull(origFlowDescriptor);
+ final FlowId fLowId = Objects.nonNull(input.getFlowRef())
+ ? input.getFlowRef().getValue().firstKeyOf(Flow.class).getId()
+ : isUpdate ? origFlowDescriptor.getFlowId() : deviceFlowRegistry.storeIfNecessary(updatedFlowRegistryKey);
+ final FlowDescriptor updatedFlowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), fLowId);
+ if (isUpdate) {
+ deviceFlowRegistry.removeDescriptor(origFlowRegistryKey);
+ deviceFlowRegistry.store(updatedFlowRegistryKey, updatedFlowDescriptor);
}
- @Override
- public void onFailure(final Throwable throwable) {
- getMessageSpy().spyMessage(FlowModInput.class, MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
- RpcResultBuilder<T> rpcResultBuilder = RpcResultBuilder.<T>failed().withError(ErrorType.APPLICATION, throwable.getMessage(), throwable);
- requestContext.setResult(rpcResultBuilder.build());
+ if (itemLifecycleListener != null) {
+ final KeyedInstanceIdentifier<Flow, FlowKey> flowPath =
+ createFlowPath(
+ updatedFlowDescriptor,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+
+ final Flow flow = new FlowBuilder(updated)
+ .setId(updatedFlowDescriptor.getFlowId())
+ .build();
- RequestContextUtil.closeRequstContext(requestContext);
+ if (Objects.nonNull(origFlowDescriptor)) {
+ itemLifecycleListener.onUpdated(flowPath, flow);
+ } else {
+ itemLifecycleListener.onAdded(flowPath, flow);
+ }
}
- });
- return requestContext.getFuture();
- }
+ }
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);
+ }
+ }
}