BUG-956 deadlock by rpc invocation - phase2
[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 java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.TimeUnit;
17
18 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.controller.sal.common.util.Rpcs;
20 import org.opendaylight.openflowjava.protocol.api.util.BinContent;
21 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
22 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowConvertor;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.GroupConvertor;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.MeterConvertor;
26 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PacketOutConvertor;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PortConvertor;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.TableFeaturesConvertor;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchReactor;
30 import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService;
31 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
32 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
33 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
34 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemovedBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutputBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutputBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutputBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemovedBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdatedBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutputBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutputBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutputBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInput;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutputBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesInput;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesOutput;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesOutputBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsInput;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsOutput;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsOutputBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemovedBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdatedBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutputBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutputBuilder;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInput;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutputBuilder;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInput;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutputBuilder;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesInput;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutput;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutputBuilder;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsInput;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsOutput;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsOutputBuilder;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Group;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupId;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Meter;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterId;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.MatchBuilder;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInputBuilder;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestAggregateCaseBuilder;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCaseBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupCaseBuilder;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupDescCaseBuilder;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupFeaturesCaseBuilder;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterCaseBuilder;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterConfigCaseBuilder;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCaseBuilder;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortStatsCaseBuilder;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestQueueCaseBuilder;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCaseBuilder;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.aggregate._case.MultipartRequestAggregateBuilder;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlowBuilder;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.group._case.MultipartRequestGroupBuilder;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.meter._case.MultipartRequestMeterBuilder;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.meter.config._case.MultipartRequestMeterConfigBuilder;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.port.stats._case.MultipartRequestPortStatsBuilder;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.queue._case.MultipartRequestQueueBuilder;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table._case.MultipartRequestTableBuilder;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
149 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;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortInput;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortOutput;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortOutputBuilder;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsInput;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutput;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetAllNodeConnectorsStatisticsOutputBuilder;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsInput;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsOutput;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.GetNodeConnectorStatisticsOutputBuilder;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsInput;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutput;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromAllPortsOutputBuilder;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortInput;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortOutput;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetAllQueuesStatisticsFromGivenPortOutputBuilder;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortInput;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortOutput;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.GetQueueStatisticsFromGivenPortOutputBuilder;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
172 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
173 import org.opendaylight.yangtools.yang.common.RpcError;
174 import org.opendaylight.yangtools.yang.common.RpcResult;
175 import org.slf4j.Logger;
176
177 import com.google.common.base.Objects;
178 import com.google.common.util.concurrent.Futures;
179 import com.google.common.util.concurrent.JdkFutureAdapters;
180 import com.google.common.util.concurrent.ListenableFuture;
181
182 /**
183  * RPC implementation of MD-switch
184  */
185 public class ModelDrivenSwitchImpl extends AbstractModelDrivenSwitch {
186
187     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(ModelDrivenSwitchImpl.class);
188     private final NodeId nodeId;
189     private final IMessageDispatchService messageService;
190     private short version = 0;
191     private NotificationProviderService rpcNotificationProviderService;
192     private OFRpcTaskContext rpcTaskContext;
193     
194     // TODO:read timeout from configSubsystem
195     protected long maxTimeout = 1000;
196     protected TimeUnit maxTimeoutUnit = TimeUnit.MILLISECONDS;
197     
198     protected ModelDrivenSwitchImpl(final NodeId nodeId, final InstanceIdentifier<Node> identifier, 
199             final SessionContext sessionContext) {
200         super(identifier, sessionContext);
201         this.nodeId = nodeId;
202         messageService = sessionContext.getMessageDispatchService();
203         version = sessionContext.getPrimaryConductor().getVersion();
204         rpcNotificationProviderService = OFSessionUtil.getSessionManager().getNotificationProviderService();
205         
206         rpcTaskContext = new OFRpcTaskContext();
207         rpcTaskContext.setSession(sessionContext);
208         rpcTaskContext.setMessageService(messageService);
209         rpcTaskContext.setRpcNotificationProviderService(rpcNotificationProviderService);
210         rpcTaskContext.setMaxTimeout(maxTimeout);
211         rpcTaskContext.setMaxTimeoutUnit(maxTimeoutUnit);
212         rpcTaskContext.setRpcPool(OFSessionUtil.getSessionManager().getRpcPool());
213     }
214
215     @Override
216     public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
217         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
218         // use primary connection
219         SwitchConnectionDistinguisher cookie = null;
220         
221         OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> task = 
222                 OFRpcTaskFactory.createAddFlowTask(rpcTaskContext, input, cookie);
223         ListenableFuture<RpcResult<UpdateFlowOutput>> result = task.submit();
224         
225         return Futures.transform(JdkFutureAdapters.listenInPoolThread(result), 
226                 OFRpcFutureResultTransformFactory.createForAddFlowOutput());
227     }
228
229
230     @Override
231     public Future<RpcResult<AddGroupOutput>> addGroup(final AddGroupInput input) {
232         LOG.debug("Calling the GroupMod RPC method on MessageDispatchService");
233         
234         // use primary connection
235         SwitchConnectionDistinguisher cookie = null;
236         
237         OFRpcTask<AddGroupInput, RpcResult<UpdateGroupOutput>> task = 
238                 OFRpcTaskFactory.createAddGroupTask(rpcTaskContext, input, cookie);
239         ListenableFuture<RpcResult<UpdateGroupOutput>> result = task.submit();
240         
241         return Futures.transform(JdkFutureAdapters.listenInPoolThread(result), 
242                 OFRpcFutureResultTransformFactory.createForAddGroupOutput());
243     }
244
245     @Override
246     public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
247         LOG.debug("Calling the MeterMod RPC method on MessageDispatchService");
248         
249         // use primary connection
250         SwitchConnectionDistinguisher cookie = null;
251         
252         OFRpcTask<AddMeterInput, RpcResult<UpdateMeterOutput>> task = 
253                 OFRpcTaskFactory.createAddMeterTask(rpcTaskContext, input, cookie);
254         ListenableFuture<RpcResult<UpdateMeterOutput>> result = task.submit();
255         
256         return Futures.transform(JdkFutureAdapters.listenInPoolThread(result), 
257                 OFRpcFutureResultTransformFactory.createForAddMeterOutput());
258     }
259
260     @Override
261     public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
262         LOG.debug("Calling the removeFlow RPC method on MessageDispatchService");
263         Long xId = null;
264         // For Flow provisioning, the SwitchConnectionDistinguisher is set to
265         // null so
266         // the request can be routed through any connection to the switch
267
268         SwitchConnectionDistinguisher cookie = null;
269         if (Objects.firstNonNull(input.isBarrier(), Boolean.FALSE)) {
270             BarrierInputBuilder barrierInput = new BarrierInputBuilder();
271             xId = sessionContext.getNextXid();
272             barrierInput.setXid(xId);
273             barrierInput.setVersion(version);
274             @SuppressWarnings("unused")
275             Future<RpcResult<BarrierOutput>> barrierOFLib = messageService.barrier(barrierInput.build(), cookie);
276         }
277
278         // Convert the RemoveFlowInput to FlowModInput
279         FlowModInputBuilder ofFlowModInput = FlowConvertor.toFlowModInput(input, version, sessionContext
280                 .getFeatures().getDatapathId());
281         xId = sessionContext.getNextXid();
282         ofFlowModInput.setXid(xId);
283
284         if (null != rpcNotificationProviderService) {
285             FlowRemovedBuilder removeFlow = new FlowRemovedBuilder(
286                     (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow) input);
287             removeFlow.setTransactionId(new TransactionId(BigInteger.valueOf(xId.intValue())));
288             removeFlow.setFlowRef(input.getFlowRef());
289             rpcNotificationProviderService.publish(removeFlow.build());
290         }
291
292         Future<RpcResult<UpdateFlowOutput>> resultFromOFLib = messageService.flowMod(ofFlowModInput.build(), cookie);
293
294         RpcResult<UpdateFlowOutput> rpcResultFromOFLib = null;
295
296         try {
297             rpcResultFromOFLib = resultFromOFLib.get();
298         } catch (Exception ex) {
299             LOG.error(" Error while getting result for remove Flow RPC" + ex.getMessage());
300         }
301
302         UpdateFlowOutput updateFlowOutput = rpcResultFromOFLib.getResult();
303
304         RemoveFlowOutputBuilder removeFlowOutput = new RemoveFlowOutputBuilder();
305         removeFlowOutput.setTransactionId(updateFlowOutput.getTransactionId());
306         RemoveFlowOutput result = removeFlowOutput.build();
307
308         Collection<RpcError> errors = rpcResultFromOFLib.getErrors();
309         RpcResult<RemoveFlowOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
310
311         LOG.debug("Returning the Remove Flow RPC result to MD-SAL");
312         return Futures.immediateFuture(rpcResult);
313     }
314
315     @Override
316     public Future<RpcResult<RemoveGroupOutput>> removeGroup(final RemoveGroupInput input) {
317         LOG.debug("Calling the Remove Group RPC method on MessageDispatchService");
318         Long xId = null;
319
320         // For Flow provisioning, the SwitchConnectionDistinguisher is set to
321         // null so
322         // the request can be routed through any connection to the switch
323
324         SwitchConnectionDistinguisher cookie = null;
325         if (Objects.firstNonNull(input.isBarrier(), Boolean.FALSE)) {
326             xId = sessionContext.getNextXid();
327             BarrierInputBuilder barrierInput = new BarrierInputBuilder();
328             barrierInput.setVersion(version);
329             barrierInput.setXid(xId);
330             @SuppressWarnings("unused")
331             Future<RpcResult<BarrierOutput>> barrierOFLib = messageService.barrier(barrierInput.build(), cookie);
332         }
333
334         // Convert the RemoveGroupInput to GroupModInput
335         GroupModInputBuilder ofGroupModInput = GroupConvertor.toGroupModInput(input, version, this.getSessionContext()
336                 .getFeatures().getDatapathId());
337         xId = sessionContext.getNextXid();
338         ofGroupModInput.setXid(xId);
339
340         if (null != rpcNotificationProviderService) {
341             GroupRemovedBuilder groupMod = new GroupRemovedBuilder(
342                     (org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group) input);
343             groupMod.setTransactionId(new TransactionId(BigInteger.valueOf(xId.intValue())));
344             groupMod.setGroupRef(input.getGroupRef());
345             rpcNotificationProviderService.publish(groupMod.build());
346         }
347
348         Future<RpcResult<UpdateGroupOutput>> resultFromOFLib = messageService.groupMod(ofGroupModInput.build(), cookie);
349
350         RpcResult<UpdateGroupOutput> rpcResultFromOFLib = null;
351
352         try {
353             rpcResultFromOFLib = resultFromOFLib.get();
354         } catch (Exception ex) {
355             LOG.error(" Error while getting result for RemoveGroup RPC" + ex.getMessage());
356         }
357
358         UpdateGroupOutput updateGroupOutput = rpcResultFromOFLib.getResult();
359
360         RemoveGroupOutputBuilder removeGroupOutput = new RemoveGroupOutputBuilder();
361         removeGroupOutput.setTransactionId(updateGroupOutput.getTransactionId());
362         RemoveGroupOutput result = removeGroupOutput.build();
363
364         Collection<RpcError> errors = rpcResultFromOFLib.getErrors();
365         RpcResult<RemoveGroupOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
366
367         LOG.debug("Returning the Remove Group RPC result to MD-SAL");
368         return Futures.immediateFuture(rpcResult);
369     }
370
371     @Override
372     public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
373         LOG.debug("Calling the Remove MeterMod RPC method on MessageDispatchService");
374         Long xId = null;
375
376         // For Meter provisioning, the SwitchConnectionDistinguisher is set to
377         // null so
378         // the request can be routed through any connection to the switch
379         SwitchConnectionDistinguisher cookie = null;
380         if (Objects.firstNonNull(input.isBarrier(), Boolean.FALSE)) {
381             xId = sessionContext.getNextXid();
382             BarrierInputBuilder barrierInput = new BarrierInputBuilder();
383             barrierInput.setVersion(version);
384             barrierInput.setXid(xId);
385             @SuppressWarnings("unused")
386             Future<RpcResult<BarrierOutput>> barrierOFLib = messageService.barrier(barrierInput.build(), cookie);
387         }
388
389         // Convert the RemoveMeterInput to MeterModInput
390         MeterModInputBuilder ofMeterModInput = MeterConvertor.toMeterModInput(input, version);
391         xId = sessionContext.getNextXid();
392         ofMeterModInput.setXid(xId);
393
394         if (null != rpcNotificationProviderService) {
395             MeterRemovedBuilder meterMod = new MeterRemovedBuilder(
396                     (org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter) input);
397             meterMod.setTransactionId(new TransactionId(BigInteger.valueOf(xId.intValue())));
398             meterMod.setMeterRef(input.getMeterRef());
399             rpcNotificationProviderService.publish(meterMod.build());
400         }
401
402         Future<RpcResult<UpdateMeterOutput>> resultFromOFLib = messageService.meterMod(ofMeterModInput.build(), cookie);
403
404         RpcResult<UpdateMeterOutput> rpcResultFromOFLib = null;
405
406         try {
407             rpcResultFromOFLib = resultFromOFLib.get();
408         } catch (Exception ex) {
409             LOG.error(" Error while getting result for RemoveMeter RPC" + ex.getMessage());
410         }
411
412         UpdateMeterOutput updatemeterOutput = rpcResultFromOFLib.getResult();
413
414         RemoveMeterOutputBuilder removeMeterOutput = new RemoveMeterOutputBuilder();
415         removeMeterOutput.setTransactionId(updatemeterOutput.getTransactionId());
416         RemoveMeterOutput result = removeMeterOutput.build();
417
418         Collection<RpcError> errors = rpcResultFromOFLib.getErrors();
419         RpcResult<RemoveMeterOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
420
421         LOG.debug("Returning the Remove Meter RPC result to MD-SAL");
422         return Futures.immediateFuture(rpcResult);
423     }
424
425     @Override
426     public Future<RpcResult<Void>> transmitPacket(final TransmitPacketInput input) {
427         LOG.debug("TransmitPacket - {}", input);
428         // Convert TransmitPacket to PacketOutInput
429         PacketOutInput message = PacketOutConvertor.toPacketOutInput(input, version, sessionContext.getNextXid(),
430                 sessionContext.getFeatures().getDatapathId());
431
432         // TODO VD NULL for yet - find how to translate cookie from
433         // TransmitPacketInput
434         // SwitchConnectionDistinguisher cookie = ( "what is need to do" )
435         // input.getCookie();
436         SwitchConnectionDistinguisher cookie = null;
437
438         LOG.debug("Calling the transmitPacket RPC method");
439         return messageService.packetOut(message, cookie);
440     }
441
442     private FlowModInputBuilder toFlowModInputBuilder(final Flow source) {
443         FlowModInputBuilder target = new FlowModInputBuilder();
444         target.setCookie(source.getCookie().getValue());
445         target.setIdleTimeout(source.getIdleTimeout());
446         target.setHardTimeout(source.getHardTimeout());
447         target.setMatch(toMatch(source.getMatch()));
448
449         return target;
450     }
451
452     private Match toMatch(final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match match) {
453         MatchBuilder target = new MatchBuilder();
454
455         target.setMatchEntries(toMatchEntries(match));
456
457         return null;
458     }
459
460     private List<MatchEntries> toMatchEntries(
461             final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match match) {
462         List<MatchEntries> entries = new ArrayList<>();
463
464         return null;
465     }
466
467     @Override
468     public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
469         LOG.debug("Calling the updateFlow RPC method on MessageDispatchService");
470         
471         // use primary connection
472         SwitchConnectionDistinguisher cookie = null;
473         
474         OFRpcTask<UpdateFlowInput, RpcResult<UpdateFlowOutput>> task = 
475                 OFRpcTaskFactory.createUpdateFlowTask(rpcTaskContext, input, cookie);
476         ListenableFuture<RpcResult<UpdateFlowOutput>> result = task.submit();
477         
478         return result;
479     }
480
481     @Override
482     public Future<RpcResult<UpdateGroupOutput>> updateGroup(final UpdateGroupInput input) {
483         LOG.debug("Calling the update Group Mod RPC method on MessageDispatchService");
484         
485         // use primary connection
486         SwitchConnectionDistinguisher cookie = null;
487         
488         OFRpcTask<UpdateGroupInput, RpcResult<UpdateGroupOutput>> task = 
489                 OFRpcTaskFactory.createUpdateGroupTask(rpcTaskContext, input, cookie);
490         ListenableFuture<RpcResult<UpdateGroupOutput>> result = task.submit();
491         
492         return result;
493     }
494
495     @Override
496     public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
497         LOG.debug("Calling the MeterMod RPC method on MessageDispatchService");
498         
499         // use primary connection
500         SwitchConnectionDistinguisher cookie = null;
501         
502         OFRpcTask<UpdateMeterInput, RpcResult<UpdateMeterOutput>> task = 
503                 OFRpcTaskFactory.createUpdateMeterTask(rpcTaskContext, input, cookie);
504         ListenableFuture<RpcResult<UpdateMeterOutput>> result = task.submit();
505         
506         return result;
507     }
508
509     @Override
510     public NodeId getNodeId() {
511         return nodeId;
512     }
513
514     /*
515      * Methods for requesting statistics from switch
516      */
517     @Override
518     public Future<RpcResult<GetAllGroupStatisticsOutput>> getAllGroupStatistics(final GetAllGroupStatisticsInput input) {
519
520         GetAllGroupStatisticsOutputBuilder output = new GetAllGroupStatisticsOutputBuilder();
521         Collection<RpcError> errors = Collections.emptyList();
522
523         if (version == OFConstants.OFP_VERSION_1_0) {
524             output.setTransactionId(null);
525             output.setGroupStats(null);
526
527             RpcResult<GetAllGroupStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
528             return Futures.immediateFuture(rpcResult);
529         }
530         // Generate xid to associate it with the request
531         Long xid = this.getSessionContext().getNextXid();
532
533         LOG.debug("Prepare statistics request for all the groups - Transaction id - {}", xid);
534
535         // Create multipart request header
536         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
537         mprInput.setType(MultipartType.OFPMPGROUP);
538         mprInput.setVersion(version);
539         mprInput.setXid(xid);
540         mprInput.setFlags(new MultipartRequestFlags(false));
541
542         // Create multipart request body for fetch all the group stats
543         MultipartRequestGroupCaseBuilder caseBuilder = new MultipartRequestGroupCaseBuilder();
544         MultipartRequestGroupBuilder mprGroupBuild = new MultipartRequestGroupBuilder();
545         mprGroupBuild.setGroupId(new GroupId(BinContent.intToUnsignedLong(Group.OFPGALL.getIntValue())));
546         caseBuilder.setMultipartRequestGroup(mprGroupBuild.build());
547
548         // Set request body to main multipart request
549         mprInput.setMultipartRequestBody(caseBuilder.build());
550
551         // Send the request, no cookies associated, use any connection
552         LOG.debug("Send group statistics request to the switch :{}", mprGroupBuild);
553         this.messageService.multipartRequest(mprInput.build(), null);
554
555         // Prepare rpc return output. Set xid and send it back.
556         LOG.debug("Return results and transaction id back to caller");
557         output.setTransactionId(generateTransactionId(xid));
558         output.setGroupStats(null);
559
560         RpcResult<GetAllGroupStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
561         return Futures.immediateFuture(rpcResult);
562
563     }
564
565     @Override
566     public Future<RpcResult<GetGroupDescriptionOutput>> getGroupDescription(final GetGroupDescriptionInput input) {
567
568         GetGroupDescriptionOutputBuilder output = new GetGroupDescriptionOutputBuilder();
569         Collection<RpcError> errors = Collections.emptyList();
570
571         if (version == OFConstants.OFP_VERSION_1_0) {
572             output.setTransactionId(null);
573             output.setGroupDescStats(null);
574
575             RpcResult<GetGroupDescriptionOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
576             return Futures.immediateFuture(rpcResult);
577         }
578         // Generate xid to associate it with the request
579         Long xid = this.getSessionContext().getNextXid();
580
581         LOG.debug("Prepare group description statistics request - Transaction id - {}", xid);
582
583         // Create multipart request header
584         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
585         mprInput.setType(MultipartType.OFPMPGROUPDESC);
586         mprInput.setVersion(version);
587         mprInput.setXid(xid);
588         mprInput.setFlags(new MultipartRequestFlags(false));
589
590         // Create multipart request body for fetch all the group description
591         // stats
592         MultipartRequestGroupDescCaseBuilder mprGroupDescBuild = new MultipartRequestGroupDescCaseBuilder();
593
594         // Set request body to main multipart request
595         mprInput.setMultipartRequestBody(mprGroupDescBuild.build());
596
597         // Send the request, no cookies associated, use any connection
598         LOG.debug("Send group desciption statistics request to switch : {}", mprGroupDescBuild);
599         this.messageService.multipartRequest(mprInput.build(), null);
600
601         // Prepare rpc return output. Set xid and send it back.
602         LOG.debug("Return results and transaction id back to caller");
603         output.setTransactionId(generateTransactionId(xid));
604         output.setGroupDescStats(null);
605
606         RpcResult<GetGroupDescriptionOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
607         return Futures.immediateFuture(rpcResult);
608
609     }
610
611     @Override
612     public Future<RpcResult<GetGroupFeaturesOutput>> getGroupFeatures(final GetGroupFeaturesInput input) {
613
614         GetGroupFeaturesOutputBuilder output = new GetGroupFeaturesOutputBuilder();
615         Collection<RpcError> errors = Collections.emptyList();
616
617         if (version == OFConstants.OFP_VERSION_1_0) {
618             output.setTransactionId(null);
619
620             RpcResult<GetGroupFeaturesOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
621             return Futures.immediateFuture(rpcResult);
622         }
623         // Generate xid to associate it with the request
624         Long xid = this.getSessionContext().getNextXid();
625
626         LOG.debug("Prepare group features statistics request - Transaction id - {}", xid);
627
628         // Create multipart request header
629         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
630         mprInput.setType(MultipartType.OFPMPGROUPFEATURES);
631         mprInput.setVersion(version);
632         mprInput.setXid(xid);
633         mprInput.setFlags(new MultipartRequestFlags(false));
634
635         // Create multipart request body for fetch all the group description
636         // stats
637         MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild = new MultipartRequestGroupFeaturesCaseBuilder();
638
639         // Set request body to main multipart request
640         mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
641
642         // Send the request, no cookies associated, use any connection
643         LOG.debug("Send group features statistics request :{}", mprGroupFeaturesBuild);
644         this.messageService.multipartRequest(mprInput.build(), null);
645
646         // Prepare rpc return output. Set xid and send it back.
647         LOG.debug("Return results and transaction id back to caller");
648         output.setTransactionId(generateTransactionId(xid));
649
650         RpcResult<GetGroupFeaturesOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
651         return Futures.immediateFuture(rpcResult);
652     }
653
654     @Override
655     public Future<RpcResult<GetGroupStatisticsOutput>> getGroupStatistics(final GetGroupStatisticsInput input) {
656
657         GetGroupStatisticsOutputBuilder output = new GetGroupStatisticsOutputBuilder();
658         Collection<RpcError> errors = Collections.emptyList();
659
660         if (version == OFConstants.OFP_VERSION_1_0) {
661             output.setTransactionId(null);
662             output.setGroupStats(null);
663
664             RpcResult<GetGroupStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
665             return Futures.immediateFuture(rpcResult);
666         }
667         // Generate xid to associate it with the request
668         Long xid = this.getSessionContext().getNextXid();
669
670         LOG.debug("Prepare statistics request for node {} group ({}) - Transaction id - {}", input.getNode(),
671                 input.getGroupId(), xid);
672
673         // Create multipart request header
674         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
675         mprInput.setType(MultipartType.OFPMPGROUP);
676         mprInput.setVersion(version);
677         mprInput.setXid(xid);
678         mprInput.setFlags(new MultipartRequestFlags(false));
679
680         // Create multipart request body for fetch all the group stats
681         MultipartRequestGroupCaseBuilder caseBuilder = new MultipartRequestGroupCaseBuilder();
682         MultipartRequestGroupBuilder mprGroupBuild = new MultipartRequestGroupBuilder();
683         mprGroupBuild.setGroupId(new GroupId(input.getGroupId().getValue()));
684         caseBuilder.setMultipartRequestGroup(mprGroupBuild.build());
685
686         // Set request body to main multipart request
687         mprInput.setMultipartRequestBody(caseBuilder.build());
688
689         // Send the request, no cookies associated, use any connection
690         LOG.debug("Send group statistics request :{}", mprGroupBuild);
691         this.messageService.multipartRequest(mprInput.build(), null);
692
693         // Prepare rpc return output. Set xid and send it back.
694         LOG.debug("Return results and transaction id back to caller");
695         output.setTransactionId(generateTransactionId(xid));
696         output.setGroupStats(null);
697
698         RpcResult<GetGroupStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
699         return Futures.immediateFuture(rpcResult);
700     }
701
702     @Override
703     public Future<RpcResult<GetAllMeterConfigStatisticsOutput>> getAllMeterConfigStatistics(
704             final GetAllMeterConfigStatisticsInput input) {
705
706         GetAllMeterConfigStatisticsOutputBuilder output = new GetAllMeterConfigStatisticsOutputBuilder();
707         Collection<RpcError> errors = Collections.emptyList();
708
709         if (version == OFConstants.OFP_VERSION_1_0) {
710             output.setTransactionId(null);
711             output.setMeterConfigStats(null);
712
713             RpcResult<GetAllMeterConfigStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
714             return Futures.immediateFuture(rpcResult);
715
716         }
717         // Generate xid to associate it with the request
718         Long xid = this.getSessionContext().getNextXid();
719
720         LOG.debug("Prepare config request for all the meters - Transaction id - {}", xid);
721
722         // Create multipart request header
723         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
724         mprInput.setType(MultipartType.OFPMPMETERCONFIG);
725         mprInput.setVersion(version);
726         mprInput.setXid(xid);
727         mprInput.setFlags(new MultipartRequestFlags(false));
728
729         // Create multipart request body for fetch all the meter stats
730         MultipartRequestMeterConfigCaseBuilder caseBuilder = new MultipartRequestMeterConfigCaseBuilder();
731         MultipartRequestMeterConfigBuilder mprMeterConfigBuild = new MultipartRequestMeterConfigBuilder();
732         mprMeterConfigBuild.setMeterId(new MeterId(BinContent.intToUnsignedLong(Meter.OFPMALL.getIntValue())));
733         caseBuilder.setMultipartRequestMeterConfig(mprMeterConfigBuild.build());
734
735         // Set request body to main multipart request
736         mprInput.setMultipartRequestBody(caseBuilder.build());
737
738         // Send the request, no cookies associated, use any connection
739         LOG.debug("Send meter statistics request :{}", mprMeterConfigBuild);
740         this.messageService.multipartRequest(mprInput.build(), null);
741
742         // Prepare rpc return output. Set xid and send it back.
743         LOG.debug("Return results and transaction id back to caller");
744         output.setTransactionId(generateTransactionId(xid));
745         output.setMeterConfigStats(null);
746
747         RpcResult<GetAllMeterConfigStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
748         return Futures.immediateFuture(rpcResult);
749     }
750
751     @Override
752     public Future<RpcResult<GetAllMeterStatisticsOutput>> getAllMeterStatistics(final GetAllMeterStatisticsInput input) {
753
754         GetAllMeterStatisticsOutputBuilder output = new GetAllMeterStatisticsOutputBuilder();
755         Collection<RpcError> errors = Collections.emptyList();
756
757         if (version == OFConstants.OFP_VERSION_1_0) {
758             output.setTransactionId(null);
759             output.setMeterStats(null);
760
761             RpcResult<GetAllMeterStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
762             return Futures.immediateFuture(rpcResult);
763         }
764         // Generate xid to associate it with the request
765         Long xid = this.getSessionContext().getNextXid();
766
767         LOG.debug("Prepare statistics request for all the meters - Transaction id - {}", xid);
768
769         // Create multipart request header
770         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
771         mprInput.setType(MultipartType.OFPMPMETER);
772         mprInput.setVersion(version);
773         mprInput.setXid(xid);
774         mprInput.setFlags(new MultipartRequestFlags(false));
775
776         // Create multipart request body for fetch all the meter stats
777         MultipartRequestMeterCaseBuilder caseBuilder = new MultipartRequestMeterCaseBuilder();
778         MultipartRequestMeterBuilder mprMeterBuild = new MultipartRequestMeterBuilder();
779         mprMeterBuild.setMeterId(new MeterId(BinContent.intToUnsignedLong(Meter.OFPMALL.getIntValue())));
780         caseBuilder.setMultipartRequestMeter(mprMeterBuild.build());
781
782         // Set request body to main multipart request
783         mprInput.setMultipartRequestBody(caseBuilder.build());
784
785         // Send the request, no cookies associated, use any connection
786         LOG.debug("Send meter statistics request :{}", mprMeterBuild);
787         this.messageService.multipartRequest(mprInput.build(), null);
788
789         // Prepare rpc return output. Set xid and send it back.
790         LOG.debug("Return results and transaction id back to caller");
791         output.setTransactionId(generateTransactionId(xid));
792         output.setMeterStats(null);
793
794         RpcResult<GetAllMeterStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
795         return Futures.immediateFuture(rpcResult);
796     }
797
798     @Override
799     public Future<RpcResult<GetMeterFeaturesOutput>> getMeterFeatures(final GetMeterFeaturesInput input) {
800
801         GetMeterFeaturesOutputBuilder output = new GetMeterFeaturesOutputBuilder();
802         Collection<RpcError> errors = Collections.emptyList();
803
804         if (version == OFConstants.OFP_VERSION_1_0) {
805             output.setTransactionId(null);
806
807             RpcResult<GetMeterFeaturesOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
808             return Futures.immediateFuture(rpcResult);
809         }
810         // Generate xid to associate it with the request
811         Long xid = this.getSessionContext().getNextXid();
812
813         LOG.debug("Prepare features statistics request for all the meters - Transaction id - {}", xid);
814
815         // Create multipart request header
816         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
817         mprInput.setType(MultipartType.OFPMPMETERFEATURES);
818         mprInput.setVersion(version);
819         mprInput.setXid(xid);
820         mprInput.setFlags(new MultipartRequestFlags(false));
821
822         // Create multipart request body for fetch all the group description
823         // stats
824         MultipartRequestMeterFeaturesCaseBuilder mprMeterFeaturesBuild = new MultipartRequestMeterFeaturesCaseBuilder();
825
826         // Set request body to main multipart request
827         mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
828
829         // Send the request, no cookies associated, use any connection
830         LOG.debug("Send meter features statistics request :{}", mprMeterFeaturesBuild);
831         this.messageService.multipartRequest(mprInput.build(), null);
832
833         // Prepare rpc return output. Set xid and send it back.
834         LOG.debug("Return results and transaction id back to caller");
835         output.setTransactionId(generateTransactionId(xid));
836
837         RpcResult<GetMeterFeaturesOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
838         return Futures.immediateFuture(rpcResult);
839     }
840
841     @Override
842     public Future<RpcResult<GetMeterStatisticsOutput>> getMeterStatistics(final GetMeterStatisticsInput input) {
843
844         GetMeterStatisticsOutputBuilder output = new GetMeterStatisticsOutputBuilder();
845         Collection<RpcError> errors = Collections.emptyList();
846
847         if (version == OFConstants.OFP_VERSION_1_0) {
848             output.setTransactionId(null);
849             output.setMeterStats(null);
850
851             RpcResult<GetMeterStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
852             return Futures.immediateFuture(rpcResult);
853
854         }
855         // Generate xid to associate it with the request
856         Long xid = this.getSessionContext().getNextXid();
857
858         LOG.debug("Preprae statistics request for Meter ({}) - Transaction id - {}", input.getMeterId().getValue(), xid);
859
860         // Create multipart request header
861         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
862         mprInput.setType(MultipartType.OFPMPMETER);
863         mprInput.setVersion(version);
864         mprInput.setXid(xid);
865         mprInput.setFlags(new MultipartRequestFlags(false));
866
867         // Create multipart request body for fetch all the meter stats
868         MultipartRequestMeterCaseBuilder caseBuilder = new MultipartRequestMeterCaseBuilder();
869         MultipartRequestMeterBuilder mprMeterBuild = new MultipartRequestMeterBuilder();
870         // Select specific meter
871         mprMeterBuild.setMeterId(new MeterId(input.getMeterId().getValue()));
872         caseBuilder.setMultipartRequestMeter(mprMeterBuild.build());
873
874         // Set request body to main multipart request
875         mprInput.setMultipartRequestBody(caseBuilder.build());
876
877         // Send the request, no cookies associated, use any connection
878         LOG.debug("Send meter statistics request :{}", mprMeterBuild);
879         this.messageService.multipartRequest(mprInput.build(), null);
880
881         // Prepare rpc return output. Set xid and send it back.
882         LOG.debug("Return results and transaction id back to caller");
883         output.setTransactionId(generateTransactionId(xid));
884         output.setMeterStats(null);
885
886         RpcResult<GetMeterStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
887         return Futures.immediateFuture(rpcResult);
888     }
889
890     @Override
891     public Future<RpcResult<GetAllNodeConnectorsStatisticsOutput>> getAllNodeConnectorsStatistics(
892             final GetAllNodeConnectorsStatisticsInput arg0) {
893
894         // Generate xid to associate it with the request
895         Long xid = this.getSessionContext().getNextXid();
896
897         LOG.debug("Prepare port statistics request for all ports of node {} - TrasactionId - {}", arg0.getNode()
898                 .getValue(), xid);
899
900         // Create multipart request header
901         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
902         mprInput.setType(MultipartType.OFPMPPORTSTATS);
903         mprInput.setVersion(version);
904         mprInput.setXid(xid);
905         mprInput.setFlags(new MultipartRequestFlags(false));
906
907         // Create multipart request body to fetch stats for all the port of the
908         // node
909         MultipartRequestPortStatsCaseBuilder caseBuilder = new MultipartRequestPortStatsCaseBuilder();
910         MultipartRequestPortStatsBuilder mprPortStatsBuilder = new MultipartRequestPortStatsBuilder();
911         // Select all ports
912         mprPortStatsBuilder.setPortNo(OFConstants.OFPP_ANY);
913         caseBuilder.setMultipartRequestPortStats(mprPortStatsBuilder.build());
914
915         // Set request body to main multipart request
916         mprInput.setMultipartRequestBody(caseBuilder.build());
917
918         // Send the request, no cookies associated, use any connection
919         LOG.debug("Send port statistics request :{}", mprPortStatsBuilder.build().toString());
920         this.messageService.multipartRequest(mprInput.build(), null);
921
922         // Prepare rpc return output. Set xid and send it back.
923         GetAllNodeConnectorsStatisticsOutputBuilder output = new GetAllNodeConnectorsStatisticsOutputBuilder();
924         output.setTransactionId(generateTransactionId(xid));
925
926         Collection<RpcError> errors = Collections.emptyList();
927         RpcResult<GetAllNodeConnectorsStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
928         return Futures.immediateFuture(rpcResult);
929     }
930
931     @Override
932     public Future<RpcResult<GetNodeConnectorStatisticsOutput>> getNodeConnectorStatistics(
933             final GetNodeConnectorStatisticsInput arg0) {
934         // Generate xid to associate it with the request
935         Long xid = this.getSessionContext().getNextXid();
936
937         LOG.debug("Prepare port statistics request for port {} of node {} - TrasactionId - {}",
938                 arg0.getNodeConnectorId(), arg0.getNode().getValue(), xid);
939
940         // Create multipart request header
941         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
942         mprInput.setType(MultipartType.OFPMPPORTSTATS);
943         mprInput.setVersion(version);
944         mprInput.setXid(xid);
945         mprInput.setFlags(new MultipartRequestFlags(false));
946
947         // Create multipart request body to fetch stats for all the port of the
948         // node
949         MultipartRequestPortStatsCaseBuilder caseBuilder = new MultipartRequestPortStatsCaseBuilder();
950         MultipartRequestPortStatsBuilder mprPortStatsBuilder = new MultipartRequestPortStatsBuilder();
951
952         // Set specific port
953         mprPortStatsBuilder
954                 .setPortNo(InventoryDataServiceUtil.portNumberfromNodeConnectorId(arg0.getNodeConnectorId()));
955         caseBuilder.setMultipartRequestPortStats(mprPortStatsBuilder.build());
956
957         // Set request body to main multipart request
958         mprInput.setMultipartRequestBody(caseBuilder.build());
959
960         // Send the request, no cookies associated, use any connection
961         LOG.debug("Send port statistics request :{}", mprPortStatsBuilder.build().toString());
962         this.messageService.multipartRequest(mprInput.build(), null);
963
964         // Prepare rpc return output. Set xid and send it back.
965         GetNodeConnectorStatisticsOutputBuilder output = new GetNodeConnectorStatisticsOutputBuilder();
966         output.setTransactionId(generateTransactionId(xid));
967
968         Collection<RpcError> errors = Collections.emptyList();
969         RpcResult<GetNodeConnectorStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
970         return Futures.immediateFuture(rpcResult);
971     }
972
973     private TransactionId generateTransactionId(final Long xid) {
974         String stringXid = xid.toString();
975         BigInteger bigIntXid = new BigInteger(stringXid);
976         return new TransactionId(bigIntXid);
977
978     }
979
980     @Override
981     public Future<RpcResult<UpdatePortOutput>> updatePort(final UpdatePortInput input) {
982         PortModInput ofPortModInput = null;
983         RpcResult<UpdatePortOutput> rpcResultFromOFLib = null;
984
985         // For Flow provisioning, the SwitchConnectionDistinguisher is set to
986         // null so
987         // the request can be routed through any connection to the switch
988
989         SwitchConnectionDistinguisher cookie = null;
990
991         // NSF sends a list of port and the ModelDrivenSwitch will
992         // send one port at a time towards the switch ( mutiple RPCs calls)
993         List<Port> inputPorts = input.getUpdatedPort().getPort().getPort();
994
995         // Get the Xid. The same Xid has to be sent in all the RPCs
996         Long Xid = sessionContext.getNextXid();
997
998         for (Port inputPort : inputPorts) {
999
1000             // Convert the UpdateGroupInput to GroupModInput
1001             ofPortModInput = PortConvertor.toPortModInput(inputPort, version);
1002
1003             // Insert the Xid ( transaction Id) before calling the RPC on the
1004             // OFLibrary
1005
1006             PortModInputBuilder mdInput = new PortModInputBuilder(ofPortModInput);
1007             mdInput.setXid(Xid);
1008
1009             LOG.debug("Calling the PortMod RPC method on MessageDispatchService");
1010             Future<RpcResult<UpdatePortOutput>> resultFromOFLib = messageService.portMod(mdInput.build(), cookie);
1011
1012             try {
1013                 rpcResultFromOFLib = resultFromOFLib.get();
1014             } catch (Exception ex) {
1015                 LOG.error(" Error while getting result for updatePort RPC" + ex.getMessage());
1016             }
1017
1018             // The Future response value for all the RPCs except the last one is
1019             // ignored
1020
1021         }
1022         // Extract the Xid only from the Future for the last RPC and
1023         // send it back to the NSF
1024         UpdatePortOutput updatePortOutputOFLib = rpcResultFromOFLib.getResult();
1025
1026         UpdatePortOutputBuilder updatePortOutput = new UpdatePortOutputBuilder();
1027         updatePortOutput.setTransactionId(updatePortOutputOFLib.getTransactionId());
1028         UpdatePortOutput result = updatePortOutput.build();
1029
1030         Collection<RpcError> errors = rpcResultFromOFLib.getErrors();
1031         RpcResult<UpdatePortOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
1032
1033         LOG.debug("Returning the Update Group RPC result to MD-SAL");
1034         return Futures.immediateFuture(rpcResult);
1035
1036     }
1037
1038     @Override
1039     public Future<RpcResult<UpdateTableOutput>> updateTable(final UpdateTableInput input) {
1040
1041         // Get the Xid. The same Xid has to be sent in all the Multipart
1042         // requests
1043         Long xid = this.getSessionContext().getNextXid();
1044
1045         LOG.debug("Prepare the Multipart Table Mod requests for Transaction Id {} ", xid);
1046
1047         // Create multipart request header
1048         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1049         mprInput.setType(MultipartType.OFPMPTABLEFEATURES);
1050         mprInput.setVersion((short) 0x04);
1051         mprInput.setXid(xid);
1052
1053         // Convert the list of all MD-SAL table feature object into OF library
1054         // object
1055         List<TableFeatures> ofTableFeatureList = TableFeaturesConvertor.toTableFeaturesRequest(input.getUpdatedTable());
1056
1057         MultipartRequestTableFeaturesCaseBuilder caseRequest = new MultipartRequestTableFeaturesCaseBuilder();
1058         MultipartRequestTableFeaturesBuilder tableFeaturesRequest = new MultipartRequestTableFeaturesBuilder();
1059
1060         mprInput.setFlags(new MultipartRequestFlags(false));
1061
1062         tableFeaturesRequest.setTableFeatures(ofTableFeatureList);
1063
1064         // Set request body to main multipart request
1065         caseRequest.setMultipartRequestTableFeatures(tableFeaturesRequest.build());
1066         mprInput.setMultipartRequestBody(caseRequest.build());
1067
1068         // Send the request, no cookies associated, use any connection
1069         LOG.debug("Send Table Feature request :{}", ofTableFeatureList);
1070         this.messageService.multipartRequest(mprInput.build(), null);
1071
1072         // Extract the Xid only from the Future for the last RPC and
1073         // send it back to the NSF
1074         LOG.debug("Returning the result and transaction id to NSF");
1075         LOG.debug("Return results and transaction id back to caller");
1076         UpdateTableOutputBuilder output = new UpdateTableOutputBuilder();
1077         output.setTransactionId(generateTransactionId(xid));
1078
1079         Collection<RpcError> errors = Collections.emptyList();
1080         RpcResult<UpdateTableOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
1081         return Futures.immediateFuture(rpcResult);
1082     }
1083
1084     @Override
1085     public Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> getAllFlowStatisticsFromFlowTable(
1086             final GetAllFlowStatisticsFromFlowTableInput arg0) {
1087
1088         // Generate xid to associate it with the request
1089         Long xid = this.getSessionContext().getNextXid();
1090
1091         LOG.debug("Prepare statistics request to get flow stats for switch tables {} - Transaction id - {}", arg0
1092                 .getTableId().getValue(), xid);
1093
1094         // Create multipart request header
1095         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1096         mprInput.setType(MultipartType.OFPMPFLOW);
1097         mprInput.setVersion(version);
1098         mprInput.setXid(xid);
1099         mprInput.setFlags(new MultipartRequestFlags(false));
1100
1101         // Create multipart request body for fetch all the group stats
1102         MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
1103         MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
1104         mprFlowRequestBuilder.setTableId(arg0.getTableId().getValue());
1105         mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
1106         mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1107         mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1108         mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1109         FlowCreatorUtil.setWildcardedFlowMatch(version, mprFlowRequestBuilder);
1110
1111         // Set request body to main multipart request
1112         multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
1113         mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
1114
1115         // Send the request, no cookies associated, use any connection
1116         LOG.debug("Send flow statistics request to the switch :{}", mprFlowRequestBuilder);
1117         this.messageService.multipartRequest(mprInput.build(), null);
1118
1119         // Prepare rpc return output. Set xid and send it back.
1120         LOG.debug("Return results and transaction id back to caller");
1121         GetAllFlowStatisticsFromFlowTableOutputBuilder output = new GetAllFlowStatisticsFromFlowTableOutputBuilder();
1122         output.setTransactionId(generateTransactionId(xid));
1123         output.setFlowAndStatisticsMapList(null);
1124
1125         Collection<RpcError> errors = Collections.emptyList();
1126         RpcResult<GetAllFlowStatisticsFromFlowTableOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
1127         return Futures.immediateFuture(rpcResult);
1128     }
1129
1130     @Override
1131     public Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> getAllFlowsStatisticsFromAllFlowTables(
1132             final GetAllFlowsStatisticsFromAllFlowTablesInput arg0) {
1133
1134         // Generate xid to associate it with the request
1135         Long xid = this.getSessionContext().getNextXid();
1136
1137         LOG.debug("Prepare statistics request to get flow stats of all switch tables - Transaction id - {}", xid);
1138
1139         // Create multipart request header
1140         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1141         mprInput.setType(MultipartType.OFPMPFLOW);
1142         mprInput.setVersion(version);
1143         mprInput.setXid(xid);
1144         mprInput.setFlags(new MultipartRequestFlags(false));
1145
1146         // Create multipart request body for fetch all the group stats
1147         MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
1148         MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
1149         mprFlowRequestBuilder.setTableId(OFConstants.OFPTT_ALL);
1150         mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
1151         mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1152         mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1153         mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1154         mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1155
1156         FlowCreatorUtil.setWildcardedFlowMatch(version, mprFlowRequestBuilder);
1157
1158         // Set request body to main multipart request
1159         multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
1160         mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
1161
1162         // Send the request, no cookies associated, use any connection
1163         LOG.debug("Send flow statistics request to the switch :{}", mprFlowRequestBuilder);
1164         this.messageService.multipartRequest(mprInput.build(), null);
1165
1166         // Prepare rpc return output. Set xid and send it back.
1167         GetAllFlowsStatisticsFromAllFlowTablesOutputBuilder output = new GetAllFlowsStatisticsFromAllFlowTablesOutputBuilder();
1168         output.setTransactionId(generateTransactionId(xid));
1169         output.setFlowAndStatisticsMapList(null);
1170
1171         Collection<RpcError> errors = Collections.emptyList();
1172         RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput> rpcResult = Rpcs.getRpcResult(true, output.build(),
1173                 errors);
1174         return Futures.immediateFuture(rpcResult);
1175
1176     }
1177
1178     @Override
1179     public Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> getFlowStatisticsFromFlowTable(
1180             final GetFlowStatisticsFromFlowTableInput arg0) {
1181         // Generate xid to associate it with the request
1182         Long xid = this.getSessionContext().getNextXid();
1183
1184         LOG.debug("Prepare statistics request to get stats for flow {} for switch tables {} - Transaction id - {}",
1185                 arg0.getMatch().toString(), arg0.getTableId(), xid);
1186
1187         // Create multipart request header
1188         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1189         mprInput.setType(MultipartType.OFPMPFLOW);
1190         mprInput.setVersion(version);
1191         mprInput.setXid(xid);
1192         mprInput.setFlags(new MultipartRequestFlags(false));
1193
1194         // Create multipart request body for fetch all the group stats
1195         MultipartRequestFlowCaseBuilder multipartRequestFlowCaseBuilder = new MultipartRequestFlowCaseBuilder();
1196         MultipartRequestFlowBuilder mprFlowRequestBuilder = new MultipartRequestFlowBuilder();
1197         mprFlowRequestBuilder.setTableId(arg0.getTableId());
1198
1199         if (arg0.getOutPort() != null)
1200             mprFlowRequestBuilder.setOutPort(arg0.getOutPort().longValue());
1201         else
1202             mprFlowRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
1203
1204         if (arg0.getOutGroup() != null)
1205             mprFlowRequestBuilder.setOutGroup(arg0.getOutGroup());
1206         else
1207             mprFlowRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1208
1209         if (arg0.getCookie() != null)
1210             mprFlowRequestBuilder.setCookie(arg0.getCookie().getValue());
1211         else
1212             mprFlowRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1213
1214         if (arg0.getCookieMask() != null)
1215             mprFlowRequestBuilder.setCookieMask(arg0.getCookieMask().getValue());
1216         else
1217             mprFlowRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1218
1219         // convert and inject match
1220         MatchReactor.getInstance().convert(arg0.getMatch(), version, mprFlowRequestBuilder,
1221                 this.getSessionContext().getFeatures().getDatapathId());
1222
1223         // Set request body to main multipart request
1224         multipartRequestFlowCaseBuilder.setMultipartRequestFlow(mprFlowRequestBuilder.build());
1225         mprInput.setMultipartRequestBody(multipartRequestFlowCaseBuilder.build());
1226
1227         // Send the request, no cookies associated, use any connection
1228         LOG.debug("Send flow statistics request to the switch :{}", mprFlowRequestBuilder);
1229         this.messageService.multipartRequest(mprInput.build(), null);
1230
1231         // Prepare rpc return output. Set xid and send it back.
1232         GetFlowStatisticsFromFlowTableOutputBuilder output = new GetFlowStatisticsFromFlowTableOutputBuilder();
1233         output.setTransactionId(generateTransactionId(xid));
1234         output.setFlowAndStatisticsMapList(null);
1235
1236         Collection<RpcError> errors = Collections.emptyList();
1237         RpcResult<GetFlowStatisticsFromFlowTableOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
1238         return Futures.immediateFuture(rpcResult);
1239     }
1240
1241     @Override
1242     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> getAggregateFlowStatisticsFromFlowTableForAllFlows(
1243             final GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput arg0) {
1244         // Generate xid to associate it with the request
1245         Long xid = this.getSessionContext().getNextXid();
1246
1247         LOG.debug(
1248                 "Prepare aggregate flow statistics request to get aggregate flow stats for all the flow installed on switch table {} - Transaction id - {}",
1249                 arg0.getTableId().getValue(), xid);
1250
1251         // Create multipart request header
1252         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1253         mprInput.setType(MultipartType.OFPMPAGGREGATE);
1254         mprInput.setVersion(version);
1255         mprInput.setXid(xid);
1256         mprInput.setFlags(new MultipartRequestFlags(false));
1257
1258         // Create multipart request body for fetch all the group stats
1259         MultipartRequestAggregateCaseBuilder multipartRequestAggregateCaseBuilder = new MultipartRequestAggregateCaseBuilder();
1260         MultipartRequestAggregateBuilder mprAggregateRequestBuilder = new MultipartRequestAggregateBuilder();
1261         mprAggregateRequestBuilder.setTableId(arg0.getTableId().getValue());
1262         mprAggregateRequestBuilder.setOutPort(OFConstants.OFPP_ANY);
1263         mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1264         mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1265         mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1266
1267         FlowCreatorUtil.setWildcardedFlowMatch(version, mprAggregateRequestBuilder);
1268
1269         // Set request body to main multipart request
1270         multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder.build());
1271         mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
1272
1273         // Send the request, no cookies associated, use any connection
1274         LOG.debug("Send request to the switch :{}", multipartRequestAggregateCaseBuilder.build().toString());
1275         this.messageService.multipartRequest(mprInput.build(), null);
1276
1277         // Prepare rpc return output. Set xid and send it back.
1278         GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder output = new GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutputBuilder();
1279         output.setTransactionId(generateTransactionId(xid));
1280
1281         Collection<RpcError> errors = Collections.emptyList();
1282         RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput> rpcResult = Rpcs.getRpcResult(true,
1283                 output.build(), errors);
1284         return Futures.immediateFuture(rpcResult);
1285     }
1286
1287     @Override
1288     public Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput>> getAggregateFlowStatisticsFromFlowTableForGivenMatch(
1289             final GetAggregateFlowStatisticsFromFlowTableForGivenMatchInput arg0) {
1290
1291         // Generate xid to associate it with the request
1292         Long xid = this.getSessionContext().getNextXid();
1293
1294         LOG.debug(
1295                 "Prepare aggregate statistics request to get aggregate stats for flows matching {} and installed in flow tables {} - Transaction id - {}",
1296                 arg0.getMatch().toString(), arg0.getTableId(), xid);
1297
1298         // Create multipart request header
1299         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1300         mprInput.setType(MultipartType.OFPMPAGGREGATE);
1301         mprInput.setVersion(version);
1302         mprInput.setXid(xid);
1303         mprInput.setFlags(new MultipartRequestFlags(false));
1304
1305         // Create multipart request body for fetch all the group stats
1306         MultipartRequestAggregateCaseBuilder multipartRequestAggregateCaseBuilder = new MultipartRequestAggregateCaseBuilder();
1307         MultipartRequestAggregateBuilder mprAggregateRequestBuilder = new MultipartRequestAggregateBuilder();
1308         mprAggregateRequestBuilder.setTableId(arg0.getTableId());
1309         mprAggregateRequestBuilder.setOutPort(arg0.getOutPort().longValue());
1310         mprAggregateRequestBuilder.setOutGroup(OFConstants.OFPG_ANY);
1311         mprAggregateRequestBuilder.setCookie(OFConstants.DEFAULT_COOKIE);
1312         mprAggregateRequestBuilder.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
1313
1314         MatchReactor.getInstance().convert(arg0.getMatch(), version, mprAggregateRequestBuilder,
1315                 this.getSessionContext().getFeatures().getDatapathId());
1316         // TODO: repeating code
1317         if (version == OFConstants.OFP_VERSION_1_3) {
1318             mprAggregateRequestBuilder.setCookie(arg0.getCookie().getValue());
1319             mprAggregateRequestBuilder.setCookieMask(arg0.getCookieMask().getValue());
1320             mprAggregateRequestBuilder.setOutGroup(arg0.getOutGroup());
1321         }
1322
1323         // Set request body to main multipart request
1324         multipartRequestAggregateCaseBuilder.setMultipartRequestAggregate(mprAggregateRequestBuilder.build());
1325         mprInput.setMultipartRequestBody(multipartRequestAggregateCaseBuilder.build());
1326
1327         // Send the request, no cookies associated, use any connection
1328         LOG.debug("Send request to the switch :{}", multipartRequestAggregateCaseBuilder.build().toString());
1329         this.messageService.multipartRequest(mprInput.build(), null);
1330
1331         // Prepare rpc return output. Set xid and send it back.
1332         GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder output = new GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutputBuilder();
1333         output.setTransactionId(generateTransactionId(xid));
1334
1335         Collection<RpcError> errors = Collections.emptyList();
1336         RpcResult<GetAggregateFlowStatisticsFromFlowTableForGivenMatchOutput> rpcResult = Rpcs.getRpcResult(true,
1337                 output.build(), errors);
1338         return Futures.immediateFuture(rpcResult);
1339     }
1340
1341     @Override
1342     public Future<RpcResult<GetFlowTablesStatisticsOutput>> getFlowTablesStatistics(final GetFlowTablesStatisticsInput arg0) {
1343         // Generate xid to associate it with the request
1344         Long xid = this.getSessionContext().getNextXid();
1345
1346         LOG.debug("Prepare flow table statistics request to get flow table stats for all tables "
1347                 + "from node {}- Transaction id - {}", arg0.getNode(), xid);
1348
1349         // Create multipart request header
1350         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1351         mprInput.setType(MultipartType.OFPMPTABLE);
1352         mprInput.setVersion(version);
1353         mprInput.setXid(xid);
1354         mprInput.setFlags(new MultipartRequestFlags(false));
1355
1356         // Create multipart request body for fetch all the group stats
1357         MultipartRequestTableCaseBuilder multipartRequestTableCaseBuilder = new MultipartRequestTableCaseBuilder();
1358         MultipartRequestTableBuilder multipartRequestTableBuilder = new MultipartRequestTableBuilder();
1359         multipartRequestTableBuilder.setEmpty(true);
1360         multipartRequestTableCaseBuilder.setMultipartRequestTable(multipartRequestTableBuilder.build());
1361
1362         // Set request body to main multipart request
1363         mprInput.setMultipartRequestBody(multipartRequestTableCaseBuilder.build());
1364
1365         // Send the request, no cookies associated, use any connection
1366         LOG.debug("Send request to the switch :{}", multipartRequestTableCaseBuilder.build().toString());
1367         this.messageService.multipartRequest(mprInput.build(), null);
1368
1369         // Prepare rpc return output. Set xid and send it back.
1370         GetFlowTablesStatisticsOutputBuilder output = new GetFlowTablesStatisticsOutputBuilder();
1371         output.setTransactionId(generateTransactionId(xid));
1372
1373         Collection<RpcError> errors = Collections.emptyList();
1374         RpcResult<GetFlowTablesStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
1375         return Futures.immediateFuture(rpcResult);
1376     }
1377
1378     @Override
1379     public Future<RpcResult<GetAllQueuesStatisticsFromAllPortsOutput>> getAllQueuesStatisticsFromAllPorts(
1380             final GetAllQueuesStatisticsFromAllPortsInput arg0) {
1381         // Generate xid to associate it with the request
1382         Long xid = this.getSessionContext().getNextXid();
1383
1384         LOG.debug(
1385                 "Prepare queue statistics request to collect stats for all queues attached to all the ports of node {} - TrasactionId - {}",
1386                 arg0.getNode().getValue(), xid);
1387
1388         // Create multipart request header
1389         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1390         mprInput.setType(MultipartType.OFPMPQUEUE);
1391         mprInput.setVersion(version);
1392         mprInput.setXid(xid);
1393         mprInput.setFlags(new MultipartRequestFlags(false));
1394
1395         // Create multipart request body to fetch stats for all the port of the
1396         // node
1397         MultipartRequestQueueCaseBuilder caseBuilder = new MultipartRequestQueueCaseBuilder();
1398         MultipartRequestQueueBuilder mprQueueBuilder = new MultipartRequestQueueBuilder();
1399         // Select all ports
1400         mprQueueBuilder.setPortNo(OFConstants.OFPP_ANY);
1401         // Select all the ports
1402         mprQueueBuilder.setQueueId(OFConstants.OFPQ_ANY);
1403
1404         caseBuilder.setMultipartRequestQueue(mprQueueBuilder.build());
1405
1406         // Set request body to main multipart request
1407         mprInput.setMultipartRequestBody(caseBuilder.build());
1408
1409         // Send the request, no cookies associated, use any connection
1410         LOG.debug("Send queue statistics request :{}", mprQueueBuilder.build().toString());
1411         this.messageService.multipartRequest(mprInput.build(), null);
1412
1413         // Prepare rpc return output. Set xid and send it back.
1414         GetAllQueuesStatisticsFromAllPortsOutputBuilder output = new GetAllQueuesStatisticsFromAllPortsOutputBuilder();
1415         output.setTransactionId(generateTransactionId(xid));
1416         output.setQueueIdAndStatisticsMap(null);
1417
1418         Collection<RpcError> errors = Collections.emptyList();
1419         RpcResult<GetAllQueuesStatisticsFromAllPortsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
1420         return Futures.immediateFuture(rpcResult);
1421     }
1422
1423     @Override
1424     public Future<RpcResult<GetAllQueuesStatisticsFromGivenPortOutput>> getAllQueuesStatisticsFromGivenPort(
1425             final GetAllQueuesStatisticsFromGivenPortInput arg0) {
1426         // Generate xid to associate it with the request
1427         Long xid = this.getSessionContext().getNextXid();
1428
1429         LOG.debug("Prepare queue statistics request to collect stats for "
1430                 + "all queues attached to given port {} of node {} - TrasactionId - {}", arg0.getNodeConnectorId()
1431                 .toString(), arg0.getNode().getValue(), xid);
1432
1433         // Create multipart request header
1434         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1435         mprInput.setType(MultipartType.OFPMPQUEUE);
1436         mprInput.setVersion(version);
1437         mprInput.setXid(xid);
1438         mprInput.setFlags(new MultipartRequestFlags(false));
1439
1440         // Create multipart request body to fetch stats for all the port of the
1441         // node
1442         MultipartRequestQueueCaseBuilder caseBuilder = new MultipartRequestQueueCaseBuilder();
1443         MultipartRequestQueueBuilder mprQueueBuilder = new MultipartRequestQueueBuilder();
1444         // Select all queues
1445         mprQueueBuilder.setQueueId(OFConstants.OFPQ_ANY);
1446         // Select specific port
1447         mprQueueBuilder.setPortNo(InventoryDataServiceUtil.portNumberfromNodeConnectorId(arg0.getNodeConnectorId()));
1448
1449         caseBuilder.setMultipartRequestQueue(mprQueueBuilder.build());
1450
1451         // Set request body to main multipart request
1452         mprInput.setMultipartRequestBody(caseBuilder.build());
1453
1454         // Send the request, no cookies associated, use any connection
1455         LOG.debug("Send queue statistics request :{}", mprQueueBuilder.build().toString());
1456         this.messageService.multipartRequest(mprInput.build(), null);
1457
1458         // Prepare rpc return output. Set xid and send it back.
1459         GetAllQueuesStatisticsFromGivenPortOutputBuilder output = new GetAllQueuesStatisticsFromGivenPortOutputBuilder();
1460         output.setTransactionId(generateTransactionId(xid));
1461         output.setQueueIdAndStatisticsMap(null);
1462
1463         Collection<RpcError> errors = Collections.emptyList();
1464         RpcResult<GetAllQueuesStatisticsFromGivenPortOutput> rpcResult = Rpcs
1465                 .getRpcResult(true, output.build(), errors);
1466         return Futures.immediateFuture(rpcResult);
1467     }
1468
1469     @Override
1470     public Future<RpcResult<GetQueueStatisticsFromGivenPortOutput>> getQueueStatisticsFromGivenPort(
1471             final GetQueueStatisticsFromGivenPortInput arg0) {
1472         // Generate xid to associate it with the request
1473         Long xid = this.getSessionContext().getNextXid();
1474
1475         LOG.debug("Prepare queue statistics request to collect stats for "
1476                 + "given queue attached to given port {} of node {} - TrasactionId - {}", arg0.getQueueId().toString(),
1477                 arg0.getNodeConnectorId().toString(), arg0.getNode().getValue(), xid);
1478
1479         // Create multipart request header
1480         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
1481         mprInput.setType(MultipartType.OFPMPQUEUE);
1482         mprInput.setVersion(version);
1483         mprInput.setXid(xid);
1484         mprInput.setFlags(new MultipartRequestFlags(false));
1485
1486         // Create multipart request body to fetch stats for all the port of the
1487         // node
1488         MultipartRequestQueueCaseBuilder caseBuilder = new MultipartRequestQueueCaseBuilder();
1489         MultipartRequestQueueBuilder mprQueueBuilder = new MultipartRequestQueueBuilder();
1490         // Select specific queue
1491         mprQueueBuilder.setQueueId(arg0.getQueueId().getValue());
1492         // Select specific port
1493         mprQueueBuilder.setPortNo(InventoryDataServiceUtil.portNumberfromNodeConnectorId(arg0.getNodeConnectorId()));
1494
1495         caseBuilder.setMultipartRequestQueue(mprQueueBuilder.build());
1496
1497         // Set request body to main multipart request
1498         mprInput.setMultipartRequestBody(caseBuilder.build());
1499
1500         // Send the request, no cookies associated, use any connection
1501         LOG.debug("Send queue statistics request :{}", mprQueueBuilder.build().toString());
1502         this.messageService.multipartRequest(mprInput.build(), null);
1503
1504         // Prepare rpc return output. Set xid and send it back.
1505         GetQueueStatisticsFromGivenPortOutputBuilder output = new GetQueueStatisticsFromGivenPortOutputBuilder();
1506         output.setTransactionId(generateTransactionId(xid));
1507         output.setQueueIdAndStatisticsMap(null);
1508
1509         Collection<RpcError> errors = Collections.emptyList();
1510         RpcResult<GetQueueStatisticsFromGivenPortOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
1511         return Futures.immediateFuture(rpcResult);
1512     }
1513 }