Bug 5592 - NodeId+DatapathId in Services (remove dependency on DeviceContext)
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleManagerImplTest.java
1 /*
2  * Copyright (c) 2016 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.role;
10
11
12 import java.math.BigInteger;
13
14 import com.google.common.base.VerifyException;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
17 import org.junit.After;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.InOrder;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
29 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
31 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
32 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
33 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
34 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
35 import org.opendaylight.openflowplugin.api.OFConstants;
36 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
37 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
38 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
40 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
41 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
42 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
43 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
44 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
49
50 /**
51  * Created by Jozef Bacigal
52  * Date: 19.4.2016.
53  * Time: 13:08
54  */
55 @RunWith(MockitoJUnitRunner.class)
56 public class RoleManagerImplTest {
57
58     @Mock
59     EntityOwnershipService entityOwnershipService;
60
61     @Mock
62     DataBroker dataBroker;
63
64     @Mock
65     DeviceContext deviceContext;
66
67     @Mock
68     DeviceManager deviceManager;
69
70     @Mock
71     EntityOwnershipListener entityOwnershipListener;
72
73     @Mock
74     EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
75
76     @Mock
77     EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
78
79     @Mock
80     ConnectionContext connectionContext;
81
82     @Mock
83     FeaturesReply featuresReply;
84
85     @Mock
86     DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
87
88     @Mock
89     DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
90
91     @Mock
92     WriteTransaction writeTransaction;
93
94     @Mock
95     LifecycleConductor conductor;
96
97     @Mock
98     DeviceState deviceState;
99
100     @Mock
101     GetFeaturesOutput featuresOutput;
102
103     private RoleManagerImpl roleManager;
104     private RoleManagerImpl roleManagerSpy;
105     private RoleContext roleContextSpy;
106     private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
107
108     private final EntityOwnershipChange masterEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), false, true, true);
109     private final EntityOwnershipChange masterTxEntity = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), false, true, true);
110     private final EntityOwnershipChange slaveEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true);
111     private final EntityOwnershipChange slaveTxEntityLast = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), true, false, false);
112
113     private InOrder inOrder;
114
115     @Before
116     public void setUp() throws Exception {
117         CheckedFuture<Void, TransactionCommitFailedException> future = Futures.immediateCheckedFuture(null);
118         Mockito.when(deviceState.getFeatures()).thenReturn(featuresOutput);
119         Mockito.when(entityOwnershipService.registerListener(Mockito.anyString(), Mockito.any(EntityOwnershipListener.class))).thenReturn(entityOwnershipListenerRegistration);
120         Mockito.when(entityOwnershipService.registerCandidate(Mockito.any(Entity.class))).thenReturn(entityOwnershipCandidateRegistration);
121         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
122         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
123         Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
124         Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
125         Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
126         Mockito.when(featuresReply.getDatapathId()).thenReturn(new BigInteger("1"));
127         Mockito.when(featuresReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
128         Mockito.doNothing().when(deviceInitializationPhaseHandler).onDeviceContextLevelUp(Mockito.<NodeId>any());
129         Mockito.doNothing().when(deviceTerminationPhaseHandler).onDeviceContextLevelDown(Mockito.<DeviceContext>any());
130         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
131         Mockito.when(writeTransaction.submit()).thenReturn(future);
132         Mockito.when(deviceManager.getDeviceContextFromNodeId(Mockito.<NodeId>any())).thenReturn(deviceContext);
133         roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker, conductor);
134         roleManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
135         roleManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
136         Mockito.when(conductor.getDeviceContext(Mockito.<NodeId>any())).thenReturn(deviceContext);
137         roleManagerSpy = Mockito.spy(roleManager);
138         Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
139         roleManagerSpy.onDeviceContextLevelUp(nodeId);
140         roleContextSpy = Mockito.spy(roleManager.getRoleContext(nodeId));
141         inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
142     }
143
144     @After
145     public void tearDown() throws Exception {
146     }
147
148     @Test(expected = VerifyException.class)
149     public void testOnDeviceContextLevelUp() throws Exception {
150         roleManagerSpy.onDeviceContextLevelUp(nodeId);
151         inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(nodeId);
152         inOrder.verifyNoMoreInteractions();
153     }
154
155     @Test
156     public void testCloseMaster() throws Exception {
157         roleManagerSpy.ownershipChanged(masterEntity);
158         roleManagerSpy.ownershipChanged(masterTxEntity);
159         roleManagerSpy.close();
160         inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
161         inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(nodeId);
162         inOrder.verifyNoMoreInteractions();
163     }
164
165     @Test
166     public void testCloseSlave() throws Exception {
167         roleManagerSpy.ownershipChanged(slaveEntity);
168         roleManagerSpy.close();
169         inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
170         inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(nodeId);
171         inOrder.verifyNoMoreInteractions();
172     }
173
174     @Test
175     public void testOnDeviceContextLevelDown() throws Exception {
176         roleManagerSpy.onDeviceContextLevelDown(deviceContext);
177         inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceContext);
178         inOrder.verifyNoMoreInteractions();
179     }
180
181     @Test
182     public void testOwnershipChanged1() throws Exception {
183         roleManagerSpy.ownershipChanged(masterEntity);
184         inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
185         inOrder.verifyNoMoreInteractions();
186     }
187
188     @Test
189     public void testOwnershipChanged2() throws Exception {
190         roleManagerSpy.ownershipChanged(masterEntity);
191         roleManagerSpy.ownershipChanged(masterTxEntity);
192         inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
193         inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
194         inOrder.verifyNoMoreInteractions();
195     }
196
197     @Test
198     public void testChangeOwnershipForMainEntity() throws Exception {
199         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
200         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
201         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
202     }
203
204     @Test
205     public void testChangeOwnershipForMainEntity2() throws Exception {
206         Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
207         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
208         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
209     }
210
211     @Test
212     public void testChangeOwnershipForTxEntity() throws Exception {
213         Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
214         roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
215         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
216         inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
217         inOrder.verify(roleContextSpy, Mockito.never()).close();
218         inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<NodeId>any());
219     }
220
221     @Test
222     public void testChangeOwnershipForTxEntity2() throws Exception {
223         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
224         roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
225         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
226         inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
227         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
228         inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
229     }
230
231     @Test
232     public void testAddListener() throws Exception {
233         roleManager.addRoleChangeListener((new RoleChangeListener() {
234             @Override
235             public void roleInitializationDone(final NodeId nodeId, final boolean success) {
236                 Assert.assertTrue(nodeId.equals(nodeId));
237                 Assert.assertTrue(success);
238             }
239
240             @Override
241             public void roleChangeOnDevice(final NodeId nodeId_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
242                 Assert.assertTrue(nodeId.equals(nodeId_));
243                 Assert.assertTrue(success);
244                 Assert.assertFalse(initializationPhase);
245                 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
246             }
247         }));
248         roleManager.notifyListenersRoleInitializationDone(nodeId, true);
249         roleManager.notifyListenersRoleChangeOnDevice(nodeId, true, OfpRole.BECOMEMASTER, false);
250     }
251
252
253 }