2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
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;
137 @RunWith(MockitoJUnitRunner.class)
138 public class StatisticsGatheringUtilsTest {
140 static final String DUMMY_NODE_ID_VALUE = "1";
141 static final NodeId DUMMY_NODE_ID = new NodeId(DUMMY_NODE_ID_VALUE);
143 private final KeyedInstanceIdentifier<Node, NodeKey> dummyNodePath = InstanceIdentifier.create(Nodes.class)
144 .child(Node.class, new NodeKey(DUMMY_NODE_ID));
146 private DeviceContext deviceContext;
148 private DeviceState deviceState;
150 private DeviceFlowRegistry deviceFlowRegistry;
152 private DeviceGroupRegistry deviceGroupRegistry;
154 private DeviceMeterRegistry deviceMeterRegistry;
156 private FlowDescriptor flowDescriptor;
158 private FlowId flowId;
160 private GetFeaturesOutput features;
162 private ReadOnlyTransaction readTx;
164 private ConnectionContext connectionAdapter;
166 private StatisticsGatherer statisticsService;
168 private DeviceInfo deviceInfo;
170 private TxFacade txFacade;
172 private SinglePurposeMultipartReplyTranslator singlePurposeMultipartReplyTranslator;
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.retrieveIdForFlow(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);
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);
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);
204 StatisticsGatheringUtils.writeFlowStatistics(prepareFlowStatisticsData(),
205 deviceInfo, deviceContext.getDeviceFlowRegistry(), deviceContext);
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());
215 private Iterable<FlowsStatisticsUpdate> prepareFlowStatisticsData() {
216 final FlowAndStatisticsMapListBuilder flowAndStatsMapListBld = new FlowAndStatisticsMapListBuilder();
217 flowAndStatsMapListBld.setTableId((short) 42);
218 flowAndStatsMapListBld.setMatch(new MatchBuilder().build());
220 final FlowsStatisticsUpdateBuilder flowStatsUpdateBld1 = new FlowsStatisticsUpdateBuilder();
221 flowStatsUpdateBld1.setFlowAndStatisticsMapList(Lists.newArrayList(flowAndStatsMapListBld.build()));
223 return Lists.newArrayList(flowStatsUpdateBld1.build());
228 public void testGatherStatistics_group() throws Exception {
229 final MultipartType type = MultipartType.OFPMPGROUP;
230 final long groupIdValue = 19L;
232 final GroupStatsBuilder groupStatsBld = new GroupStatsBuilder()
233 .setBucketStats(Lists.newArrayList(createBucketStat(21L, 42L)))
234 .setByteCount(BigInteger.valueOf(84L))
235 .setPacketCount(BigInteger.valueOf(63L))
237 .setDurationNsec(12L)
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());
245 final MultipartReply groupStatsUpdated = assembleMPReplyMessage(type, mpReplyGroupCaseBld.build());
246 final List<MultipartReply> statsData = Collections.singletonList(groupStatsUpdated);
248 fireAndCheck(type, statsData);
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));
260 public void testGatherStatistics_groupDesc() throws Exception {
261 final MultipartType type = MultipartType.OFPMPGROUPDESC;
262 final long groupIdValue = 27L;
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());
275 final MultipartReply groupStatsUpdated = assembleMPReplyMessage(type, mpReplyGroupCaseBld.build());
276 final List<MultipartReply> statsData = Collections.singletonList(groupStatsUpdated);
278 fireAndCheck(type, statsData);
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));
285 verify(deviceContext, Mockito.never()).addDeleteToTxChain(
286 Matchers.eq(LogicalDatastoreType.OPERATIONAL), Matchers.<InstanceIdentifier<?>>any());
287 verify(deviceGroupRegistry).removeMarked();
288 verify(deviceGroupRegistry).store(storedGroupId);
289 verify(txFacade).writeToTransaction(
290 Matchers.eq(LogicalDatastoreType.OPERATIONAL), Matchers.eq(groupPath), Matchers.any(Group.class));
294 public void testGatherStatistics_meter() throws Exception {
295 final MultipartType type = MultipartType.OFPMPMETER;
296 final long meterIdValue = 19L;
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)
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());
314 final MultipartReply meterStatsUpdated = assembleMPReplyMessage(type, mpReplyMeterCaseBld.build());
315 final List<MultipartReply> statsData = Collections.singletonList(meterStatsUpdated);
317 fireAndCheck(type, statsData);
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));
329 public void testGatherStatistics_nodeConnector() throws Exception {
330 final MultipartType type = MultipartType.OFPMPPORTSTATS;
332 final PortStatsBuilder portStatsBld = new PortStatsBuilder()
334 final MultipartReplyPortStatsBuilder mpReplyMeterBld = new MultipartReplyPortStatsBuilder();
335 mpReplyMeterBld.setPortStats(Lists.newArrayList(portStatsBld.build()));
336 final MultipartReplyPortStatsCaseBuilder mpReplyMeterCaseBld = new MultipartReplyPortStatsCaseBuilder();
337 mpReplyMeterCaseBld.setMultipartReplyPortStats(mpReplyMeterBld.build());
339 final MultipartReply meterStatsUpdated = assembleMPReplyMessage(type, mpReplyMeterCaseBld.build());
340 final List<MultipartReply> statsData = Collections.singletonList(meterStatsUpdated);
342 fireAndCheck(type, statsData);
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));
356 public void testGatherStatistics_table() throws Exception {
357 final MultipartType type = MultipartType.OFPMPTABLE;
359 final TableStatsBuilder tableStatsBld = new TableStatsBuilder()
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());
369 final MultipartReply meterStatsUpdated = assembleMPReplyMessage(type, mpReplyTableCaseBld.build());
370 final List<MultipartReply> statsData = Collections.singletonList(meterStatsUpdated);
372 fireAndCheck(type, statsData);
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));
386 public void testGatherStatistics_queue() throws Exception {
387 final MultipartType type = MultipartType.OFPMPQUEUE;
389 final long queueIdValue = 4L;
390 final QueueStatsBuilder queueStatsBld = new QueueStatsBuilder()
392 .setTxBytes(BigInteger.valueOf(44L))
393 .setTxErrors(BigInteger.valueOf(45L))
394 .setTxPackets(BigInteger.valueOf(46L))
396 .setDurationNsec(48L)
397 .setQueueId(queueIdValue);
399 final MultipartReplyQueueBuilder mpReplyQueueBld = new MultipartReplyQueueBuilder();
400 mpReplyQueueBld.setQueueStats(Lists.newArrayList(queueStatsBld.build()));
401 final MultipartReplyQueueCaseBuilder mpReplyQueueCaseBld = new MultipartReplyQueueCaseBuilder();
402 mpReplyQueueCaseBld.setMultipartReplyQueue(mpReplyQueueBld.build());
404 final MultipartReply meterStatsUpdated = assembleMPReplyMessage(type, mpReplyQueueCaseBld.build());
405 final List<MultipartReply> statsData = Collections.singletonList(meterStatsUpdated);
407 fireAndCheck(type, statsData);
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));
421 public void testGatherStatistics_flow() throws Exception {
422 final short tableId = 0;
423 final MultipartType type = MultipartType.OFPMPFLOW;
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);
437 final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder
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))
445 .setDurationNsec(58L)
446 .setTableId((short) 0)
447 .setMatch(matchBld.build())
448 .setFlags(new FlowModFlags(true, false, false, false, true));
450 final MultipartReplyFlowBuilder mpReplyFlowBld = new MultipartReplyFlowBuilder();
451 mpReplyFlowBld.setFlowStats(Lists.newArrayList(flowStatsBld.build()));
452 final MultipartReplyFlowCaseBuilder mpReplyFlowCaseBld = new MultipartReplyFlowCaseBuilder();
453 mpReplyFlowCaseBld.setMultipartReplyFlow(mpReplyFlowBld.build());
455 final MultipartReply flowStatsUpdated = assembleMPReplyMessage(type, mpReplyFlowCaseBld.build());
456 final List<MultipartReply> statsData = Collections.singletonList(flowStatsUpdated);
457 fireAndCheck(type, statsData);
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));
466 verify(deviceContext, Mockito.never()).addDeleteToTxChain(
467 Matchers.eq(LogicalDatastoreType.OPERATIONAL), Matchers.<InstanceIdentifier<?>>any());
469 final InOrder inOrder = Mockito.inOrder(txFacade);
470 inOrder.verify(txFacade).writeToTransaction(Matchers.eq(
471 LogicalDatastoreType.OPERATIONAL), Matchers.eq(tablePath), Matchers.any(Table.class));
475 public void testGatherStatistics_meterConfig() throws Exception {
476 final MultipartType type = MultipartType.OFPMPMETERCONFIG;
477 final Long meterIdValue = 55L;
479 final MeterConfigBuilder meterConfigBld = new MeterConfigBuilder()
480 .setMeterId(new MeterId(meterIdValue))
481 .setFlags(new MeterFlags(false, true, false, true))
482 .setBands(Collections.<Bands>emptyList());
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());
490 final MultipartReply meterConfigUpdated = assembleMPReplyMessage(type, mpReplyMeterConfigCaseBld.build());
491 final List<MultipartReply> statsData = Collections.singletonList(meterConfigUpdated);
493 fireAndCheck(type, statsData);
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));
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()));
512 final ListenableFuture<Boolean> gatherStatisticsResult = StatisticsGatheringUtils.gatherStatistics(
519 singlePurposeMultipartReplyTranslator);
520 Assert.assertTrue(gatherStatisticsResult.get(1, TimeUnit.SECONDS));
521 verify(txFacade).submitTransaction();
524 private static MultipartReplyMessage assembleMPReplyMessage(
525 final MultipartType type,
526 final MultipartReplyBody mpReplyGroupCaseBld) {
527 return new MultipartReplyMessageBuilder()
528 .setMultipartReplyBody(mpReplyGroupCaseBld)
530 .setFlags(new MultipartRequestFlags(false))
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();
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));
556 StatisticsGatheringUtils.deleteAllKnownFlows(deviceInfo,
557 deviceContext.getDeviceFlowRegistry(), txFacade);
559 verify(txFacade).writeToTransaction(
560 LogicalDatastoreType.OPERATIONAL,
562 tableDataBld.setFlow(Collections.<Flow>emptyList()).build());