2 * Copyright (c) 2015 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.impl.device;
11 import com.google.common.base.Function;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.SettableFuture;
15 import javax.annotation.Nullable;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Matchers;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
30 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 @RunWith(MockitoJUnitRunner.class)
35 public class DeviceTransactionChainManagerProviderTest {
39 DataBroker dataBroker;
41 ConnectionContext connectionContext;
43 ConnectionContext concurrentConnectionContex;
45 private BindingTransactionChain txChain;
47 DeviceManager deviceManager;
49 private WriteTransaction writeTx;
51 private ReadyForNewTransactionChainHandler readyForNewTransactionChainHandler;
53 private static final NodeId nodeId = new NodeId("OPF:TEST");
54 private DeviceTransactionChainManagerProvider deviceTransactionChainManagerProvider;
58 deviceTransactionChainManagerProvider = new DeviceTransactionChainManagerProvider(dataBroker);
59 Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
60 Mockito.when(concurrentConnectionContex.getNodeId()).thenReturn(nodeId);
62 final ReadOnlyTransaction readOnlyTx = Mockito.mock(ReadOnlyTransaction.class);
63 //final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture = Futures.immediateCheckedFuture(Optional.<Node>absent());
64 // Mockito.when(readOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent)).thenReturn(noExistNodeFuture);
65 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTx);
66 Mockito.when(dataBroker.createTransactionChain(Matchers.any(TransactionChainListener.class)))
69 // nodeKeyIdent = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
70 // txChainManager = new TransactionChainManager(dataBroker, nodeKeyIdent, registration);
71 Mockito.when(txChain.newWriteOnlyTransaction()).thenReturn(writeTx);
73 // path = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
74 // Mockito.when(writeTx.submit()).thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
78 * This test verifies code path for registering new connection when no {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
79 * is present in registry.
84 public void testProvideTransactionChainManagerOrWaitForNotification1() throws Exception {
85 DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
86 final TransactionChainManager txChainManager = transactionChainManagerRegistration.getTransactionChainManager();
88 Assert.assertTrue(transactionChainManagerRegistration.ownedByInvokingConnectionContext());
89 Assert.assertNotNull(txChainManager);
90 Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING, txChainManager.getTransactionChainManagerStatus());
94 * This test verifies code path for registering new connection when {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
95 * is present in registry.
100 public void testProvideTransactionChainManagerOrWaitForNotification2() throws Exception {
101 DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration_1 = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
102 Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING, transactionChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
103 DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration_2 = deviceTransactionChainManagerProvider.provideTransactionChainManager(concurrentConnectionContex);
104 Assert.assertFalse(transactionChainManagerRegistration_2.ownedByInvokingConnectionContext());
108 * This test verifies code path for registering new connection when {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
109 * is present in registry and in SHUTTING_DOWN state (finished).
114 public void testProvideTransactionChainManagerRecreate1() throws Exception {
115 DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration txChainManagerRegistration_1 = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
116 final TransactionChainManager txChainManager = txChainManagerRegistration_1.getTransactionChainManager();
117 Assert.assertTrue(txChainManagerRegistration_1.ownedByInvokingConnectionContext());
118 Assert.assertNotNull(txChainManager);
119 Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING,
120 txChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
122 CheckedFuture<Void, TransactionCommitFailedException> checkedSubmitCleanFuture = Futures.immediateCheckedFuture(null);
123 Mockito.when(writeTx.submit()).thenReturn(checkedSubmitCleanFuture);
124 txChainManager.close();
125 Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.SHUTTING_DOWN,
126 txChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
127 txChainManager.attemptToRegisterHandler(readyForNewTransactionChainHandler);
128 Mockito.verify(readyForNewTransactionChainHandler).onReadyForNewTransactionChain();
133 * This test verifies code path for registering new connection when {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
134 * is present in registry and in SHUTTING_DOWN state (unfinished).
139 public void testProvideTransactionChainManagerRecreate2() throws Exception {
140 DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration txChainManagerRegistration_1 = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
141 final TransactionChainManager txChainManager = txChainManagerRegistration_1.getTransactionChainManager();
142 Assert.assertTrue(txChainManagerRegistration_1.ownedByInvokingConnectionContext());
143 Assert.assertNotNull(txChainManager);
144 Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING,
145 txChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
147 SettableFuture<Void> submitCleanFuture = SettableFuture.create();
148 CheckedFuture<Void, TransactionCommitFailedException> checkedSubmitCleanFuture =
149 Futures.makeChecked(submitCleanFuture, new Function<Exception, TransactionCommitFailedException>() {
152 public TransactionCommitFailedException apply(Exception input) {
153 return new TransactionCommitFailedException("tx failed..", input);
156 Mockito.when(writeTx.submit()).thenReturn(checkedSubmitCleanFuture);
157 txChainManager.close();
158 Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.SHUTTING_DOWN,
159 txChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
160 txChainManager.attemptToRegisterHandler(readyForNewTransactionChainHandler);
161 Mockito.verify(readyForNewTransactionChainHandler, Mockito.never()).onReadyForNewTransactionChain();
163 submitCleanFuture.set(null);
164 Mockito.verify(readyForNewTransactionChainHandler).onReadyForNewTransactionChain();