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.multilayer;
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;
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.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMap;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlow;
44 import org.opendaylight.yangtools.yang.common.Uint32;
45 import org.opendaylight.yangtools.yang.common.Uint8;
47 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
48 static final Uint8 TABLE_NO = Uint8.ONE;
49 private FlowDirectStatisticsService service;
53 service = new FlowDirectStatisticsService(requestContextStack,
56 multipartWriterProvider);
57 final DeviceFlowRegistry registry = mock(DeviceFlowRegistry.class);
58 when(registry.retrieveDescriptor(any())).thenReturn(FlowDescriptorFactory.create(TABLE_NO, new FlowId("1")));
59 when(deviceContext.getDeviceFlowRegistry()).thenReturn(registry);
63 public void testBuildRequestBody() {
64 final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
66 when(input.getTableId()).thenReturn(TABLE_NO);
68 final MultipartRequestFlowCase body = (MultipartRequestFlowCase) ((MultipartRequestInput) service
69 .buildRequest(new Xid(Uint32.valueOf(42L)), input))
70 .getMultipartRequestBody();
72 final MultipartRequestFlow flow = body.getMultipartRequestFlow();
74 assertEquals(TABLE_NO, flow.getTableId());
78 public void testBuildReply() {
79 final MultipartReply reply = mock(MultipartReply.class);
80 final MultipartReplyFlowCase flowCase = mock(MultipartReplyFlowCase.class);
81 final MultipartReplyFlow flow = mock(MultipartReplyFlow.class);
82 final FlowStats flowStat = new FlowStatsBuilder()
86 .setByteCount(BigInteger.ONE)
87 .setPacketCount(BigInteger.ONE)
88 .setFlags(mock(FlowModFlags.class))
89 .setMatch(new org.opendaylight.yang.gen.v1.urn
90 .opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder()
91 .setMatchEntry(Collections.emptyList())
95 final List<FlowStats> flowStats = Collections.singletonList(flowStat);
96 final List<MultipartReply> input = Collections.singletonList(reply);
98 when(flow.getFlowStats()).thenReturn(flowStats);
99 when(flowCase.getMultipartReplyFlow()).thenReturn(flow);
100 when(reply.getMultipartReplyBody()).thenReturn(flowCase);
102 final GetFlowStatisticsOutput output = service.buildReply(input, true);
103 assertTrue(output.nonnullFlowAndStatisticsMapList().size() > 0);
105 final FlowAndStatisticsMap stats = output.nonnullFlowAndStatisticsMapList().values().iterator().next();
107 assertEquals(stats.getTableId(), TABLE_NO);
111 public void testStoreStatistics() {
112 final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
113 when(stat.getTableId()).thenReturn(TABLE_NO);
114 when(stat.getMatch()).thenReturn(new MatchBuilder().build());
116 final Map<FlowAndStatisticsMapListKey, FlowAndStatisticsMapList> stats
117 = Collections.singletonMap(stat.key(), stat);
118 final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
119 when(output.nonnullFlowAndStatisticsMapList()).thenReturn(stats);
121 multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).get().write(output, true);
122 verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());