Switch to MD-SAL APIs
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / compatibility / AbstractCompatibleStatServiceTest.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.compatibility;
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 com.google.common.util.concurrent.ListenableFuture;
16 import java.math.BigInteger;
17 import java.util.Collections;
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.Captor;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.stubbing.Answer;
26 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
30 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
31 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
33 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
34 import org.opendaylight.openflowplugin.impl.statistics.services.AbstractStatsServiceTest;
35 import org.opendaylight.openflowplugin.impl.statistics.services.AggregateFlowsInTableService;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.AggregateFlowStatisticsUpdate;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatistics;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatisticsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.aggregate._case.MultipartReplyAggregateBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
55
56 /**
57  * Test for {@link AbstractCompatibleStatService}.
58  */
59 public class AbstractCompatibleStatServiceTest extends AbstractStatsServiceTest {
60
61     private static final NodeId NODE_ID = new NodeId("unit-test-node:123");
62     @Captor
63     private ArgumentCaptor<MultipartRequestInput> requestInput;
64     @Mock
65     private NotificationPublishService notificationPublishService;
66     @Mock
67     private DeviceState deviceState;
68     @Mock
69     private DeviceInfo deviceInfo;
70     @Mock
71     private MessageTranslator<Object, Object> translator;
72     @Mock
73     private GetFeaturesOutput featuresOutput;
74
75     private AbstractRequestContext<Object> rqContext;
76
77     private RpcResult<Object> rpcResult;
78
79     private AbstractCompatibleStatService<GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput,
80             GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput, AggregateFlowStatisticsUpdate> service;
81
82     @Override
83     public void setUp() {
84         rqContext = new AbstractRequestContext<Object>(42L) {
85             @Override
86             public void close() {
87                 //NOOP
88             }
89         };
90         final Answer closeRequestFutureAnswer = invocation -> {
91             rqContext.setResult(rpcResult);
92             rqContext.close();
93             return null;
94         };
95
96         Mockito.lenient().when(featuresOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
97         Mockito.when(rqContextStack.<Object>createRequestContext()).thenReturn(rqContext);
98         Mockito.lenient().when(deviceContext.getDeviceState()).thenReturn(deviceState);
99         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
100         Mockito.when(deviceInfo.getNodeId()).thenReturn(NODE_ID);
101         Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
102         Mockito.doAnswer(closeRequestFutureAnswer).when(multiMsgCollector).endCollecting(null);
103         Mockito.lenient().doAnswer(closeRequestFutureAnswer).when(multiMsgCollector)
104                 .endCollecting(any(EventIdentifier.class));
105
106         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
107                 .commitEntry(eq(42L), requestInput.capture(), any(FutureCallback.class));
108
109         Mockito.when(translatorLibrary.lookupTranslator(any(TranslatorKey.class))).thenReturn(translator);
110
111         service = AggregateFlowsInTableService.createWithOook(rqContextStack, deviceContext, new AtomicLong(20L));
112     }
113
114     @Test
115     public void testGetOfVersion() throws Exception {
116         Assert.assertEquals(OFConstants.OFP_VERSION_1_3, service.getOfVersion().getVersion());
117     }
118
119     @Test
120     public void testHandleAndNotify() throws Exception {
121         GetAggregateFlowStatisticsFromFlowTableForAllFlowsInput input =
122                 new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder()
123                         .setNode(createNodeRef("unitProt:123"))
124                         .setTableId(new TableId((short) 1))
125                         .build();
126
127         rpcResult = RpcResultBuilder.<Object>success(Collections.singletonList(
128                 new MultipartReplyMessageBuilder()
129                         .setVersion(OFConstants.OFP_VERSION_1_3)
130                         .setMultipartReplyBody(new MultipartReplyAggregateCaseBuilder()
131                                 .setMultipartReplyAggregate(new MultipartReplyAggregateBuilder()
132                                         .setByteCount(BigInteger.valueOf(11L))
133                                         .setFlowCount(12L)
134                                         .setPacketCount(BigInteger.valueOf(13L))
135                                         .build())
136                                 .build())
137                         .build()
138         )).build();
139
140         AggregatedFlowStatistics aggregatedStats = new AggregatedFlowStatisticsBuilder()
141                 .setByteCount(new Counter64(BigInteger.valueOf(11L)))
142                 .setFlowCount(new Counter32(12L))
143                 .setPacketCount(new Counter64(BigInteger.valueOf(13L)))
144                 .build();
145         Mockito.when(translator.translate(any(MultipartReply.class), eq(deviceInfo), any()))
146                 .thenReturn(aggregatedStats);
147
148
149         ListenableFuture<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> resultFuture =
150                 service.handleAndNotify(input, notificationPublishService);
151
152         Assert.assertTrue(resultFuture.isDone());
153         final RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput> result = resultFuture.get();
154         Assert.assertTrue(result.isSuccessful());
155         Assert.assertEquals(MultipartType.OFPMPAGGREGATE, requestInput.getValue().getType());
156         Mockito.verify(notificationPublishService, Mockito.timeout(500))
157                 .offerNotification(any(AggregateFlowStatisticsUpdate.class));
158     }
159 }