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