2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.services;
10 import com.google.common.base.Function;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Future;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
25 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
26 import org.opendaylight.openflowplugin.api.openflow.device.exception.DeviceDataException;
27 import org.opendaylight.openflowplugin.api.openflow.flow.registry.FlowDescriptor;
28 import org.opendaylight.openflowplugin.api.openflow.flow.registry.FlowHash;
29 import org.opendaylight.openflowplugin.impl.flow.registry.FlowDescriptorFactory;
30 import org.opendaylight.openflowplugin.impl.flow.registry.FlowHashFactory;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowConvertor;
32 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
48 import org.opendaylight.yangtools.yang.common.RpcError;
49 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
52 import org.slf4j.Logger;
54 public class SalFlowServiceImpl extends CommonService implements SalFlowService {
56 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalFlowServiceImpl.class);
58 public SalFlowServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
59 super(requestContextStack, deviceContext);
62 <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
63 final FlowModInputBuilder flowModInputBuilder, final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function) {
64 LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
66 final RequestContext<T> requestContext = requestContextStack.createRequestContext();
67 final SettableFuture<RpcResult<T>> result = requestContextStack.storeOrFail(requestContext);
68 final DataCrate<T> dataCrate = DataCrateBuilder.<T>builder().setiDConnection(connectionID)
69 .setRequestContext(requestContext).setFlowModInputBuilder(flowModInputBuilder).build();
71 if (!result.isDone()) {
72 final ListenableFuture<RpcResult<F>> resultFromOFLib = function.apply(dataCrate);
74 final OFJResult2RequestCtxFuture<T> OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture<>(requestContext, deviceContext);
75 OFJResult2RequestCtxFuture.processResultFromOfJava(resultFromOFLib);
78 RequestContextUtil.closeRequstContext(requestContext);
84 public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
86 final FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
88 final FlowHash flowHash = FlowHashFactory.create(input);
89 final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
90 deviceContext.getDeviceFlowRegistry().store(flowHash, flowDescriptor);
92 final List<FlowModInputBuilder> ofFlowModInputs = FlowConvertor.toFlowModInputs(input, version, datapathId);
93 final ListenableFuture future = processFlowModInputBuilders(ofFlowModInputs);
95 Futures.addCallback(future, new FutureCallback() {
97 public void onSuccess(final Object o) {
98 LOG.debug("flow add finished without error, id={}", flowId.getValue());
102 public void onFailure(final Throwable throwable) {
103 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowHash);
104 LOG.trace("Service call for adding flows failed, id={}.", flowId.getValue(), throwable);
112 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
113 LOG.trace("Calling remove flow for flow with ID ={}.", input.getFlowRef());
114 return this.<RemoveFlowOutput, Void>handleServiceCall(PRIMARY_CONNECTION,
115 new Function<DataCrate<RemoveFlowOutput>, ListenableFuture<RpcResult<Void>>>() {
117 public ListenableFuture<RpcResult<Void>> apply(final DataCrate<RemoveFlowOutput> data) {
118 final FlowModInputBuilder ofFlowModInput = FlowConvertor.toFlowModInput(input, version,
120 final ListenableFuture<RpcResult<Void>> future = createResultForFlowMod(data, ofFlowModInput);
121 Futures.addCallback(future, new FutureCallback() {
123 public void onSuccess(final Object o) {
124 FlowHash flowHash = FlowHashFactory.create(input);
125 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowHash);
129 public void onFailure(final Throwable throwable) {
130 StringBuffer errors = new StringBuffer();
132 RpcResult<Void> result = future.get();
133 Collection<RpcError> rpcErrors = result.getErrors();
134 if (null != rpcErrors && rpcErrors.size() > 0) {
135 for (RpcError rpcError : rpcErrors) {
136 errors.append(rpcError.getMessage());
139 } catch (InterruptedException | ExecutionException e) {
140 LOG.trace("Flow modification failed. Can't read errors from RpcResult.");
142 LOG.trace("Flow modification failed. Errors : {}", errors.toString());
151 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
152 final UpdateFlowInput in = input;
153 final UpdatedFlow updated = in.getUpdatedFlow();
154 final OriginalFlow original = in.getOriginalFlow();
156 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
157 List<FlowModInputBuilder> ofFlowModInputs;
159 if (!FlowCreatorUtil.canModifyFlow(original, updated, version)) {
160 // We would need to remove original and add updated.
163 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
164 final List<FlowModInputBuilder> ofFlowRemoveInput = FlowConvertor.toFlowModInputs(removeflow.build(),
165 version, datapathId);
166 // remove flow should be the first
167 allFlowMods.addAll(ofFlowRemoveInput);
168 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
169 ofFlowModInputs = FlowConvertor.toFlowModInputs(addFlowInputBuilder.build(), version, datapathId);
171 ofFlowModInputs = FlowConvertor.toFlowModInputs(updated, version, datapathId);
174 allFlowMods.addAll(ofFlowModInputs);
175 ListenableFuture future = processFlowModInputBuilders(allFlowMods);
176 Futures.addCallback(future, new FutureCallback() {
178 public void onSuccess(final Object o) {
179 FlowHash flowHash = FlowHashFactory.create(original);
180 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowHash);
182 flowHash = FlowHashFactory.create(updated);
183 FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
184 FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
185 deviceContext.getDeviceFlowRegistry().store(flowHash, flowDescriptor);
190 public void onFailure(final Throwable throwable) {
197 private <T> ListenableFuture<RpcResult<T>> processFlowModInputBuilders(
198 final List<FlowModInputBuilder> ofFlowModInputs) {
199 final List<ListenableFuture<RpcResult<T>>> partialFutures = new ArrayList<>();
200 for (FlowModInputBuilder flowModInputBuilder : ofFlowModInputs) {
201 ListenableFuture<RpcResult<T>> partialFuture = handleServiceCall(PRIMARY_CONNECTION, flowModInputBuilder,
202 new Function<DataCrate<T>, ListenableFuture<RpcResult<Void>>>() {
204 public ListenableFuture<RpcResult<Void>> apply(final DataCrate<T> data) {
205 return createResultForFlowMod(data);
208 partialFutures.add(partialFuture);
211 final ListenableFuture<List<RpcResult<T>>> allFutures = Futures.allAsList(partialFutures);
212 final SettableFuture<RpcResult<T>> finalFuture = SettableFuture.create();
213 Futures.addCallback(allFutures, new FutureCallback<List<RpcResult<T>>>() {
215 public void onSuccess(List<RpcResult<T>> result) {
216 LOG.warn("Positive confirmation of flow push is not supported by OF-spec");
217 for (FlowModInputBuilder ofFlowModInput : ofFlowModInputs) {
218 LOG.warn("flow future result was successful [{}] = this should have never happen",
219 ofFlowModInput.getXid());
221 finalFuture.setException(new DeviceDataException("positive confirmation of flow occurred"));
225 public void onFailure(Throwable t) {
226 LOG.trace("Flow mods chained future failed.");
227 RpcResultBuilder<T> resultBuilder;
228 if (allFutures.isCancelled()) {
229 if (LOG.isTraceEnabled()) {
230 for (FlowModInputBuilder ofFlowModInput : ofFlowModInputs) {
231 LOG.trace("flow future result was cancelled [{}] = barrier passed it without error",
232 ofFlowModInput.getXid());
235 resultBuilder = RpcResultBuilder.<T>success();
237 resultBuilder = RpcResultBuilder.<T>failed().withError(ErrorType.APPLICATION, "", t.getMessage());
239 finalFuture.set(resultBuilder.build());
246 protected <T> ListenableFuture<RpcResult<Void>> createResultForFlowMod(final DataCrate<T> data) {
247 return createResultForFlowMod(data, data.getFlowModInputBuilder());
250 protected <T> ListenableFuture<RpcResult<Void>> createResultForFlowMod(final DataCrate<T> data, final FlowModInputBuilder flowModInput) {
251 final Xid xId = deviceContext.getNextXid();
252 flowModInput.setXid(xId.getValue());
253 data.getRequestContext().setXid(xId);
254 Future<RpcResult<Void>> flowModResult = provideConnectionAdapter(data.getiDConnection()).flowMod(
255 flowModInput.build());
256 return JdkFutureAdapters.listenInPoolThread(flowModResult);