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