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.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
24 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
25 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
26 import org.opendaylight.openflowplugin.impl.statistics.services.direct.AbstractDirectStatisticsServiceTest;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMap;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.multipart.reply.multipart.reply.body.MultipartReplyFlowStatsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.MultipartRequestFlowStats;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReplyBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequest;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
49 import org.opendaylight.yangtools.yang.common.Uint16;
50 import org.opendaylight.yangtools.yang.common.Uint32;
51 import org.opendaylight.yangtools.yang.common.Uint64;
52 import org.opendaylight.yangtools.yang.common.Uint8;
54 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
55 static final Uint8 TABLE_NO = Uint8.ONE;
56 private FlowDirectStatisticsService service;
60 final DeviceFlowRegistry registry = mock(DeviceFlowRegistry.class);
61 service = new FlowDirectStatisticsService(requestContextStack,
64 multipartWriterProvider);
65 when(registry.retrieveDescriptor(any())).thenReturn(FlowDescriptorFactory.create(TABLE_NO, new FlowId("1")));
66 when(deviceContext.getDeviceFlowRegistry()).thenReturn(registry);
70 public void testBuildRequestBody() {
71 final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
73 when(input.getTableId()).thenReturn(TABLE_NO);
75 final MultipartRequestFlowStats body = (MultipartRequestFlowStats) ((MultipartRequest) service
76 .buildRequest(new Xid(Uint32.valueOf(42L)), input))
77 .getMultipartRequestBody();
79 assertEquals(TABLE_NO, body.getFlowStats().getTableId());
83 public void testBuildReply() {
84 final FlowAndStatisticsMapList flowStat = new FlowAndStatisticsMapListBuilder()
85 .setDuration(new DurationBuilder()
86 .setSecond(new Counter32(1L))
87 .setNanosecond(new Counter32(2L))
89 .setTableId(Uint8.valueOf(50))
90 .setByteCount(new Counter64(BigInteger.valueOf(7094)))
91 .setPacketCount(new Counter64(BigInteger.valueOf(63)))
92 .setCookie(new FlowCookie(Uint64.valueOf(134419365)))
93 .setFlags(new FlowModFlags(true, false, false, false, false))
94 .setMatch(new MatchBuilder().setEthernetMatch(
95 new EthernetMatchBuilder()
96 .setEthernetDestination(
97 new EthernetDestinationBuilder()
98 .setAddress(new MacAddress("fa:16:3e:92:81:45"))
99 .build()).build()).build())
100 .setInstructions(new InstructionsBuilder().build())
101 .setPriority(Uint16.valueOf(20))
104 final MultipartReply reply = new MultipartReplyBuilder()
105 .setMultipartReplyBody(new MultipartReplyFlowStatsBuilder()
106 .setFlowAndStatisticsMapList(Collections.singletonList(flowStat))
111 final FlowAndStatisticsMapList flowStat1 = new FlowAndStatisticsMapListBuilder()
112 .setDuration(new DurationBuilder()
113 .setSecond(new Counter32(1L))
114 .setNanosecond(new Counter32(2L))
116 .setTableId(Uint8.valueOf(51))
117 .setByteCount(new Counter64(BigInteger.valueOf(9853)))
118 .setPacketCount(new Counter64(BigInteger.valueOf(99)))
119 .setCookie(new FlowCookie(Uint64.valueOf(134550437)))
120 .setFlags(new FlowModFlags(true, false, false, false, false))
121 .setMatch(new MatchBuilder().setEthernetMatch(
122 new EthernetMatchBuilder()
123 .setEthernetDestination(
124 new EthernetDestinationBuilder()
125 .setAddress(new MacAddress("fa:16:3e:92:81:45"))
126 .build()).build()).build())
127 .setInstructions(new InstructionsBuilder().build())
128 .setPriority(Uint16.valueOf(20))
131 final MultipartReply reply1 = new MultipartReplyBuilder()
132 .setMultipartReplyBody(new MultipartReplyFlowStatsBuilder()
133 .setFlowAndStatisticsMapList(Collections.singletonList(flowStat1))
136 final List<MultipartReply> input = new ArrayList<>();
139 final GetFlowStatisticsOutput output = service.buildReply(input, true);
140 assertTrue(output.getFlowAndStatisticsMapList().size() > 0);
141 FlowAndStatisticsMap stats = output.nonnullFlowAndStatisticsMapList().iterator().next();
142 assertEquals(Uint8.valueOf(50),stats.getTableId());
143 stats = output.nonnullFlowAndStatisticsMapList().get(1);
144 assertEquals(Uint8.valueOf(51),stats.getTableId());
148 public void testStoreStatistics() {
149 final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
150 when(stat.getTableId()).thenReturn(TABLE_NO);
151 when(stat.getMatch()).thenReturn(new MatchBuilder().build());
153 final List<FlowAndStatisticsMapList> stats
154 = Collections.singletonList(stat);
155 final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
156 when(output.nonnullFlowAndStatisticsMapList()).thenReturn(stats);
158 multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).get().write(output, true);
159 verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());