Wrap service handlers to method handleServiceCall.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalFlowServiceImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.services;
9
10 import com.google.common.base.Function;
11
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;
39
40 public class SalFlowServiceImpl extends CommonService implements SalFlowService {
41
42     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalFlowServiceImpl.class);
43
44     public SalFlowServiceImpl(final RpcContext rpcContext) {
45         super(rpcContext);
46     }
47
48     @Override
49     public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
50         return this.<AddFlowOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
51                 new Function<BigInteger, Future<RpcResult<Void>>>() {
52                     @Override
53                     public ListenableFuture<RpcResult<Void>> apply(final BigInteger IDConnection) {
54                         final List<FlowModInputBuilder> ofFlowModInputs = FlowConvertor.toFlowModInputs(input, version,
55                                 datapathId);
56                         return chainFlowMods(ofFlowModInputs, 0, IDConnection);
57                     }
58                 });
59     }
60
61     @Override
62     public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
63         return this.<RemoveFlowOutput, Void> handleServiceCall(PRIMARY_CONNECTION,
64                 new Function<BigInteger, Future<RpcResult<Void>>>() {
65                     @Override
66                     public Future<RpcResult<Void>> apply(final BigInteger IDConnection) {
67                         final FlowModInputBuilder ofFlowModInput = FlowConvertor.toFlowModInput(input, version,
68                                 datapathId);
69                         return createResultForFlowMod(ofFlowModInput, IDConnection);
70                     }
71                 });
72     }
73
74     @Override
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();
79
80         final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
81         List<FlowModInputBuilder> ofFlowModInputs;
82
83         if (!FlowCreatorUtil.canModifyFlow(original, updated, version)) {
84             // We would need to remove original and add updated.
85
86             // remove flow
87             final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
88             final List<FlowModInputBuilder> ofFlowRemoveInput = FlowConvertor.toFlowModInputs(removeflow.build(),
89                     version, datapathId);
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);
94         } else {
95             ofFlowModInputs = FlowConvertor.toFlowModInputs(updated, version, datapathId);
96         }
97
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>>>() {
103                     @Override
104                     public Future<RpcResult<Void>> apply(final BigInteger cookie) {
105                         return chainFlowMods(allFlowMods, 0, cookie);
106                     }
107                 });
108     }
109
110     /**
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
115      * the notification
116      */
117     protected ListenableFuture<RpcResult<Void>> chainFlowMods(final List<FlowModInputBuilder> ofFlowModInputs,
118             final int index, final BigInteger cookie) {
119
120         final Future<RpcResult<Void>> resultFromOFLib = createResultForFlowMod(ofFlowModInputs.get(index), cookie);
121
122         final ListenableFuture<RpcResult<Void>> result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
123
124         if (ofFlowModInputs.size() > index + 1) {
125             // there are more flowmods to chain
126             return Futures.transform(result, new AsyncFunction<RpcResult<Void>, RpcResult<Void>>() {
127                 @Override
128                 public ListenableFuture<RpcResult<Void>> apply(final RpcResult<Void> input) throws Exception {
129                     if (input.isSuccessful()) {
130                         return chainFlowMods(ofFlowModInputs, index + 1, cookie);
131                     } else {
132                         LOG.warn("Flowmod failed. Any chained flowmods are ignored. xid:{}", ofFlowModInputs.get(index)
133                                 .getXid());
134                         return Futures.immediateFuture(input);
135                     }
136                 }
137             });
138         } else {
139             return result;
140         }
141     }
142
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());
147     }
148
149 }