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.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;
51 * Created by Jozef Bacigal
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 GetFeaturesOutput featuresOutput;
103 private RoleManagerImpl roleManager;
104 private RoleManagerImpl roleManagerSpy;
105 private RoleContext roleContextSpy;
106 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
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);
113 private InOrder inOrder;
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);
145 public void tearDown() throws Exception {
148 @Test(expected = VerifyException.class)
149 public void testOnDeviceContextLevelUp() throws Exception {
150 roleManagerSpy.onDeviceContextLevelUp(nodeId);
151 inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(nodeId);
152 inOrder.verifyNoMoreInteractions();
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();
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();
175 public void testOnDeviceContextLevelDown() throws Exception {
176 roleManagerSpy.onDeviceContextLevelDown(deviceContext);
177 inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceContext);
178 inOrder.verifyNoMoreInteractions();
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();
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();
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());
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();
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());
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());
232 public void testAddListener() throws Exception {
233 roleManager.addRoleChangeListener((new RoleChangeListener() {
235 public void roleInitializationDone(final NodeId nodeId, final boolean success) {
236 Assert.assertTrue(nodeId.equals(nodeId));
237 Assert.assertTrue(success);
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));
248 roleManager.notifyListenersRoleInitializationDone(nodeId, true);
249 roleManager.notifyListenersRoleChangeOnDevice(nodeId, true, OfpRole.BECOMEMASTER, false);