2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
8 package org.opendaylight.openflowplugin.impl.statistics.services.direct.singlelayer;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import java.math.BigInteger;
19 import java.util.Collections;
20 import java.util.List;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
23 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
24 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
25 import org.opendaylight.openflowplugin.impl.statistics.services.direct.AbstractDirectStatisticsServiceTest;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMap;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowStatsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.MultipartRequestFlowStats;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReplyBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequest;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
44 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
45 static final Short TABLE_NO = 1;
46 private FlowDirectStatisticsService service;
49 public void setUp() throws Exception {
50 service = new FlowDirectStatisticsService(requestContextStack,
53 multipartWriterProvider);
54 final DeviceFlowRegistry registry = mock(DeviceFlowRegistry.class);
55 when(registry.retrieveDescriptor(any())).thenReturn(FlowDescriptorFactory.create(TABLE_NO, new FlowId("1")));
56 when(deviceContext.getDeviceFlowRegistry()).thenReturn(registry);
60 public void testBuildRequestBody() throws Exception {
61 final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
63 when(input.getTableId()).thenReturn(TABLE_NO);
65 final MultipartRequestFlowStats body = (MultipartRequestFlowStats) ((MultipartRequest) service
66 .buildRequest(new Xid(42L), input))
67 .getMultipartRequestBody();
69 assertEquals(TABLE_NO, body.getFlowStats().getTableId());
73 public void testBuildReply() throws Exception {
74 final FlowAndStatisticsMapList flowStat = new FlowAndStatisticsMapListBuilder()
75 .setDuration(new DurationBuilder()
76 .setSecond(new Counter32(1L))
77 .setNanosecond(new Counter32(1L))
80 .setByteCount(new Counter64(BigInteger.ONE))
81 .setPacketCount(new Counter64(BigInteger.ONE))
82 .setFlags(new FlowModFlags(true, false, false, false, false))
83 .setMatch(new MatchBuilder().build())
86 final MultipartReply reply = new MultipartReplyBuilder()
87 .setMultipartReplyBody(new MultipartReplyFlowStatsBuilder()
88 .setFlowAndStatisticsMapList(Collections.singletonList(flowStat))
92 final List<MultipartReply> input = Collections.singletonList(reply);
94 final GetFlowStatisticsOutput output = service.buildReply(input, true);
95 assertTrue(output.getFlowAndStatisticsMapList().size() > 0);
97 final FlowAndStatisticsMap stats = output.getFlowAndStatisticsMapList().get(0);
99 assertEquals(stats.getTableId(), TABLE_NO);
103 public void testStoreStatistics() throws Exception {
104 final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
105 when(stat.getTableId()).thenReturn(TABLE_NO);
106 when(stat.getMatch()).thenReturn(new MatchBuilder().build());
108 final List<FlowAndStatisticsMapList> stats = Collections.singletonList(stat);
109 final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
110 when(output.getFlowAndStatisticsMapList()).thenReturn(stats);
112 multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).get().write(output, true);
113 verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());