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 GetFeaturesOutput featuresOutput;
107 private RoleManagerImpl roleManager;
108 private RoleManagerImpl roleManagerSpy;
109 private RoleContext roleContextSpy;
110 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
112 private final EntityOwnershipChange masterEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), false, true, true);
113 private final EntityOwnershipChange masterTxEntity = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), false, true, true);
114 private final EntityOwnershipChange slaveEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true);
115 private final EntityOwnershipChange slaveTxEntityLast = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), true, false, false);
116 private final EntityOwnershipChange masterEntityNotOwner = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true);
118 private InOrder inOrder;
121 public void setUp() throws Exception {
122 CheckedFuture<Void, TransactionCommitFailedException> future = Futures.immediateCheckedFuture(null);
123 Mockito.when(deviceState.getFeatures()).thenReturn(featuresOutput);
124 Mockito.when(entityOwnershipService.registerListener(Mockito.anyString(), Mockito.any(EntityOwnershipListener.class))).thenReturn(entityOwnershipListenerRegistration);
125 Mockito.when(entityOwnershipService.registerCandidate(Mockito.any(Entity.class))).thenReturn(entityOwnershipCandidateRegistration);
126 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
127 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
128 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
129 Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
130 Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
131 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
132 Mockito.when(featuresReply.getDatapathId()).thenReturn(new BigInteger("1"));
133 Mockito.when(featuresReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
134 Mockito.doNothing().when(deviceInitializationPhaseHandler).onDeviceContextLevelUp(Mockito.<DeviceInfo>any());
135 Mockito.doNothing().when(deviceTerminationPhaseHandler).onDeviceContextLevelDown(Mockito.<DeviceInfo>any());
136 Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
137 Mockito.when(writeTransaction.submit()).thenReturn(future);
138 Mockito.when(deviceManager.getDeviceContextFromNodeId(Mockito.<NodeId>any())).thenReturn(deviceContext);
139 roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker, conductor);
140 roleManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
141 roleManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
142 Mockito.when(conductor.getDeviceContext(Mockito.<NodeId>any())).thenReturn(deviceContext);
143 roleManagerSpy = Mockito.spy(roleManager);
144 roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
145 roleContextSpy = Mockito.spy(roleManager.getRoleContext(nodeId));
146 inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
150 public void tearDown() throws Exception {
153 @Test(expected = VerifyException.class)
154 public void testOnDeviceContextLevelUp() throws Exception {
155 roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
156 inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(deviceInfo);
157 inOrder.verifyNoMoreInteractions();
161 public void testCloseMaster() throws Exception {
162 roleManagerSpy.ownershipChanged(masterEntity);
163 roleManagerSpy.ownershipChanged(masterTxEntity);
164 roleManagerSpy.close();
165 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
166 inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(nodeId);
167 inOrder.verifyNoMoreInteractions();
171 public void testCloseSlave() throws Exception {
172 roleManagerSpy.ownershipChanged(slaveEntity);
173 roleManagerSpy.close();
174 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
175 inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(nodeId);
176 inOrder.verifyNoMoreInteractions();
180 public void testOnDeviceContextLevelDown() throws Exception {
181 roleManagerSpy.onDeviceContextLevelDown(deviceInfo);
182 inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceInfo);
183 inOrder.verifyNoMoreInteractions();
187 public void testOwnershipChanged1() throws Exception {
188 roleManagerSpy.ownershipChanged(masterEntity);
189 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
190 inOrder.verifyNoMoreInteractions();
194 public void testOwnershipChanged2() throws Exception {
195 Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
196 roleManagerSpy.ownershipChanged(masterEntity);
197 roleManagerSpy.ownershipChanged(masterTxEntity);
198 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
199 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
200 inOrder.verifyNoMoreInteractions();
204 public void testChangeOwnershipForMainEntity() throws Exception {
205 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
206 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
207 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
211 public void testChangeOwnershipForMainEntity2() throws Exception {
212 Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
213 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
214 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
218 public void testChangeOwnershipForTxEntity() throws Exception {
219 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
220 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
221 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
222 inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
223 inOrder.verify(roleContextSpy, Mockito.never()).close();
224 inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<NodeId>any());
228 public void testChangeOwnershipForTxEntity2() throws Exception {
229 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
230 roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
231 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
232 inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
233 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
234 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
238 public void testChangeOwnershipForTxEntity3() throws Exception {
239 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(false);
240 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
241 verify(roleContextSpy).close();
242 verify(roleContextSpy).getNodeId();
243 verify(conductor).closeConnection(nodeId);
247 public void testChangeOwnershipForTxEntity4() throws Exception {
248 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
249 roleManagerSpy.changeOwnershipForTxEntity(masterEntityNotOwner, roleContextSpy);
250 verify(roleContextSpy).close();
251 verify(conductor).closeConnection(nodeId);
255 public void testAddListener() throws Exception {
256 roleManager.addRoleChangeListener((new RoleChangeListener() {
258 public void roleInitializationDone(final NodeId nodeId, final boolean success) {
259 Assert.assertTrue(nodeId.equals(nodeId));
260 Assert.assertTrue(success);
264 public void roleChangeOnDevice(final NodeId nodeId_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
265 Assert.assertTrue(nodeId.equals(nodeId_));
266 Assert.assertTrue(success);
267 Assert.assertFalse(initializationPhase);
268 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
271 roleManager.notifyListenersRoleInitializationDone(nodeId, true);
272 roleManager.notifyListenersRoleChangeOnDevice(nodeId, true, OfpRole.BECOMEMASTER, false);
276 public void testMakeDeviceRoleChange() throws Exception{
277 roleManagerSpy.makeDeviceRoleChange(OfpRole.BECOMEMASTER, roleContextSpy, true);
278 verify(roleManagerSpy, atLeastOnce()).sendRoleChangeToDevice(Mockito.<OfpRole>any(), Mockito.<RoleContext>any());
279 verify(roleManagerSpy, atLeastOnce()).notifyListenersRoleChangeOnDevice(Mockito.<NodeId>any(), eq(true), Mockito.<OfpRole>any(), eq(true));
283 public void testServicesChangeDone() throws Exception {
284 final NodeId nodeId2 = NodeId.getDefaultInstance("openflow:2");
285 roleManagerSpy.setRoleContext(nodeId2, roleContextSpy);
286 roleManagerSpy.servicesChangeDone(nodeId2, true);
287 verify(roleContextSpy).unregisterCandidate(Mockito.<Entity>any());
291 public void testServicesChangeDoneContextIsNull() throws Exception {
292 final NodeId nodeId2 = NodeId.getDefaultInstance("openflow:2");
293 roleManagerSpy.setRoleContext(nodeId, roleContextSpy);
294 roleManagerSpy.servicesChangeDone(nodeId2, true);
295 verify(roleContextSpy, never()).unregisterCandidate(Mockito.<Entity>any());