X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Fservices%2FSalFlowServiceImpl.java;h=d5d8605d240c68a33ecad96beeb8297f23620598;hb=0ac8877c3539d82e55ffe49dc0a2c8bcb0c82b19;hp=afd5e2041b0d2c25ac6d9a35994521da26539e60;hpb=1689afcfdbe24ac5ab7960ddd40111260b87b65e;p=openflowplugin.git
diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalFlowServiceImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalFlowServiceImpl.java
index afd5e2041b..5a4ed71f75 100644
--- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalFlowServiceImpl.java
+++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/SalFlowServiceImpl.java
@@ -1,38 +1,38 @@
/**
* Copyright (c) 2015 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,
* 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.JdkFutureAdapters;
import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.SettableFuture;
-import java.math.BigInteger;
import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
import java.util.List;
-import java.util.concurrent.ExecutionException;
+import java.util.Objects;
import java.util.concurrent.Future;
+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.device.Xid;
+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.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;
@@ -45,230 +45,222 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.Upda
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.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.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;
-
-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);
+import org.slf4j.LoggerFactory;
+
+public class SalFlowServiceImpl implements SalFlowService, ItemLifeCycleSource {
+ private static final Logger LOG = LoggerFactory.getLogger(SalFlowServiceImpl.class);
+ private final FlowService flowUpdate;
+ private final FlowService flowAdd;
+ private final FlowService 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);
}
- ListenableFuture> handleServiceCall(final BigInteger connectionID,
- final FlowModInputBuilder flowModInputBuilder, final Function, ListenableFuture>> function) {
- LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
-
- final RequestContext requestContext = requestContextStack.createRequestContext();
- final SettableFuture> result = requestContextStack.storeOrFail(requestContext);
- final DataCrate dataCrate = DataCrateBuilder.builder().setiDConnection(connectionID)
- .setRequestContext(requestContext).setFlowModInputBuilder(flowModInputBuilder).build();
-
- if (!result.isDone()) {
- final ListenableFuture> resultFromOFLib = function.apply(dataCrate);
-
- final OFJResult2RequestCtxFuture OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture<>(requestContext, deviceContext);
- OFJResult2RequestCtxFuture.processResultFromOfJava(resultFromOFLib);
-
- } else {
- RequestContextUtil.closeRequstContext(requestContext);
- }
- return result;
+ @Override
+ public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
+ this.itemLifecycleListener = itemLifecycleListener;
}
@Override
public Future> addFlow(final AddFlowInput input) {
- final FlowId flowId;
- if(null != input.getFlowRef()) {
- flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
- } else {
- flowId = FlowUtil.createAlienFlowId(input.getTableId());
- }
-
-
- final FlowHash flowHash = FlowHashFactory.create(input);
- final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
- deviceContext.getDeviceFlowRegistry().store(flowHash, flowDescriptor);
-
- final List ofFlowModInputs = FlowConvertor.toFlowModInputs(input, version, datapathId);
- final ListenableFuture future = processFlowModInputBuilders(ofFlowModInputs);
-
- Futures.addCallback(future, new FutureCallback() {
- @Override
- public void onSuccess(final Object o) {
- LOG.debug("flow add finished without 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> future =
+ flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
+ Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey));
return future;
}
@Override
public Future> removeFlow(final RemoveFlowInput input) {
- LOG.trace("Calling remove flow for flow with ID ={}.", input.getFlowRef());
- return this.handleServiceCall(PRIMARY_CONNECTION,
- new Function, ListenableFuture>>() {
- @Override
- public ListenableFuture> apply(final DataCrate data) {
- final FlowModInputBuilder ofFlowModInput = FlowConvertor.toFlowModInput(input, version,
- datapathId);
- final ListenableFuture> future = createResultForFlowMod(data, ofFlowModInput);
- Futures.addCallback(future, new FutureCallback() {
- @Override
- public void onSuccess(final Object o) {
- FlowHash flowHash = FlowHashFactory.create(input);
- deviceContext.getDeviceFlowRegistry().markToBeremoved(flowHash);
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- StringBuffer errors = new StringBuffer();
- try {
- RpcResult result = future.get();
- Collection 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.");
- }
- LOG.trace("Flow modification failed. Errors : {}", errors.toString());
- }
- });
- return future;
- }
- });
+ final ListenableFuture> future =
+ flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
+ Futures.addCallback(future, new RemoveFlowCallback(input));
+ return future;
}
@Override
public Future> 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 allFlowMods = new ArrayList<>();
- List ofFlowModInputs;
+ final List ofFlowModInputs;
- if (!FlowCreatorUtil.canModifyFlow(original, updated, version)) {
+ 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 ofFlowRemoveInput = FlowConvertor.toFlowModInputs(removeflow.build(),
- version, datapathId);
+ final List 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(), version, datapathId);
+ ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
} else {
- ofFlowModInputs = FlowConvertor.toFlowModInputs(updated, version, datapathId);
+ ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
}
allFlowMods.addAll(ofFlowModInputs);
- ListenableFuture future = processFlowModInputBuilders(allFlowMods);
- Futures.addCallback(future, new FutureCallback() {
- @Override
- public void onSuccess(final Object o) {
- FlowHash flowHash = FlowHashFactory.create(original);
- deviceContext.getDeviceFlowRegistry().markToBeremoved(flowHash);
-
- flowHash = FlowHashFactory.create(updated);
- FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
- FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
- deviceContext.getDeviceFlowRegistry().store(flowHash, flowDescriptor);
+ ListenableFuture> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
+ Futures.addCallback(future, new UpdateFlowCallback(input));
+ return future;
+ }
- }
+ @VisibleForTesting
+ private static KeyedInstanceIdentifier createFlowPath(FlowDescriptor flowDescriptor,
+ KeyedInstanceIdentifier nodePath) {
+ return nodePath.augmentation(FlowCapableNode.class)
+ .child(Table.class, flowDescriptor.getTableKey())
+ .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));
+ }
+
+ private class AddFlowCallback implements FutureCallback> {
+ private final AddFlowInput input;
+ private final FlowRegistryKey flowRegistryKey;
+
+ private AddFlowCallback(final AddFlowInput input,
+ final FlowRegistryKey flowRegistryKey) {
+ this.input = input;
+ this.flowRegistryKey = flowRegistryKey;
+ }
+
+ @Override
+ public void onSuccess(final RpcResult rpcResult) {
+ if (rpcResult.isSuccessful()) {
+ final FlowDescriptor flowDescriptor;
+
+ 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);
+ }
- @Override
- public void onFailure(final Throwable throwable) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
+ }
+ if (itemLifecycleListener != null) {
+ KeyedInstanceIdentifier 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 future;
+ }
+
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.warn("Service call for adding flow={} failed, reason: {}", input, throwable);
+ }
}
- private ListenableFuture> processFlowModInputBuilders(
- final List ofFlowModInputs) {
- final List>> partialFutures = new ArrayList<>();
- for (FlowModInputBuilder flowModInputBuilder : ofFlowModInputs) {
- ListenableFuture> partialFuture = handleServiceCall(PRIMARY_CONNECTION, flowModInputBuilder,
- new Function, ListenableFuture>>() {
- @Override
- public ListenableFuture> apply(final DataCrate data) {
- return createResultForFlowMod(data);
- }
- });
- partialFutures.add(partialFuture);
+ private class RemoveFlowCallback implements FutureCallback> {
+ private final RemoveFlowInput input;
+
+ private RemoveFlowCallback(final RemoveFlowInput input) {
+ this.input = input;
}
- final ListenableFuture>> allFutures = Futures.allAsList(partialFutures);
- final SettableFuture> finalFuture = SettableFuture.create();
- Futures.addCallback(allFutures, new FutureCallback>>() {
- @Override
- public void onSuccess(List> results) {
- Iterator flowModInputBldIterator = ofFlowModInputs.iterator();
- List rpcErrorLot = new ArrayList<>();
- for (RpcResult rpcResult : results) {
- FlowModInputBuilder flowModInputBld = flowModInputBldIterator.next();
- if (rpcResult.isSuccessful()) {
- Long xid = flowModInputBld.getXid();
- LOG.warn("Positive confirmation of flow push is not supported by OF-spec");
- LOG.warn("flow future result was successful [{}] = this should have never happen",
- xid);
- rpcErrorLot.add(RpcResultBuilder.newError(ErrorType.APPLICATION, "",
- "flow future result was successful ["+xid+"] = this should have never happen"));
- } else {
- rpcErrorLot.addAll(rpcResult.getErrors());
- }
+ @Override
+ public void onSuccess(final RpcResult result) {
+ if (result.isSuccessful()) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Flow remove finished without error for flow={}", input);
}
- finalFuture.set(RpcResultBuilder.failed().withRpcErrors(rpcErrorLot).build());
- }
-
- @Override
- public void onFailure(Throwable t) {
- LOG.trace("Flow mods chained future failed.");
- RpcResultBuilder resultBuilder;
- if (allFutures.isCancelled()) {
- if (LOG.isTraceEnabled()) {
- for (FlowModInputBuilder ofFlowModInput : ofFlowModInputs) {
- LOG.trace("flow future result was cancelled [{}] = barrier passed it without error",
- ofFlowModInput.getXid());
- }
+ 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 flowPath = createFlowPath(flowDescriptor,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+ itemLifecycleListener.onRemoved(flowPath);
}
- resultBuilder = RpcResultBuilder.success();
- } else {
- resultBuilder = RpcResultBuilder.failed().withError(ErrorType.APPLICATION, "", t.getMessage());
}
- finalFuture.set(resultBuilder.build());
+ } else {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Flow remove failed for flow={}, errors={}", input,
+ ErrorUtil.errorsToString(result.getErrors()));
+ }
}
- });
+ }
- return finalFuture;
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.warn("Service call for removing flow={} failed, reason: {}", input, throwable);
+ }
}
- protected ListenableFuture> createResultForFlowMod(final DataCrate data) {
- return createResultForFlowMod(data, data.getFlowModInputBuilder());
- }
+ private class UpdateFlowCallback implements FutureCallback> {
+ private final UpdateFlowInput input;
- protected ListenableFuture> createResultForFlowMod(final DataCrate data, final FlowModInputBuilder flowModInput) {
- final Xid xid = data.getRequestContext().getXid();
- flowModInput.setXid(xid.getValue());
- Future> flowModResult = provideConnectionAdapter(data.getiDConnection()).flowMod(
- flowModInput.build());
- return JdkFutureAdapters.listenInPoolThread(flowModResult);
- }
+ private UpdateFlowCallback(UpdateFlowInput input) {
+ this.input = input;
+ }
+
+ @Override
+ public void onSuccess(final RpcResult 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);
+ }
+
+ if (itemLifecycleListener != null) {
+ final KeyedInstanceIdentifier flowPath =
+ createFlowPath(
+ updatedFlowDescriptor,
+ deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
+
+ final Flow flow = new FlowBuilder(updated)
+ .setId(updatedFlowDescriptor.getFlowId())
+ .build();
+ if (Objects.nonNull(origFlowDescriptor)) {
+ itemLifecycleListener.onUpdated(flowPath, flow);
+ } else {
+ itemLifecycleListener.onAdded(flowPath, flow);
+ }
+ }
+ }
+
+ @Override
+ public void onFailure(final Throwable throwable) {
+ LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);
+ }
+ }
}