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 // Mockito.when(roleManagerSpy.sendRoleChangeToDevice(Mockito.<OfpRole>any(), Mockito.<RoleContext>any())).thenReturn(rpcFuture);
164 inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
168 public void tearDown() throws Exception {
171 @Test(expected = VerifyException.class)
172 public void testOnDeviceContextLevelUp() throws Exception {
173 roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
174 inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(deviceInfo);
175 inOrder.verifyNoMoreInteractions();
179 public void testCloseMaster() throws Exception {
180 roleManagerSpy.ownershipChanged(masterEntity);
181 roleManagerSpy.ownershipChanged(masterTxEntity);
182 roleManagerSpy.close();
183 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
184 inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(deviceInfo);
185 inOrder.verifyNoMoreInteractions();
189 public void testCloseSlave() throws Exception {
190 roleManagerSpy.ownershipChanged(slaveEntity);
191 roleManagerSpy.close();
192 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
193 inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(deviceInfo);
194 inOrder.verifyNoMoreInteractions();
198 public void testOnDeviceContextLevelDown() throws Exception {
199 roleManagerSpy.onDeviceContextLevelDown(deviceInfo);
200 inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceInfo);
201 inOrder.verifyNoMoreInteractions();
205 public void testOwnershipChanged1() throws Exception {
206 roleManagerSpy.ownershipChanged(masterEntity);
207 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
208 inOrder.verifyNoMoreInteractions();
212 public void testOwnershipChanged2() throws Exception {
213 Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
214 roleManagerSpy.ownershipChanged(masterEntity);
215 roleManagerSpy.ownershipChanged(masterTxEntity);
216 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
217 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
218 inOrder.verifyNoMoreInteractions();
222 public void testChangeOwnershipForMainEntity() throws Exception {
223 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
224 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
225 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
229 public void testChangeOwnershipForMainEntity2() throws Exception {
230 Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
231 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
232 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
236 public void testChangeOwnershipForTxEntity() throws Exception {
237 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
238 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
239 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
240 inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
241 inOrder.verify(roleContextSpy, Mockito.never()).close();
242 inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
246 public void testChangeOwnershipForTxEntity2() throws Exception {
247 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
248 roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
249 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
250 inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
251 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
252 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
256 public void testChangeOwnershipForTxEntity3() throws Exception {
257 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(false);
258 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
259 verify(roleContextSpy).close();
260 verify(conductor).closeConnection(deviceInfo);
264 public void testChangeOwnershipForTxEntity4() throws Exception {
265 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
266 roleManagerSpy.changeOwnershipForTxEntity(masterEntityNotOwner, roleContextSpy);
267 verify(roleContextSpy).close();
268 verify(conductor).closeConnection(deviceInfo);
272 public void testAddListener() throws Exception {
273 roleManager.addRoleChangeListener((new RoleChangeListener() {
275 public void roleInitializationDone(final DeviceInfo deviceInfo_, final boolean success) {
276 Assert.assertTrue(deviceInfo.equals(deviceInfo_));
277 Assert.assertTrue(success);
281 public void roleChangeOnDevice(final DeviceInfo deviceInfo_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
282 Assert.assertTrue(RoleManagerImplTest.this.deviceInfo.equals(deviceInfo_));
283 Assert.assertTrue(success);
284 Assert.assertFalse(initializationPhase);
285 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
288 roleManager.notifyListenersRoleInitializationDone(deviceInfo, true);
289 roleManager.notifyListenersRoleChangeOnDevice(deviceInfo, true, OfpRole.BECOMEMASTER, false);
293 public void testServicesChangeDone() throws Exception {
294 roleManagerSpy.setRoleContext(deviceInfo2, roleContextSpy);
295 roleManagerSpy.servicesChangeDone(deviceInfo2, true);
296 verify(roleContextSpy).unregisterCandidate(Mockito.<Entity>any());
300 public void testServicesChangeDoneContextIsNull() throws Exception {
301 roleManagerSpy.setRoleContext(deviceInfo, roleContextSpy);
302 roleManagerSpy.servicesChangeDone(deviceInfo2, true);
303 verify(roleContextSpy, never()).unregisterCandidate(Mockito.<Entity>any());