e4fc2c16e8c8f112dda2c2e2424715a931ea2a3e
[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 static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.verify;
14
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;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class RoleManagerImplTest {
57
58     @Mock
59     EntityOwnershipService entityOwnershipService;
60
61     @Mock
62     DataBroker dataBroker;
63
64     @Mock
65     DeviceContext deviceContext;
66
67     @Mock
68     DeviceManager deviceManager;
69
70     @Mock
71     EntityOwnershipListener entityOwnershipListener;
72
73     @Mock
74     EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
75
76     @Mock
77     EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
78
79     @Mock
80     ConnectionContext connectionContext;
81
82     @Mock
83     FeaturesReply featuresReply;
84
85     @Mock
86     DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
87
88     @Mock
89     DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
90
91     @Mock
92     WriteTransaction writeTransaction;
93
94     @Mock
95     LifecycleConductor conductor;
96
97     @Mock
98     DeviceState deviceState;
99
100     @Mock
101     DeviceInfo deviceInfo;
102
103     @Mock
104     DeviceInfo deviceInfo2;
105
106     @Mock
107     MessageSpy messageSpy;
108
109     @Mock
110     OutboundQueue outboundQueue;
111
112     @Mock
113     GetFeaturesOutput featuresOutput;
114
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");
120
121
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);
127
128     private InOrder inOrder;
129
130     @Before
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         inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
164     }
165
166     @After
167     public void tearDown() throws Exception {
168     }
169
170     @Test(expected = VerifyException.class)
171     public void testOnDeviceContextLevelUp() throws Exception {
172         roleManagerSpy.onDeviceContextLevelUp(deviceInfo);
173         inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(deviceInfo);
174         inOrder.verifyNoMoreInteractions();
175     }
176
177     @Test
178     public void testCloseMaster() throws Exception {
179         roleManagerSpy.ownershipChanged(masterEntity);
180         roleManagerSpy.ownershipChanged(masterTxEntity);
181         roleManagerSpy.close();
182         inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
183         inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(Mockito.eq(deviceInfo), Mockito.anyInt());
184         inOrder.verifyNoMoreInteractions();
185     }
186
187     @Test
188     public void testCloseSlave() throws Exception {
189         roleManagerSpy.ownershipChanged(slaveEntity);
190         roleManagerSpy.close();
191         inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
192         inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(Mockito.eq(deviceInfo), Mockito.anyInt());
193         inOrder.verifyNoMoreInteractions();
194     }
195
196     @Test
197     public void testOnDeviceContextLevelDown() throws Exception {
198         roleManagerSpy.onDeviceContextLevelDown(deviceInfo);
199         inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceInfo);
200         inOrder.verifyNoMoreInteractions();
201     }
202
203     @Test
204     public void testOwnershipChanged1() throws Exception {
205         roleManagerSpy.ownershipChanged(masterEntity);
206         inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
207         inOrder.verifyNoMoreInteractions();
208     }
209
210     @Test
211     public void testOwnershipChanged2() throws Exception {
212         Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
213         roleManagerSpy.ownershipChanged(masterEntity);
214         roleManagerSpy.ownershipChanged(masterTxEntity);
215         inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
216         inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
217         inOrder.verifyNoMoreInteractions();
218     }
219
220     @Test
221     public void testChangeOwnershipForMainEntity() throws Exception {
222         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
223         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
224         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
225     }
226
227     @Test
228     public void testChangeOwnershipForMainEntity2() throws Exception {
229         Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
230         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
231         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
232     }
233
234     @Test
235     public void testChangeOwnershipForTxEntity() throws Exception {
236         Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
237         roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
238         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
239         inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
240         inOrder.verify(roleContextSpy, Mockito.never()).close();
241         inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.any(), Mockito.anyInt());
242     }
243
244     @Test
245     public void testChangeOwnershipForTxEntity2() throws Exception {
246         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
247         roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
248         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
249         inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
250         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
251         inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
252     }
253
254     @Test
255     public void testChangeOwnershipForTxEntity3() throws Exception {
256         Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(false);
257         roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
258         verify(roleContextSpy).close();
259         verify(conductor).closeConnection(deviceInfo);
260     }
261
262     @Test
263     public void testChangeOwnershipForTxEntity4() throws Exception {
264         Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
265         roleManagerSpy.changeOwnershipForTxEntity(masterEntityNotOwner, roleContextSpy);
266         verify(roleContextSpy).close();
267         verify(conductor).closeConnection(deviceInfo);
268     }
269
270     @Test
271     public void testAddListener() throws Exception {
272         roleManager.addRoleChangeListener((new RoleChangeListener() {
273             @Override
274             public void roleInitializationDone(final DeviceInfo deviceInfo_, final boolean success) {
275                 Assert.assertTrue(deviceInfo.equals(deviceInfo_));
276                 Assert.assertTrue(success);
277             }
278
279             @Override
280             public void roleChangeOnDevice(final DeviceInfo deviceInfo_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
281                 Assert.assertTrue(RoleManagerImplTest.this.deviceInfo.equals(deviceInfo_));
282                 Assert.assertTrue(success);
283                 Assert.assertFalse(initializationPhase);
284                 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
285             }
286         }));
287         roleManager.notifyListenersRoleInitializationDone(deviceInfo, true);
288         roleManager.notifyListenersRoleChangeOnDevice(deviceInfo, true, OfpRole.BECOMEMASTER, false);
289     }
290
291     @Test
292     public void testServicesChangeDone() throws Exception {
293         roleManagerSpy.setRoleContext(deviceInfo2, roleContextSpy);
294         roleManagerSpy.servicesChangeDone(deviceInfo2, true);
295         verify(roleContextSpy).unregisterCandidate(Mockito.<Entity>any());
296     }
297
298     @Test
299     public void testServicesChangeDoneContextIsNull() throws Exception {
300         roleManagerSpy.setRoleContext(deviceInfo, roleContextSpy);
301         roleManagerSpy.servicesChangeDone(deviceInfo2, true);
302         verify(roleContextSpy, never()).unregisterCandidate(Mockito.<Entity>any());
303     }
304 }