a0798eeb485ccbb5fa2ba411aa8a13726ac1cb6e
[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 import org.opendaylight.yangtools.yang.common.Uint32;
43 import org.opendaylight.yangtools.yang.common.Uint8;
44
45 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
46     static final Uint8 TABLE_NO = Uint8.ONE;
47     private FlowDirectStatisticsService service;
48
49     @Override
50     public void setUp() {
51         service = new FlowDirectStatisticsService(requestContextStack,
52                                                   deviceContext,
53                                                   convertorManager,
54                                                   multipartWriterProvider);
55         final DeviceFlowRegistry registry = mock(DeviceFlowRegistry.class);
56         when(registry.retrieveDescriptor(any())).thenReturn(FlowDescriptorFactory.create(TABLE_NO, new FlowId("1")));
57         when(deviceContext.getDeviceFlowRegistry()).thenReturn(registry);
58     }
59
60     @Override
61     public void testBuildRequestBody() {
62         final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
63
64         when(input.getTableId()).thenReturn(TABLE_NO);
65
66         final MultipartRequestFlowCase body = (MultipartRequestFlowCase) ((MultipartRequestInput) service
67             .buildRequest(new Xid(Uint32.valueOf(42L)), input))
68             .getMultipartRequestBody();
69
70         final MultipartRequestFlow flow = body.getMultipartRequestFlow();
71
72         assertEquals(TABLE_NO, flow.getTableId());
73     }
74
75     @Override
76     public void testBuildReply() {
77         final MultipartReply reply = mock(MultipartReply.class);
78         final MultipartReplyFlowCase flowCase = mock(MultipartReplyFlowCase.class);
79         final MultipartReplyFlow flow = mock(MultipartReplyFlow.class);
80         final FlowStats flowStat = new FlowStatsBuilder()
81                 .setDurationSec(1L)
82                 .setDurationNsec(1L)
83                 .setTableId(TABLE_NO)
84                 .setByteCount(BigInteger.ONE)
85                 .setPacketCount(BigInteger.ONE)
86                 .setFlags(mock(FlowModFlags.class))
87                 .setMatch(new org.opendaylight.yang.gen.v1.urn
88                         .opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder()
89                         .setMatchEntry(Collections.emptyList())
90                         .build())
91                 .build();
92
93         final List<FlowStats> flowStats = Collections.singletonList(flowStat);
94         final List<MultipartReply> input = Collections.singletonList(reply);
95
96         when(flow.getFlowStats()).thenReturn(flowStats);
97         when(flowCase.getMultipartReplyFlow()).thenReturn(flow);
98         when(reply.getMultipartReplyBody()).thenReturn(flowCase);
99
100         final GetFlowStatisticsOutput output = service.buildReply(input, true);
101         assertTrue(output.getFlowAndStatisticsMapList().size() > 0);
102
103         final FlowAndStatisticsMap stats = output.getFlowAndStatisticsMapList().get(0);
104
105         assertEquals(stats.getTableId(), TABLE_NO);
106     }
107
108     @Override
109     public void testStoreStatistics() {
110         final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
111         when(stat.getTableId()).thenReturn(TABLE_NO);
112         when(stat.getMatch()).thenReturn(new MatchBuilder().build());
113
114         final List<FlowAndStatisticsMapList> stats = Collections.singletonList(stat);
115         final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
116         when(output.getFlowAndStatisticsMapList()).thenReturn(stats);
117
118         multipartWriterProvider.lookup(MultipartType.OFPMPFLOW).get().write(output, true);
119         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
120     }
121 }