Bug 5540 - PacketOutConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / ModelDrivenSwitchImpl.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.openflow.md.core.sal;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.math.BigInteger;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
18 import org.opendaylight.openflowplugin.api.OFConstants;
19 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PacketOutConvertor;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.PacketOutConvertorData;
25 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
26 import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesOutput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsOutput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInput;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesInput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsInput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsOutput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigOutput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCaseBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortDescCaseBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.ConnectionCookie;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortInput;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortOutput;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInput;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutput;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsInput;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsOutput;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsInput;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutput;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortInput;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortOutput;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortInput;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortOutput;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
101 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
102 import org.opendaylight.yangtools.yang.common.RpcResult;
103 import org.slf4j.Logger;
104
105 /**
106  * RPC implementation of MD-switch
107  */
108 public class ModelDrivenSwitchImpl extends AbstractModelDrivenSwitch {
109
110     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(ModelDrivenSwitchImpl.class);
111     private final NodeId nodeId;
112     private final IMessageDispatchService messageService;
113     private short version = 0;
114     private OFRpcTaskContext rpcTaskContext;
115
116     // TODO:read timeout from configSubsystem
117     protected long maxTimeout = 1000;
118     protected TimeUnit maxTimeoutUnit = TimeUnit.MILLISECONDS;
119
120     protected ModelDrivenSwitchImpl(final NodeId nodeId, final InstanceIdentifier<Node> identifier,
121                                     final SessionContext sessionContext) {
122         super(identifier, sessionContext);
123         this.nodeId = nodeId;
124         messageService = sessionContext.getMessageDispatchService();
125         version = sessionContext.getPrimaryConductor().getVersion();
126         final NotificationProviderService rpcNotificationProviderService = OFSessionUtil.getSessionManager().getNotificationProviderService();
127
128         rpcTaskContext = new OFRpcTaskContext();
129         rpcTaskContext.setSession(sessionContext);
130         rpcTaskContext.setMessageService(messageService);
131         rpcTaskContext.setRpcNotificationProviderService(rpcNotificationProviderService);
132         rpcTaskContext.setMaxTimeout(maxTimeout);
133         rpcTaskContext.setMaxTimeoutUnit(maxTimeoutUnit);
134         rpcTaskContext.setRpcPool(OFSessionUtil.getSessionManager().getRpcPool());
135         rpcTaskContext.setMessageSpy(OFSessionUtil.getSessionManager().getMessageSpy());
136
137     }
138
139     @Override
140     public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
141         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
142         // use primary connection
143         SwitchConnectionDistinguisher cookie = null;
144
145         OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> task =
146                 OFRpcTaskFactory.createAddFlowTask(rpcTaskContext, input, cookie);
147         ListenableFuture<RpcResult<UpdateFlowOutput>> result = task.submit();
148
149         return Futures.transform(result, OFRpcFutureResultTransformFactory.createForAddFlowOutput());
150     }
151
152
153     @Override
154     public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
155         LOG.debug("Calling the GroupMod RPC method on MessageDispatchService");
156
157         // use primary connection
158         SwitchConnectionDistinguisher cookie = null;
159
160         OFRpcTask<AddGroupInput, RpcResult<UpdateGroupOutput>> task =
161                 OFRpcTaskFactory.createAddGroupTask(rpcTaskContext, input, cookie);
162         ListenableFuture<RpcResult<UpdateGroupOutput>> result = task.submit();
163
164         return Futures.transform(result, OFRpcFutureResultTransformFactory.createForAddGroupOutput());
165     }
166
167     @Override
168     public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
169         LOG.debug("Calling the MeterMod RPC method on MessageDispatchService");
170
171         // use primary connection
172         SwitchConnectionDistinguisher cookie = null;
173
174         OFRpcTask<AddMeterInput, RpcResult<UpdateMeterOutput>> task =
175                 OFRpcTaskFactory.createAddMeterTask(rpcTaskContext, input, cookie);
176         ListenableFuture<RpcResult<UpdateMeterOutput>> result = task.submit();
177
178         return Futures.transform(result, OFRpcFutureResultTransformFactory.createForAddMeterOutput());
179     }
180
181     @Override
182     public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
183         LOG.debug("Calling the removeFlow RPC method on MessageDispatchService");
184
185         // use primary connection
186         SwitchConnectionDistinguisher cookie = null;
187         OFRpcTask<RemoveFlowInput, RpcResult<UpdateFlowOutput>> task =
188                 OFRpcTaskFactory.createRemoveFlowTask(rpcTaskContext, input, cookie);
189         ListenableFuture<RpcResult<UpdateFlowOutput>> result = task.submit();
190
191         return Futures.transform(result, OFRpcFutureResultTransformFactory.createForRemoveFlowOutput());
192     }
193
194     @Override
195     public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
196         LOG.debug("Calling the Remove Group RPC method on MessageDispatchService");
197
198         SwitchConnectionDistinguisher cookie = null;
199         OFRpcTask<RemoveGroupInput, RpcResult<UpdateGroupOutput>> task =
200                 OFRpcTaskFactory.createRemoveGroupTask(rpcTaskContext, input, cookie);
201         ListenableFuture<RpcResult<UpdateGroupOutput>> result = task.submit();
202
203         return Futures.transform(result, OFRpcFutureResultTransformFactory.createForRemoveGroupOutput());
204     }
205
206     @Override
207     public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
208         LOG.debug("Calling the Remove MeterMod RPC method on MessageDispatchService");
209
210         SwitchConnectionDistinguisher cookie = null;
211         OFRpcTask<RemoveMeterInput, RpcResult<UpdateMeterOutput>> task =
212                 OFRpcTaskFactory.createRemoveMeterTask(rpcTaskContext, input, cookie);
213         ListenableFuture<RpcResult<UpdateMeterOutput>> result = task.submit();
214
215         return Futures.transform(result, OFRpcFutureResultTransformFactory.createForRemoveMeterOutput());
216     }
217
218     @Override
219     public Future<RpcResult<Void>> transmitPacket(final TransmitPacketInput input) {
220         LOG.debug("TransmitPacket - {}", input);
221         // Convert TransmitPacket to PacketOutInput
222         final PacketOutConvertorData data = new PacketOutConvertorData(version);
223         data.setDatapathId(sessionContext.getFeatures().getDatapathId());
224         data.setXid(sessionContext.getNextXid());
225
226         final java.util.Optional<PacketOutInput> message = ConvertorManager.getInstance().convert(input, data);
227
228         SwitchConnectionDistinguisher cookie = null;
229         ConnectionCookie connectionCookie = input.getConnectionCookie();
230         if (connectionCookie != null && connectionCookie.getValue() != null) {
231             cookie = new SwitchConnectionCookieOFImpl(connectionCookie.getValue());
232         }
233
234         LOG.debug("Calling the transmitPacket RPC method");
235         return messageService.packetOut(message
236                 .orElse(PacketOutConvertor.defaultResult(version)), cookie);
237     }
238
239     @Override
240     public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
241         LOG.debug("Calling the updateFlow RPC method on MessageDispatchService");
242
243         // use primary connection
244         SwitchConnectionDistinguisher cookie = null;
245         final ReadWriteTransaction rwTx = OFSessionUtil.getSessionManager().getDataBroker().newReadWriteTransaction();
246         OFRpcTask<UpdateFlowInput, RpcResult<UpdateFlowOutput>> task =
247                 OFRpcTaskFactory.createUpdateFlowTask(rpcTaskContext, input, cookie, rwTx);
248         ListenableFuture<RpcResult<UpdateFlowOutput>> result = task.submit();
249
250         return result;
251     }
252
253     @Override
254     public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
255         LOG.debug("Calling the update Group Mod RPC method on MessageDispatchService");
256
257         // use primary connection
258         SwitchConnectionDistinguisher cookie = null;
259
260         OFRpcTask<UpdateGroupInput, RpcResult<UpdateGroupOutput>> task =
261                 OFRpcTaskFactory.createUpdateGroupTask(rpcTaskContext, input, cookie);
262         ListenableFuture<RpcResult<UpdateGroupOutput>> result = task.submit();
263
264         return result;
265     }
266
267     @Override
268     public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
269         LOG.debug("Calling the MeterMod RPC method on MessageDispatchService");
270
271         // use primary connection
272         SwitchConnectionDistinguisher cookie = null;
273
274         OFRpcTask<UpdateMeterInput, RpcResult<UpdateMeterOutput>> task =
275                 OFRpcTaskFactory.createUpdateMeterTask(rpcTaskContext, input, cookie);
276         ListenableFuture<RpcResult<UpdateMeterOutput>> result = task.submit();
277
278         return result;
279     }
280
281     @Override
282     public NodeId getNodeId() {
283         return nodeId;
284     }
285
286
287     @Override
288     public Future<RpcResult<GetAllGroupStatisticsOutput>> getAllGroupStatistics(final GetAllGroupStatisticsInput input) {
289         // use primary connection
290         LOG.debug("Calling the getAllGroupStatistics RPC method on MessageDispatchService");
291         SwitchConnectionDistinguisher cookie = null;
292
293         OFRpcTask<GetAllGroupStatisticsInput, RpcResult<GetAllGroupStatisticsOutput>> task =
294                 OFRpcTaskFactory.createGetAllGroupStatisticsTask(rpcTaskContext, input, cookie);
295         ListenableFuture<RpcResult<GetAllGroupStatisticsOutput>> result = task.submit();
296
297         return result;
298
299     }
300
301     @Override
302     public Future<RpcResult<GetGroupDescriptionOutput>> getGroupDescription(final GetGroupDescriptionInput input) {
303         LOG.debug("Calling the getGroupDescription RPC method on MessageDispatchService");
304
305         OFRpcTask<GetGroupDescriptionInput, RpcResult<GetGroupDescriptionOutput>> task =
306                 OFRpcTaskFactory.createGetGroupDescriptionTask(rpcTaskContext, input, null);
307         return task.submit();
308     }
309
310     @Override
311     public Future<RpcResult<GetGroupFeaturesOutput>> getGroupFeatures(final GetGroupFeaturesInput input) {
312         LOG.debug("Calling the getGroupFeatures RPC method on MessageDispatchService");
313
314         OFRpcTask<GetGroupFeaturesInput, RpcResult<GetGroupFeaturesOutput>> task =
315                 OFRpcTaskFactory.createGetGroupFeaturesTask(rpcTaskContext, input, null);
316         return task.submit();
317     }
318
319     @Override
320     public Future<RpcResult<GetGroupStatisticsOutput>> getGroupStatistics(final GetGroupStatisticsInput input) {
321         LOG.debug("Calling the getGroupStatistics RPC method on MessageDispatchService");
322
323         OFRpcTask<GetGroupStatisticsInput, RpcResult<GetGroupStatisticsOutput>> task =
324                 OFRpcTaskFactory.createGetGroupStatisticsTask(rpcTaskContext, input, null);
325         return task.submit();
326     }
327
328     @Override
329     public Future<RpcResult<GetAllMeterConfigStatisticsOutput>> getAllMeterConfigStatistics(
330             final GetAllMeterConfigStatisticsInput input) {
331         LOG.debug("Calling the getAllMeterConfigStatistics RPC method on MessageDispatchService");
332
333         OFRpcTask<GetAllMeterConfigStatisticsInput, RpcResult<GetAllMeterConfigStatisticsOutput>> task =
334                 OFRpcTaskFactory.createGetAllMeterConfigStatisticsTask(rpcTaskContext, input, null);
335         return task.submit();
336     }
337
338     @Override
339     public Future<RpcResult<GetAllMeterStatisticsOutput>> getAllMeterStatistics(
340             final GetAllMeterStatisticsInput input) {
341         LOG.debug("Calling the getAllMeterStatistics RPC method on MessageDispatchService");
342
343         OFRpcTask<GetAllMeterStatisticsInput, RpcResult<GetAllMeterStatisticsOutput>> task =
344                 OFRpcTaskFactory.createGetAllMeterStatisticsTask(rpcTaskContext, input, null);
345         return task.submit();
346     }
347
348     @Override
349     public Future<RpcResult<GetMeterFeaturesOutput>> getMeterFeatures(
350             final GetMeterFeaturesInput input) {
351         LOG.debug("Calling the getMeterFeatures RPC method on MessageDispatchService");
352
353         OFRpcTask<GetMeterFeaturesInput, RpcResult<GetMeterFeaturesOutput>> task =
354                 OFRpcTaskFactory.createGetMeterFeaturesTask(rpcTaskContext, input, null);
355         return task.submit();
356     }
357
358     @Override
359     public Future<RpcResult<GetMeterStatisticsOutput>> getMeterStatistics(
360             final GetMeterStatisticsInput input) {
361         LOG.debug("Calling the getMeterStatistics RPC method on MessageDispatchService");
362
363         OFRpcTask<GetMeterStatisticsInput, RpcResult<GetMeterStatisticsOutput>> task =
364                 OFRpcTaskFactory.createGetMeterStatisticsTask(rpcTaskContext, input, null);
365         return task.submit();
366     }
367
368     @Override
369     public Future<RpcResult<GetAllNodeConnectorsStatisticsOutput>> getAllNodeConnectorsStatistics(
370             final GetAllNodeConnectorsStatisticsInput input) {
371         LOG.debug("Calling the getAllNodeConnectorsStatistics RPC method on MessageDispatchService");
372
373         OFRpcTask<GetAllNodeConnectorsStatisticsInput, RpcResult<GetAllNodeConnectorsStatisticsOutput>> task =
374                 OFRpcTaskFactory.createGetAllNodeConnectorsStatisticsTask(rpcTaskContext, input, null);
375         return task.submit();
376     }
377
378     @Override
379     public Future<RpcResult<GetNodeConnectorStatisticsOutput>> getNodeConnectorStatistics(
380             final GetNodeConnectorStatisticsInput input) {
381         LOG.debug("Calling the getNodeConnectorStatistics RPC method on MessageDispatchService");
382
383         OFRpcTask<GetNodeConnectorStatisticsInput, RpcResult<GetNodeConnectorStatisticsOutput>> task =
384                 OFRpcTaskFactory.createGetNodeConnectorStatisticsTask(rpcTaskContext, input, null);
385         return task.submit();
386     }
387
388     @Override
389     public Future<RpcResult<UpdatePortOutput>> updatePort(final UpdatePortInput input) {
390         LOG.debug("Calling the updatePort RPC method on MessageDispatchService");
391
392         OFRpcTask<UpdatePortInput, RpcResult<UpdatePortOutput>> task =
393                 OFRpcTaskFactory.createUpdatePortTask(rpcTaskContext, input, null);
394         return task.submit();
395     }
396
397     @Override
398     public Future<RpcResult<UpdateTableOutput>> updateTable(final UpdateTableInput input) {
399         LOG.debug("Calling the updateTable RPC method on MessageDispatchService");
400
401         OFRpcTask<UpdateTableInput, RpcResult<UpdateTableOutput>> task =
402                 OFRpcTaskFactory.createUpdateTableTask(rpcTaskContext, input, null);
403         return task.submit();
404     }
405
406     @Override
407     public Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> getAllFlowStatisticsFromFlowTable(
408             final GetAllFlowStatisticsFromFlowTableInput input) {
409         LOG.debug("Calling the getAllFlowStatisticsFromFlowTable RPC method on MessageDispatchService");
410
411         OFRpcTask<GetAllFlowStatisticsFromFlowTableInput, RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> task =
412                 OFRpcTaskFactory.createGetAllFlowStatisticsFromFlowTableTask(rpcTaskContext, input, null);
413         return task.submit();
414     }
415
416     @Override
417     public Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> getAllFlowsStatisticsFromAllFlowTables(
418             final GetAllFlowsStatisticsFromAllFlowTablesInput input) {
419         LOG.debug("Calling the getAllFlowsStatisticsFromAllFlowTables RPC method on MessageDispatchService");
420
421         OFRpcTask<GetAllFlowsStatisticsFromAllFlowTablesInput, RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> task =
422                 OFRpcTaskFactory.createGetAllFlowsStatisticsFromAllFlowTablesTask(rpcTaskContext, input, null);
423         return task.submit();
424     }
425
426     @Override
427     public Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> getFlowStatisticsFromFlowTable(
428             final GetFlowStatisticsFromFlowTableInput input) {
429         LOG.debug("Calling the getFlowStatisticsFromFlowTable RPC method on MessageDispatchService");
430
431         OFRpcTask<GetFlowStatisticsFromFlowTableInput, RpcResult<GetFlowStatisticsFromFlowTableOutput>> task =
432                 OFRpcTaskFactory.createGetFlowStatisticsFromFlowTableTask(rpcTaskContext, input, null);
433         return task.submit();
434     }
435
436     @Override
437     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> getAggregateFlowStatisticsFromFlowTableForAllFlows(
438             final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input) {
439         LOG.debug("Calling the getAggregateFlowStatisticsFromFlowTableForAllFlows RPC method on MessageDispatchService");
440
441         OFRpcTask<GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput, RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> task =
442                 OFRpcTaskFactory.createGetAggregateFlowStatisticsFromFlowTableForAllFlowsTask(rpcTaskContext, input, null);
443         return task.submit();
444     }
445
446     @Override
447     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> getAggregateFlowStatisticsFromFlowTableForGivenMatch(
448             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput input) {
449         LOG.debug("Calling the getAggregateFlowStatisticsFromFlowTableForGivenMatch RPC method on MessageDispatchService");
450
451         OFRpcTask<GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput, RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> task =
452                 OFRpcTaskFactory.createGetAggregateFlowStatisticsFromFlowTableForGivenMatchTask(rpcTaskContext, input, null);
453         return task.submit();
454     }
455
456     @Override
457     public Future<RpcResult<GetFlowTablesStatisticsOutput>> getFlowTablesStatistics(
458             final GetFlowTablesStatisticsInput input) {
459         LOG.debug("Calling the getFlowTablesStatistics RPC method on MessageDispatchService");
460
461         OFRpcTask<GetFlowTablesStatisticsInput, RpcResult<GetFlowTablesStatisticsOutput>> task =
462                 OFRpcTaskFactory.createGetFlowTablesStatisticsTask(rpcTaskContext, input, null);
463         return task.submit();
464     }
465
466     @Override
467     public Future<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> getAllQueuesStatisticsFromAllPorts(
468             final GetAllQueuesStatisticsFromAllPortsInput input) {
469         LOG.debug("Calling the getAllQueuesStatisticsFromAllPorts RPC method on MessageDispatchService");
470
471         OFRpcTask<GetAllQueuesStatisticsFromAllPortsInput, RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> task =
472                 OFRpcTaskFactory.createGetAllQueuesStatisticsFromAllPortsTask(rpcTaskContext, input, null);
473         return task.submit();
474     }
475
476     @Override
477     public Future<RpcResult<GetAllQueuesStatisticsFromGivenPortOutput>> getAllQueuesStatisticsFromGivenPort(
478             final GetAllQueuesStatisticsFromGivenPortInput input) {
479         LOG.debug("Calling the getAllQueuesStatisticsFromGivenPort RPC method on MessageDispatchService");
480
481         OFRpcTask<GetAllQueuesStatisticsFromGivenPortInput, RpcResult<GetAllQueuesStatisticsFromGivenPortOutput>> task =
482                 OFRpcTaskFactory.createGetAllQueuesStatisticsFromGivenPortTask(rpcTaskContext, input, null);
483         return task.submit();
484     }
485
486     @Override
487     public Future<RpcResult<GetQueueStatisticsFromGivenPortOutput>> getQueueStatisticsFromGivenPort(
488             final GetQueueStatisticsFromGivenPortInput input) {
489         LOG.debug("Calling the getQueueStatisticsFromGivenPort RPC method on MessageDispatchService");
490
491         OFRpcTask<GetQueueStatisticsFromGivenPortInput, RpcResult<GetQueueStatisticsFromGivenPortOutput>> task =
492                 OFRpcTaskFactory.createGetQueueStatisticsFromGivenPortTask(rpcTaskContext, input, null);
493         return task.submit();
494     }
495
496     @Override
497     public Future<RpcResult<SetConfigOutput>> setConfig(SetConfigInput input) {
498         OFRpcTask<SetConfigInput, RpcResult<SetConfigOutput>> task = OFRpcTaskFactory.createSetNodeConfigTask(rpcTaskContext, input, null);
499         return task.submit();
500     }
501     @Override
502     public Optional<BigInteger> sendEmptyTableFeatureRequest() {
503         LOG.debug("Send table feature request to {}",nodeId);
504
505         final Long xid = rpcTaskContext.getSession().getNextXid();
506
507         MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
508         MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
509         caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
510
511         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
512         mprInput.setType(MultipartType.OFPMPTABLEFEATURES);
513         mprInput.setVersion(rpcTaskContext.getSession().getPrimaryConductor().getVersion());
514         mprInput.setXid(xid);
515         mprInput.setFlags(new MultipartRequestFlags(false));
516
517         mprInput.setMultipartRequestBody(caseBuilder.build());
518
519         Future<RpcResult<Void>> resultFromOFLib = rpcTaskContext.getMessageService()
520                 .multipartRequest(mprInput.build(), null);
521
522         return Optional.of(BigInteger.valueOf(xid));
523
524     }
525
526     @Override
527     public void requestSwitchDetails(){
528         // post-handshake actions
529         if (version == OFConstants.OFP_VERSION_1_3) {
530             requestPorts();
531         }
532
533         requestDesc();
534     }
535
536     /*
537      * Send an OFPMP_DESC request message to the switch
538      */
539     private void requestDesc() {
540         MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
541         builder.setType(MultipartType.OFPMPDESC);
542         builder.setVersion(version);
543         builder.setFlags(new MultipartRequestFlags(false));
544         builder.setMultipartRequestBody(new MultipartRequestDescCaseBuilder()
545                 .build());
546         builder.setXid(getSessionContext().getNextXid());
547         rpcTaskContext.getSession().getPrimaryConductor().getConnectionAdapter().multipartRequest(builder.build());
548     }
549
550     private void requestPorts() {
551         MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
552         builder.setType(MultipartType.OFPMPPORTDESC);
553         builder.setVersion(version);
554         builder.setFlags(new MultipartRequestFlags(false));
555         builder.setMultipartRequestBody(new MultipartRequestPortDescCaseBuilder()
556                 .build());
557         builder.setXid(getSessionContext().getNextXid());
558         rpcTaskContext.getSession().getPrimaryConductor().getConnectionAdapter().multipartRequest(builder.build());
559     }
560 }