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.DeviceManager;
39 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
40 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
41 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
42 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
43 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
49 * Created by Jozef Bacigal
53 @RunWith(MockitoJUnitRunner.class)
54 public class RoleManagerImplTest {
57 EntityOwnershipService entityOwnershipService;
60 DataBroker dataBroker;
63 DeviceContext deviceContext;
66 DeviceManager deviceManager;
69 EntityOwnershipListener entityOwnershipListener;
72 EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
75 EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
78 ConnectionContext connectionContext;
81 FeaturesReply featuresReply;
84 DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
87 DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
90 WriteTransaction writeTransaction;
93 LifecycleConductor conductor;
95 private RoleManagerImpl roleManager;
96 private RoleManagerImpl roleManagerSpy;
97 private RoleContext roleContextSpy;
98 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
100 private final EntityOwnershipChange masterEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), false, true, true);
101 private final EntityOwnershipChange masterTxEntity = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), false, true, true);
102 private final EntityOwnershipChange slaveEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true);
103 private final EntityOwnershipChange slaveTxEntityLast = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), true, false, false);
105 private InOrder inOrder;
108 public void setUp() throws Exception {
109 CheckedFuture<Void, TransactionCommitFailedException> future = Futures.immediateCheckedFuture(null);
110 Mockito.when(entityOwnershipService.registerListener(Mockito.anyString(), Mockito.any(EntityOwnershipListener.class))).thenReturn(entityOwnershipListenerRegistration);
111 Mockito.when(entityOwnershipService.registerCandidate(Mockito.any(Entity.class))).thenReturn(entityOwnershipCandidateRegistration);
112 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
113 Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
114 Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
115 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
116 Mockito.when(featuresReply.getDatapathId()).thenReturn(new BigInteger("1"));
117 Mockito.when(featuresReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
118 Mockito.doNothing().when(deviceInitializationPhaseHandler).onDeviceContextLevelUp(Mockito.<NodeId>any());
119 Mockito.doNothing().when(deviceTerminationPhaseHandler).onDeviceContextLevelDown(Mockito.<DeviceContext>any());
120 Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
121 Mockito.when(writeTransaction.submit()).thenReturn(future);
122 Mockito.when(deviceManager.getDeviceContextFromNodeId(Mockito.<NodeId>any())).thenReturn(deviceContext);
123 roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker, conductor);
124 roleManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
125 roleManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
126 Mockito.when(conductor.getDeviceContext(Mockito.<NodeId>any())).thenReturn(deviceContext);
127 roleManagerSpy = Mockito.spy(roleManager);
128 Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
129 roleManagerSpy.onDeviceContextLevelUp(nodeId);
130 roleContextSpy = Mockito.spy(roleManager.getRoleContext(nodeId));
131 inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
135 public void tearDown() throws Exception {
138 @Test(expected = VerifyException.class)
139 public void testOnDeviceContextLevelUp() throws Exception {
140 roleManagerSpy.onDeviceContextLevelUp(nodeId);
141 inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(nodeId);
142 inOrder.verifyNoMoreInteractions();
146 public void testCloseMaster() throws Exception {
147 roleManagerSpy.ownershipChanged(masterEntity);
148 roleManagerSpy.ownershipChanged(masterTxEntity);
149 roleManagerSpy.close();
150 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
151 inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(nodeId);
152 inOrder.verifyNoMoreInteractions();
156 public void testCloseSlave() throws Exception {
157 roleManagerSpy.ownershipChanged(slaveEntity);
158 roleManagerSpy.close();
159 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
160 inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(nodeId);
161 inOrder.verifyNoMoreInteractions();
165 public void testOnDeviceContextLevelDown() throws Exception {
166 roleManagerSpy.onDeviceContextLevelDown(deviceContext);
167 inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceContext);
168 inOrder.verifyNoMoreInteractions();
172 public void testOwnershipChanged1() throws Exception {
173 roleManagerSpy.ownershipChanged(masterEntity);
174 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
175 inOrder.verifyNoMoreInteractions();
179 public void testOwnershipChanged2() throws Exception {
180 roleManagerSpy.ownershipChanged(masterEntity);
181 roleManagerSpy.ownershipChanged(masterTxEntity);
182 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
183 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
184 inOrder.verifyNoMoreInteractions();
188 public void testChangeOwnershipForMainEntity() throws Exception {
189 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
190 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
191 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
195 public void testChangeOwnershipForMainEntity2() throws Exception {
196 Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
197 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
198 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
202 public void testChangeOwnershipForTxEntity() throws Exception {
203 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
204 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
205 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
206 inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
207 inOrder.verify(roleContextSpy, Mockito.never()).close();
208 inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<NodeId>any());
212 public void testChangeOwnershipForTxEntity2() throws Exception {
213 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
214 roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
215 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
216 inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
217 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
218 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
222 public void testAddListener() throws Exception {
223 roleManager.addRoleChangeListener((new RoleChangeListener() {
225 public void roleInitializationDone(final NodeId nodeId, final boolean success) {
226 Assert.assertTrue(nodeId.equals(nodeId));
227 Assert.assertTrue(success);
231 public void roleChangeOnDevice(final NodeId nodeId_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
232 Assert.assertTrue(nodeId.equals(nodeId_));
233 Assert.assertTrue(success);
234 Assert.assertFalse(initializationPhase);
235 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
238 roleManager.notifyListenersRoleInitializationDone(nodeId, true);
239 roleManager.notifyListenersRoleChangeOnDevice(nodeId, true, OfpRole.BECOMEMASTER, false);