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 com.google.common.util.concurrent.ListenableFuture;
18 import org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.InOrder;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
31 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
32 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
33 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
34 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
35 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
36 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
37 import org.opendaylight.openflowplugin.api.OFConstants;
38 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
40 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
41 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
42 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
43 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
44 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
45 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
46 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
47 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
48 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import static org.mockito.Matchers.eq;
57 import static org.mockito.Mockito.atLeastOnce;
58 import static org.mockito.Mockito.never;
59 import static org.mockito.Mockito.verify;
61 @RunWith(MockitoJUnitRunner.class)
62 public class RoleManagerImplTest {
65 EntityOwnershipService entityOwnershipService;
68 DataBroker dataBroker;
71 DeviceContext deviceContext;
74 DeviceManager deviceManager;
77 EntityOwnershipListener entityOwnershipListener;
80 EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
83 EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
86 ConnectionContext connectionContext;
89 FeaturesReply featuresReply;
92 DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
95 DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
98 WriteTransaction writeTransaction;
101 LifecycleConductor conductor;
104 DeviceState deviceState;
107 DeviceInfo deviceInfo;
110 DeviceInfo deviceInfo2;
113 MessageSpy messageSpy;
116 OutboundQueue outboundQueue;
119 GetFeaturesOutput featuresOutput;
121 private RoleManagerImpl roleManager;
122 private RoleManagerImpl roleManagerSpy;
123 private RoleContext roleContextSpy;
124 private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
125 private final NodeId nodeId2 = NodeId.getDefaultInstance("openflow:2");
128 private final EntityOwnershipChange masterEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), false, true, true, false);
129 private final EntityOwnershipChange masterTxEntity = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), false, true, true, false);
130 private final EntityOwnershipChange slaveEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true, false);
131 private final EntityOwnershipChange slaveTxEntityLast = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), true, false, false, false);
132 private final EntityOwnershipChange masterEntityNotOwner = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true, false);
134 private InOrder inOrder;
137 public void setUp() throws Exception {
138 CheckedFuture<Void, TransactionCommitFailedException> future = Futures.immediateCheckedFuture(null);
139 Mockito.when(entityOwnershipService.registerListener(Mockito.anyString(), Mockito.any(EntityOwnershipListener.class))).thenReturn(entityOwnershipListenerRegistration);
140 Mockito.when(entityOwnershipService.registerCandidate(Mockito.any(Entity.class))).thenReturn(entityOwnershipCandidateRegistration);
141 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
142 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
143 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
144 Mockito.when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
145 Mockito.when(deviceContext.getPrimaryConnectionContext().getOutboundQueueProvider()).thenReturn(outboundQueue);
146 Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
147 Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
148 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
149 Mockito.when(deviceInfo.getDatapathId()).thenReturn(new BigInteger("1"));
150 Mockito.when(deviceInfo.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
151 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
152 Mockito.doNothing().when(deviceInitializationPhaseHandler).onDeviceContextLevelUp(Mockito.<DeviceInfo>any());
153 Mockito.doNothing().when(deviceTerminationPhaseHandler).onDeviceContextLevelDown(Mockito.<DeviceInfo>any());
154 Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
155 Mockito.when(writeTransaction.submit()).thenReturn(future);
156 Mockito.when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
157 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
158 Mockito.when(deviceInfo2.getNodeId()).thenReturn(nodeId2);
159 Mockito.when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
160 roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker, conductor);
161 roleManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
162 roleManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
163 Mockito.when(conductor.getDeviceContext(deviceInfo)).thenReturn(deviceContext);
164 roleManagerSpy = Mockito.spy(roleManager);
165 roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
166 Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
167 roleContextSpy = Mockito.spy(roleManager.getRoleContext(deviceInfo));
168 Mockito.when(roleContextSpy.getDeviceInfo()).thenReturn(deviceInfo);
169 Mockito.when(roleContextSpy.getDeviceInfo().getNodeId()).thenReturn(nodeId);
170 // Mockito.when(roleManagerSpy.sendRoleChangeToDevice(Mockito.<OfpRole>any(), Mockito.<RoleContext>any())).thenReturn(rpcFuture);
171 inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
175 public void tearDown() throws Exception {
178 @Test(expected = VerifyException.class)
179 public void testOnDeviceContextLevelUp() throws Exception {
180 roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
181 inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(deviceInfo);
182 inOrder.verifyNoMoreInteractions();
186 public void testCloseMaster() throws Exception {
187 roleManagerSpy.ownershipChanged(masterEntity);
188 roleManagerSpy.ownershipChanged(masterTxEntity);
189 roleManagerSpy.close();
190 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
191 inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(deviceInfo);
192 inOrder.verifyNoMoreInteractions();
196 public void testCloseSlave() throws Exception {
197 roleManagerSpy.ownershipChanged(slaveEntity);
198 roleManagerSpy.close();
199 inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
200 inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(deviceInfo);
201 inOrder.verifyNoMoreInteractions();
205 public void testOnDeviceContextLevelDown() throws Exception {
206 roleManagerSpy.onDeviceContextLevelDown(deviceInfo);
207 inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceInfo);
208 inOrder.verifyNoMoreInteractions();
212 public void testOwnershipChanged1() throws Exception {
213 roleManagerSpy.ownershipChanged(masterEntity);
214 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
215 inOrder.verifyNoMoreInteractions();
219 public void testOwnershipChanged2() throws Exception {
220 Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
221 roleManagerSpy.ownershipChanged(masterEntity);
222 roleManagerSpy.ownershipChanged(masterTxEntity);
223 inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
224 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
225 inOrder.verifyNoMoreInteractions();
229 public void testChangeOwnershipForMainEntity() throws Exception {
230 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
231 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
232 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
236 public void testChangeOwnershipForMainEntity2() throws Exception {
237 Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
238 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
239 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
243 public void testChangeOwnershipForTxEntity() throws Exception {
244 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
245 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
246 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
247 inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
248 inOrder.verify(roleContextSpy, Mockito.never()).close();
249 inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<DeviceInfo>any());
253 public void testChangeOwnershipForTxEntity2() throws Exception {
254 roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
255 roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
256 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
257 inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
258 inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
259 inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
263 public void testChangeOwnershipForTxEntity3() throws Exception {
264 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(false);
265 roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
266 verify(roleContextSpy).close();
267 verify(conductor).closeConnection(deviceInfo);
271 public void testChangeOwnershipForTxEntity4() throws Exception {
272 Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
273 roleManagerSpy.changeOwnershipForTxEntity(masterEntityNotOwner, roleContextSpy);
274 verify(roleContextSpy).close();
275 verify(conductor).closeConnection(deviceInfo);
279 public void testAddListener() throws Exception {
280 roleManager.addRoleChangeListener((new RoleChangeListener() {
282 public void roleInitializationDone(final DeviceInfo deviceInfo_, final boolean success) {
283 Assert.assertTrue(deviceInfo.equals(deviceInfo_));
284 Assert.assertTrue(success);
288 public void roleChangeOnDevice(final DeviceInfo deviceInfo_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
289 Assert.assertTrue(RoleManagerImplTest.this.deviceInfo.equals(deviceInfo_));
290 Assert.assertTrue(success);
291 Assert.assertFalse(initializationPhase);
292 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
295 roleManager.notifyListenersRoleInitializationDone(deviceInfo, true);
296 roleManager.notifyListenersRoleChangeOnDevice(deviceInfo, true, OfpRole.BECOMEMASTER, false);
300 public void testServicesChangeDone() throws Exception {
301 roleManagerSpy.setRoleContext(deviceInfo2, roleContextSpy);
302 roleManagerSpy.servicesChangeDone(deviceInfo2, true);
303 verify(roleContextSpy).unregisterCandidate(Mockito.<Entity>any());
307 public void testServicesChangeDoneContextIsNull() throws Exception {
308 roleManagerSpy.setRoleContext(deviceInfo, roleContextSpy);
309 roleManagerSpy.servicesChangeDone(deviceInfo2, true);
310 verify(roleContextSpy, never()).unregisterCandidate(Mockito.<Entity>any());