c134efc3aa1f8757bc5a4768bc6e5da3d76a4aad
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleManagerImplTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.role;
10
11
12 import java.math.BigInteger;
13
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;
50
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;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public class RoleManagerImplTest {
58
59     @Mock
60     EntityOwnershipService entityOwnershipService;
61
62     @Mock
63     DataBroker dataBroker;
64
65     @Mock
66     DeviceContext deviceContext;
67
68     @Mock
69     DeviceManager deviceManager;
70
71     @Mock
72     EntityOwnershipListener entityOwnershipListener;
73
74     @Mock
75     EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
76
77     @Mock
78     EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
79
80     @Mock
81     ConnectionContext connectionContext;
82
83     @Mock
84     FeaturesReply featuresReply;
85
86     @Mock
87     DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
88
89     @Mock
90     DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
91
92     @Mock
93     WriteTransaction writeTransaction;
94
95     @Mock
96     LifecycleConductor conductor;
97
98     @Mock
99     DeviceState deviceState;
100
101     @Mock
102     DeviceInfo deviceInfo;
103
104     @Mock
105     GetFeaturesOutput featuresOutput;
106
107     private RoleManagerImpl roleManager;
108     private RoleManagerImpl roleManagerSpy;
109     private RoleContext roleContextSpy;
110     private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
111
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);
117
118     private InOrder inOrder;
119
120     @Before
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);
147     }
148
149     @After
150     public void tearDown() throws Exception {
151     }
152
153     @Test(expected = VerifyException.class)
154     public void testOnDeviceContextLevelUp() throws Exception {
155         roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
156         inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(deviceInfo);
157         inOrder.verifyNoMoreInteractions();
158     }
159
160     @Test
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();
168     }
169
170     @Test
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();
177     }
178
179     @Test
180     public void testOnDeviceContextLevelDown() throws Exception {
181         roleManagerSpy.onDeviceContextLevelDown(deviceInfo);
182         inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceInfo);
183         inOrder.verifyNoMoreInteractions();
184     }
185
186     @Test
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();
191     }
192
193     @Test
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();
201     }
202
203     @Test
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());
208     }
209
210     @Test
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();
215     }
216
217     @Test
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());
225     }
226
227     @Test
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());
235     }
236
237     @Test
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);
244     }
245
246     @Test
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);
252     }
253
254     @Test
255     public void testAddListener() throws Exception {
256         roleManager.addRoleChangeListener((new RoleChangeListener() {
257             @Override
258             public void roleInitializationDone(final NodeId nodeId, final boolean success) {
259                 Assert.assertTrue(nodeId.equals(nodeId));
260                 Assert.assertTrue(success);
261             }
262
263             @Override
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));
269             }
270         }));
271         roleManager.notifyListenersRoleInitializationDone(nodeId, true);
272         roleManager.notifyListenersRoleChangeOnDevice(nodeId, true, OfpRole.BECOMEMASTER, false);
273     }
274
275     @Test
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));
280     }
281
282     @Test
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());
288     }
289
290     @Test
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());
296     }
297 }