2 * Copyright (c) 2016 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.role;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.verify;
15 import com.google.common.base.VerifyException;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import java.math.BigInteger;
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.InOrder;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
31 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
32 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
33 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
34 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
35 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
38 import org.opendaylight.openflowplugin.api.OFConstants;
39 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
40 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
41 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
42 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
43 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
44 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
45 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
46 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
47 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
48 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
49 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
55 @RunWith(MockitoJUnitRunner.class)
56 public class RoleManagerImplTest {
59 EntityOwnershipService entityOwnershipService;
62 DataBroker dataBroker;
65 DeviceContext deviceContext;
68 DeviceManager deviceManager;
71 EntityOwnershipListener entityOwnershipListener;
74 EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
77 EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
80 ConnectionContext connectionContext;
83 FeaturesReply featuresReply;
86 DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
89 DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
92 WriteTransaction writeTransaction;
95 LifecycleConductor conductor;
98 DeviceState deviceState;
101 DeviceInfo deviceInfo;
104 DeviceInfo deviceInfo2;
107 MessageSpy messageSpy;
110 OutboundQueue outboundQueue;
113 GetFeaturesOutput featuresOutput;
115 private RoleManagerImpl roleManager;
116 private RoleManagerImpl roleManagerSpy;
117 private RoleContext roleContextSpy;
118 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
119 private final NodeId nodeId2 = NodeId.getDefaultInstance("openflow:2");
122 private final EntityOwnershipChange masterEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), false, true, true, false);
123 private final EntityOwnershipChange masterTxEntity = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), false, true, true, false);
124 private final EntityOwnershipChange slaveEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true, false);
125 private final EntityOwnershipChange slaveTxEntityLast = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), true, false, false, false);
126 private final EntityOwnershipChange masterEntityNotOwner = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true, false);
128 private InOrder inOrder;
131 public void setUp() throws Exception {
132 CheckedFuture<Void, TransactionCommitFailedException> future = Futures.immediateCheckedFuture(null);
133 Mockito.when(entityOwnershipService.registerListener(Mockito.anyString(), Mockito.any(EntityOwnershipListener.class))).thenReturn(entityOwnershipListenerRegistration);
134 Mockito.when(entityOwnershipService.registerCandidate(Mockito.any(Entity.class))).thenReturn(entityOwnershipCandidateRegistration);
135 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
136 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
137 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
138 Mockito.when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
139 Mockito.when(deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider()).thenReturn(outboundQueue);
140 Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
141 Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
142 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
143 Mockito.when(deviceInfo.getDatapathId()).thenReturn(new BigInteger("1"));
144 Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
145 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
146 Mockito.doNothing().when(deviceInitializationPhaseHandler).onDeviceContextLevelUp(Mockito.<DeviceInfo>any());
147 Mockito.doNothing().when(deviceTerminationPhaseHandler).onDeviceContextLevelDown(Mockito.<DeviceInfo>any());
148 Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
149 Mockito.when(writeTransaction.submit()).thenReturn(future);
150 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
151 Mockito.when(deviceInfo2.getNodeId()).thenReturn(nodeId2);
152 Mockito.when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
153 roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker, conductor);
154 roleManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
155 roleManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
156 Mockito.when(conductor.getDeviceContext(deviceInfo)).thenReturn(deviceContext);
157 roleManagerSpy = Mockito.spy(roleManager);
158 roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
159 Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
160 roleContextSpy = Mockito.spy(roleManager.getRoleContext(deviceInfo));
161 Mockito.when(roleContextSpy.getDeviceInfo()).thenReturn(deviceInfo);
162 Mockito.when(roleContextSpy.getDeviceInfo().getNodeId()).thenReturn(nodeId);
163 inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
167 public void tearDown() throws Exception {
170 @Test(expected = VerifyException.class)
171 public void testOnDeviceContextLevelUp() throws Exception {
172 roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
173 inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(deviceInfo);
174 inOrder.verifyNoMoreInteractions();
178 public void testCloseMaster() throws Exception {
179 roleManagerSpy.ownershipChanged(masterEntity);
180 roleManagerSpy.ownershipChanged(masterTxEntity);
181 roleManagerSpy.close();
182 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
183 inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(deviceInfo);
184 inOrder.verifyNoMoreInteractions();
188 public void testCloseSlave() throws Exception {
189 roleManagerSpy.ownershipChanged(slaveEntity);
190 roleManagerSpy.close();
191 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
192 inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(deviceInfo);
193 inOrder.verifyNoMoreInteractions();
197 public void testOnDeviceContextLevelDown() throws Exception {
198 roleManagerSpy.onDeviceContextLevelDown(deviceInfo);
199 inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceInfo);
200 inOrder.verifyNoMoreInteractions();
204 public void testOwnershipChanged1() throws Exception {
205 roleManagerSpy.ownershipChanged(masterEntity);
206 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
207 inOrder.verifyNoMoreInteractions();
211 public void testOwnershipChanged2() throws Exception {
212 Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
213 roleManagerSpy.ownershipChanged(masterEntity);
214 roleManagerSpy.ownershipChanged(masterTxEntity);
215 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
216 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
217 inOrder.verifyNoMoreInteractions();
221 public void testChangeOwnershipForMainEntity() throws Exception {
222 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
223 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
224 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
228 public void testChangeOwnershipForMainEntity2() throws Exception {
229 Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
230 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
231 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
235 public void testChangeOwnershipForTxEntity() throws Exception {
236 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
237 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
238 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
239 inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
240 inOrder.verify(roleContextSpy, Mockito.never()).close();
241 inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
245 public void testChangeOwnershipForTxEntity2() throws Exception {
246 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
247 roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
248 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
249 inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
250 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
251 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
255 public void testChangeOwnershipForTxEntity3() throws Exception {
256 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(false);
257 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
258 verify(roleContextSpy).close();
259 verify(conductor).closeConnection(deviceInfo);
263 public void testChangeOwnershipForTxEntity4() throws Exception {
264 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
265 roleManagerSpy.changeOwnershipForTxEntity(masterEntityNotOwner, roleContextSpy);
266 verify(roleContextSpy).close();
267 verify(conductor).closeConnection(deviceInfo);
271 public void testAddListener() throws Exception {
272 roleManager.addRoleChangeListener((new RoleChangeListener() {
274 public void roleInitializationDone(final DeviceInfo deviceInfo_, final boolean success) {
275 Assert.assertTrue(deviceInfo.equals(deviceInfo_));
276 Assert.assertTrue(success);
280 public void roleChangeOnDevice(final DeviceInfo deviceInfo_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
281 Assert.assertTrue(RoleManagerImplTest.this.deviceInfo.equals(deviceInfo_));
282 Assert.assertTrue(success);
283 Assert.assertFalse(initializationPhase);
284 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
287 roleManager.notifyListenersRoleInitializationDone(deviceInfo, true);
288 roleManager.notifyListenersRoleChangeOnDevice(deviceInfo, true, OfpRole.BECOMEMASTER, false);
292 public void testServicesChangeDone() throws Exception {
293 roleManagerSpy.setRoleContext(deviceInfo2, roleContextSpy);
294 roleManagerSpy.servicesChangeDone(deviceInfo2, true);
295 verify(roleContextSpy).unregisterCandidate(Mockito.<Entity>any());
299 public void testServicesChangeDoneContextIsNull() throws Exception {
300 roleManagerSpy.setRoleContext(deviceInfo, roleContextSpy);
301 roleManagerSpy.servicesChangeDone(deviceInfo2, true);
302 verify(roleContextSpy, never()).unregisterCandidate(Mockito.<Entity>any());