2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.when;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import java.util.Collection;
16 import java.util.concurrent.Callable;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.sal.NotificationComposer;
27 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
28 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yangtools.yang.common.RpcError;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
38 @RunWith(MockitoJUnitRunner.class)
39 public class OFRpcTaskUtilTest {
42 private OFRpcTaskContext taskContext;
44 private SwitchConnectionDistinguisher connectionDistinguisher;
46 private SessionContext sessionContext;
48 private IMessageDispatchService messageDispatchService;
50 private GetFeaturesOutput featuresOutput;
52 private ListenableFuture<RpcResult<BarrierOutput>> resultListenableFuture;
54 private ListenableFuture<RpcResult<UpdateFlowOutput>> updateFlowRpcResultListenableFuture;
56 private OFRpcTaskContext ofRpcTaskContext;
58 private NotificationProviderService notificationProviderService;
60 private NotificationComposer<?> notificationComposer;
62 ListeningExecutorService executorService;
67 when(taskContext.getSession()).thenReturn(sessionContext);
68 when(taskContext.getMessageService()).thenReturn(messageDispatchService);
69 when(sessionContext.getNextXid()).thenReturn(new Long(10));
70 when(sessionContext.getFeatures()).thenReturn(featuresOutput);
71 when(featuresOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
72 when(messageDispatchService.barrier(Mockito.any(BarrierInput.class), Mockito.any(SwitchConnectionDistinguisher.class))).thenReturn(resultListenableFuture);
73 when(ofRpcTaskContext.getRpcPool()).thenReturn(executorService);
74 when(executorService.submit(Mockito.<Callable<RpcResult<UpdateFlowOutput>>> any())).thenReturn(updateFlowRpcResultListenableFuture);
79 public void testManageBarrier() throws Exception {
80 final Collection<RpcError> rpcErrors = OFRpcTaskUtil.manageBarrier(taskContext, true, connectionDistinguisher);
81 assertNotNull(rpcErrors);
85 public void testHookFutureNotification() throws Exception {
86 final AddFlowInputBuilder flowInputBuilder = new AddFlowInputBuilder();
87 final OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> addFlowInputRpcResultOFRpcTask = OFRpcTaskFactory.createAddFlowTask(ofRpcTaskContext, flowInputBuilder.build(), connectionDistinguisher);
88 OFRpcTaskUtil.hookFutureNotification(addFlowInputRpcResultOFRpcTask, updateFlowRpcResultListenableFuture, notificationProviderService, notificationComposer);
92 public void testChainFutureBarrier() throws Exception {
93 final AddFlowInputBuilder flowInputBuilder = new AddFlowInputBuilder();
94 flowInputBuilder.setBarrier(true);
95 final OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> addFlowInputRpcResultOFRpcTask = OFRpcTaskFactory.createAddFlowTask(ofRpcTaskContext, flowInputBuilder.build(), connectionDistinguisher);
96 OFRpcTaskUtil.chainFutureBarrier(addFlowInputRpcResultOFRpcTask, updateFlowRpcResultListenableFuture);