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;
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.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMap;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlow;
43 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
44 static final Short TABLE_NO = 1;
45 private FlowDirectStatisticsService service;
48 public void setUp() throws Exception {
49 service = new FlowDirectStatisticsService(requestContextStack,
52 multipartWriterProvider);
53 final DeviceFlowRegistry registry = mock(DeviceFlowRegistry.class);
54 when(registry.retrieveDescriptor(any())).thenReturn(FlowDescriptorFactory.create(TABLE_NO, new FlowId("1")));
55 when(deviceContext.getDeviceFlowRegistry()).thenReturn(registry);
59 public void testBuildRequestBody() throws Exception {
60 final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
62 when(input.getTableId()).thenReturn(TABLE_NO);
64 final MultipartRequestFlowCase body = (MultipartRequestFlowCase) ((MultipartRequestInput) service
65 .buildRequest(new Xid(42L), input))
66 .getMultipartRequestBody();
68 final MultipartRequestFlow flow = body.getMultipartRequestFlow();
70 assertEquals(TABLE_NO, flow.getTableId());
74 public void testBuildReply() throws Exception {
75 final MultipartReply reply = mock(MultipartReply.class);
76 final MultipartReplyFlowCase flowCase = mock(MultipartReplyFlowCase.class);
77 final MultipartReplyFlow flow = mock(MultipartReplyFlow.class);
78 final FlowStats flowStat = new FlowStatsBuilder()
82 .setByteCount(BigInteger.ONE)
83 .setPacketCount(BigInteger.ONE)
84 .setFlags(mock(FlowModFlags.class))
85 .setMatch(new org.opendaylight.yang.gen.v1.urn
86 .opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder()
87 .setMatchEntry(Collections.emptyList())
91 final List<FlowStats> flowStats = Collections.singletonList(flowStat);
92 final List<MultipartReply> input = Collections.singletonList(reply);
94 when(flow.getFlowStats()).thenReturn(flowStats);
95 when(flowCase.getMultipartReplyFlow()).thenReturn(flow);
96 when(reply.getMultipartReplyBody()).thenReturn(flowCase);
98 final GetFlowStatisticsOutput output = service.buildReply(input, true);
99 assertTrue(output.getFlowAndStatisticsMapList().size() > 0);
101 final FlowAndStatisticsMap stats = output.getFlowAndStatisticsMapList().get(0);
103 assertEquals(stats.getTableId(), TABLE_NO);
107 public void testStoreStatistics() throws Exception {
108 final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
109 when(stat.getTableId()).thenReturn(TABLE_NO);
110 when(stat.getMatch()).thenReturn(new MatchBuilder().build());
112 final List<FlowAndStatisticsMapList> stats = Collections.singletonList(stat);
113 final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
114 when(output.getFlowAndStatisticsMapList()).thenReturn(stats);
116 multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).get().write(output, true);
117 verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());