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;
12 import com.google.common.util.concurrent.AsyncFunction;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.Future;
21 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.OFRpcTaskFactory;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowConvertor;
24 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
40 public class SalFlowServiceImpl extends CommonService implements SalFlowService {
42 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalFlowServiceImpl.class);
44 public SalFlowServiceImpl(final RpcContext rpcContext) {
49 public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
50 return this.<AddFlowOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
51 new Function<BigInteger, Future<RpcResult<Void>>>() {
53 public ListenableFuture<RpcResult<Void>> apply(final BigInteger IDConnection) {
54 final List<FlowModInputBuilder> ofFlowModInputs = FlowConvertor.toFlowModInputs(input, version,
56 return chainFlowMods(ofFlowModInputs, 0, IDConnection);
62 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
63 return this.<RemoveFlowOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
64 new Function<BigInteger, Future<RpcResult<Void>>>() {
66 public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
67 final FlowModInputBuilder ofFlowModInput = FlowConvertor.toFlowModInput(input, version,
69 return createResultForFlowMod(ofFlowModInput, IDConnection);
75 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
76 final UpdateFlowInput in = input;
77 final UpdatedFlow updated = in.getUpdatedFlow();
78 final OriginalFlow original = in.getOriginalFlow();
80 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
81 List<FlowModInputBuilder> ofFlowModInputs;
83 if (!FlowCreatorUtil.canModifyFlow(original, updated, version)) {
84 // We would need to remove original and add updated.
87 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
88 final List<FlowModInputBuilder> ofFlowRemoveInput = FlowConvertor.toFlowModInputs(removeflow.build(),
90 // remove flow should be the first
91 allFlowMods.addAll(ofFlowRemoveInput);
92 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
93 ofFlowModInputs = FlowConvertor.toFlowModInputs(addFlowInputBuilder.build(), version, datapathId);
95 ofFlowModInputs = FlowConvertor.toFlowModInputs(updated, version, datapathId);
98 allFlowMods.addAll(ofFlowModInputs);
99 LOG.debug("Number of flows to push to switch: {}", allFlowMods.size());
100 Collections.<String> emptyList();
101 return this.<UpdateFlowOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
102 new Function<BigInteger, Future<RpcResult<Void>>>() {
104 public Future<RpcResult<Void>> apply(final BigInteger cookie) {
105 return chainFlowMods(allFlowMods, 0, cookie);
111 * Recursive helper method for
112 * {@link OFRpcTaskFactory#chainFlowMods(java.util.List, int, org.opendaylight.openflowplugin.openflow.md.core.sal.OFRpcTaskContext, org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher)}
113 * {@link OFRpcTaskFactory#createUpdateFlowTask()} to chain results of multiple flowmods. The next flowmod gets
114 * executed if the earlier one is successful. All the flowmods should have the same xid, in-order to cross-reference
117 protected ListenableFuture<RpcResult<Void>> chainFlowMods(final List<FlowModInputBuilder> ofFlowModInputs,
118 final int index, final BigInteger cookie) {
120 final Future<RpcResult<Void>> resultFromOFLib = createResultForFlowMod(ofFlowModInputs.get(index), cookie);
122 final ListenableFuture<RpcResult<Void>> result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
124 if (ofFlowModInputs.size() > index + 1) {
125 // there are more flowmods to chain
126 return Futures.transform(result, new AsyncFunction<RpcResult<Void>, RpcResult<Void>>() {
128 public ListenableFuture<RpcResult<Void>> apply(final RpcResult<Void> input) throws Exception {
129 if (input.isSuccessful()) {
130 return chainFlowMods(ofFlowModInputs, index + 1, cookie);
132 LOG.warn("Flowmod failed. Any chained flowmods are ignored. xid:{}", ofFlowModInputs.get(index)
134 return Futures.immediateFuture(input);
143 protected Future<RpcResult<Void>> createResultForFlowMod(final FlowModInputBuilder flowModInput,
144 final BigInteger cookie) {
145 flowModInput.setXid(deviceContext.getNextXid().getValue());
146 return provideConnectionAdapter(cookie).flowMod(flowModInput.build());