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