MD-SAL Statistics Manager- Implemented rpc/notification calls for group/meter statistics
[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
17 import org.opendaylight.controller.sal.common.util.Rpcs;
18 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowConvertor;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.GroupConvertor;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.MeterConvertor;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService;
23 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetAllGroupStatisticsOutputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupDescriptionOutputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupFeaturesOutputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsOutput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsOutputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutputBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutputBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutputBuilder;
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.GetAllMeterConfigStatisticsOutputBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInput;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutputBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesInput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutputBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsInput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsOutput;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsOutputBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Group;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Meter;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.MatchEntries;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.match.grouping.Match;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.match.grouping.MatchBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupDescBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupFeaturesBuilder;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterConfigBuilder;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
98 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
99 import org.opendaylight.yangtools.yang.common.RpcError;
100 import org.opendaylight.yangtools.yang.common.RpcResult;
101 import org.slf4j.Logger;
102
103 import com.google.common.util.concurrent.Futures;
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
111             .getLogger(ModelDrivenSwitchImpl.class);
112     private final NodeId nodeId;
113     private final IMessageDispatchService messageService ;
114     private short version = 0;
115
116     protected ModelDrivenSwitchImpl(NodeId nodeId,
117             InstanceIdentifier<Node> identifier, SessionContext context) {
118         super(identifier, context);
119         this.nodeId = nodeId;
120         messageService = sessionContext.getMessageDispatchService() ;
121         version = context.getPrimaryConductor().getVersion();
122     }
123
124     @Override
125     public Future<RpcResult<AddFlowOutput>> addFlow(AddFlowInput input) {
126         // Convert the AddFlowInput to FlowModInput
127         FlowModInput ofFlowModInput = FlowConvertor.toFlowModInput(input, version);
128
129         // For Flow provisioning, the SwitchConnectionDistinguisher is set to null so
130         // the request can be routed through any connection to the switch
131
132         SwitchConnectionDistinguisher cookie = null ;
133
134         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
135         Future<RpcResult<UpdateFlowOutput>> resultFromOFLib = messageService.flowMod(ofFlowModInput, cookie) ;
136
137         RpcResult<UpdateFlowOutput> rpcResultFromOFLib = null ;
138
139         try {
140                 rpcResultFromOFLib = resultFromOFLib.get();
141         } catch( Exception ex ) {
142                 LOG.error( " Error while getting result for AddFlow RPC" + ex.getMessage());
143         }
144
145         UpdateFlowOutput updateFlowOutput = rpcResultFromOFLib.getResult() ;
146
147         AddFlowOutputBuilder addFlowOutput = new AddFlowOutputBuilder() ;
148         addFlowOutput.setTransactionId(updateFlowOutput.getTransactionId()) ;
149         AddFlowOutput result = addFlowOutput.build();
150
151         Collection<RpcError> errors = rpcResultFromOFLib.getErrors() ;
152         RpcResult<AddFlowOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
153
154         LOG.debug("Returning the Add Flow RPC result to MD-SAL");
155         return Futures.immediateFuture(rpcResult);
156     }
157
158     @Override
159     public Future<RpcResult<AddGroupOutput>> addGroup(AddGroupInput input) {
160         // Convert the AddGroupInput to GroupModInput
161         GroupModInput ofGroupModInput = GroupConvertor.toGroupModInput(input, version);
162
163
164         // For Flow provisioning, the SwitchConnectionDistinguisher is set to null so
165         // the request can be routed through any connection to the switch
166
167         SwitchConnectionDistinguisher cookie = null ;
168
169         LOG.debug("Calling the GroupMod RPC method on MessageDispatchService");
170         Future<RpcResult<UpdateGroupOutput>> resultFromOFLib = messageService.groupMod(ofGroupModInput, cookie) ;
171
172         RpcResult<UpdateGroupOutput> rpcResultFromOFLib = null ;
173
174         try {
175                 rpcResultFromOFLib = resultFromOFLib.get();
176         } catch( Exception ex ) {
177                 LOG.error( " Error while getting result for AddGroup RPC" + ex.getMessage());
178         }
179
180         UpdateGroupOutput updateGroupOutput = rpcResultFromOFLib.getResult() ;
181
182         AddGroupOutputBuilder addGroupOutput = new AddGroupOutputBuilder() ;
183         addGroupOutput.setTransactionId(updateGroupOutput.getTransactionId()) ;
184         AddGroupOutput result = addGroupOutput.build();
185
186         Collection<RpcError> errors = rpcResultFromOFLib.getErrors() ;
187         RpcResult<AddGroupOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
188
189         LOG.debug("Returning the Add Group RPC result to MD-SAL");
190         return Futures.immediateFuture(rpcResult);
191     }
192
193     @Override
194     public Future<RpcResult<AddMeterOutput>> addMeter(AddMeterInput input) {
195         // Convert the AddMeterInput to MeterModInput
196         MeterModInput ofMeterModInput = MeterConvertor.toMeterModInput(input, version);
197
198
199         // For Meter provisioning, the SwitchConnectionDistinguisher is set to null so
200         // the request can be routed through any connection to the switch
201
202         SwitchConnectionDistinguisher cookie = null ;
203
204         LOG.debug("Calling the MeterMod RPC method on MessageDispatchService");
205         Future<RpcResult<UpdateMeterOutput>> resultFromOFLib = messageService.meterMod(ofMeterModInput, cookie) ;
206
207         RpcResult<UpdateMeterOutput> rpcResultFromOFLib = null ;
208
209         try {
210                 rpcResultFromOFLib = resultFromOFLib.get();
211         } catch( Exception ex ) {
212                 LOG.error( " Error while getting result for AddMeter RPC" + ex.getMessage());
213         }
214
215         UpdateMeterOutput updateMeterOutput = rpcResultFromOFLib.getResult() ;
216
217         AddMeterOutputBuilder addMeterOutput = new AddMeterOutputBuilder() ;
218         addMeterOutput.setTransactionId(updateMeterOutput.getTransactionId()) ;
219         AddMeterOutput result = addMeterOutput.build();
220
221         Collection<RpcError> errors = rpcResultFromOFLib.getErrors() ;
222         RpcResult<AddMeterOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
223
224         LOG.debug("Returning the Add Meter RPC result to MD-SAL");
225         return Futures.immediateFuture(rpcResult);
226     }
227
228     @Override
229     public Future<RpcResult<RemoveFlowOutput>> removeFlow(RemoveFlowInput input) {
230         // Convert the RemoveFlowInput to FlowModInput
231         FlowModInput ofFlowModInput = FlowConvertor.toFlowModInput(input, version);
232
233
234         // For Flow provisioning, the SwitchConnectionDistinguisher is set to null so
235         // the request can be routed through any connection to the switch
236
237         SwitchConnectionDistinguisher cookie = null ;
238
239         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
240         Future<RpcResult<UpdateFlowOutput>> resultFromOFLib = messageService.flowMod(ofFlowModInput, cookie) ;
241
242         RpcResult<UpdateFlowOutput> rpcResultFromOFLib = null ;
243
244         try {
245                 rpcResultFromOFLib = resultFromOFLib.get();
246         } catch( Exception ex ) {
247                 LOG.error( " Error while getting result for remove Flow RPC" + ex.getMessage());
248         }
249
250         UpdateFlowOutput updateFlowOutput = rpcResultFromOFLib.getResult() ;
251
252         RemoveFlowOutputBuilder removeFlowOutput = new RemoveFlowOutputBuilder() ;
253         removeFlowOutput.setTransactionId(updateFlowOutput.getTransactionId()) ;
254         RemoveFlowOutput result = removeFlowOutput.build();
255
256         Collection<RpcError> errors = rpcResultFromOFLib.getErrors() ;
257         RpcResult<RemoveFlowOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
258
259         LOG.debug("Returning the Remove Flow RPC result to MD-SAL");
260         return Futures.immediateFuture(rpcResult);
261     }
262
263     @Override
264     public Future<RpcResult<RemoveGroupOutput>> removeGroup(
265             RemoveGroupInput input) {
266         // Convert the RemoveGroupInput to GroupModInput
267         GroupModInput ofGroupModInput = GroupConvertor.toGroupModInput(input.getUpdatedGroup(), version);
268
269
270         // For Flow provisioning, the SwitchConnectionDistinguisher is set to null so
271         // the request can be routed through any connection to the switch
272
273         SwitchConnectionDistinguisher cookie = null ;
274
275         LOG.debug("Calling the GroupMod RPC method on MessageDispatchService");
276         Future<RpcResult<UpdateGroupOutput>> resultFromOFLib = messageService.groupMod(ofGroupModInput, cookie) ;
277
278         RpcResult<UpdateGroupOutput> rpcResultFromOFLib = null ;
279
280         try {
281                 rpcResultFromOFLib = resultFromOFLib.get();
282         } catch( Exception ex ) {
283                 LOG.error( " Error while getting result for RemoveGroup RPC" + ex.getMessage());
284         }
285
286         UpdateGroupOutput updateGroupOutput = rpcResultFromOFLib.getResult() ;
287
288         RemoveGroupOutputBuilder removeGroupOutput = new RemoveGroupOutputBuilder() ;
289         removeGroupOutput.setTransactionId(updateGroupOutput.getTransactionId()) ;
290         RemoveGroupOutput result = removeGroupOutput.build();
291
292         Collection<RpcError> errors = rpcResultFromOFLib.getErrors() ;
293         RpcResult<RemoveGroupOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
294
295         LOG.debug("Returning the Remove Group RPC result to MD-SAL");
296         return Futures.immediateFuture(rpcResult);
297     }
298
299     @Override
300     public Future<RpcResult<RemoveMeterOutput>> removeMeter(
301             RemoveMeterInput input) {
302         // Convert the RemoveMeterInput to MeterModInput
303         MeterModInput ofMeterModInput = MeterConvertor.toMeterModInput(input, version);
304
305
306         // For Meter provisioning, the SwitchConnectionDistinguisher is set to null so
307         // the request can be routed through any connection to the switch
308
309         SwitchConnectionDistinguisher cookie = null ;
310
311         LOG.debug("Calling the MeterMod RPC method on MessageDispatchService");
312         Future<RpcResult<UpdateMeterOutput>> resultFromOFLib = messageService.meterMod(ofMeterModInput, cookie) ;
313
314         RpcResult<UpdateMeterOutput> rpcResultFromOFLib = null ;
315
316         try {
317                 rpcResultFromOFLib = resultFromOFLib.get();
318         } catch( Exception ex ) {
319                 LOG.error( " Error while getting result for RemoveMeter RPC" + ex.getMessage());
320         }
321
322         UpdateMeterOutput updatemeterOutput = rpcResultFromOFLib.getResult() ;
323
324         RemoveMeterOutputBuilder removeMeterOutput = new RemoveMeterOutputBuilder() ;
325         removeMeterOutput.setTransactionId(updatemeterOutput.getTransactionId()) ;
326         RemoveMeterOutput result = removeMeterOutput.build();
327
328         Collection<RpcError> errors = rpcResultFromOFLib.getErrors() ;
329         RpcResult<RemoveMeterOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
330
331         LOG.debug("Returning the Remove Meter RPC result to MD-SAL");
332         return Futures.immediateFuture(rpcResult);
333     }
334
335     @Override
336     public Future<RpcResult<Void>> transmitPacket(TransmitPacketInput input) {
337         // TODO Auto-generated method stub
338         return null;
339     }
340
341     private FlowModInputBuilder toFlowModInputBuilder(Flow source) {
342         FlowModInputBuilder target = new FlowModInputBuilder();
343         target.setCookie(source.getCookie());
344         target.setIdleTimeout(source.getIdleTimeout());
345         target.setHardTimeout(source.getHardTimeout());
346         target.setMatch(toMatch(source.getMatch()));
347
348         return target;
349     }
350
351     private Match toMatch(
352             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match match) {
353         MatchBuilder target = new MatchBuilder();
354
355         target.setMatchEntries(toMatchEntries(match));
356
357         return null;
358     }
359
360     private List<MatchEntries> toMatchEntries(
361             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match match) {
362         List<MatchEntries> entries = new ArrayList<>();
363
364         return null;
365     }
366
367     @Override
368     public Future<RpcResult<UpdateFlowOutput>> updateFlow(UpdateFlowInput input) {
369         // Convert the UpdateFlowInput to FlowModInput
370         FlowModInput ofFlowModInput = FlowConvertor.toFlowModInput(input.getUpdatedFlow(), version);
371
372         // Call the RPC method on MessageDispatchService
373
374         // For Flow provisioning, the SwitchConnectionDistinguisher is set to null so
375         // the request can be routed through any connection to the switch
376
377         SwitchConnectionDistinguisher cookie = null ;
378
379         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
380         Future<RpcResult<UpdateFlowOutput>> resultFromOFLib = messageService.flowMod(ofFlowModInput, cookie) ;
381
382         RpcResult<UpdateFlowOutput> rpcResultFromOFLib = null ;
383
384         try {
385                 rpcResultFromOFLib = resultFromOFLib.get();
386         } catch( Exception ex ) {
387                 LOG.error( " Error while getting result for UpdateFlow RPC" + ex.getMessage());
388         }
389
390         UpdateFlowOutput updateFlowOutputOFLib = rpcResultFromOFLib.getResult() ;
391
392         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder() ;
393         updateFlowOutput.setTransactionId(updateFlowOutputOFLib.getTransactionId()) ;
394         UpdateFlowOutput result = updateFlowOutput.build();
395
396         Collection<RpcError> errors = rpcResultFromOFLib.getErrors() ;
397         RpcResult<UpdateFlowOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
398
399         LOG.debug("Returning the Update Flow RPC result to MD-SAL");
400         return Futures.immediateFuture(rpcResult);
401     }
402
403     @Override
404     public Future<RpcResult<UpdateGroupOutput>> updateGroup(
405             UpdateGroupInput input) {
406         // Convert the UpdateGroupInput to GroupModInput
407         GroupModInput ofGroupModInput = GroupConvertor.toGroupModInput(input.getUpdatedGroup(), version);
408
409
410         // For Flow provisioning, the SwitchConnectionDistinguisher is set to null so
411         // the request can be routed through any connection to the switch
412
413         SwitchConnectionDistinguisher cookie = null ;
414
415         LOG.debug("Calling the GroupMod RPC method on MessageDispatchService");
416         Future<RpcResult<UpdateGroupOutput>> resultFromOFLib = messageService.groupMod(ofGroupModInput, cookie) ;
417
418         RpcResult<UpdateGroupOutput> rpcResultFromOFLib = null ;
419
420         try {
421                 rpcResultFromOFLib = resultFromOFLib.get();
422         } catch( Exception ex ) {
423                 LOG.error( " Error while getting result for updateGroup RPC" + ex.getMessage());
424         }
425
426         UpdateGroupOutput updateGroupOutputOFLib = rpcResultFromOFLib.getResult() ;
427
428         UpdateGroupOutputBuilder updateGroupOutput = new UpdateGroupOutputBuilder() ;
429         updateGroupOutput.setTransactionId(updateGroupOutputOFLib.getTransactionId()) ;
430         UpdateGroupOutput result = updateGroupOutput.build();
431
432         Collection<RpcError> errors = rpcResultFromOFLib.getErrors() ;
433         RpcResult<UpdateGroupOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
434
435         LOG.debug("Returning the Update Group RPC result to MD-SAL");
436         return Futures.immediateFuture(rpcResult);
437     }
438
439     @Override
440     public Future<RpcResult<UpdateMeterOutput>> updateMeter(
441             UpdateMeterInput input) {
442         // Convert the UpdateMeterInput to MeterModInput
443         MeterModInput ofMeterModInput = MeterConvertor.toMeterModInput(input.getUpdatedMeter(), version);
444
445
446         // For Meter provisioning, the SwitchConnectionDistinguisher is set to null so
447         // the request can be routed through any connection to the switch
448
449         SwitchConnectionDistinguisher cookie = null ;
450
451         LOG.debug("Calling the MeterMod RPC method on MessageDispatchService");
452         Future<RpcResult<UpdateMeterOutput>> resultFromOFLib = messageService.meterMod(ofMeterModInput, cookie) ;
453
454         RpcResult<UpdateMeterOutput> rpcResultFromOFLib = null ;
455
456         try {
457                 rpcResultFromOFLib = resultFromOFLib.get();
458         } catch( Exception ex ) {
459                 LOG.error( " Error while getting result for UpdateMeter RPC" + ex.getMessage());
460         }
461
462         UpdateMeterOutput updateMeterOutputFromOFLib = rpcResultFromOFLib.getResult() ;
463
464         UpdateMeterOutputBuilder updateMeterOutput = new UpdateMeterOutputBuilder() ;
465         updateMeterOutput.setTransactionId(updateMeterOutputFromOFLib.getTransactionId()) ;
466         UpdateMeterOutput result = updateMeterOutput.build();
467
468         Collection<RpcError> errors = rpcResultFromOFLib.getErrors() ;
469         RpcResult<UpdateMeterOutput> rpcResult = Rpcs.getRpcResult(true, result, errors);
470
471         LOG.debug("Returning the Update Meter RPC result to MD-SAL");
472         return Futures.immediateFuture(rpcResult);
473     }
474
475     @Override
476     public NodeId getNodeId() {
477         return nodeId;
478     }
479
480     /*
481      * Methods for requesting statistics from switch
482      */
483     @Override
484     public Future<RpcResult<GetAllGroupStatisticsOutput>> getAllGroupStatistics(GetAllGroupStatisticsInput input) {
485
486         //Generate xid to associate it with the request
487         Long xid = this.getSessionContext().getNextXid();
488
489         LOG.debug("Prepare statistics request for all the groups - Transaction id - {}",xid);
490
491         // Create multipart request header
492         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
493         mprInput.setType(MultipartType.OFPMPGROUP);
494         mprInput.setVersion((short)0x04);
495         mprInput.setXid(xid);
496         mprInput.setFlags(new MultipartRequestFlags(false));
497
498         // Create multipart request body for fetch all the group stats
499         MultipartRequestGroupBuilder mprGroupBuild = new MultipartRequestGroupBuilder();
500         mprGroupBuild.setGroupId((long) Group.OFPGALL.getIntValue());
501
502         //Set request body to main multipart request
503         mprInput.setMultipartRequestBody(mprGroupBuild.build());
504
505         //Send the request, no cookies associated, use any connection
506         LOG.debug("Send group statistics request to the switch :{}",mprGroupBuild);
507         this.messageService.multipartRequest(mprInput.build(), null);
508
509         // Prepare rpc return output. Set xid and send it back.
510         LOG.debug("Return results and transaction id back to caller");
511         GetAllGroupStatisticsOutputBuilder output = new GetAllGroupStatisticsOutputBuilder();
512         output.setTransactionId(generateTransactionId(xid));
513         output.setGroupStats(null);
514
515         Collection<RpcError> errors = Collections.emptyList();
516         RpcResult<GetAllGroupStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
517         return Futures.immediateFuture(rpcResult);
518
519     }
520
521     @Override
522     public Future<RpcResult<GetGroupDescriptionOutput>> getGroupDescription(GetGroupDescriptionInput input) {
523
524         //Generate xid to associate it with the request
525         Long xid = this.getSessionContext().getNextXid();
526
527         LOG.debug("Prepare group description statistics request - Transaction id - {}",xid);
528
529         // Create multipart request header
530         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
531         mprInput.setType(MultipartType.OFPMPGROUPDESC);
532         mprInput.setVersion((short)0x04);
533         mprInput.setXid(xid);
534         mprInput.setFlags(new MultipartRequestFlags(false));
535
536         // Create multipart request body for fetch all the group description stats
537         MultipartRequestGroupDescBuilder mprGroupDescBuild = new MultipartRequestGroupDescBuilder();
538
539         //Set request body to main multipart request
540         mprInput.setMultipartRequestBody(mprGroupDescBuild.build());
541
542         //Send the request, no cookies associated, use any connection
543         LOG.debug("Send group desciption statistics request to switch : {}",mprGroupDescBuild);
544         this.messageService.multipartRequest(mprInput.build(), null);
545
546         // Prepare rpc return output. Set xid and send it back.
547         LOG.debug("Return results and transaction id back to caller");
548         GetGroupDescriptionOutputBuilder output = new GetGroupDescriptionOutputBuilder();
549         output.setTransactionId(generateTransactionId(xid));
550         output.setGroupDescStats(null);
551
552         Collection<RpcError> errors = Collections.emptyList();
553         RpcResult<GetGroupDescriptionOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
554         return Futures.immediateFuture(rpcResult);
555
556     }
557
558     @Override
559     public Future<RpcResult<GetGroupFeaturesOutput>> getGroupFeatures(GetGroupFeaturesInput input) {
560
561         //Generate xid to associate it with the request
562         Long xid = this.getSessionContext().getNextXid();
563
564         LOG.debug("Prepare group features statistics request - Transaction id - {}",xid);
565
566         // Create multipart request header
567         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
568         mprInput.setType(MultipartType.OFPMPGROUPFEATURES);
569         mprInput.setVersion((short)0x04);
570         mprInput.setXid(xid);
571         mprInput.setFlags(new MultipartRequestFlags(false));
572
573         // Create multipart request body for fetch all the group description stats
574         MultipartRequestGroupFeaturesBuilder mprGroupFeaturesBuild = new MultipartRequestGroupFeaturesBuilder();
575
576         //Set request body to main multipart request
577         mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
578
579         //Send the request, no cookies associated, use any connection
580         LOG.debug("Send group features statistics request :{}",mprGroupFeaturesBuild);
581         this.messageService.multipartRequest(mprInput.build(), null);
582
583         // Prepare rpc return output. Set xid and send it back.
584         LOG.debug("Return results and transaction id back to caller");
585         GetGroupFeaturesOutputBuilder output = new GetGroupFeaturesOutputBuilder();
586         output.setTransactionId(generateTransactionId(xid));
587
588         Collection<RpcError> errors = Collections.emptyList();
589         RpcResult<GetGroupFeaturesOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
590         return Futures.immediateFuture(rpcResult);
591     }
592
593     @Override
594     public Future<RpcResult<GetGroupStatisticsOutput>> getGroupStatistics(GetGroupStatisticsInput input) {
595
596         //Generate xid to associate it with the request
597         Long xid = this.getSessionContext().getNextXid();
598
599         LOG.debug("Prepare statistics request for group ({}) - Transaction id - {}",input.getId().toString(),xid);
600
601         // Create multipart request header
602         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
603         mprInput.setType(MultipartType.OFPMPGROUP);
604         mprInput.setVersion((short)0x04);
605         mprInput.setXid(xid);
606         mprInput.setFlags(new MultipartRequestFlags(false));
607
608         // Create multipart request body for fetch all the group stats
609         MultipartRequestGroupBuilder mprGroupBuild = new MultipartRequestGroupBuilder();
610         mprGroupBuild.setGroupId(input.getGroupId().getValue());
611
612         //Set request body to main multipart request
613         mprInput.setMultipartRequestBody(mprGroupBuild.build());
614
615         //Send the request, no cookies associated, use any connection
616         LOG.debug("Send group statistics request :{}",mprGroupBuild);
617         this.messageService.multipartRequest(mprInput.build(), null);
618
619         // Prepare rpc return output. Set xid and send it back.
620         LOG.debug("Return results and transaction id back to caller");
621         GetGroupStatisticsOutputBuilder output = new GetGroupStatisticsOutputBuilder();
622         output.setTransactionId(generateTransactionId(xid));
623         output.setGroupStats(null);
624
625         Collection<RpcError> errors = Collections.emptyList();
626         RpcResult<GetGroupStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
627         return Futures.immediateFuture(rpcResult);
628     }
629
630     @Override
631     public Future<RpcResult<GetAllMeterConfigStatisticsOutput>> getAllMeterConfigStatistics(
632             GetAllMeterConfigStatisticsInput input) {
633
634         //Generate xid to associate it with the request
635         Long xid = this.getSessionContext().getNextXid();
636
637         LOG.debug("Prepare config request for all the meters - Transaction id - {}",xid);
638
639         // Create multipart request header
640         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
641         mprInput.setType(MultipartType.OFPMPMETERCONFIG);
642         mprInput.setVersion((short)0x04);
643         mprInput.setXid(xid);
644         mprInput.setFlags(new MultipartRequestFlags(false));
645
646         // Create multipart request body for fetch all the meter stats
647         MultipartRequestMeterConfigBuilder mprMeterConfigBuild = new MultipartRequestMeterConfigBuilder();
648         mprMeterConfigBuild.setMeterId((long) Meter.OFPMALL.getIntValue());
649
650         //Set request body to main multipart request
651         mprInput.setMultipartRequestBody(mprMeterConfigBuild.build());
652
653         //Send the request, no cookies associated, use any connection
654         LOG.debug("Send meter statistics request :{}",mprMeterConfigBuild);
655         this.messageService.multipartRequest(mprInput.build(), null);
656
657         // Prepare rpc return output. Set xid and send it back.
658         LOG.debug("Return results and transaction id back to caller");
659         GetAllMeterConfigStatisticsOutputBuilder output = new GetAllMeterConfigStatisticsOutputBuilder();
660         output.setTransactionId(generateTransactionId(xid));
661         output.setMeterConfigStats(null);
662
663         Collection<RpcError> errors = Collections.emptyList();
664         RpcResult<GetAllMeterConfigStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
665         return Futures.immediateFuture(rpcResult);
666     }
667
668     @Override
669     public Future<RpcResult<GetAllMeterStatisticsOutput>> getAllMeterStatistics(GetAllMeterStatisticsInput input) {
670
671         //Generate xid to associate it with the request
672         Long xid = this.getSessionContext().getNextXid();
673
674         LOG.debug("Prepare statistics request for all the meters - Transaction id - {}",xid);
675
676         // Create multipart request header
677         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
678         mprInput.setType(MultipartType.OFPMPMETER);
679         mprInput.setVersion((short)0x04);
680         mprInput.setXid(xid);
681         mprInput.setFlags(new MultipartRequestFlags(false));
682
683         // Create multipart request body for fetch all the meter stats
684         MultipartRequestMeterBuilder mprMeterBuild = new MultipartRequestMeterBuilder();
685         mprMeterBuild.setMeterId((long) Meter.OFPMALL.getIntValue());
686
687         //Set request body to main multipart request
688         mprInput.setMultipartRequestBody(mprMeterBuild.build());
689
690         //Send the request, no cookies associated, use any connection
691         LOG.debug("Send meter statistics request :{}",mprMeterBuild);
692         this.messageService.multipartRequest(mprInput.build(), null);
693
694         // Prepare rpc return output. Set xid and send it back.
695         LOG.debug("Return results and transaction id back to caller");
696         GetAllMeterStatisticsOutputBuilder output = new GetAllMeterStatisticsOutputBuilder();
697         output.setTransactionId(generateTransactionId(xid));
698         output.setMeterStats(null);
699
700         Collection<RpcError> errors = Collections.emptyList();
701         RpcResult<GetAllMeterStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
702         return Futures.immediateFuture(rpcResult);
703     }
704
705     @Override
706     public Future<RpcResult<GetMeterFeaturesOutput>> getMeterFeatures(GetMeterFeaturesInput input) {
707
708         //Generate xid to associate it with the request
709         Long xid = this.getSessionContext().getNextXid();
710
711         LOG.debug("Prepare features statistics request for all the meters - Transaction id - {}",xid);
712
713         // Create multipart request header
714         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
715         mprInput.setType(MultipartType.OFPMPMETERFEATURES);
716         mprInput.setVersion((short)0x04);
717         mprInput.setXid(xid);
718         mprInput.setFlags(new MultipartRequestFlags(false));
719
720         // Create multipart request body for fetch all the group description stats
721         MultipartRequestMeterFeaturesBuilder mprMeterFeaturesBuild = new MultipartRequestMeterFeaturesBuilder();
722
723         //Set request body to main multipart request
724         mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
725
726         //Send the request, no cookies associated, use any connection
727         LOG.debug("Send meter features statistics request :{}",mprMeterFeaturesBuild);
728         this.messageService.multipartRequest(mprInput.build(), null);
729
730         // Prepare rpc return output. Set xid and send it back.
731         LOG.debug("Return results and transaction id back to caller");
732         GetMeterFeaturesOutputBuilder output = new GetMeterFeaturesOutputBuilder();
733         output.setTransactionId(generateTransactionId(xid));
734
735         Collection<RpcError> errors = Collections.emptyList();
736         RpcResult<GetMeterFeaturesOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
737         return Futures.immediateFuture(rpcResult);
738     }
739
740     @Override
741     public Future<RpcResult<GetMeterStatisticsOutput>> getMeterStatistics(GetMeterStatisticsInput input) {
742         //Generate xid to associate it with the request
743         Long xid = this.getSessionContext().getNextXid();
744
745         LOG.debug("Preprae statistics request for Meter ({}) - Transaction id - {}",input.getMeterId().getValue(),xid);
746
747         // Create multipart request header
748         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
749         mprInput.setType(MultipartType.OFPMPMETER);
750         mprInput.setVersion((short)0x04);
751         mprInput.setXid(xid);
752         mprInput.setFlags(new MultipartRequestFlags(false));
753
754         // Create multipart request body for fetch all the meter stats
755         MultipartRequestMeterBuilder mprMeterBuild = new MultipartRequestMeterBuilder();
756         //Select specific meter
757         mprMeterBuild.setMeterId(input.getMeterId().getValue());
758
759         //Set request body to main multipart request
760         mprInput.setMultipartRequestBody(mprMeterBuild.build());
761
762         //Send the request, no cookies associated, use any connection
763         LOG.debug("Send meter statistics request :{}",mprMeterBuild);
764         this.messageService.multipartRequest(mprInput.build(), null);
765
766         // Prepare rpc return output. Set xid and send it back.
767         LOG.debug("Return results and transaction id back to caller");
768         GetMeterStatisticsOutputBuilder output = new GetMeterStatisticsOutputBuilder();
769         output.setTransactionId(generateTransactionId(xid));
770         output.setMeterStats(null);
771
772         Collection<RpcError> errors = Collections.emptyList();
773         RpcResult<GetMeterStatisticsOutput> rpcResult = Rpcs.getRpcResult(true, output.build(), errors);
774         return Futures.immediateFuture(rpcResult);
775     }
776
777     private TransactionId generateTransactionId(Long xid){
778         String stringXid =xid.toString();
779         BigInteger bigIntXid = new BigInteger( stringXid );
780         return new TransactionId(bigIntXid);
781
782     }
783 }