External api proposal
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OFRpcTaskFactory.java
1 /**
2  * Copyright (c) 2013-2014 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.openflow.md.core.sal;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.Future;
14
15 import org.opendaylight.openflowjava.protocol.api.util.BinContent;
16 import org.opendaylight.openflowplugin.api.OFConstants;
17 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
18 import org.opendaylight.openflowplugin.api.openflow.md.core.sal.NotificationComposer;
19 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowConvertor;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.GroupConvertor;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.MeterConvertor;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PortConvertor;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.TableFeaturesConvertor;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
26 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
27 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemovedBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdatedBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutputBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAdded;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAddedBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemoved;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemovedBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdatedBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutputBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutputBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesInput;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesOutput;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesOutputBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsInput;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsOutput;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsOutputBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterAdded;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterAddedBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemoved;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemovedBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdated;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdatedBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInput;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutputBuilder;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInput;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutputBuilder;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesInput;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutput;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutputBuilder;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsInput;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsOutput;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsOutputBuilder;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInput;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutputBuilder;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupId;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterId;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInputBuilder;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestAggregateCaseBuilder;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupCaseBuilder;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupDescCaseBuilder;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupFeaturesCaseBuilder;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterCaseBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterConfigCaseBuilder;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCaseBuilder;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortStatsCaseBuilder;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestQueueCaseBuilder;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCaseBuilder;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.aggregate._case.MultipartRequestAggregateBuilder;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.group._case.MultipartRequestGroupBuilder;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.meter._case.MultipartRequestMeterBuilder;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.meter.config._case.MultipartRequestMeterConfigBuilder;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.port.stats._case.MultipartRequestPortStatsBuilder;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.queue._case.MultipartRequestQueueBuilder;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table._case.MultipartRequestTableBuilder;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.multipart.request.table.features.TableFeatures;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortInput;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortOutput;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInput;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutput;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutputBuilder;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsInput;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsOutput;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsOutputBuilder;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsInput;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutput;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutputBuilder;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortInput;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortOutput;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortOutputBuilder;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortInput;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortOutput;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortOutputBuilder;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
166 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
167 import org.opendaylight.yangtools.yang.common.RpcResult;
168 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
169 import org.slf4j.Logger;
170 import org.slf4j.LoggerFactory;
171
172 import com.google.common.util.concurrent.AsyncFunction;
173 import com.google.common.util.concurrent.FutureCallback;
174 import com.google.common.util.concurrent.Futures;
175 import com.google.common.util.concurrent.JdkFutureAdapters;
176 import com.google.common.util.concurrent.ListenableFuture;
177 import com.google.common.util.concurrent.SettableFuture;
178
179 /**
180  *
181  */
182 public abstract class OFRpcTaskFactory {
183     protected static final Logger LOG = LoggerFactory.getLogger(OFRpcTaskFactory.class);
184
185     private OFRpcTaskFactory() {
186         // hiding implicit constructor
187     }
188     
189     /**
190      * @param taskContext
191      * @param input
192      * @param cookie
193      * @return UpdateFlow task
194      */
195     public static OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> createAddFlowTask(
196             OFRpcTaskContext taskContext, AddFlowInput input,
197             SwitchConnectionDistinguisher cookie) {
198
199         class OFRpcTaskImpl extends OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> {
200
201             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie, AddFlowInput input) {
202                 super(taskContext, cookie, input);
203             }
204
205             @Override
206             public ListenableFuture<RpcResult<UpdateFlowOutput>> call() {
207                 ListenableFuture<RpcResult<UpdateFlowOutput>> result = SettableFuture.create();
208
209                 // Convert the AddFlowInput to FlowModInput
210                 List<FlowModInputBuilder> ofFlowModInputs = FlowConvertor.toFlowModInputs(getInput(),
211                         getVersion(), getSession().getFeatures().getDatapathId());
212                 LOG.debug("Number of flows to push to switch: {}", ofFlowModInputs.size());
213                 result = chainFlowMods(ofFlowModInputs, 0, getTaskContext(), getCookie());
214                 result = OFRpcTaskUtil.chainFutureBarrier(this, result);
215                 OFRpcTaskUtil.hookFutureNotification(this, result,
216                         getRpcNotificationProviderService(),
217                         createFlowAddedNotification(getInput()));
218                 return result;
219             }
220
221             @Override
222             public Boolean isBarrier() {
223                 return getInput().isBarrier();
224             }
225         }
226
227         return new OFRpcTaskImpl(taskContext, cookie, input);
228     }
229
230     /**
231      * Recursive helper method for {@link OFRpcTaskFactory#createAddFlowTask()}
232      * and {@link OFRpcTaskFactory#createUpdateFlowTask()} to chain results
233      * of multiple flowmods.
234      * The next flowmod gets executed if the earlier one is successful.
235      * All the flowmods should have the same xid, in-order to cross-reference
236      * the notification
237      */
238     protected static ListenableFuture<RpcResult<UpdateFlowOutput>> chainFlowMods(
239             final List<FlowModInputBuilder> ofFlowModInputs, final int index,
240             final OFRpcTaskContext taskContext, final SwitchConnectionDistinguisher cookie) {
241
242         Future<RpcResult<UpdateFlowOutput>> resultFromOFLib =
243                 createResultForFlowMod(taskContext, ofFlowModInputs.get(index), cookie);
244
245         ListenableFuture<RpcResult<UpdateFlowOutput>> result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
246
247         if (ofFlowModInputs.size() > index + 1) {
248             // there are more flowmods to chain
249             return Futures.transform(result,
250                     new AsyncFunction<RpcResult<UpdateFlowOutput>, RpcResult<UpdateFlowOutput>>() {
251                         @Override
252                         public ListenableFuture<RpcResult<UpdateFlowOutput>> apply(RpcResult<UpdateFlowOutput> input) throws Exception {
253                             if (input.isSuccessful()) {
254                                 return chainFlowMods(ofFlowModInputs, index + 1, taskContext, cookie);
255                             } else {
256                                 LOG.warn("Flowmod failed. Any chained flowmods are ignored. xid:{}",
257                                         ofFlowModInputs.get(index).getXid());
258                                 return Futures.immediateFuture(input);
259                             }
260                         }
261                     }
262             );
263         } else {
264             return result;
265         }
266     }
267
268     private static Future<RpcResult<UpdateFlowOutput>> createResultForFlowMod(
269             OFRpcTaskContext taskContext, FlowModInputBuilder flowModInput,
270             SwitchConnectionDistinguisher cookie) {
271         flowModInput.setXid(taskContext.getSession().getNextXid());
272         return taskContext.getMessageService().flowMod(flowModInput.build(), cookie);
273     }
274
275
276     /**
277      * @param input
278      * @return
279      */
280     protected static NotificationComposer<FlowAdded> createFlowAddedNotification(
281             final AddFlowInput input) {
282         return new NotificationComposer<FlowAdded>() {
283             @Override
284             public FlowAdded compose(TransactionId tXid) {
285                 FlowAddedBuilder newFlow = new FlowAddedBuilder((Flow) input);
286                 newFlow.setTransactionId(tXid);
287                 newFlow.setFlowRef(input.getFlowRef());
288                 return newFlow.build();
289             }
290         };
291     }
292
293     /**
294      * @param taskContext
295      * @param input
296      * @param cookie
297      * @return UpdateFlow task
298      */
299     public static OFRpcTask<UpdateFlowInput, RpcResult<UpdateFlowOutput>> createUpdateFlowTask(
300             final OFRpcTaskContext taskContext, UpdateFlowInput input,
301             SwitchConnectionDistinguisher cookie) {
302
303         class OFRpcTaskImpl extends OFRpcTask<UpdateFlowInput, RpcResult<UpdateFlowOutput>> {
304
305             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
306                     UpdateFlowInput input) {
307                 super(taskContext, cookie, input);
308             }
309
310             @Override
311             public ListenableFuture<RpcResult<UpdateFlowOutput>> call() {
312                 ListenableFuture<RpcResult<UpdateFlowOutput>> result = null;
313
314                 UpdateFlowInput in = getInput();
315                 UpdatedFlow updated = in.getUpdatedFlow();
316                 OriginalFlow original = in.getOriginalFlow();
317                 Short version = getVersion();
318
319                 List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
320                 List<FlowModInputBuilder> ofFlowModInputs;
321
322                 if (!FlowCreatorUtil.canModifyFlow(original, updated, version)) {
323                     // We would need to remove original and add updated.
324
325                     //remove flow
326                     RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
327                     List<FlowModInputBuilder> ofFlowRemoveInput = FlowConvertor.toFlowModInputs(removeflow.build(),
328                             version, getSession().getFeatures().getDatapathId());
329                     // remove flow should be the first
330                     allFlowMods.addAll(ofFlowRemoveInput);
331                     AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
332                     ofFlowModInputs = FlowConvertor.toFlowModInputs(addFlowInputBuilder.build(),
333                             version, getSession().getFeatures().getDatapathId());
334                 } else {
335                     ofFlowModInputs = FlowConvertor.toFlowModInputs(updated,
336                             version, getSession().getFeatures().getDatapathId());
337                 }
338
339                 allFlowMods.addAll(ofFlowModInputs);
340                 LOG.debug("Number of flows to push to switch: {}", allFlowMods.size());
341                 result = chainFlowMods(allFlowMods, 0, getTaskContext(), getCookie());
342
343                 result = OFRpcTaskUtil.chainFutureBarrier(this, result);
344                 OFRpcTaskUtil.hookFutureNotification(this, result,
345                         getRpcNotificationProviderService(),
346                         createFlowUpdatedNotification(in));
347                 return result;
348             }
349
350             @Override
351             public Boolean isBarrier() {
352                 return getInput().getUpdatedFlow().isBarrier();
353             }
354         }
355         return new OFRpcTaskImpl(taskContext, cookie, input);
356     }
357
358
359     /**
360      * @param xId
361      * @param input
362      * @return
363      */
364     protected static NotificationComposer<FlowUpdated> createFlowUpdatedNotification(final UpdateFlowInput input) {
365         return new NotificationComposer<FlowUpdated>() {
366             @Override
367             public FlowUpdated compose(TransactionId tXid) {
368                 FlowUpdatedBuilder updFlow = new FlowUpdatedBuilder(input.getUpdatedFlow());
369                 updFlow.setTransactionId(tXid);
370                 updFlow.setFlowRef(input.getFlowRef());
371                 return updFlow.build();
372             }
373         };
374     }
375
376     /**
377      * @param taskContext
378      * @param input
379      * @param cookie
380      * @return update group task
381      */
382     public static OFRpcTask<AddGroupInput, RpcResult<UpdateGroupOutput>> createAddGroupTask(
383             final OFRpcTaskContext taskContext, AddGroupInput input,
384             final SwitchConnectionDistinguisher cookie) {
385         class OFRpcTaskImpl extends OFRpcTask<AddGroupInput, RpcResult<UpdateGroupOutput>> {
386
387             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie, AddGroupInput input) {
388                 super(taskContext, cookie, input);
389             }
390
391             @Override
392             public ListenableFuture<RpcResult<UpdateGroupOutput>> call() {
393                 ListenableFuture<RpcResult<UpdateGroupOutput>> result = SettableFuture.create();
394
395                 // Convert the AddGroupInput to GroupModInput
396                 GroupModInputBuilder ofGroupModInput = GroupConvertor.toGroupModInput(getInput(),
397                         getVersion(), getSession().getFeatures().getDatapathId());
398                 final Long xId = getSession().getNextXid();
399                 ofGroupModInput.setXid(xId);
400
401                 Future<RpcResult<UpdateGroupOutput>> resultFromOFLib = getMessageService()
402                         .groupMod(ofGroupModInput.build(), getCookie());
403                 result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
404
405                 result = OFRpcTaskUtil.chainFutureBarrier(this, result);
406                 OFRpcTaskUtil.hookFutureNotification(this, result,
407                         getRpcNotificationProviderService(), createGroupAddedNotification(getInput()));
408
409                 return result;
410             }
411
412             @Override
413             public Boolean isBarrier() {
414                 return getInput().isBarrier();
415             }
416         };
417
418         return new OFRpcTaskImpl(taskContext, cookie, input);
419     }
420
421
422     /**
423      * @param input
424      * @return
425      */
426     protected static NotificationComposer<GroupAdded> createGroupAddedNotification(
427             final AddGroupInput input) {
428         return new NotificationComposer<GroupAdded>() {
429             @Override
430             public GroupAdded compose(TransactionId tXid) {
431                 GroupAddedBuilder groupMod = new GroupAddedBuilder((Group) input);
432                 groupMod.setTransactionId(tXid);
433                 groupMod.setGroupRef(input.getGroupRef());
434                 return groupMod.build();
435             }
436         };
437     }
438
439     /**
440      * @param taskContext
441      * @param input
442      * @param cookie
443      * @return update meter task
444      */
445     public static OFRpcTask<AddMeterInput, RpcResult<UpdateMeterOutput>> createAddMeterTask(
446             OFRpcTaskContext taskContext, AddMeterInput input,
447             SwitchConnectionDistinguisher cookie) {
448         class OFRpcTaskImpl extends OFRpcTask<AddMeterInput, RpcResult<UpdateMeterOutput>> {
449
450             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie, AddMeterInput input) {
451                 super(taskContext, cookie, input);
452             }
453
454             @Override
455             public ListenableFuture<RpcResult<UpdateMeterOutput>> call() {
456                 ListenableFuture<RpcResult<UpdateMeterOutput>> result = SettableFuture.create();
457
458                 // Convert the AddGroupInput to GroupModInput
459                 MeterModInputBuilder ofMeterModInput = MeterConvertor.toMeterModInput(getInput(), getVersion());
460                 final Long xId = getSession().getNextXid();
461                 ofMeterModInput.setXid(xId);
462
463                 Future<RpcResult<UpdateMeterOutput>> resultFromOFLib = getMessageService()
464                         .meterMod(ofMeterModInput.build(), getCookie());
465                 result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
466
467                 result = OFRpcTaskUtil.chainFutureBarrier(this, result);
468                 OFRpcTaskUtil.hookFutureNotification(this, result,
469                         getRpcNotificationProviderService(), createMeterAddedNotification(getInput()));
470
471                 return result;
472             }
473
474             @Override
475             public Boolean isBarrier() {
476                 return getInput().isBarrier();
477             }
478         };
479
480         return new OFRpcTaskImpl(taskContext, cookie, input);
481     }
482
483     /**
484      * @param input
485      * @return
486      */
487     protected static NotificationComposer<MeterAdded> createMeterAddedNotification(
488             final AddMeterInput input) {
489         return new NotificationComposer<MeterAdded>() {
490             @Override
491             public MeterAdded compose(TransactionId tXid) {
492                 MeterAddedBuilder meterMod = new MeterAddedBuilder((Meter) input);
493                 meterMod.setTransactionId(tXid);
494                 meterMod.setMeterRef(input.getMeterRef());
495                 return meterMod.build();
496             }
497         };
498     }
499
500     /**
501      * @param taskContext
502      * @param input
503      * @param cookie
504      * @return UpdateFlow task
505      */
506     public static OFRpcTask<UpdateGroupInput, RpcResult<UpdateGroupOutput>> createUpdateGroupTask(
507             OFRpcTaskContext taskContext, UpdateGroupInput input,
508             SwitchConnectionDistinguisher cookie) {
509         class OFRpcTaskImpl extends OFRpcTask<UpdateGroupInput, RpcResult<UpdateGroupOutput>> {
510
511             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
512                     UpdateGroupInput input) {
513                 super(taskContext, cookie, input);
514             }
515
516             @Override
517             public ListenableFuture<RpcResult<UpdateGroupOutput>> call() {
518                 ListenableFuture<RpcResult<UpdateGroupOutput>> result = null;
519
520                 // Convert the UpdateGroupInput to GroupModInput
521                 GroupModInputBuilder ofGroupModInput = GroupConvertor.toGroupModInput(
522                         getInput().getUpdatedGroup(), getVersion(),
523                         getSession().getFeatures().getDatapathId());
524                 final Long xId = getSession().getNextXid();
525                 ofGroupModInput.setXid(xId);
526
527                 Future<RpcResult<UpdateGroupOutput>> resultFromOFLib =
528                         getMessageService().groupMod(ofGroupModInput.build(), getCookie());
529                 result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
530
531                 result = OFRpcTaskUtil.chainFutureBarrier(this, result);
532                 OFRpcTaskUtil.hookFutureNotification(this, result,
533                         getRpcNotificationProviderService(), createGroupUpdatedNotification(getInput()));
534
535                 return result;
536             }
537         }
538
539         return new OFRpcTaskImpl(taskContext, cookie, input);
540     }
541
542     /**
543      * @param input
544      * @return
545      */
546     protected static NotificationComposer<GroupUpdated> createGroupUpdatedNotification(
547             final UpdateGroupInput input) {
548         return new NotificationComposer<GroupUpdated>() {
549             @Override
550             public GroupUpdated compose(TransactionId tXid) {
551                 GroupUpdatedBuilder groupMod = new GroupUpdatedBuilder(input.getUpdatedGroup());
552                 groupMod.setTransactionId(tXid);
553                 groupMod.setGroupRef(input.getGroupRef());
554                 return groupMod.build();
555             }
556         };
557     }
558
559     /**
560      * @param taskContext
561      * @param input
562      * @param cookie
563      * @return update meter task
564      */
565     public static OFRpcTask<UpdateMeterInput, RpcResult<UpdateMeterOutput>> createUpdateMeterTask(
566             OFRpcTaskContext taskContext, UpdateMeterInput input,
567             SwitchConnectionDistinguisher cookie) {
568         class OFRpcTaskImpl extends OFRpcTask<UpdateMeterInput, RpcResult<UpdateMeterOutput>> {
569
570             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
571                     UpdateMeterInput input) {
572                 super(taskContext, cookie, input);
573             }
574
575             @Override
576             public ListenableFuture<RpcResult<UpdateMeterOutput>> call() {
577                 ListenableFuture<RpcResult<UpdateMeterOutput>> result = null;
578
579                 // Convert the UpdateMeterInput to MeterModInput
580                 MeterModInputBuilder ofMeterModInput = MeterConvertor.toMeterModInput(
581                         getInput().getUpdatedMeter(), getVersion());
582                 final Long xId = getSession().getNextXid();
583                 ofMeterModInput.setXid(xId);
584
585                 Future<RpcResult<UpdateMeterOutput>> resultFromOFLib =
586                         getMessageService().meterMod(ofMeterModInput.build(), getCookie());
587                 result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
588
589                 result = OFRpcTaskUtil.chainFutureBarrier(this, result);
590                 OFRpcTaskUtil.hookFutureNotification(this, result,
591                         getRpcNotificationProviderService(), createMeterUpdatedNotification(getInput()));
592                 return result;
593             }
594         }
595
596         return new OFRpcTaskImpl(taskContext, cookie, input);
597     }
598
599     /**
600      * @param input
601      * @return
602      */
603     protected static NotificationComposer<MeterUpdated> createMeterUpdatedNotification(
604             final UpdateMeterInput input) {
605         return new NotificationComposer<MeterUpdated>() {
606             @Override
607             public MeterUpdated compose(TransactionId tXid) {
608                 MeterUpdatedBuilder meterMod = new MeterUpdatedBuilder(input.getUpdatedMeter());
609                 meterMod.setTransactionId(tXid);
610                 meterMod.setMeterRef(input.getMeterRef());
611                 return meterMod.build();
612             }
613         };
614     }
615
616
617     /**
618      * @param taskContext
619      * @param input
620      * @param cookie
621      * @return task
622      */
623     public static OFRpcTask<RemoveFlowInput, RpcResult<UpdateFlowOutput>> createRemoveFlowTask(
624             OFRpcTaskContext taskContext, RemoveFlowInput input,
625             SwitchConnectionDistinguisher cookie) {
626         class OFRpcTaskImpl extends OFRpcTask<RemoveFlowInput, RpcResult<UpdateFlowOutput>> {
627
628             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
629                     RemoveFlowInput input) {
630                 super(taskContext, cookie, input);
631             }
632
633             @Override
634             public ListenableFuture<RpcResult<UpdateFlowOutput>> call() {
635                 ListenableFuture<RpcResult<UpdateFlowOutput>> result = SettableFuture.create();
636
637                 // Convert the AddFlowInput to FlowModInput
638                 FlowModInputBuilder ofFlowModInput = FlowConvertor.toFlowModInput(getInput(),
639                         getVersion(), getSession().getFeatures().getDatapathId());
640                 final Long xId = getSession().getNextXid();
641                 ofFlowModInput.setXid(xId);
642
643                 Future<RpcResult<UpdateFlowOutput>> resultFromOFLib =
644                         getMessageService().flowMod(ofFlowModInput.build(), getCookie());
645                 result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
646
647                 result = OFRpcTaskUtil.chainFutureBarrier(this, result);
648                 OFRpcTaskUtil.hookFutureNotification(this, result,
649                         getRpcNotificationProviderService(), createFlowRemovedNotification(getInput()));
650
651                 return result;
652             }
653         }
654
655         return new OFRpcTaskImpl(taskContext, cookie, input);
656     }
657
658     /**
659      * @param input
660      * @return
661      */
662     protected static NotificationComposer<FlowRemoved> createFlowRemovedNotification(
663             final RemoveFlowInput input) {
664         return new NotificationComposer<FlowRemoved>() {
665             @Override
666             public FlowRemoved compose(TransactionId tXid) {
667                 FlowRemovedBuilder removedFlow = new FlowRemovedBuilder((Flow) input);
668                 removedFlow.setTransactionId(tXid);
669                 removedFlow.setFlowRef(input.getFlowRef());
670                 return removedFlow.build();
671             }
672         };
673     }
674
675
676     /**
677      * @param taskContext
678      * @param input
679      * @param cookie
680      * @return task
681      */
682     public static OFRpcTask<RemoveGroupInput, RpcResult<UpdateGroupOutput>> createRemoveGroupTask(
683             final OFRpcTaskContext taskContext, RemoveGroupInput input,
684             final SwitchConnectionDistinguisher cookie) {
685         class OFRpcTaskImpl extends OFRpcTask<RemoveGroupInput, RpcResult<UpdateGroupOutput>> {
686
687             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
688                     RemoveGroupInput input) {
689                 super(taskContext, cookie, input);
690             }
691
692             @Override
693             public ListenableFuture<RpcResult<UpdateGroupOutput>> call() {
694                 ListenableFuture<RpcResult<UpdateGroupOutput>> result = SettableFuture.create();
695
696                 // Convert the AddGroupInput to GroupModInput
697                 GroupModInputBuilder ofGroupModInput = GroupConvertor.toGroupModInput(getInput(),
698                         getVersion(), getSession().getFeatures().getDatapathId());
699                 final Long xId = getSession().getNextXid();
700                 ofGroupModInput.setXid(xId);
701
702                 Future<RpcResult<UpdateGroupOutput>> resultFromOFLib = getMessageService()
703                         .groupMod(ofGroupModInput.build(), getCookie());
704                 result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
705
706                 result = OFRpcTaskUtil.chainFutureBarrier(this, result);
707                 OFRpcTaskUtil.hookFutureNotification(this, result,
708                         getRpcNotificationProviderService(), createGroupRemovedNotification(getInput()));
709
710                 return result;
711             }
712         }
713
714         return new OFRpcTaskImpl(taskContext, cookie, input);
715     }
716
717     /**
718      * @param input
719      * @return
720      */
721     protected static NotificationComposer<GroupRemoved> createGroupRemovedNotification(
722             final RemoveGroupInput input) {
723         return new NotificationComposer<GroupRemoved>() {
724             @Override
725             public GroupRemoved compose(TransactionId tXid) {
726                 GroupRemovedBuilder removedGroup = new GroupRemovedBuilder((Group) input);
727                 removedGroup.setTransactionId(tXid);
728                 removedGroup.setGroupRef(input.getGroupRef());
729                 return removedGroup.build();
730             }
731         };
732     }
733
734     /**
735      * @param taskContext
736      * @param input
737      * @param cookie
738      * @return task
739      */
740     public static OFRpcTask<RemoveMeterInput, RpcResult<UpdateMeterOutput>> createRemoveMeterTask(
741             OFRpcTaskContext taskContext, RemoveMeterInput input,
742             SwitchConnectionDistinguisher cookie) {
743
744         class OFRpcTaskImpl extends OFRpcTask<RemoveMeterInput, RpcResult<UpdateMeterOutput>> {
745
746             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
747                     RemoveMeterInput input) {
748                 super(taskContext, cookie, input);
749             }
750
751             @Override
752             public ListenableFuture<RpcResult<UpdateMeterOutput>> call() {
753                 ListenableFuture<RpcResult<UpdateMeterOutput>> result = SettableFuture.create();
754
755                 // Convert the AddGroupInput to GroupModInput
756                 MeterModInputBuilder ofMeterModInput = MeterConvertor.toMeterModInput(getInput(), getVersion());
757                 final Long xId = getSession().getNextXid();
758                 ofMeterModInput.setXid(xId);
759
760                 Future<RpcResult<UpdateMeterOutput>> resultFromOFLib = getMessageService()
761                         .meterMod(ofMeterModInput.build(), getCookie());
762                 result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
763
764                 result = OFRpcTaskUtil.chainFutureBarrier(this, result);
765                 OFRpcTaskUtil.hookFutureNotification(this, result,
766                         getRpcNotificationProviderService(), createMeterRemovedNotification(getInput()));
767
768                 return result;
769             }
770         }
771
772         return new OFRpcTaskImpl(taskContext, cookie, input);
773
774     }
775
776     /**
777      * @param input
778      * @return
779      */
780     protected static NotificationComposer<MeterRemoved> createMeterRemovedNotification(
781             final RemoveMeterInput input) {
782         return new NotificationComposer<MeterRemoved>() {
783             @Override
784             public MeterRemoved compose(TransactionId tXid) {
785                 MeterRemovedBuilder meterRemoved = new MeterRemovedBuilder((Meter) input);
786                 meterRemoved.setTransactionId(tXid);
787                 meterRemoved.setMeterRef(input.getMeterRef());
788                 return meterRemoved.build();
789             }
790         };
791     }
792
793     /**
794      * @param taskContext
795      * @param input
796      * @param cookie
797      * @return task
798      */
799     public static OFRpcTask<GetAllGroupStatisticsInput, RpcResult<GetAllGroupStatisticsOutput>> createGetAllGroupStatisticsTask(
800             final OFRpcTaskContext taskContext, GetAllGroupStatisticsInput input,
801             SwitchConnectionDistinguisher cookie) {
802
803         class OFRpcTaskImpl extends OFRpcTask<GetAllGroupStatisticsInput, RpcResult<GetAllGroupStatisticsOutput>> {
804
805             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
806                     GetAllGroupStatisticsInput input) {
807                 super(taskContext, cookie, input);
808             }
809
810             @Override
811             public ListenableFuture<RpcResult<GetAllGroupStatisticsOutput>> call() {
812                 final SettableFuture<RpcResult<GetAllGroupStatisticsOutput>> result = SettableFuture.create();
813
814                 if (taskContext.getSession().getPrimaryConductor().getVersion() == OFConstants.OFP_VERSION_1_0) {
815                     RpcResult<GetAllGroupStatisticsOutput> rpcResult = RpcResultBuilder.success(
816                             new GetAllGroupStatisticsOutputBuilder().build()).build();
817
818                     return Futures.immediateFuture(rpcResult);
819                 } else {
820
821                     // Generate xid to associate it with the request
822                     final Long xid = taskContext.getSession().getNextXid();
823
824                     // Create multipart request body for fetch all the group stats
825                     MultipartRequestGroupCaseBuilder caseBuilder = new MultipartRequestGroupCaseBuilder();
826                     MultipartRequestGroupBuilder mprGroupBuild = new MultipartRequestGroupBuilder();
827                     mprGroupBuild.setGroupId(new GroupId(BinContent.intToUnsignedLong(
828                             org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731
829                                     .Group.OFPGALL.getIntValue())));
830                     caseBuilder.setMultipartRequestGroup(mprGroupBuild.build());
831
832                     // Create multipart request header
833                     MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPGROUP,
834                             taskContext, xid);
835
836                     // Set request body to main multipart request
837                     mprInput.setMultipartRequestBody(caseBuilder.build());
838
839                     // Send the request, no cookies associated, use any connection
840
841                     Future<RpcResult<Void>> resultFromOFLib = getMessageService()
842                             .multipartRequest(mprInput.build(), getCookie());
843                     ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
844
845                     Futures.addCallback(resultLib, new ResultCallback<GetAllGroupStatisticsOutput>(result) {
846                         @Override
847                         public GetAllGroupStatisticsOutput createResult() {
848                             GetAllGroupStatisticsOutputBuilder groupStatBuilder = new GetAllGroupStatisticsOutputBuilder()
849                                     .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
850                             return groupStatBuilder.build();
851                         }
852                     });
853
854                     return result;
855                 }
856             }
857         }
858
859         return new OFRpcTaskImpl(taskContext, cookie, input);
860     }
861
862     /**
863      * @param taskContext
864      * @param input
865      * @param cookie
866      * @return task
867      */
868     public static OFRpcTask<GetGroupDescriptionInput, RpcResult<GetGroupDescriptionOutput>> createGetGroupDescriptionTask(
869             final OFRpcTaskContext taskContext, GetGroupDescriptionInput input,
870             SwitchConnectionDistinguisher cookie) {
871         class OFRpcTaskImpl extends OFRpcTask<GetGroupDescriptionInput, RpcResult<GetGroupDescriptionOutput>> {
872
873             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
874                     GetGroupDescriptionInput input) {
875                 super(taskContext, cookie, input);
876             }
877
878             @Override
879             public ListenableFuture<RpcResult<GetGroupDescriptionOutput>> call()
880                     throws Exception {
881                 final SettableFuture<RpcResult<GetGroupDescriptionOutput>> result = SettableFuture.create();
882
883                 if (taskContext.getSession().getPrimaryConductor().getVersion() == OFConstants.OFP_VERSION_1_0) {
884                     RpcResult<GetGroupDescriptionOutput> rpcResult = RpcResultBuilder.success(
885                             new GetGroupDescriptionOutputBuilder().build()).build();
886                     return Futures.immediateFuture(rpcResult);
887                 } else {
888                     final Long xid = taskContext.getSession().getNextXid();
889
890                     MultipartRequestGroupDescCaseBuilder mprGroupDescCaseBuild =
891                             new MultipartRequestGroupDescCaseBuilder();
892                     MultipartRequestInputBuilder mprInput =
893                             createMultipartHeader(MultipartType.OFPMPGROUPDESC, taskContext, xid);
894                     mprInput.setMultipartRequestBody(mprGroupDescCaseBuild.build());
895                     Future<RpcResult<Void>> resultFromOFLib = getMessageService()
896                             .multipartRequest(mprInput.build(), getCookie());
897                     ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
898
899                     Futures.addCallback(resultLib, new ResultCallback<GetGroupDescriptionOutput>(result) {
900                         @Override
901                         public GetGroupDescriptionOutput createResult() {
902                             GetGroupDescriptionOutputBuilder groupStatBuilder = new GetGroupDescriptionOutputBuilder()
903                                     .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
904                             return groupStatBuilder.build();
905                         }
906                     });
907                     return result;
908                 }
909             }
910         }
911
912         return new OFRpcTaskImpl(taskContext, cookie, input);
913     }
914
915     /**
916      * @param taskContext
917      * @param input
918      * @param cookie
919      * @return task
920      */
921     public static OFRpcTask<GetGroupFeaturesInput, RpcResult<GetGroupFeaturesOutput>> createGetGroupFeaturesTask(
922             final OFRpcTaskContext taskContext, GetGroupFeaturesInput input,
923             SwitchConnectionDistinguisher cookie) {
924         class OFRpcTaskImpl extends OFRpcTask<GetGroupFeaturesInput, RpcResult<GetGroupFeaturesOutput>> {
925
926             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
927                     GetGroupFeaturesInput input) {
928                 super(taskContext, cookie, input);
929                 // TODO Auto-generated constructor stub
930             }
931
932             @Override
933             public ListenableFuture<RpcResult<GetGroupFeaturesOutput>> call()
934                     throws Exception {
935                 final SettableFuture<RpcResult<GetGroupFeaturesOutput>> result = SettableFuture.create();
936
937                 if (taskContext.getSession().getPrimaryConductor().getVersion() == OFConstants.OFP_VERSION_1_0) {
938                     RpcResult<GetGroupFeaturesOutput> rpcResult = RpcResultBuilder.success(
939                             new GetGroupFeaturesOutputBuilder().build()).build();
940                     return Futures.immediateFuture(rpcResult);
941                 } else {
942                     final Long xid = taskContext.getSession().getNextXid();
943
944                     MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild =
945                             new MultipartRequestGroupFeaturesCaseBuilder();
946                     MultipartRequestInputBuilder mprInput =
947                             createMultipartHeader(MultipartType.OFPMPGROUPFEATURES, taskContext, xid);
948                     mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
949                     Future<RpcResult<Void>> resultFromOFLib = getMessageService()
950                             .multipartRequest(mprInput.build(), getCookie());
951                     ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
952
953                     Futures.addCallback(resultLib, new ResultCallback<GetGroupFeaturesOutput>(result) {
954                         @Override
955                         public GetGroupFeaturesOutput createResult() {
956                             GetGroupFeaturesOutputBuilder groupFeatureBuilder = new GetGroupFeaturesOutputBuilder()
957                                     .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
958                             return groupFeatureBuilder.build();
959                         }
960                     });
961                     return result;
962                 }
963             }
964         }
965
966         return new OFRpcTaskImpl(taskContext, cookie, input);
967     }
968
969     /**
970      * @param taskContext
971      * @param input
972      * @param cookie
973      * @return task
974      */
975     public static OFRpcTask<GetGroupStatisticsInput, RpcResult<GetGroupStatisticsOutput>> createGetGroupStatisticsTask(
976             final OFRpcTaskContext taskContext, final GetGroupStatisticsInput input,
977             SwitchConnectionDistinguisher cookie) {
978
979         class OFRpcTaskImpl extends OFRpcTask<GetGroupStatisticsInput, RpcResult<GetGroupStatisticsOutput>> {
980
981             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
982                     GetGroupStatisticsInput input) {
983                 super(taskContext, cookie, input);
984             }
985
986             @Override
987             public ListenableFuture<RpcResult<GetGroupStatisticsOutput>> call()
988                     throws Exception {
989                 final SettableFuture<RpcResult<GetGroupStatisticsOutput>> result = SettableFuture.create();
990
991                 if (taskContext.getSession().getPrimaryConductor().getVersion() == OFConstants.OFP_VERSION_1_0) {
992                     RpcResult<GetGroupStatisticsOutput> rpcResult = RpcResultBuilder.success(
993                             new GetGroupStatisticsOutputBuilder().build()).build();
994                     return Futures.immediateFuture(rpcResult);
995                 } else {
996                     final Long xid = taskContext.getSession().getNextXid();
997
998                     MultipartRequestGroupCaseBuilder caseBuilder = new MultipartRequestGroupCaseBuilder();
999                     MultipartRequestGroupBuilder mprGroupBuild = new MultipartRequestGroupBuilder();
1000                     mprGroupBuild.setGroupId(new GroupId(input.getGroupId().getValue()));
1001                     caseBuilder.setMultipartRequestGroup(mprGroupBuild.build());
1002
1003                     MultipartRequestInputBuilder mprInput =
1004                             createMultipartHeader(MultipartType.OFPMPGROUP, taskContext, xid);
1005                     mprInput.setMultipartRequestBody(caseBuilder.build());
1006                     Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1007                             .multipartRequest(mprInput.build(), getCookie());
1008                     ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1009
1010                     Futures.addCallback(resultLib, new ResultCallback<GetGroupStatisticsOutput>(result) {
1011                         @Override
1012                         public GetGroupStatisticsOutput createResult() {
1013                             GetGroupStatisticsOutputBuilder groupStatisticsBuilder =
1014                                     new GetGroupStatisticsOutputBuilder()
1015                                             .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1016                             return groupStatisticsBuilder.build();
1017                         }
1018                     });
1019                     return result;
1020                 }
1021             }
1022         }
1023
1024         return new OFRpcTaskImpl(taskContext, cookie, input);
1025     }
1026
1027     /**
1028      * @param taskContext
1029      * @param input
1030      * @param cookie
1031      * @return task
1032      */
1033     public static OFRpcTask<GetAllMeterConfigStatisticsInput, RpcResult<GetAllMeterConfigStatisticsOutput>> createGetAllMeterConfigStatisticsTask(
1034             final OFRpcTaskContext taskContext, final GetAllMeterConfigStatisticsInput input,
1035             SwitchConnectionDistinguisher cookie) {
1036         class OFRpcTaskImpl extends OFRpcTask<GetAllMeterConfigStatisticsInput, RpcResult<GetAllMeterConfigStatisticsOutput>> {
1037
1038             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1039                     GetAllMeterConfigStatisticsInput input) {
1040                 super(taskContext, cookie, input);
1041             }
1042
1043             @Override
1044             public ListenableFuture<RpcResult<GetAllMeterConfigStatisticsOutput>> call()
1045                     throws Exception {
1046                 final SettableFuture<RpcResult<GetAllMeterConfigStatisticsOutput>> result = SettableFuture.create();
1047
1048                 if (taskContext.getSession().getPrimaryConductor().getVersion() == OFConstants.OFP_VERSION_1_0) {
1049                     RpcResult<GetAllMeterConfigStatisticsOutput> rpcResult = RpcResultBuilder.success(
1050                             new GetAllMeterConfigStatisticsOutputBuilder().build()).build();
1051                     return Futures.immediateFuture(rpcResult);
1052                 } else {
1053                     final Long xid = taskContext.getSession().getNextXid();
1054
1055                     MultipartRequestMeterConfigCaseBuilder caseBuilder =
1056                             new MultipartRequestMeterConfigCaseBuilder();
1057                     MultipartRequestMeterConfigBuilder mprMeterConfigBuild =
1058                             new MultipartRequestMeterConfigBuilder();
1059                     mprMeterConfigBuild.setMeterId(new MeterId(BinContent.intToUnsignedLong(
1060                             org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common
1061                                     .types.rev130731.Meter.OFPMALL.getIntValue())));
1062                     caseBuilder.setMultipartRequestMeterConfig(mprMeterConfigBuild.build());
1063
1064                     MultipartRequestInputBuilder mprInput =
1065                             createMultipartHeader(MultipartType.OFPMPMETERCONFIG, taskContext, xid);
1066                     mprInput.setMultipartRequestBody(caseBuilder.build());
1067                     Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1068                             .multipartRequest(mprInput.build(), getCookie());
1069                     ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1070
1071                     Futures.addCallback(resultLib, new ResultCallback<GetAllMeterConfigStatisticsOutput>(result) {
1072                         @Override
1073                         public GetAllMeterConfigStatisticsOutput createResult() {
1074                             GetAllMeterConfigStatisticsOutputBuilder allMeterConfStatBuilder =
1075                                     new GetAllMeterConfigStatisticsOutputBuilder()
1076                                             .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1077                             return allMeterConfStatBuilder.build();
1078                         }
1079                     });
1080                     return result;
1081                 }
1082             }
1083         }
1084
1085         return new OFRpcTaskImpl(taskContext, cookie, input);
1086     }
1087
1088     /**
1089      * @param taskContext
1090      * @param input
1091      * @param cookie
1092      * @return task
1093      */
1094     public static OFRpcTask<GetAllMeterStatisticsInput, RpcResult<GetAllMeterStatisticsOutput>> createGetAllMeterStatisticsTask(
1095             final OFRpcTaskContext taskContext, final GetAllMeterStatisticsInput input,
1096             SwitchConnectionDistinguisher cookie) {
1097         class OFRpcTaskImpl extends OFRpcTask<GetAllMeterStatisticsInput, RpcResult<GetAllMeterStatisticsOutput>> {
1098
1099             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1100                     GetAllMeterStatisticsInput input) {
1101                 super(taskContext, cookie, input);
1102                 // TODO Auto-generated constructor stub
1103             }
1104
1105             @Override
1106             public ListenableFuture<RpcResult<GetAllMeterStatisticsOutput>> call()
1107                     throws Exception {
1108                 final SettableFuture<RpcResult<GetAllMeterStatisticsOutput>> result = SettableFuture.create();
1109
1110                 if (taskContext.getSession().getPrimaryConductor().getVersion() == OFConstants.OFP_VERSION_1_0) {
1111                     RpcResult<GetAllMeterStatisticsOutput> rpcResult = RpcResultBuilder.success(
1112                             new GetAllMeterStatisticsOutputBuilder().build()).build();
1113                     return Futures.immediateFuture(rpcResult);
1114                 } else {
1115                     final Long xid = taskContext.getSession().getNextXid();
1116
1117                     MultipartRequestMeterCaseBuilder caseBuilder =
1118                             new MultipartRequestMeterCaseBuilder();
1119                     MultipartRequestMeterBuilder mprMeterBuild =
1120                             new MultipartRequestMeterBuilder();
1121                     mprMeterBuild.setMeterId(new MeterId(BinContent.intToUnsignedLong(
1122                             org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common
1123                                     .types.rev130731.Meter.OFPMALL.getIntValue())));
1124                     caseBuilder.setMultipartRequestMeter(mprMeterBuild.build());
1125
1126                     MultipartRequestInputBuilder mprInput =
1127                             createMultipartHeader(MultipartType.OFPMPMETER, taskContext, xid);
1128                     mprInput.setMultipartRequestBody(caseBuilder.build());
1129                     Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1130                             .multipartRequest(mprInput.build(), getCookie());
1131                     ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1132
1133                     Futures.addCallback(resultLib, new ResultCallback<GetAllMeterStatisticsOutput>(result) {
1134                         @Override
1135                         public GetAllMeterStatisticsOutput createResult() {
1136                             GetAllMeterStatisticsOutputBuilder allMeterStatBuilder =
1137                                     new GetAllMeterStatisticsOutputBuilder()
1138                                             .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1139                             return allMeterStatBuilder.build();
1140                         }
1141                     });
1142                     return result;
1143                 }
1144             }
1145         }
1146
1147         return new OFRpcTaskImpl(taskContext, cookie, input);
1148     }
1149
1150     /**
1151      * @param taskContext
1152      * @param input
1153      * @param cookie
1154      * @return task
1155      */
1156     public static OFRpcTask<GetMeterFeaturesInput, RpcResult<GetMeterFeaturesOutput>> createGetMeterFeaturesTask(
1157             final OFRpcTaskContext taskContext, final GetMeterFeaturesInput input,
1158             SwitchConnectionDistinguisher cookie) {
1159
1160         class OFRpcTaskImpl extends OFRpcTask<GetMeterFeaturesInput, RpcResult<GetMeterFeaturesOutput>> {
1161
1162             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1163                     GetMeterFeaturesInput input) {
1164                 super(taskContext, cookie, input);
1165             }
1166
1167             @Override
1168             public ListenableFuture<RpcResult<GetMeterFeaturesOutput>> call()
1169                     throws Exception {
1170                 final SettableFuture<RpcResult<GetMeterFeaturesOutput>> result = SettableFuture.create();
1171
1172                 if (taskContext.getSession().getPrimaryConductor().getVersion() == OFConstants.OFP_VERSION_1_0) {
1173                     RpcResult<GetMeterFeaturesOutput> rpcResult = RpcResultBuilder.success(
1174                             new GetMeterFeaturesOutputBuilder().build()).build();
1175                     return Futures.immediateFuture(rpcResult);
1176                 } else {
1177                     final Long xid = taskContext.getSession().getNextXid();
1178
1179                     MultipartRequestMeterFeaturesCaseBuilder mprMeterFeaturesBuild =
1180                             new MultipartRequestMeterFeaturesCaseBuilder();
1181
1182                     MultipartRequestInputBuilder mprInput =
1183                             createMultipartHeader(MultipartType.OFPMPMETERFEATURES, taskContext, xid);
1184                     mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
1185                     Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1186                             .multipartRequest(mprInput.build(), getCookie());
1187                     ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1188
1189                     Futures.addCallback(resultLib, new ResultCallback<GetMeterFeaturesOutput>(result) {
1190                         @Override
1191                         public GetMeterFeaturesOutput createResult() {
1192                             GetMeterFeaturesOutputBuilder meterFeaturesBuilder =
1193                                     new GetMeterFeaturesOutputBuilder()
1194                                             .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1195                             return meterFeaturesBuilder.build();
1196                         }
1197                     });
1198                     return result;
1199                 }
1200             }
1201         }
1202
1203         return new OFRpcTaskImpl(taskContext, cookie, input);
1204     }
1205
1206     /**
1207      * @param taskContext
1208      * @param input
1209      * @param cookie
1210      * @return task
1211      */
1212     public static OFRpcTask<GetMeterStatisticsInput, RpcResult<GetMeterStatisticsOutput>> createGetMeterStatisticsTask(
1213             final OFRpcTaskContext taskContext, final GetMeterStatisticsInput input,
1214             SwitchConnectionDistinguisher cookie) {
1215
1216         class OFRpcTaskImpl extends OFRpcTask<GetMeterStatisticsInput, RpcResult<GetMeterStatisticsOutput>> {
1217
1218             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1219                     GetMeterStatisticsInput input) {
1220                 super(taskContext, cookie, input);
1221             }
1222
1223             @Override
1224             public ListenableFuture<RpcResult<GetMeterStatisticsOutput>> call()
1225                     throws Exception {
1226                 final SettableFuture<RpcResult<GetMeterStatisticsOutput>> result = SettableFuture.create();
1227
1228                 if (taskContext.getSession().getPrimaryConductor().getVersion() == OFConstants.OFP_VERSION_1_0) {
1229                     RpcResult<GetMeterStatisticsOutput> rpcResult = RpcResultBuilder.success(
1230                             new GetMeterStatisticsOutputBuilder().build()).build();
1231                     return Futures.immediateFuture(rpcResult);
1232                 } else {
1233                     final Long xid = taskContext.getSession().getNextXid();
1234
1235                     MultipartRequestMeterCaseBuilder caseBuilder =
1236                             new MultipartRequestMeterCaseBuilder();
1237                     MultipartRequestMeterBuilder mprMeterBuild =
1238                             new MultipartRequestMeterBuilder();
1239                     mprMeterBuild.setMeterId(new MeterId(input.getMeterId().getValue()));
1240                     caseBuilder.setMultipartRequestMeter(mprMeterBuild.build());
1241
1242                     MultipartRequestInputBuilder mprInput =
1243                             createMultipartHeader(MultipartType.OFPMPMETER, taskContext, xid);
1244                     mprInput.setMultipartRequestBody(caseBuilder.build());
1245                     Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1246                             .multipartRequest(mprInput.build(), getCookie());
1247                     ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1248
1249                     Futures.addCallback(resultLib, new ResultCallback<GetMeterStatisticsOutput>(result) {
1250                         @Override
1251                         public GetMeterStatisticsOutput createResult() {
1252                             GetMeterStatisticsOutputBuilder meterStatBuilder =
1253                                     new GetMeterStatisticsOutputBuilder()
1254                                             .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1255                             return meterStatBuilder.build();
1256                         }
1257                     });
1258                     return result;
1259                 }
1260             }
1261         }
1262
1263         return new OFRpcTaskImpl(taskContext, cookie, input);
1264     }
1265
1266     /**
1267      * @param taskContext
1268      * @param input
1269      * @param cookie
1270      * @return task
1271      */
1272     public static OFRpcTask<GetAllNodeConnectorsStatisticsInput, RpcResult<GetAllNodeConnectorsStatisticsOutput>>
1273     createGetAllNodeConnectorsStatisticsTask(
1274             final OFRpcTaskContext taskContext, final GetAllNodeConnectorsStatisticsInput input,
1275             SwitchConnectionDistinguisher cookie) {
1276         class OFRpcTaskImpl extends OFRpcTask<GetAllNodeConnectorsStatisticsInput, RpcResult<GetAllNodeConnectorsStatisticsOutput>> { 
1277
1278             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1279                     GetAllNodeConnectorsStatisticsInput input) {
1280                 super(taskContext, cookie, input);
1281             }
1282
1283             @Override
1284             public ListenableFuture<RpcResult<GetAllNodeConnectorsStatisticsOutput>> call()
1285                     throws Exception {
1286                 final SettableFuture<RpcResult<GetAllNodeConnectorsStatisticsOutput>> result = SettableFuture.create();
1287
1288                 final Long xid = taskContext.getSession().getNextXid();
1289
1290                 MultipartRequestPortStatsCaseBuilder caseBuilder =
1291                         new MultipartRequestPortStatsCaseBuilder();
1292                 MultipartRequestPortStatsBuilder mprPortStatsBuilder =
1293                         new MultipartRequestPortStatsBuilder();
1294                 // Select all ports
1295                 mprPortStatsBuilder.setPortNo(OFConstants.OFPP_ANY);
1296                 caseBuilder.setMultipartRequestPortStats(mprPortStatsBuilder.build());
1297
1298                 MultipartRequestInputBuilder mprInput =
1299                         createMultipartHeader(MultipartType.OFPMPPORTSTATS, taskContext, xid);
1300                 mprInput.setMultipartRequestBody(caseBuilder.build());
1301                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1302                         .multipartRequest(mprInput.build(), getCookie());
1303                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1304
1305                 Futures.addCallback(resultLib, new ResultCallback<GetAllNodeConnectorsStatisticsOutput>(result) {
1306                     @Override
1307                     public GetAllNodeConnectorsStatisticsOutput createResult() {
1308                         GetAllNodeConnectorsStatisticsOutputBuilder allNodeConnectorStatBuilder =
1309                                 new GetAllNodeConnectorsStatisticsOutputBuilder()
1310                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1311                         return allNodeConnectorStatBuilder.build();
1312                     }
1313                 });
1314                 return result;
1315             }
1316         }
1317
1318         return new OFRpcTaskImpl(taskContext, cookie, input);
1319     }
1320
1321     /**
1322      * @param taskContext
1323      * @param input
1324      * @param cookie
1325      * @return task
1326      */
1327     public static OFRpcTask<GetNodeConnectorStatisticsInput, RpcResult<GetNodeConnectorStatisticsOutput>>
1328     createGetNodeConnectorStatisticsTask(
1329             final OFRpcTaskContext taskContext, final GetNodeConnectorStatisticsInput input,
1330             SwitchConnectionDistinguisher cookie) {
1331         class OFRpcTaskImpl extends OFRpcTask<GetNodeConnectorStatisticsInput, RpcResult<GetNodeConnectorStatisticsOutput>> {
1332
1333             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1334                     GetNodeConnectorStatisticsInput input) {
1335                 super(taskContext, cookie, input);
1336             }
1337
1338             @Override
1339             public ListenableFuture<RpcResult<GetNodeConnectorStatisticsOutput>> call()
1340                     throws Exception {
1341                 final SettableFuture<RpcResult<GetNodeConnectorStatisticsOutput>> result = SettableFuture.create();
1342
1343                 final Long xid = taskContext.getSession().getNextXid();
1344
1345                 MultipartRequestPortStatsCaseBuilder caseBuilder =
1346                         new MultipartRequestPortStatsCaseBuilder();
1347                 MultipartRequestPortStatsBuilder mprPortStatsBuilder =
1348                         new MultipartRequestPortStatsBuilder();
1349                 // Set specific port
1350                 mprPortStatsBuilder
1351                         .setPortNo(InventoryDataServiceUtil.portNumberfromNodeConnectorId(
1352                                 OpenflowVersion.get(taskContext.getSession().getFeatures().getVersion()),
1353                                 input.getNodeConnectorId()));
1354                 caseBuilder.setMultipartRequestPortStats(mprPortStatsBuilder.build());
1355
1356                 MultipartRequestInputBuilder mprInput =
1357                         createMultipartHeader(MultipartType.OFPMPPORTSTATS, taskContext, xid);
1358                 mprInput.setMultipartRequestBody(caseBuilder.build());
1359                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1360                         .multipartRequest(mprInput.build(), getCookie());
1361                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1362
1363                 Futures.addCallback(resultLib, new ResultCallback<GetNodeConnectorStatisticsOutput>(result) {
1364                     @Override
1365                     public GetNodeConnectorStatisticsOutput createResult() {
1366                         GetNodeConnectorStatisticsOutputBuilder allNodeConnectorStatBuilder =
1367                                 new GetNodeConnectorStatisticsOutputBuilder()
1368                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1369                         return allNodeConnectorStatBuilder.build();
1370                     }
1371                 });
1372                 return result;
1373             }
1374         }
1375
1376         return new OFRpcTaskImpl(taskContext, cookie, input);
1377     }
1378
1379     /**
1380      * @param taskContext
1381      * @param input
1382      * @param cookie
1383      * @return task
1384      */
1385     public static OFRpcTask<GetAllFlowStatisticsFromFlowTableInput, RpcResult<GetAllFlowStatisticsFromFlowTableOutput>>
1386     createGetAllFlowStatisticsFromFlowTableTask(
1387             final OFRpcTaskContext taskContext,
1388             final GetAllFlowStatisticsFromFlowTableInput input,
1389             SwitchConnectionDistinguisher cookie) {
1390         class OFRpcTaskImpl extends OFRpcTask<GetAllFlowStatisticsFromFlowTableInput, RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> {
1391
1392             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1393                     GetAllFlowStatisticsFromFlowTableInput input) {
1394                 super(taskContext, cookie, input);
1395             }
1396
1397             @Override
1398             public ListenableFuture<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> call() throws Exception {
1399                 final SettableFuture<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> result = SettableFuture.create();
1400
1401                 final Long xid = taskContext.getSession().getNextXid();
1402
1403                 MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
1404                 mprFlowRequestBuilder.setTableId(input.getTableId().getValue());
1405                 mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
1406                 mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1407                 mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1408                 mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1409                 FlowCreatorUtil.setWildcardedFlowMatch(taskContext.getSession()
1410                         .getPrimaryConductor().getVersion(), mprFlowRequestBuilder);
1411
1412                 MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
1413                 multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
1414
1415                 MultipartRequestInputBuilder mprInput =
1416                         createMultipartHeader(MultipartType.OFPMPFLOW, taskContext, xid);
1417                 mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
1418                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1419                         .multipartRequest(mprInput.build(), getCookie());
1420                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1421
1422                 Futures.addCallback(resultLib, new ResultCallback<GetAllFlowStatisticsFromFlowTableOutput>(result) {
1423                     @Override
1424                     public GetAllFlowStatisticsFromFlowTableOutput createResult() {
1425                         GetAllFlowStatisticsFromFlowTableOutputBuilder allFlowStatsFromFlowTableBuilder =
1426                                 new GetAllFlowStatisticsFromFlowTableOutputBuilder()
1427                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1428                         return allFlowStatsFromFlowTableBuilder.build();
1429                     }
1430                 });
1431                 return result;
1432             }
1433         }
1434
1435         return new OFRpcTaskImpl(taskContext, cookie, input);
1436     }
1437
1438     /**
1439      * @param taskContext
1440      * @param input
1441      * @param cookie
1442      * @return task
1443      */
1444     public static OFRpcTask<GetAllFlowsStatisticsFromAllFlowTablesInput, RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>>
1445     createGetAllFlowsStatisticsFromAllFlowTablesTask(
1446             final OFRpcTaskContext taskContext,
1447             final GetAllFlowsStatisticsFromAllFlowTablesInput input,
1448             SwitchConnectionDistinguisher cookie) {
1449         class OFRpcTaskImpl extends OFRpcTask<GetAllFlowsStatisticsFromAllFlowTablesInput,
1450                 RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> {
1451
1452             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1453                     GetAllFlowsStatisticsFromAllFlowTablesInput input) {
1454                 super(taskContext, cookie, input);
1455             }
1456
1457             @Override
1458             public ListenableFuture<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> call() throws Exception {
1459                 final SettableFuture<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> result = SettableFuture.create();
1460
1461                 final Long xid = taskContext.getSession().getNextXid();
1462
1463                 // Create multipart request body for fetch all the group stats
1464                 MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder =
1465                         new MultipartRequestFlowCaseBuilder();
1466                 MultipartRequestFlowBuilder mprFlowRequestBuilder =
1467                         new MultipartRequestFlowBuilder();
1468                 mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
1469                 mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
1470                 mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1471                 mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1472                 mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1473                 FlowCreatorUtil.setWildcardedFlowMatch(taskContext.getSession()
1474                         .getPrimaryConductor().getVersion(), mprFlowRequestBuilder);
1475
1476                 MultipartRequestInputBuilder mprInput =
1477                         createMultipartHeader(MultipartType.OFPMPFLOW, taskContext, xid);
1478                 multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
1479                 mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
1480                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1481                         .multipartRequest(mprInput.build(), getCookie());
1482                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1483
1484                 Futures.addCallback(resultLib, new ResultCallback<GetAllFlowsStatisticsFromAllFlowTablesOutput>(result) {
1485                     @Override
1486                     public GetAllFlowsStatisticsFromAllFlowTablesOutput createResult() {
1487                         GetAllFlowsStatisticsFromAllFlowTablesOutputBuilder allFlowStatsFromAllFlowTableBuilder =
1488                                 new GetAllFlowsStatisticsFromAllFlowTablesOutputBuilder()
1489                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1490                         return allFlowStatsFromAllFlowTableBuilder.build();
1491                     }
1492                 });
1493                 return result;
1494             }
1495         }
1496
1497         return new OFRpcTaskImpl(taskContext, cookie, input);
1498     }
1499
1500     /**
1501      * @param taskContext
1502      * @param input
1503      * @param cookie
1504      * @return task
1505      */
1506     public static OFRpcTask<GetFlowStatisticsFromFlowTableInput, RpcResult<GetFlowStatisticsFromFlowTableOutput>>
1507     createGetFlowStatisticsFromFlowTableTask(
1508             final OFRpcTaskContext taskContext,
1509             final GetFlowStatisticsFromFlowTableInput input, SwitchConnectionDistinguisher cookie) {
1510
1511         class OFRpcTaskImpl extends OFRpcTask<GetFlowStatisticsFromFlowTableInput, RpcResult<GetFlowStatisticsFromFlowTableOutput>> {
1512
1513             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1514                     GetFlowStatisticsFromFlowTableInput input) {
1515                 super(taskContext, cookie, input);
1516             }
1517
1518             @Override
1519             public ListenableFuture<RpcResult<GetFlowStatisticsFromFlowTableOutput>> call() throws Exception {
1520                 final SettableFuture<RpcResult<GetFlowStatisticsFromFlowTableOutput>> result = SettableFuture.create();
1521
1522                 final Long xid = taskContext.getSession().getNextXid();
1523
1524                 // Create multipart request body for fetch all the group stats
1525                 MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
1526                 MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
1527                 mprFlowRequestBuilder.setTableId(input.getTableId());
1528
1529                 if (input.getOutPort() != null) {
1530                     mprFlowRequestBuilder.setOutPort(input.getOutPort().longValue());
1531                 } else {
1532                     mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
1533                 }
1534
1535                 if (input.getOutGroup() != null) {
1536                     mprFlowRequestBuilder.setOutGroup(input.getOutGroup());
1537                 } else {
1538                     mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1539                 }
1540
1541                 if (input.getCookie() != null) {
1542                     mprFlowRequestBuilder.setCookie(input.getCookie().getValue());
1543                 } else {
1544                     mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1545                 }
1546
1547                 if (input.getCookieMask() != null) {
1548                     mprFlowRequestBuilder.setCookieMask(input.getCookieMask().getValue());
1549                 } else {
1550                     mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1551                 }
1552
1553                 // convert and inject match
1554                 MatchReactor.getInstance().convert(input.getMatch(), taskContext.getSession()
1555                                 .getPrimaryConductor().getVersion(), mprFlowRequestBuilder,
1556                         taskContext.getSession().getFeatures().getDatapathId());
1557
1558                 // Set request body to main multipart request
1559                 multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
1560                 MultipartRequestInputBuilder mprInput =
1561                         createMultipartHeader(MultipartType.OFPMPFLOW, taskContext, xid);
1562                 mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
1563                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1564                         .multipartRequest(mprInput.build(), getCookie());
1565                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1566
1567                 Futures.addCallback(resultLib, new ResultCallback<GetFlowStatisticsFromFlowTableOutput>(result) {
1568                     @Override
1569                     public GetFlowStatisticsFromFlowTableOutput createResult() {
1570                         GetFlowStatisticsFromFlowTableOutputBuilder flowStatsFromFlowTableBuilder =
1571                                 new GetFlowStatisticsFromFlowTableOutputBuilder()
1572                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1573                         return flowStatsFromFlowTableBuilder.build();
1574                     }
1575                 });
1576                 return result;
1577             }
1578         }
1579
1580         return new OFRpcTaskImpl(taskContext, cookie, input);
1581     }
1582
1583     /**
1584      * @param taskContext
1585      * @param input
1586      * @param cookie
1587      * @return task
1588      */
1589     public static OFRpcTask<GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput, RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>>
1590     createGetAggregateFlowStatisticsFromFlowTableForAllFlowsTask(
1591             final OFRpcTaskContext taskContext,
1592             final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input, SwitchConnectionDistinguisher cookie) {
1593         class OFRpcTaskImpl extends OFRpcTask<GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput, RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> {
1594
1595             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1596                     GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) {
1597                 super(taskContext, cookie, input);
1598             }
1599
1600             @Override
1601             public ListenableFuture<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> call() throws Exception {
1602                 final SettableFuture<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> result = SettableFuture.create();
1603
1604                 final Long xid = taskContext.getSession().getNextXid();
1605
1606                 // Create multipart request body for fetch all the group stats
1607                 MultipartRequestAggregateCaseBuilder multipartRequestAggregateCaseBuilder = new MultipartRequestAggregateCaseBuilder();
1608                 MultipartRequestAggregateBuilder mprAggregateRequestBuilder = new MultipartRequestAggregateBuilder();
1609                 mprAggregateRequestBuilder.setTableId(input.getTableId().getValue());
1610                 mprAggregateRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
1611                 mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1612                 mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1613                 mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1614
1615                 FlowCreatorUtil.setWildcardedFlowMatch(taskContext.getSession()
1616                         .getPrimaryConductor().getVersion(), mprAggregateRequestBuilder);
1617
1618                 // Set request body to main multipart request
1619                 multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder.build());
1620                 MultipartRequestInputBuilder mprInput =
1621                         createMultipartHeader(MultipartType.OFPMPAGGREGATE, taskContext, xid);
1622                 mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
1623                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1624                         .multipartRequest(mprInput.build(), getCookie());
1625                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1626
1627                 Futures.addCallback(resultLib, new ResultCallback<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>(result) {
1628                     @Override
1629                     public GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput createResult() {
1630                         GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder flowStatsFromFlowTableBuilder =
1631                                 new GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder()
1632                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1633                         return flowStatsFromFlowTableBuilder.build();
1634                     }
1635                 });
1636                 return result;
1637             }
1638         }
1639
1640         return new OFRpcTaskImpl(taskContext, cookie, input);
1641     }
1642
1643     /**
1644      * @param taskContext
1645      * @param input
1646      * @param cookie
1647      * @return task
1648      */
1649     public static OFRpcTask<GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput, RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>>
1650     createGetAggregateFlowStatisticsFromFlowTableForGivenMatchTask(
1651             final OFRpcTaskContext taskContext,
1652             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input, SwitchConnectionDistinguisher cookie) {
1653         class OFRpcTaskImpl extends OFRpcTask<GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput, RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> {
1654
1655             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1656                     GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
1657                 super(taskContext, cookie, input);
1658             }
1659
1660             @Override
1661             public ListenableFuture<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> call() throws Exception {
1662                 final SettableFuture<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> result = SettableFuture.create();
1663
1664                 final Long xid = taskContext.getSession().getNextXid();
1665
1666                 MultipartRequestAggregateCaseBuilder multipartRequestAggregateCaseBuilder = new MultipartRequestAggregateCaseBuilder();
1667                 MultipartRequestAggregateBuilder mprAggregateRequestBuilder = new MultipartRequestAggregateBuilder();
1668                 mprAggregateRequestBuilder.setTableId(input.getTableId());
1669                 mprAggregateRequestBuilder.setOutPort(input.getOutPort().longValue());
1670                 // TODO: repeating code
1671                 if (taskContext.getSession().getPrimaryConductor().getVersion() ==
1672                         OFConstants.OFP_VERSION_1_3) {
1673                     mprAggregateRequestBuilder.setCookie(input.getCookie().getValue());
1674                     mprAggregateRequestBuilder.setCookieMask(input.getCookieMask().getValue());
1675                     mprAggregateRequestBuilder.setOutGroup(input.getOutGroup());
1676                 } else {
1677                     mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1678                     mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1679                     mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1680                 }
1681
1682                 MatchReactor.getInstance().convert(input.getMatch(), taskContext.getSession()
1683                                 .getPrimaryConductor().getVersion(), mprAggregateRequestBuilder,
1684                         taskContext.getSession().getFeatures().getDatapathId());
1685
1686                 FlowCreatorUtil.setWildcardedFlowMatch(taskContext.getSession()
1687                         .getPrimaryConductor().getVersion(), mprAggregateRequestBuilder);
1688
1689                 // Set request body to main multipart request
1690                 multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder.build());
1691                 MultipartRequestInputBuilder mprInput =
1692                         createMultipartHeader(MultipartType.OFPMPAGGREGATE, taskContext, xid);
1693                 mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
1694                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1695                         .multipartRequest(mprInput.build(), getCookie());
1696                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1697
1698                 Futures.addCallback(resultLib, new ResultCallback<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>(result) {
1699                     @Override
1700                     public GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput createResult() {
1701                         GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder aggregFlowStatsFromFlowTableBuilder =
1702                                 new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder()
1703                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1704                         return aggregFlowStatsFromFlowTableBuilder.build();
1705                     }
1706                 });
1707                 return result;
1708             }
1709         }
1710
1711         return new OFRpcTaskImpl(taskContext, cookie, input);
1712     }
1713
1714     /**
1715      * @param taskContext
1716      * @param input
1717      * @param cookie
1718      * @return task
1719      */
1720     public static OFRpcTask<GetFlowTablesStatisticsInput, RpcResult<GetFlowTablesStatisticsOutput>> createGetFlowTablesStatisticsTask(
1721             final OFRpcTaskContext taskContext, final GetFlowTablesStatisticsInput input, SwitchConnectionDistinguisher cookie) {
1722
1723         class OFRpcTaskImpl extends OFRpcTask<GetFlowTablesStatisticsInput, RpcResult<GetFlowTablesStatisticsOutput>> {
1724
1725             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1726                     GetFlowTablesStatisticsInput input) {
1727                 super(taskContext, cookie, input);
1728             }
1729
1730             @Override
1731             public ListenableFuture<RpcResult<GetFlowTablesStatisticsOutput>> call() throws Exception {
1732                 final SettableFuture<RpcResult<GetFlowTablesStatisticsOutput>> result = SettableFuture.create();
1733
1734                 final Long xid = taskContext.getSession().getNextXid();
1735
1736                 // Create multipart request body for fetch all the group stats
1737                 MultipartRequestTableCaseBuilder multipartRequestTableCaseBuilder = new MultipartRequestTableCaseBuilder();
1738                 MultipartRequestTableBuilder multipartRequestTableBuilder = new MultipartRequestTableBuilder();
1739                 multipartRequestTableBuilder.setEmpty(true);
1740                 multipartRequestTableCaseBuilder.setMultipartRequestTable(multipartRequestTableBuilder.build());
1741
1742                 // Set request body to main multipart request
1743                 MultipartRequestInputBuilder mprInput =
1744                         createMultipartHeader(MultipartType.OFPMPTABLE, taskContext, xid);
1745                 mprInput.setMultipartRequestBody(multipartRequestTableCaseBuilder.build());
1746                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1747                         .multipartRequest(mprInput.build(), getCookie());
1748                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1749
1750                 Futures.addCallback(resultLib, new ResultCallback<GetFlowTablesStatisticsOutput>(result) {
1751                     @Override
1752                     public GetFlowTablesStatisticsOutput createResult() {
1753                         GetFlowTablesStatisticsOutputBuilder flowTableStatsBuilder =
1754                                 new GetFlowTablesStatisticsOutputBuilder()
1755                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1756                         return flowTableStatsBuilder.build();
1757                     }
1758                 });
1759                 return result;
1760             }
1761         }
1762
1763         return new OFRpcTaskImpl(taskContext, cookie, input);
1764     }
1765
1766     /**
1767      * @param taskContext
1768      * @param input
1769      * @param cookie
1770      * @return task
1771      */
1772     public static OFRpcTask<GetAllQueuesStatisticsFromAllPortsInput, RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> createGetAllQueuesStatisticsFromAllPortsTask(
1773             final OFRpcTaskContext taskContext, final GetAllQueuesStatisticsFromAllPortsInput input, SwitchConnectionDistinguisher cookie) {
1774
1775         class OFRpcTaskImpl extends OFRpcTask<GetAllQueuesStatisticsFromAllPortsInput, RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> {
1776
1777             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1778                     GetAllQueuesStatisticsFromAllPortsInput input) {
1779                 super(taskContext, cookie, input);
1780             }
1781
1782             @Override
1783             public ListenableFuture<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> call() throws Exception {
1784                 final SettableFuture<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> result = SettableFuture.create();
1785
1786                 final Long xid = taskContext.getSession().getNextXid();
1787
1788                 MultipartRequestQueueCaseBuilder caseBuilder = new MultipartRequestQueueCaseBuilder();
1789                 MultipartRequestQueueBuilder mprQueueBuilder = new MultipartRequestQueueBuilder();
1790                 // Select all ports
1791                 mprQueueBuilder.setPortNo(OFConstants.OFPP_ANY);
1792                 // Select all the ports
1793                 mprQueueBuilder.setQueueId(OFConstants.OFPQ_ANY);
1794                 caseBuilder.setMultipartRequestQueue(mprQueueBuilder.build());
1795
1796                 // Set request body to main multipart request
1797                 MultipartRequestInputBuilder mprInput =
1798                         createMultipartHeader(MultipartType.OFPMPQUEUE, taskContext, xid);
1799                 mprInput.setMultipartRequestBody(caseBuilder.build());
1800                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1801                         .multipartRequest(mprInput.build(), getCookie());
1802                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1803
1804                 Futures.addCallback(resultLib, new ResultCallback<GetAllQueuesStatisticsFromAllPortsOutput>(result) {
1805                     @Override
1806                     public GetAllQueuesStatisticsFromAllPortsOutput createResult() {
1807                         GetAllQueuesStatisticsFromAllPortsOutputBuilder allQueueStatsBuilder =
1808                                 new GetAllQueuesStatisticsFromAllPortsOutputBuilder()
1809                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1810                         return allQueueStatsBuilder.build();
1811                     }
1812                 });
1813                 return result;
1814             }
1815         }
1816
1817         return new OFRpcTaskImpl(taskContext, cookie, input);
1818     }
1819
1820     /**
1821      * @param taskContext
1822      * @param input
1823      * @param cookie
1824      * @return task
1825      */
1826     public static OFRpcTask<GetAllQueuesStatisticsFromGivenPortInput, RpcResult<GetAllQueuesStatisticsFromGivenPortOutput>> createGetAllQueuesStatisticsFromGivenPortTask(
1827             final OFRpcTaskContext taskContext, final GetAllQueuesStatisticsFromGivenPortInput input, SwitchConnectionDistinguisher cookie) {
1828
1829         class OFRpcTaskImpl extends OFRpcTask<GetAllQueuesStatisticsFromGivenPortInput, RpcResult<GetAllQueuesStatisticsFromGivenPortOutput>> {
1830
1831             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1832                     GetAllQueuesStatisticsFromGivenPortInput input) {
1833                 super(taskContext, cookie, input);
1834             }
1835
1836             @Override
1837             public ListenableFuture<RpcResult<GetAllQueuesStatisticsFromGivenPortOutput>> call() throws Exception {
1838                 final SettableFuture<RpcResult<GetAllQueuesStatisticsFromGivenPortOutput>> result = SettableFuture.create();
1839
1840                 final Long xid = taskContext.getSession().getNextXid();
1841
1842                 MultipartRequestQueueCaseBuilder caseBuilder = new MultipartRequestQueueCaseBuilder();
1843                 MultipartRequestQueueBuilder mprQueueBuilder = new MultipartRequestQueueBuilder();
1844                 // Select all queues
1845                 mprQueueBuilder.setQueueId(OFConstants.OFPQ_ANY);
1846                 // Select specific port
1847                 mprQueueBuilder.setPortNo(InventoryDataServiceUtil.portNumberfromNodeConnectorId(
1848                         OpenflowVersion.get(taskContext.getSession().getFeatures().getVersion()),
1849                         input.getNodeConnectorId()));
1850                 caseBuilder.setMultipartRequestQueue(mprQueueBuilder.build());
1851
1852                 // Set request body to main multipart request
1853                 MultipartRequestInputBuilder mprInput =
1854                         createMultipartHeader(MultipartType.OFPMPQUEUE, taskContext, xid);
1855                 mprInput.setMultipartRequestBody(caseBuilder.build());
1856                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1857                         .multipartRequest(mprInput.build(), getCookie());
1858                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1859
1860                 Futures.addCallback(resultLib, new ResultCallback<GetAllQueuesStatisticsFromGivenPortOutput>(result) {
1861                     @Override
1862                     public GetAllQueuesStatisticsFromGivenPortOutput createResult() {
1863                         GetAllQueuesStatisticsFromGivenPortOutputBuilder allQueueStatsBuilder =
1864                                 new GetAllQueuesStatisticsFromGivenPortOutputBuilder()
1865                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1866                         return allQueueStatsBuilder.build();
1867                     }
1868                 });
1869                 return result;
1870             }
1871         }
1872
1873         return new OFRpcTaskImpl(taskContext, cookie, input);
1874     }
1875
1876     /**
1877      * @param taskContext
1878      * @param input
1879      * @param cookie
1880      * @return task
1881      */
1882     public static OFRpcTask<GetQueueStatisticsFromGivenPortInput, RpcResult<GetQueueStatisticsFromGivenPortOutput>> createGetQueueStatisticsFromGivenPortTask(
1883             final OFRpcTaskContext taskContext, final GetQueueStatisticsFromGivenPortInput input, SwitchConnectionDistinguisher cookie) {
1884
1885         class OFRpcTaskImpl extends OFRpcTask<GetQueueStatisticsFromGivenPortInput, RpcResult<GetQueueStatisticsFromGivenPortOutput>> {
1886
1887             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1888                     GetQueueStatisticsFromGivenPortInput input) {
1889                 super(taskContext, cookie, input);
1890             }
1891
1892             @Override
1893             public ListenableFuture<RpcResult<GetQueueStatisticsFromGivenPortOutput>> call() throws Exception {
1894                 final SettableFuture<RpcResult<GetQueueStatisticsFromGivenPortOutput>> result = SettableFuture.create();
1895
1896                 final Long xid = taskContext.getSession().getNextXid();
1897
1898                 MultipartRequestQueueCaseBuilder caseBuilder = new MultipartRequestQueueCaseBuilder();
1899                 MultipartRequestQueueBuilder mprQueueBuilder = new MultipartRequestQueueBuilder();
1900                 // Select specific queue
1901                 mprQueueBuilder.setQueueId(input.getQueueId().getValue());
1902                 // Select specific port
1903                 mprQueueBuilder.setPortNo(InventoryDataServiceUtil.portNumberfromNodeConnectorId(
1904                         OpenflowVersion.get(taskContext.getSession().getFeatures().getVersion()),
1905                         input.getNodeConnectorId()));
1906                 caseBuilder.setMultipartRequestQueue(mprQueueBuilder.build());
1907
1908                 // Set request body to main multipart request
1909                 MultipartRequestInputBuilder mprInput =
1910                         createMultipartHeader(MultipartType.OFPMPQUEUE, taskContext, xid);
1911                 mprInput.setMultipartRequestBody(caseBuilder.build());
1912                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
1913                         .multipartRequest(mprInput.build(), getCookie());
1914                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
1915
1916                 Futures.addCallback(resultLib, new ResultCallback<GetQueueStatisticsFromGivenPortOutput>(result) {
1917                     @Override
1918                     public GetQueueStatisticsFromGivenPortOutput createResult() {
1919                         GetQueueStatisticsFromGivenPortOutputBuilder queueStatsFromPortBuilder =
1920                                 new GetQueueStatisticsFromGivenPortOutputBuilder()
1921                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
1922                         return queueStatsFromPortBuilder.build();
1923                     }
1924                 });
1925                 return result;
1926             }
1927         }
1928
1929         return new OFRpcTaskImpl(taskContext, cookie, input);
1930     }
1931
1932     static MultipartRequestInputBuilder createMultipartHeader(MultipartType multipart,
1933                                                               OFRpcTaskContext taskContext, Long xid) {
1934         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1935         mprInput.setType(multipart);
1936         mprInput.setVersion(taskContext.getSession().getPrimaryConductor().getVersion());
1937         mprInput.setXid(xid);
1938         mprInput.setFlags(new MultipartRequestFlags(false));
1939         return mprInput;
1940     }
1941
1942     private abstract static class ResultCallback<T> implements FutureCallback<RpcResult<Void>> {
1943
1944         private SettableFuture<RpcResult<T>> result;
1945
1946         /**
1947          * @param result
1948          */
1949         public ResultCallback(SettableFuture<RpcResult<T>> result) {
1950             this.result = result;
1951         }
1952
1953         public abstract T createResult();
1954
1955         @Override
1956         public void onSuccess(RpcResult<Void> resultArg) {
1957             result.set(RpcResultBuilder.success(createResult()).build());
1958         }
1959
1960         @Override
1961         public void onFailure(Throwable t) {
1962             result.set(RpcResultBuilder.<T>failed().withWarning(
1963                     ErrorType.RPC,
1964                     OFConstants.ERROR_TAG_TIMEOUT,
1965                     "something wrong happened",
1966                     OFConstants.APPLICATION_TAG,
1967                     "", t).build());
1968         }
1969     }
1970
1971     /**
1972      * @param taskContext
1973      * @param input
1974      * @param cookie
1975      * @return task
1976      */
1977     public static OFRpcTask<UpdatePortInput, RpcResult<UpdatePortOutput>> createUpdatePortTask(
1978             final OFRpcTaskContext taskContext, final UpdatePortInput input,
1979             final SwitchConnectionDistinguisher cookie) {
1980
1981         class OFRpcTaskImpl extends OFRpcTask<UpdatePortInput, RpcResult<UpdatePortOutput>> {
1982
1983             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
1984                     UpdatePortInput input) {
1985                 super(taskContext, cookie, input);
1986             }
1987
1988             @Override
1989             public ListenableFuture<RpcResult<UpdatePortOutput>> call() {
1990                 ListenableFuture<RpcResult<UpdatePortOutput>> result = SettableFuture.create();
1991                 final Long xid = taskContext.getSession().getNextXid();
1992                 Port inputPort = input.getUpdatedPort().getPort().getPort().get(0);
1993
1994                 PortModInput ofPortModInput = PortConvertor.toPortModInput(inputPort,
1995                         taskContext.getSession().getPrimaryConductor().getVersion());
1996
1997                 PortModInputBuilder mdInput = new PortModInputBuilder(ofPortModInput);
1998                 mdInput.setXid(xid);
1999
2000                 Future<RpcResult<UpdatePortOutput>> resultFromOFLib = getMessageService()
2001                         .portMod(mdInput.build(), cookie);
2002                 result = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
2003
2004                 return result;
2005             }
2006         }
2007
2008         return new OFRpcTaskImpl(taskContext, cookie, input);
2009     }
2010
2011     /**
2012      * @param taskContext
2013      * @param input
2014      * @param cookie
2015      * @return task
2016      */
2017     public static OFRpcTask<UpdateTableInput, RpcResult<UpdateTableOutput>> createUpdateTableTask(
2018             final OFRpcTaskContext taskContext, final UpdateTableInput input,
2019             final SwitchConnectionDistinguisher cookie) {
2020
2021         class OFRpcTaskImpl extends OFRpcTask<UpdateTableInput, RpcResult<UpdateTableOutput>> {
2022
2023             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
2024                     UpdateTableInput input) {
2025                 super(taskContext, cookie, input);
2026             }
2027
2028             @Override
2029             public ListenableFuture<RpcResult<UpdateTableOutput>> call() {
2030                 final SettableFuture<RpcResult<UpdateTableOutput>> result = SettableFuture.create();
2031
2032                 final Long xid = taskContext.getSession().getNextXid();
2033
2034                 MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
2035                 MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
2036                 List<TableFeatures> ofTableFeatureList = TableFeaturesConvertor
2037                         .toTableFeaturesRequest(input.getUpdatedTable());
2038                 requestBuilder.setTableFeatures(ofTableFeatureList);
2039                 caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
2040
2041                 // Set request body to main multipart request
2042                 MultipartRequestInputBuilder mprInput =
2043                         createMultipartHeader(MultipartType.OFPMPTABLEFEATURES, taskContext, xid);
2044                 mprInput.setMultipartRequestBody(caseBuilder.build());
2045
2046                 Future<RpcResult<Void>> resultFromOFLib = getMessageService()
2047                         .multipartRequest(mprInput.build(), getCookie());
2048                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(resultFromOFLib);
2049
2050                 Futures.addCallback(resultLib, new ResultCallback<UpdateTableOutput>(result) {
2051                     @Override
2052                     public UpdateTableOutput createResult() {
2053                         UpdateTableOutputBuilder queueStatsFromPortBuilder =
2054                                 new UpdateTableOutputBuilder()
2055                                         .setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
2056                         return queueStatsFromPortBuilder.build();
2057                     }
2058                 });
2059                 return result;
2060             }
2061         }
2062
2063         return new OFRpcTaskImpl(taskContext, cookie, input);
2064     }
2065
2066     public static OFRpcTask<SetConfigInput, RpcResult<SetConfigOutput>> createSetNodeConfigTask(final OFRpcTaskContext taskContext,
2067                                                                                                 final SetConfigInput input,
2068                                                                                                 final SwitchConnectionDistinguisher cookie) {
2069         class OFRpcTaskImpl extends OFRpcTask<SetConfigInput, RpcResult<SetConfigOutput>> {
2070
2071             public OFRpcTaskImpl(OFRpcTaskContext taskContext, SwitchConnectionDistinguisher cookie,
2072                     SetConfigInput input) {
2073                 super(taskContext, cookie, input);
2074             }
2075
2076             @Override
2077             public ListenableFuture<RpcResult<SetConfigOutput>> call() throws Exception {
2078
2079                 final SettableFuture<RpcResult<SetConfigOutput>> result = SettableFuture.create();
2080                 final Long xid = taskContext.getSession().getNextXid();
2081
2082                 SetConfigInputBuilder builder = new SetConfigInputBuilder();
2083                 SwitchConfigFlag flag = SwitchConfigFlag.valueOf(input.getFlag());
2084                 builder.setXid(xid);
2085                 builder.setFlags(flag);
2086                 builder.setMissSendLen(input.getMissSearchLength());
2087                 builder.setVersion(getVersion());
2088                 ListenableFuture<RpcResult<Void>> resultLib = JdkFutureAdapters.listenInPoolThread(taskContext.getSession().getPrimaryConductor().getConnectionAdapter().setConfig(builder.build()));
2089                 Futures.addCallback(resultLib, new ResultCallback<SetConfigOutput>(result) {
2090                     @Override
2091                     public SetConfigOutput createResult() {
2092                         SetConfigOutputBuilder setConfigOutputBuilder = new SetConfigOutputBuilder();
2093                         setConfigOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
2094                         return setConfigOutputBuilder.build();
2095                     }
2096                 });
2097                 return result;
2098             }
2099         }
2100
2101         return new OFRpcTaskImpl(taskContext, cookie, input);
2102     }
2103 }