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