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