Merge "BUG-4117: add support of Old Notif. for Statistics"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / direct / FlowDirectStatisticsServiceTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.openflowplugin.impl.statistics.services.direct;
10
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsInput;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.GetFlowStatisticsOutput;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowAndStatisticsMap;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestFlowCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.flow._case.MultipartRequestFlow;
26
27 import java.math.BigInteger;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
31
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertTrue;
34 import static org.mockito.Matchers.any;
35 import static org.mockito.Matchers.eq;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
39
40 public class FlowDirectStatisticsServiceTest extends AbstractDirectStatisticsServiceTest {
41     static final Short TABLE_NO = 1;
42     private FlowDirectStatisticsService service;
43
44     @Override
45     public void setUp() throws Exception {
46         service = new FlowDirectStatisticsService(requestContextStack, deviceContext);
47         final DeviceFlowRegistry registry = mock(DeviceFlowRegistry.class);
48         when(registry.storeIfNecessary(any(), eq(TABLE_NO))).thenReturn(new FlowId("1"));
49         when(deviceContext.getDeviceFlowRegistry()).thenReturn(registry);
50     }
51
52     @Override
53     public void testBuildRequestBody() throws Exception {
54         final GetFlowStatisticsInput input = mock(GetFlowStatisticsInput.class);
55
56         when(input.getNode()).thenReturn(createNodeRef(NODE_ID));
57         when(input.getTableId()).thenReturn(TABLE_NO);
58
59         final MultipartRequestFlowCase body = (MultipartRequestFlowCase) service.buildRequestBody(input);
60         final MultipartRequestFlow flow = body.getMultipartRequestFlow();
61
62         assertEquals(TABLE_NO, flow.getTableId());
63     }
64
65     @Override
66     public void testBuildReply() throws Exception {
67         final MultipartReply reply = mock(MultipartReply.class);
68         final MultipartReplyFlowCase flowCase = mock(MultipartReplyFlowCase.class);
69         final MultipartReplyFlow flow = mock(MultipartReplyFlow.class);
70         final FlowStats flowStat = mock(FlowStats.class);
71         final List<FlowStats> flowStats = Arrays.asList(flowStat);
72         final List<MultipartReply> input = Arrays.asList(reply);
73
74         when(flow.getFlowStats()).thenReturn(flowStats);
75         when(flowCase.getMultipartReplyFlow()).thenReturn(flow);
76         when(reply.getMultipartReplyBody()).thenReturn(flowCase);
77
78         when(flowStat.getTableId()).thenReturn(TABLE_NO);
79         when(flowStat.getByteCount()).thenReturn(BigInteger.ONE);
80         when(flowStat.getPacketCount()).thenReturn(BigInteger.ONE);
81         when(flowStat.getFlags()).thenReturn(mock(FlowModFlags.class));
82         when(flowStat.getMatch()).thenReturn(new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder()
83                 .setMatchEntry(Collections.emptyList())
84                 .build());
85
86         final GetFlowStatisticsOutput output = service.buildReply(input, true);
87         assertTrue(output.getFlowAndStatisticsMapList().size() > 0);
88
89         final FlowAndStatisticsMap stats = output.getFlowAndStatisticsMapList().get(0);
90
91         assertEquals(stats.getTableId(), TABLE_NO);
92     }
93
94     @Override
95     public void testStoreStatistics() throws Exception {
96         final FlowAndStatisticsMapList stat = mock(FlowAndStatisticsMapList.class);
97         when(stat.getTableId()).thenReturn(TABLE_NO);
98         when(stat.getMatch()).thenReturn(new MatchBuilder().build());
99
100         final List<FlowAndStatisticsMapList> stats = Arrays.asList(stat);
101         final GetFlowStatisticsOutput output = mock(GetFlowStatisticsOutput.class);
102         when(output.getFlowAndStatisticsMapList()).thenReturn(stats);
103
104         service.storeStatistics(output);
105         verify(deviceContext).writeToTransactionWithParentsSlow(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
106     }
107 }