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 java.math.BigInteger;
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.DeviceInfo;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
40 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
41 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
42 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
43 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
44 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
45 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
51 import static org.mockito.Matchers.eq;
52 import static org.mockito.Mockito.atLeastOnce;
53 import static org.mockito.Mockito.never;
54 import static org.mockito.Mockito.verify;
56 @RunWith(MockitoJUnitRunner.class)
57 public class RoleManagerImplTest {
60 EntityOwnershipService entityOwnershipService;
63 DataBroker dataBroker;
66 DeviceContext deviceContext;
69 DeviceManager deviceManager;
72 EntityOwnershipListener entityOwnershipListener;
75 EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
78 EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
81 ConnectionContext connectionContext;
84 FeaturesReply featuresReply;
87 DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
90 DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
93 WriteTransaction writeTransaction;
96 LifecycleConductor conductor;
99 DeviceState deviceState;
102 DeviceInfo deviceInfo;
105 DeviceInfo deviceInfo2;
109 GetFeaturesOutput featuresOutput;
111 private RoleManagerImpl roleManager;
112 private RoleManagerImpl roleManagerSpy;
113 private RoleContext roleContextSpy;
114 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
115 private final NodeId nodeId2 = NodeId.getDefaultInstance("openflow:2");
118 private final EntityOwnershipChange masterEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), false, true, true, false);
119 private final EntityOwnershipChange masterTxEntity = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), false, true, true, false);
120 private final EntityOwnershipChange slaveEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true, false);
121 private final EntityOwnershipChange slaveTxEntityLast = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), true, false, false, false);
122 private final EntityOwnershipChange masterEntityNotOwner = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true, false);
124 private InOrder inOrder;
127 public void setUp() throws Exception {
128 CheckedFuture<Void, TransactionCommitFailedException> future = Futures.immediateCheckedFuture(null);
129 Mockito.when(entityOwnershipService.registerListener(Mockito.anyString(), Mockito.any(EntityOwnershipListener.class))).thenReturn(entityOwnershipListenerRegistration);
130 Mockito.when(entityOwnershipService.registerCandidate(Mockito.any(Entity.class))).thenReturn(entityOwnershipCandidateRegistration);
131 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
132 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
133 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
134 Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
135 Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
136 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
137 Mockito.when(featuresReply.getDatapathId()).thenReturn(new BigInteger("1"));
138 Mockito.when(featuresReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
139 Mockito.doNothing().when(deviceInitializationPhaseHandler).onDeviceContextLevelUp(Mockito.<DeviceInfo>any());
140 Mockito.doNothing().when(deviceTerminationPhaseHandler).onDeviceContextLevelDown(Mockito.<DeviceInfo>any());
141 Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
142 Mockito.when(writeTransaction.submit()).thenReturn(future);
143 Mockito.when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
144 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
145 Mockito.when(deviceInfo2.getNodeId()).thenReturn(nodeId2);
146 Mockito.when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
147 roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker, conductor);
148 roleManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
149 roleManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
150 Mockito.when(conductor.getDeviceContext(deviceInfo)).thenReturn(deviceContext);
151 roleManagerSpy = Mockito.spy(roleManager);
152 roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
153 roleContextSpy = Mockito.spy(roleManager.getRoleContext(nodeId));
154 Mockito.when(roleContextSpy.getDeviceInfo().getNodeId()).thenReturn(nodeId);
155 inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
159 public void tearDown() throws Exception {
162 @Test(expected = VerifyException.class)
163 public void testOnDeviceContextLevelUp() throws Exception {
164 roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
165 inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(deviceInfo);
166 inOrder.verifyNoMoreInteractions();
170 public void testCloseMaster() throws Exception {
171 roleManagerSpy.ownershipChanged(masterEntity);
172 roleManagerSpy.ownershipChanged(masterTxEntity);
173 roleManagerSpy.close();
174 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
175 inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(nodeId);
176 inOrder.verifyNoMoreInteractions();
180 public void testCloseSlave() throws Exception {
181 roleManagerSpy.ownershipChanged(slaveEntity);
182 roleManagerSpy.close();
183 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
184 inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(nodeId);
185 inOrder.verifyNoMoreInteractions();
189 public void testOnDeviceContextLevelDown() throws Exception {
190 roleManagerSpy.onDeviceContextLevelDown(deviceInfo);
191 inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceInfo);
192 inOrder.verifyNoMoreInteractions();
196 public void testOwnershipChanged1() throws Exception {
197 roleManagerSpy.ownershipChanged(masterEntity);
198 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
199 inOrder.verifyNoMoreInteractions();
203 public void testOwnershipChanged2() throws Exception {
204 Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
205 roleManagerSpy.ownershipChanged(masterEntity);
206 roleManagerSpy.ownershipChanged(masterTxEntity);
207 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
208 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
209 inOrder.verifyNoMoreInteractions();
213 public void testChangeOwnershipForMainEntity() throws Exception {
214 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
215 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
216 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
220 public void testChangeOwnershipForMainEntity2() throws Exception {
221 Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
222 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
223 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
227 public void testChangeOwnershipForTxEntity() throws Exception {
228 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
229 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
230 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
231 inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
232 inOrder.verify(roleContextSpy, Mockito.never()).close();
233 inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<NodeId>any());
237 public void testChangeOwnershipForTxEntity2() throws Exception {
238 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
239 roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
240 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
241 inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
242 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
243 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
247 public void testChangeOwnershipForTxEntity3() throws Exception {
248 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(false);
249 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
250 verify(roleContextSpy).close();
251 verify(conductor).closeConnection(deviceInfo);
255 public void testChangeOwnershipForTxEntity4() throws Exception {
256 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
257 roleManagerSpy.changeOwnershipForTxEntity(masterEntityNotOwner, roleContextSpy);
258 verify(roleContextSpy).close();
259 verify(conductor).closeConnection(deviceInfo);
263 public void testAddListener() throws Exception {
264 roleManager.addRoleChangeListener((new RoleChangeListener() {
266 public void roleInitializationDone(final DeviceInfo deviceInfo_, final boolean success) {
267 Assert.assertTrue(deviceInfo.equals(deviceInfo_));
268 Assert.assertTrue(success);
272 public void roleChangeOnDevice(final DeviceInfo deviceInfo_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
273 Assert.assertTrue(deviceInfo.equals(deviceInfo_));
274 Assert.assertTrue(success);
275 Assert.assertFalse(initializationPhase);
276 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
279 roleManager.notifyListenersRoleInitializationDone(deviceInfo, true);
280 roleManager.notifyListenersRoleChangeOnDevice(deviceInfo, true, OfpRole.BECOMEMASTER, false);
284 public void testMakeDeviceRoleChange() throws Exception{
285 roleManagerSpy.makeDeviceRoleChange(OfpRole.BECOMEMASTER, roleContextSpy, true);
286 verify(roleManagerSpy, atLeastOnce()).sendRoleChangeToDevice(Mockito.<OfpRole>any(), Mockito.<RoleContext>any());
287 verify(roleManagerSpy, atLeastOnce()).notifyListenersRoleChangeOnDevice(Mockito.<DeviceInfo>any(), eq(true), Mockito.<OfpRole>any(), eq(true));
291 public void testServicesChangeDone() throws Exception {
292 roleManagerSpy.setRoleContext(nodeId2, roleContextSpy);
293 roleManagerSpy.servicesChangeDone(deviceInfo2, true);
294 verify(roleContextSpy).unregisterCandidate(Mockito.<Entity>any());
298 public void testServicesChangeDoneContextIsNull() throws Exception {
299 roleManagerSpy.setRoleContext(nodeId, roleContextSpy);
300 roleManagerSpy.servicesChangeDone(deviceInfo2, true);
301 verify(roleContextSpy, never()).unregisterCandidate(Mockito.<Entity>any());