Switch to MD-SAL APIs
[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.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;
42
43 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
44     static final Short TABLE_NO = 1;
45     private FlowDirectStatisticsService service;
46
47     @Override
48     public void setUp() throws Exception {
49         service = new FlowDirectStatisticsService(requestContextStack,
50                                                   deviceContext,
51                                                   convertorManager,
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);
56     }
57
58     @Override
59     public void testBuildRequestBody() throws Exception {
60         final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
61
62         when(input.getTableId()).thenReturn(TABLE_NO);
63
64         final MultipartRequestFlowCase body = (MultipartRequestFlowCase) ((MultipartRequestInput) service
65             .buildRequest(new Xid(42L), input))
66             .getMultipartRequestBody();
67
68         final MultipartRequestFlow flow = body.getMultipartRequestFlow();
69
70         assertEquals(TABLE_NO, flow.getTableId());
71     }
72
73     @Override
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()
79                 .setDurationSec(1L)
80                 .setDurationNsec(1L)
81                 .setTableId(TABLE_NO)
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())
88                         .build())
89                 .build();
90
91         final List<FlowStats> flowStats = Collections.singletonList(flowStat);
92         final List<MultipartReply> input = Collections.singletonList(reply);
93
94         when(flow.getFlowStats()).thenReturn(flowStats);
95         when(flowCase.getMultipartReplyFlow()).thenReturn(flow);
96         when(reply.getMultipartReplyBody()).thenReturn(flowCase);
97
98         final GetFlowStatisticsOutput output = service.buildReply(input, true);
99         assertTrue(output.getFlowAndStatisticsMapList().size() > 0);
100
101         final FlowAndStatisticsMap stats = output.getFlowAndStatisticsMapList().get(0);
102
103         assertEquals(stats.getTableId(), TABLE_NO);
104     }
105
106     @Override
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());
111
112         final List<FlowAndStatisticsMapList> stats = Collections.singletonList(stat);
113         final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
114         when(output.getFlowAndStatisticsMapList()).thenReturn(stats);
115
116         multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).get().write(output, true);
117         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
118     }
119 }