Modernize codebase
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / multilayer / FlowDirectStatisticsServiceTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.statistics.services.direct.multilayer;
9
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;
17
18 import java.util.List;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
21 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
22 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
23 import org.opendaylight.openflowplugin.impl.statistics.services.direct.AbstractDirectStatisticsServiceTest;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMap;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlow;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42 import org.opendaylight.yangtools.yang.common.Uint8;
43
44 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
45     static final Uint8 TABLE_NO = Uint8.ONE;
46     private FlowDirectStatisticsService service;
47
48     @Override
49     public void setUp() {
50         service = new FlowDirectStatisticsService(requestContextStack,
51                                                   deviceContext,
52                                                   convertorManager,
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);
57     }
58
59     @Override
60     public void testBuildRequestBody() {
61         final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
62
63         when(input.getTableId()).thenReturn(TABLE_NO);
64
65         final MultipartRequestFlowCase body = (MultipartRequestFlowCase) ((MultipartRequestInput) service
66             .buildRequest(new Xid(Uint32.valueOf(42L)), input))
67             .getMultipartRequestBody();
68
69         final MultipartRequestFlow flow = body.getMultipartRequestFlow();
70
71         assertEquals(TABLE_NO, flow.getTableId());
72     }
73
74     @Override
75     public void testBuildReply() {
76         final MultipartReply reply = mock(MultipartReply.class);
77         final MultipartReplyFlowCase flowCase = mock(MultipartReplyFlowCase.class);
78         final MultipartReplyFlow flow = mock(MultipartReplyFlow.class);
79         final FlowStats flowStat = new FlowStatsBuilder()
80                 .setDurationSec(Uint32.ONE)
81                 .setDurationNsec(Uint32.ONE)
82                 .setTableId(TABLE_NO)
83                 .setByteCount(Uint64.ONE)
84                 .setPacketCount(Uint64.ONE)
85                 .setFlags(mock(FlowModFlags.class))
86                 .setMatch(new org.opendaylight.yang.gen.v1.urn
87                         .opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder()
88                         .setMatchEntry(List.of())
89                         .build())
90                 .build();
91
92         final List<FlowStats> flowStats = List.of(flowStat);
93         final List<MultipartReply> input = List.of(reply);
94
95         when(flow.getFlowStats()).thenReturn(flowStats);
96         when(flowCase.getMultipartReplyFlow()).thenReturn(flow);
97         when(reply.getMultipartReplyBody()).thenReturn(flowCase);
98
99         final GetFlowStatisticsOutput output = service.buildReply(input, true);
100         assertTrue(output.nonnullFlowAndStatisticsMapList().size() > 0);
101
102         final FlowAndStatisticsMap stats = output.nonnullFlowAndStatisticsMapList().iterator().next();
103
104         assertEquals(stats.getTableId(), TABLE_NO);
105     }
106
107     @Override
108     public void testStoreStatistics() {
109         final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
110         when(stat.getTableId()).thenReturn(TABLE_NO);
111         when(stat.getMatch()).thenReturn(new MatchBuilder().build());
112
113         final List<FlowAndStatisticsMapList> stats = List.of(stat);
114         final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
115         when(output.nonnullFlowAndStatisticsMapList()).thenReturn(stats);
116
117         multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).orElseThrow().write(output, true);
118         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
119     }
120 }