BUG-4099: Groups pointing to ports
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / device / DeviceTransactionChainManagerProviderTest.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.device;
10
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;
33
34 @RunWith(MockitoJUnitRunner.class)
35 public class DeviceTransactionChainManagerProviderTest {
36
37
38     @Mock
39     DataBroker dataBroker;
40     @Mock
41     ConnectionContext connectionContext;
42     @Mock
43     ConnectionContext concurrentConnectionContex;
44     @Mock
45     private BindingTransactionChain txChain;
46     @Mock
47     DeviceManager deviceManager;
48     @Mock
49     private WriteTransaction writeTx;
50     @Mock
51     private ReadyForNewTransactionChainHandler readyForNewTransactionChainHandler;
52
53     private static final NodeId nodeId = new NodeId("OPF:TEST");
54     private DeviceTransactionChainManagerProvider deviceTransactionChainManagerProvider;
55
56     @Before
57     public void setup() {
58         deviceTransactionChainManagerProvider = new DeviceTransactionChainManagerProvider(dataBroker);
59         Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
60         Mockito.when(concurrentConnectionContex.getNodeId()).thenReturn(nodeId);
61
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)))
67                 .thenReturn(txChain);
68
69 //        nodeKeyIdent = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
70 //        txChainManager = new TransactionChainManager(dataBroker, nodeKeyIdent, registration);
71         Mockito.when(txChain.newWriteOnlyTransaction()).thenReturn(writeTx);
72
73 //        path = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
74 //        Mockito.when(writeTx.submit()).thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
75     }
76
77     /**
78      * This test verifies code path for registering new connection when no {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
79      * is present in registry.
80      *
81      * @throws Exception
82      */
83     @Test
84     public void testProvideTransactionChainManagerOrWaitForNotification1() throws Exception {
85         DeviceTransactionChainManagerProvider.TransactionChainManagerRegistration transactionChainManagerRegistration = deviceTransactionChainManagerProvider.provideTransactionChainManager(connectionContext);
86         final TransactionChainManager txChainManager = transactionChainManagerRegistration.getTransactionChainManager();
87
88         Assert.assertTrue(transactionChainManagerRegistration.ownedByInvokingConnectionContext());
89         Assert.assertNotNull(txChainManager);
90         Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.WORKING, txChainManager.getTransactionChainManagerStatus());
91     }
92
93     /**
94      * This test verifies code path for registering new connection when {@link org.opendaylight.openflowplugin.impl.device.TransactionChainManager}
95      * is present in registry.
96      *
97      * @throws Exception
98      */
99     @Test
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());
105     }
106
107     /**
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).
110      *
111      * @throws Exception
112      */
113     @Test
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());
121
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();
129     }
130
131
132     /**
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).
135      *
136      * @throws Exception
137      */
138     @Test
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());
146
147         SettableFuture<Void> submitCleanFuture = SettableFuture.create();
148         CheckedFuture<Void, TransactionCommitFailedException> checkedSubmitCleanFuture =
149                 Futures.makeChecked(submitCleanFuture, new Function<Exception, TransactionCommitFailedException>() {
150                     @Nullable
151                     @Override
152                     public TransactionCommitFailedException apply(Exception input) {
153                         return new TransactionCommitFailedException("tx failed..", input);
154                     }
155                 });
156         Mockito.when(writeTx.submit()).thenReturn(checkedSubmitCleanFuture);
157         txChainManager.cleanupPostClosure();
158         Assert.assertEquals(TransactionChainManager.TransactionChainManagerStatus.SHUTTING_DOWN,
159                 txChainManagerRegistration_1.getTransactionChainManager().getTransactionChainManagerStatus());
160         txChainManager.attemptToRegisterHandler(readyForNewTransactionChainHandler);
161         Mockito.verify(readyForNewTransactionChainHandler, Mockito.never()).onReadyForNewTransactionChain();
162
163         submitCleanFuture.set(null);
164         Mockito.verify(readyForNewTransactionChainHandler).onReadyForNewTransactionChain();
165     }
166
167 }