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