3d6add0f1f51550fd1398984f149b12a432a5321
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsGatheringUtilsTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.impl.statistics;
10
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
13
14 import com.google.common.base.Optional;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.math.BigInteger;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.InOrder;
31 import org.mockito.Matchers;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.runners.MockitoJUnitRunner;
35 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
36 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
37 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
38 import org.opendaylight.openflowplugin.api.OFConstants;
39 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
40 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
41 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
42 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
43 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
44 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
45 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
46 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
47 import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry;
48 import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry;
49 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
50 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.StatisticsGatherer;
51 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
52 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.queue.rev130925.QueueId;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupId;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupType;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterId;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsListBuilder;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCaseBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupCaseBuilder;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyGroupDescCaseBuilder;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterCaseBuilder;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterConfigCaseBuilder;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyPortStatsCaseBuilder;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyQueueCaseBuilder;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableCaseBuilder;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlowBuilder;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group._case.MultipartReplyGroupBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group._case.multipart.reply.group.GroupStatsBuilder;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group._case.multipart.reply.group.group.stats.BucketStats;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group._case.multipart.reply.group.group.stats.BucketStatsBuilder;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.desc._case.MultipartReplyGroupDescBuilder;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.desc._case.multipart.reply.group.desc.GroupDescBuilder;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.MultipartReplyMeterBuilder;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.MeterStatsBuilder;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.meter.stats.MeterBandStatsBuilder;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.MultipartReplyMeterConfigBuilder;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.multipart.reply.meter.config.MeterConfigBuilder;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.multipart.reply.meter.config.meter.config.Bands;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.MultipartReplyPortStatsBuilder;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.stats._case.multipart.reply.port.stats.PortStatsBuilder;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.MultipartReplyQueueBuilder;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.queue._case.multipart.reply.queue.QueueStatsBuilder;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.MultipartReplyTableBuilder;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.multipart.reply.table.TableStatsBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatistics;
132 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
133 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
134 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
135
136
137 @RunWith(MockitoJUnitRunner.class)
138 public class StatisticsGatheringUtilsTest {
139
140     static final String DUMMY_NODE_ID_VALUE = "1";
141     static final NodeId DUMMY_NODE_ID = new NodeId(DUMMY_NODE_ID_VALUE);
142
143     private final KeyedInstanceIdentifier<Node, NodeKey> dummyNodePath = InstanceIdentifier.create(Nodes.class)
144             .child(Node.class, new NodeKey(DUMMY_NODE_ID));
145     @Mock
146     private DeviceContext deviceContext;
147     @Mock
148     private DeviceState deviceState;
149     @Mock
150     private DeviceFlowRegistry deviceFlowRegistry;
151     @Mock
152     private DeviceGroupRegistry deviceGroupRegistry;
153     @Mock
154     private DeviceMeterRegistry deviceMeterRegistry;
155     @Mock
156     private FlowDescriptor flowDescriptor;
157     @Mock
158     private FlowId flowId;
159     @Mock
160     private GetFeaturesOutput features;
161     @Mock
162     private ReadOnlyTransaction readTx;
163     @Mock
164     private ConnectionContext connectionAdapter;
165     @Mock
166     private StatisticsGatherer statisticsService;
167     @Mock
168     private DeviceInfo deviceInfo;
169     @Mock
170     private TxFacade txFacade;
171
172     private SinglePurposeMultipartReplyTranslator singlePurposeMultipartReplyTranslator;
173
174     @Before
175     public void setUp() throws Exception {
176         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
177         singlePurposeMultipartReplyTranslator = new SinglePurposeMultipartReplyTranslator(convertorManager);
178         when(deviceContext.getDeviceState()).thenReturn(deviceState);
179         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
180         when(deviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
181         when(deviceContext.getDeviceGroupRegistry()).thenReturn(deviceGroupRegistry);
182         when(deviceContext.getDeviceMeterRegistry()).thenReturn(deviceMeterRegistry);
183         when(deviceFlowRegistry.retrieveDescriptor(Matchers.any(FlowRegistryKey.class))).thenReturn(flowDescriptor);
184         when(deviceContext.getReadTransaction()).thenReturn(readTx);
185         when(txFacade.getReadTransaction()).thenReturn(readTx);
186         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionAdapter);
187         when(connectionAdapter.getNodeId()).thenReturn(DUMMY_NODE_ID);
188         when(connectionAdapter.getFeatures()).thenReturn(features);
189         when(features.getDatapathId()).thenReturn(BigInteger.ONE);
190         when(features.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
191
192         when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
193         when(deviceInfo.getDatapathId()).thenReturn(BigInteger.ONE);
194         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(dummyNodePath);
195         when(deviceInfo.getNodeId()).thenReturn(DUMMY_NODE_ID);
196     }
197
198     @Test
199     public void testWriteFlowStatistics() throws Exception {
200         final ArgumentCaptor<LogicalDatastoreType> dataStoreType = ArgumentCaptor.forClass(LogicalDatastoreType.class);
201         final ArgumentCaptor<InstanceIdentifier> flowPath = ArgumentCaptor.forClass(InstanceIdentifier.class);
202         final ArgumentCaptor<Flow> flow = ArgumentCaptor.forClass(Flow.class);
203
204         StatisticsGatheringUtils.writeFlowStatistics(prepareFlowStatisticsData(),
205                 deviceInfo, deviceContext.getDeviceFlowRegistry(), deviceContext);
206
207         Mockito.verify(deviceContext).writeToTransaction(
208                 dataStoreType.capture(), flowPath.capture(), flow.capture());
209         Assert.assertEquals(LogicalDatastoreType.OPERATIONAL, dataStoreType.getValue());
210         final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = flowPath.getValue();
211         Assert.assertEquals(DUMMY_NODE_ID, flowCapableNodePath.firstKeyOf(Node.class, NodeKey.class).getId());
212         Assert.assertEquals(42, flow.getValue().getTableId().intValue());
213     }
214
215     private Iterable<FlowsStatisticsUpdate> prepareFlowStatisticsData() {
216         final FlowAndStatisticsMapListBuilder flowAndStatsMapListBld = new FlowAndStatisticsMapListBuilder();
217         flowAndStatsMapListBld.setTableId((short) 42);
218         flowAndStatsMapListBld.setMatch(new MatchBuilder().build());
219
220         final FlowsStatisticsUpdateBuilder flowStatsUpdateBld1 = new FlowsStatisticsUpdateBuilder();
221         flowStatsUpdateBld1.setFlowAndStatisticsMapList(Lists.newArrayList(flowAndStatsMapListBld.build()));
222
223         return Lists.newArrayList(flowStatsUpdateBld1.build());
224     }
225
226
227     @Test
228     public void testGatherStatistics_group() throws Exception {
229         final MultipartType type = MultipartType.OFPMPGROUP;
230         final long groupIdValue = 19L;
231
232         final GroupStatsBuilder groupStatsBld = new GroupStatsBuilder()
233                 .setBucketStats(Lists.newArrayList(createBucketStat(21L, 42L)))
234                 .setByteCount(BigInteger.valueOf(84L))
235                 .setPacketCount(BigInteger.valueOf(63L))
236                 .setDurationSec(11L)
237                 .setDurationNsec(12L)
238                 .setRefCount(13L)
239                 .setGroupId(new GroupId(groupIdValue));
240         final MultipartReplyGroupBuilder mpReplyGroupBld = new MultipartReplyGroupBuilder();
241         mpReplyGroupBld.setGroupStats(Lists.newArrayList(groupStatsBld.build()));
242         final MultipartReplyGroupCaseBuilder mpReplyGroupCaseBld = new MultipartReplyGroupCaseBuilder();
243         mpReplyGroupCaseBld.setMultipartReplyGroup(mpReplyGroupBld.build());
244
245         final MultipartReply groupStatsUpdated = assembleMPReplyMessage(type, mpReplyGroupCaseBld.build());
246         final List<MultipartReply> statsData = Collections.singletonList(groupStatsUpdated);
247
248         fireAndCheck(type, statsData);
249
250         final InstanceIdentifier<GroupStatistics> groupPath = dummyNodePath.augmentation(FlowCapableNode.class)
251                 .child(Group.class, new GroupKey(
252                         new org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId(groupIdValue)))
253                 .augmentation(NodeGroupStatistics.class)
254                 .child(GroupStatistics.class);
255         verify(txFacade).writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
256                 Matchers.eq(groupPath), Matchers.any(GroupStatistics.class));
257     }
258
259     @Test
260     public void testGatherStatistics_groupDesc() throws Exception {
261         final MultipartType type = MultipartType.OFPMPGROUPDESC;
262         final long groupIdValue = 27L;
263
264         final BucketsListBuilder bucketsListBld = new BucketsListBuilder()
265                 .setWatchPort(new PortNumber(5L));
266         final GroupDescBuilder groupStatsBld = new GroupDescBuilder()
267                 .setBucketsList(Lists.newArrayList(bucketsListBld.build()))
268                 .setGroupId(new GroupId(groupIdValue))
269                 .setType(GroupType.OFPGTALL);
270         final MultipartReplyGroupDescBuilder mpReplyGroupBld = new MultipartReplyGroupDescBuilder();
271         mpReplyGroupBld.setGroupDesc(Lists.newArrayList(groupStatsBld.build()));
272         final MultipartReplyGroupDescCaseBuilder mpReplyGroupCaseBld = new MultipartReplyGroupDescCaseBuilder();
273         mpReplyGroupCaseBld.setMultipartReplyGroupDesc(mpReplyGroupBld.build());
274
275         final MultipartReply groupStatsUpdated = assembleMPReplyMessage(type, mpReplyGroupCaseBld.build());
276         final List<MultipartReply> statsData = Collections.singletonList(groupStatsUpdated);
277
278         fireAndCheck(type, statsData);
279
280         final org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId storedGroupId
281                 = new org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId(groupIdValue);
282         final KeyedInstanceIdentifier<Group, GroupKey> groupPath
283                 = dummyNodePath.augmentation(FlowCapableNode.class).child(Group.class, new GroupKey(storedGroupId));
284
285         verify(deviceContext, Mockito.never()).addDeleteToTxChain(
286                 Matchers.eq(LogicalDatastoreType.OPERATIONAL), Matchers.<InstanceIdentifier<?>>any());
287         verify(deviceGroupRegistry).processMarks();
288         verify(deviceGroupRegistry).store(storedGroupId);
289         verify(txFacade).writeToTransaction(
290                 Matchers.eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(groupPath), Matchers.any(Group.class));
291     }
292
293     @Test
294     public void testGatherStatistics_meter() throws Exception {
295         final MultipartType type = MultipartType.OFPMPMETER;
296         final long meterIdValue = 19L;
297
298         final MeterBandStatsBuilder meterBandStatsBld = new MeterBandStatsBuilder()
299                 .setByteBandCount(BigInteger.valueOf(91L))
300                 .setPacketBandCount(BigInteger.valueOf(92L));
301         final MeterStatsBuilder meterStatsBld = new MeterStatsBuilder()
302                 .setMeterId(new MeterId(meterIdValue))
303                 .setByteInCount(BigInteger.valueOf(111L))
304                 .setDurationSec(112L)
305                 .setDurationNsec(113L)
306                 .setFlowCount(114L)
307                 .setPacketInCount(BigInteger.valueOf(115L))
308                 .setMeterBandStats(Lists.newArrayList(meterBandStatsBld.build()));
309         final MultipartReplyMeterBuilder mpReplyMeterBld = new MultipartReplyMeterBuilder();
310         mpReplyMeterBld.setMeterStats(Lists.newArrayList(meterStatsBld.build()));
311         final MultipartReplyMeterCaseBuilder mpReplyMeterCaseBld = new MultipartReplyMeterCaseBuilder();
312         mpReplyMeterCaseBld.setMultipartReplyMeter(mpReplyMeterBld.build());
313
314         final MultipartReply meterStatsUpdated = assembleMPReplyMessage(type, mpReplyMeterCaseBld.build());
315         final List<MultipartReply> statsData = Collections.singletonList(meterStatsUpdated);
316
317         fireAndCheck(type, statsData);
318
319         final InstanceIdentifier<MeterStatistics> meterPath = dummyNodePath.augmentation(FlowCapableNode.class)
320                 .child(Meter.class, new MeterKey(
321                         new org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId(meterIdValue)))
322                 .augmentation(NodeMeterStatistics.class)
323                 .child(MeterStatistics.class);
324         verify(txFacade).writeToTransaction(Matchers.eq(LogicalDatastoreType.OPERATIONAL),
325                 Matchers.eq(meterPath), Matchers.any(MeterStatistics.class));
326     }
327
328     @Test
329     public void testGatherStatistics_nodeConnector() throws Exception {
330         final MultipartType type = MultipartType.OFPMPPORTSTATS;
331
332         final PortStatsBuilder portStatsBld = new PortStatsBuilder()
333                 .setPortNo(11L);
334         final MultipartReplyPortStatsBuilder mpReplyMeterBld = new MultipartReplyPortStatsBuilder();
335         mpReplyMeterBld.setPortStats(Lists.newArrayList(portStatsBld.build()));
336         final MultipartReplyPortStatsCaseBuilder mpReplyMeterCaseBld = new MultipartReplyPortStatsCaseBuilder();
337         mpReplyMeterCaseBld.setMultipartReplyPortStats(mpReplyMeterBld.build());
338
339         final MultipartReply meterStatsUpdated = assembleMPReplyMessage(type, mpReplyMeterCaseBld.build());
340         final List<MultipartReply> statsData = Collections.singletonList(meterStatsUpdated);
341
342         fireAndCheck(type, statsData);
343
344         final InstanceIdentifier<FlowCapableNodeConnectorStatistics> portPath = dummyNodePath
345                 .child(NodeConnector.class, new NodeConnectorKey(
346                         new NodeConnectorId("openflow:" + DUMMY_NODE_ID_VALUE + ":11")))
347                 .augmentation(FlowCapableNodeConnectorStatisticsData.class)
348                 .child(FlowCapableNodeConnectorStatistics.class);
349         verify(txFacade).writeToTransaction(
350                 Matchers.eq(LogicalDatastoreType.OPERATIONAL),
351                 Matchers.eq(portPath),
352                 Matchers.any(FlowCapableNodeConnectorStatistics.class));
353     }
354
355     @Test
356     public void testGatherStatistics_table() throws Exception {
357         final MultipartType type = MultipartType.OFPMPTABLE;
358
359         final TableStatsBuilder tableStatsBld = new TableStatsBuilder()
360                 .setActiveCount(33L)
361                 .setLookupCount(BigInteger.valueOf(34L))
362                 .setMatchedCount(BigInteger.valueOf(35L))
363                 .setTableId((short) 0);
364         final MultipartReplyTableBuilder mpReplyTableBld = new MultipartReplyTableBuilder();
365         mpReplyTableBld.setTableStats(Lists.newArrayList(tableStatsBld.build()));
366         final MultipartReplyTableCaseBuilder mpReplyTableCaseBld = new MultipartReplyTableCaseBuilder();
367         mpReplyTableCaseBld.setMultipartReplyTable(mpReplyTableBld.build());
368
369         final MultipartReply meterStatsUpdated = assembleMPReplyMessage(type, mpReplyTableCaseBld.build());
370         final List<MultipartReply> statsData = Collections.singletonList(meterStatsUpdated);
371
372         fireAndCheck(type, statsData);
373
374         final InstanceIdentifier<FlowTableStatistics> tablePath = dummyNodePath
375                 .augmentation(FlowCapableNode.class)
376                 .child(Table.class, new TableKey((short) 0))
377                 .augmentation(FlowTableStatisticsData.class)
378                 .child(FlowTableStatistics.class);
379         verify(txFacade).writeToTransaction(
380                 Matchers.eq(LogicalDatastoreType.OPERATIONAL),
381                 Matchers.eq(tablePath),
382                 Matchers.any(FlowTableStatistics.class));
383     }
384
385     @Test
386     public void testGatherStatistics_queue() throws Exception {
387         final MultipartType type = MultipartType.OFPMPQUEUE;
388
389         final long queueIdValue = 4L;
390         final QueueStatsBuilder queueStatsBld = new QueueStatsBuilder()
391                 .setPortNo(11L)
392                 .setTxBytes(BigInteger.valueOf(44L))
393                 .setTxErrors(BigInteger.valueOf(45L))
394                 .setTxPackets(BigInteger.valueOf(46L))
395                 .setDurationSec(47L)
396                 .setDurationNsec(48L)
397                 .setQueueId(queueIdValue);
398
399         final MultipartReplyQueueBuilder mpReplyQueueBld = new MultipartReplyQueueBuilder();
400         mpReplyQueueBld.setQueueStats(Lists.newArrayList(queueStatsBld.build()));
401         final MultipartReplyQueueCaseBuilder mpReplyQueueCaseBld = new MultipartReplyQueueCaseBuilder();
402         mpReplyQueueCaseBld.setMultipartReplyQueue(mpReplyQueueBld.build());
403
404         final MultipartReply meterStatsUpdated = assembleMPReplyMessage(type, mpReplyQueueCaseBld.build());
405         final List<MultipartReply> statsData = Collections.singletonList(meterStatsUpdated);
406
407         fireAndCheck(type, statsData);
408
409         final KeyedInstanceIdentifier<Queue, QueueKey> queuePath = dummyNodePath
410                 .child(NodeConnector.class, new NodeConnectorKey(
411                         new NodeConnectorId("openflow:" + DUMMY_NODE_ID_VALUE + ":11")))
412                 .augmentation(FlowCapableNodeConnector.class)
413                 .child(Queue.class, new QueueKey(new QueueId(queueIdValue)));
414         verify(txFacade).writeToTransaction(
415                 Matchers.eq(LogicalDatastoreType.OPERATIONAL),
416                 Matchers.eq(queuePath),
417                 Matchers.any(Queue.class));
418     }
419
420     @Test
421     public void testGatherStatistics_flow() throws Exception {
422         final short tableId = 0;
423         final MultipartType type = MultipartType.OFPMPFLOW;
424
425         final InstanceIdentifier<FlowCapableNode> nodePath
426                 = deviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
427         final TableBuilder tableDataBld = new TableBuilder();
428         tableDataBld.setId(tableId);
429         final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
430         flowNodeBuilder.setTable(Collections.singletonList(tableDataBld.build()));
431         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
432         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture
433                 = Futures.immediateCheckedFuture(flowNodeOpt);
434         when(readTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
435         when(flowDescriptor.getFlowId()).thenReturn(flowId);
436
437         final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder
438                 matchBld =
439                 new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder()
440                         .setMatchEntry(Collections.<MatchEntry>emptyList());
441         final FlowStatsBuilder flowStatsBld = new FlowStatsBuilder()
442                 .setByteCount(BigInteger.valueOf(55L))
443                 .setPacketCount(BigInteger.valueOf(56L))
444                 .setDurationSec(57L)
445                 .setDurationNsec(58L)
446                 .setTableId((short) 0)
447                 .setMatch(matchBld.build())
448                 .setFlags(new FlowModFlags(true, false, false, false, true));
449
450         final MultipartReplyFlowBuilder mpReplyFlowBld = new MultipartReplyFlowBuilder();
451         mpReplyFlowBld.setFlowStats(Lists.newArrayList(flowStatsBld.build()));
452         final MultipartReplyFlowCaseBuilder mpReplyFlowCaseBld = new MultipartReplyFlowCaseBuilder();
453         mpReplyFlowCaseBld.setMultipartReplyFlow(mpReplyFlowBld.build());
454
455         final MultipartReply flowStatsUpdated = assembleMPReplyMessage(type, mpReplyFlowCaseBld.build());
456         final List<MultipartReply> statsData = Collections.singletonList(flowStatsUpdated);
457         fireAndCheck(type, statsData);
458
459         final FlowBuilder flowBld = new FlowBuilder()
460                 .setTableId((short) 0)
461                 .setMatch(new MatchBuilder().build());
462         final KeyedInstanceIdentifier<Table, TableKey> tablePath = dummyNodePath.augmentation(FlowCapableNode.class)
463                 .child(Table.class, new TableKey((short) 0));
464         final KeyedInstanceIdentifier<Flow, FlowKey> flowPath =  tablePath.child(Flow.class, new FlowKey(flowId));
465
466         verify(deviceContext, Mockito.never()).addDeleteToTxChain(
467                 Matchers.eq(LogicalDatastoreType.OPERATIONAL), Matchers.<InstanceIdentifier<?>>any());
468
469         final InOrder inOrder = Mockito.inOrder(txFacade);
470         inOrder.verify(txFacade).writeToTransaction(Matchers.eq(
471                 LogicalDatastoreType.OPERATIONAL), Matchers.eq(tablePath), Matchers.any(Table.class));
472     }
473
474     @Test
475     public void testGatherStatistics_meterConfig() throws Exception {
476         final MultipartType type = MultipartType.OFPMPMETERCONFIG;
477         final Long meterIdValue = 55L;
478
479         final MeterConfigBuilder meterConfigBld = new MeterConfigBuilder()
480                 .setMeterId(new MeterId(meterIdValue))
481                 .setFlags(new MeterFlags(false, true, false, true))
482                 .setBands(Collections.<Bands>emptyList());
483
484         final MultipartReplyMeterConfigBuilder mpReplyMeterConfigBld = new MultipartReplyMeterConfigBuilder();
485         mpReplyMeterConfigBld.setMeterConfig(Lists.newArrayList(meterConfigBld.build()));
486         final MultipartReplyMeterConfigCaseBuilder mpReplyMeterConfigCaseBld
487                 = new MultipartReplyMeterConfigCaseBuilder();
488         mpReplyMeterConfigCaseBld.setMultipartReplyMeterConfig(mpReplyMeterConfigBld.build());
489
490         final MultipartReply meterConfigUpdated = assembleMPReplyMessage(type, mpReplyMeterConfigCaseBld.build());
491         final List<MultipartReply> statsData = Collections.singletonList(meterConfigUpdated);
492
493         fireAndCheck(type, statsData);
494
495         final org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId meterId =
496                 new org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId(meterIdValue);
497         final KeyedInstanceIdentifier<Meter, MeterKey> meterPath = dummyNodePath.augmentation(FlowCapableNode.class)
498                 .child(Meter.class, new MeterKey(meterId));
499         verify(deviceContext, Mockito.never()).addDeleteToTxChain(
500                 Matchers.eq(LogicalDatastoreType.OPERATIONAL), Matchers.<InstanceIdentifier<?>>any());
501         verify(deviceMeterRegistry).store(meterId);
502         verify(txFacade).writeToTransaction(
503                 Matchers.eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(meterPath), Matchers.any(Meter.class));
504     }
505
506     private void fireAndCheck(
507             final MultipartType type,
508             final List<MultipartReply> statsData) throws InterruptedException, ExecutionException, TimeoutException {
509         when(statisticsService.getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.eq(type)))
510                 .thenReturn(Futures.immediateFuture(RpcResultBuilder.success(statsData).build()));
511
512         final ListenableFuture<Boolean> gatherStatisticsResult = StatisticsGatheringUtils.gatherStatistics(
513                 statisticsService,
514                 deviceInfo,
515                 type,
516                 txFacade,
517                 deviceContext,
518                 false,
519                 singlePurposeMultipartReplyTranslator);
520         Assert.assertTrue(gatherStatisticsResult.get(1, TimeUnit.SECONDS));
521         verify(txFacade).submitTransaction();
522     }
523
524     private static MultipartReplyMessage assembleMPReplyMessage(
525             final MultipartType type,
526             final MultipartReplyBody mpReplyGroupCaseBld) {
527         return new MultipartReplyMessageBuilder()
528                 .setMultipartReplyBody(mpReplyGroupCaseBld)
529                 .setType(type)
530                 .setFlags(new MultipartRequestFlags(false))
531                 .setXid(42L)
532                 .build();
533     }
534
535     private static BucketStats createBucketStat(final long byteCount, final long packetCount) {
536         return new BucketStatsBuilder().setByteCount(
537                 BigInteger.valueOf(byteCount)).setPacketCount(BigInteger.valueOf(packetCount)).build();
538     }
539
540     @Test
541     public void testDeleteAllKnownFlows() throws Exception {
542         final short tableId = 0;
543         final InstanceIdentifier<FlowCapableNode> nodePath
544                 = deviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
545         final TableBuilder tableDataBld = new TableBuilder();
546         tableDataBld.setId(tableId);
547         final FlowCapableNodeBuilder flowNodeBuilder = new FlowCapableNodeBuilder();
548         flowNodeBuilder.setTable(Collections.singletonList(tableDataBld.build()));
549         final Optional<FlowCapableNode> flowNodeOpt = Optional.of(flowNodeBuilder.build());
550         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowNodeFuture
551                 = Futures.immediateCheckedFuture(flowNodeOpt);
552         when(readTx.read(LogicalDatastoreType.OPERATIONAL, nodePath)).thenReturn(flowNodeFuture);
553         final KeyedInstanceIdentifier<Table, TableKey> tablePath = deviceInfo.getNodeInstanceIdentifier()
554                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId));
555
556         StatisticsGatheringUtils.deleteAllKnownFlows(deviceInfo,
557                 deviceContext.getDeviceFlowRegistry(), txFacade);
558
559         verify(txFacade).writeToTransaction(
560                 LogicalDatastoreType.OPERATIONAL,
561                 tablePath,
562                 tableDataBld.setFlow(Collections.<Flow>emptyList()).build());
563     }
564 }