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;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import java.util.Collection;
17 import java.util.concurrent.Callable;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
27 import org.opendaylight.openflowplugin.api.openflow.md.core.sal.NotificationComposer;
28 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
29 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
36 import org.opendaylight.yangtools.yang.common.RpcError;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
39 @RunWith(MockitoJUnitRunner.class)
40 public class OFRpcTaskUtilTest {
42 @MockitoAnnotations.Mock
43 private OFRpcTaskContext taskContext;
44 @MockitoAnnotations.Mock
45 private SwitchConnectionDistinguisher connectionDistinguisher;
46 @MockitoAnnotations.Mock
47 private SessionContext sessionContext;
48 @MockitoAnnotations.Mock
49 private IMessageDispatchService messageDispatchService;
50 @MockitoAnnotations.Mock
51 private GetFeaturesOutput featuresOutput;
52 @MockitoAnnotations.Mock
53 private ListenableFuture<RpcResult<BarrierOutput>> resultListenableFuture;
54 @MockitoAnnotations.Mock
55 private ListenableFuture<RpcResult<UpdateFlowOutput>> updateFlowRpcResultListenableFuture;
56 @MockitoAnnotations.Mock
57 private OFRpcTaskContext ofRpcTaskContext;
58 @MockitoAnnotations.Mock
59 private NotificationProviderService notificationProviderService;
60 @MockitoAnnotations.Mock
61 private NotificationComposer notificationComposer;
62 @MockitoAnnotations.Mock
63 ListeningExecutorService executorService;
68 when(taskContext.getSession()).thenReturn(sessionContext);
69 when(taskContext.getMessageService()).thenReturn(messageDispatchService);
70 when(sessionContext.getNextXid()).thenReturn(new Long(10));
71 when(sessionContext.getFeatures()).thenReturn(featuresOutput);
72 when(featuresOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
73 when(messageDispatchService.barrier(Mockito.any(BarrierInput.class), Mockito.any(SwitchConnectionDistinguisher.class))).thenReturn(resultListenableFuture);
74 when(ofRpcTaskContext.getRpcPool()).thenReturn(executorService);
75 when(executorService.submit(Mockito.any(Callable.class))).thenReturn(updateFlowRpcResultListenableFuture);
80 public void testManageBarrier() throws Exception {
81 Collection<RpcError> rpcErrors = OFRpcTaskUtil.manageBarrier(taskContext, true, connectionDistinguisher);
82 assertNotNull(rpcErrors);
86 public void testHookFutureNotification() throws Exception {
87 AddFlowInputBuilder flowInputBuilder = new AddFlowInputBuilder();
88 OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> addFlowInputRpcResultOFRpcTask = OFRpcTaskFactory.createAddFlowTask(ofRpcTaskContext, flowInputBuilder.build(), connectionDistinguisher);
89 OFRpcTaskUtil.hookFutureNotification(addFlowInputRpcResultOFRpcTask, updateFlowRpcResultListenableFuture, notificationProviderService, notificationComposer);
93 public void testChainFutureBarrier() throws Exception {
94 AddFlowInputBuilder flowInputBuilder = new AddFlowInputBuilder();
95 flowInputBuilder.setBarrier(true);
96 OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> addFlowInputRpcResultOFRpcTask = OFRpcTaskFactory.createAddFlowTask(ofRpcTaskContext, flowInputBuilder.build(), connectionDistinguisher);
97 OFRpcTaskUtil.chainFutureBarrier(addFlowInputRpcResultOFRpcTask, updateFlowRpcResultListenableFuture);