OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightFlowTableStatisticsServiceImplTest.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;
10
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
13
14 import com.google.common.util.concurrent.FutureCallback;
15 import java.math.BigInteger;
16 import java.util.Collections;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.atomic.AtomicLong;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.ArgumentMatchers;
23 import org.mockito.Captor;
24 import org.mockito.Mockito;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.MultipartReplyTableBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table._case.multipart.reply.table.TableStatsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableCase;
37 import org.opendaylight.yangtools.yang.binding.Notification;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40
41 /**
42  * Test for {@link OpendaylightFlowTableStatisticsServiceImpl}.
43  */
44 public class OpendaylightFlowTableStatisticsServiceImplTest extends AbstractSingleStatsServiceTest {
45
46     private static final Short TABLE_ID = (short) 123;
47     @Captor
48     private ArgumentCaptor<MultipartRequestInput> requestInput;
49
50     private OpendaylightFlowTableStatisticsServiceImpl flowTableStatisticsService;
51
52     public void setUp() {
53         flowTableStatisticsService = new OpendaylightFlowTableStatisticsServiceImpl(rqContextStack, deviceContext,
54                 new AtomicLong(), notificationPublishService);
55     }
56
57     @Test
58     public void testGetFlowTablesStatistics() throws Exception {
59         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
60                 .commitEntry(eq(42L), requestInput.capture(), any(FutureCallback.class));
61
62         GetFlowTablesStatisticsInputBuilder input = new GetFlowTablesStatisticsInputBuilder()
63                 .setNode(createNodeRef("unitProt:123"));
64
65         rpcResult = RpcResultBuilder.<Object>success(Collections.singletonList(
66                 new MultipartReplyMessageBuilder()
67                         .setVersion(OFConstants.OFP_VERSION_1_3)
68                         .setMultipartReplyBody(new MultipartReplyTableCaseBuilder()
69                                 .setMultipartReplyTable(new MultipartReplyTableBuilder()
70                                         .setTableStats(Collections.singletonList(new TableStatsBuilder()
71                                                 .setActiveCount(31L)
72                                                 .setLookupCount(BigInteger.valueOf(32L))
73                                                 .setMatchedCount(BigInteger.valueOf(33L))
74                                                 .setMaxEntries(34L)
75                                                 .setName("test-table")
76                                                 .setNwDstMask((short) 35)
77                                                 .setNwSrcMask((short) 36)
78                                                 .setTableId(TABLE_ID)
79                                                 .build()))
80                                         .build())
81                                 .build())
82                         .build()
83         )).build();
84
85         final Future<RpcResult<GetFlowTablesStatisticsOutput>> resultFuture
86                 = flowTableStatisticsService.getFlowTablesStatistics(input.build());
87
88         Assert.assertTrue(resultFuture.isDone());
89         final RpcResult<GetFlowTablesStatisticsOutput> rpcResult = resultFuture.get();
90         Assert.assertTrue(rpcResult.isSuccessful());
91         Assert.assertEquals(MultipartType.OFPMPTABLE, requestInput.getValue().getType());
92
93         Mockito.verify(notificationPublishService).offerNotification(ArgumentMatchers.<Notification>any());
94     }
95
96     @Test
97     public void testBuildRequest() throws Exception {
98         Xid xid = new Xid(42L);
99         GetFlowTablesStatisticsInputBuilder input = new GetFlowTablesStatisticsInputBuilder()
100                 .setNode(createNodeRef("unitProt:123"));
101         final OfHeader request = flowTableStatisticsService.buildRequest(xid, input.build());
102         Assert.assertTrue(request instanceof MultipartRequestInput);
103         final MultipartRequestInput mpRequest = (MultipartRequestInput) request;
104         Assert.assertEquals(MultipartType.OFPMPTABLE, mpRequest.getType());
105         Assert.assertTrue(mpRequest.getMultipartRequestBody() instanceof MultipartRequestTableCase);
106         final MultipartRequestTableCase mpRequestBody =
107                 (MultipartRequestTableCase) (mpRequest.getMultipartRequestBody());
108         Assert.assertTrue(mpRequestBody.getMultipartRequestTable().isEmpty());
109     }
110 }