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