Merge "BUG-4117: add support of Old Notif. for Statistics"
[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.DeviceManager;
39 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
40 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
41 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
42 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
43 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
44 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
49
50 import static org.mockito.Matchers.eq;
51 import static org.mockito.Mockito.atLeastOnce;
52 import static org.mockito.Mockito.never;
53 import static org.mockito.Mockito.verify;
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     GetFeaturesOutput featuresOutput;
102
103     private RoleManagerImpl roleManager;
104     private RoleManagerImpl roleManagerSpy;
105     private RoleContext roleContextSpy;
106     private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
107
108     private final EntityOwnershipChange masterEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), false, true, true);
109     private final EntityOwnershipChange masterTxEntity = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), false, true, true);
110     private final EntityOwnershipChange slaveEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true);
111     private final EntityOwnershipChange slaveTxEntityLast = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), true, false, false);
112     private final EntityOwnershipChange masterEntityNotOwner = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true);
113
114     private InOrder inOrder;
115
116     @Before
117     public void setUp() throws Exception {
118         CheckedFuture<Void, TransactionCommitFailedException> future = Futures.immediateCheckedFuture(null);
119         Mockito.when(deviceState.getFeatures()).thenReturn(featuresOutput);
120         Mockito.when(entityOwnershipService.registerListener(Mockito.anyString(), Mockito.any(EntityOwnershipListener.class))).thenReturn(entityOwnershipListenerRegistration);
121         Mockito.when(entityOwnershipService.registerCandidate(Mockito.any(Entity.class))).thenReturn(entityOwnershipCandidateRegistration);
122         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
123         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
124         Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
125         Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
126         Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
127         Mockito.when(featuresReply.getDatapathId()).thenReturn(new BigInteger("1"));
128         Mockito.when(featuresReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
129         Mockito.doNothing().when(deviceInitializationPhaseHandler).onDeviceContextLevelUp(Mockito.<NodeId>any());
130         Mockito.doNothing().when(deviceTerminationPhaseHandler).onDeviceContextLevelDown(Mockito.<DeviceContext>any());
131         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
132         Mockito.when(writeTransaction.submit()).thenReturn(future);
133         Mockito.when(deviceManager.getDeviceContextFromNodeId(Mockito.<NodeId>any())).thenReturn(deviceContext);
134         roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker, conductor);
135         roleManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
136         roleManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
137         Mockito.when(conductor.getDeviceContext(Mockito.<NodeId>any())).thenReturn(deviceContext);
138         roleManagerSpy = Mockito.spy(roleManager);
139         roleManagerSpy.onDeviceContextLevelUp(nodeId);
140         roleContextSpy = Mockito.spy(roleManager.getRoleContext(nodeId));
141         inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
142     }
143
144     @After
145     public void tearDown() throws Exception {
146     }
147
148     @Test(expected = VerifyException.class)
149     public void testOnDeviceContextLevelUp() throws Exception {
150         roleManagerSpy.onDeviceContextLevelUp(nodeId);
151         inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(nodeId);
152         inOrder.verifyNoMoreInteractions();
153     }
154
155     @Test
156     public void testCloseMaster() throws Exception {
157         roleManagerSpy.ownershipChanged(masterEntity);
158         roleManagerSpy.ownershipChanged(masterTxEntity);
159         roleManagerSpy.close();
160         inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
161         inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(nodeId);
162         inOrder.verifyNoMoreInteractions();
163     }
164
165     @Test
166     public void testCloseSlave() throws Exception {
167         roleManagerSpy.ownershipChanged(slaveEntity);
168         roleManagerSpy.close();
169         inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
170         inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(nodeId);
171         inOrder.verifyNoMoreInteractions();
172     }
173
174     @Test
175     public void testOnDeviceContextLevelDown() throws Exception {
176         roleManagerSpy.onDeviceContextLevelDown(deviceContext);
177         inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceContext);
178         inOrder.verifyNoMoreInteractions();
179     }
180
181     @Test
182     public void testOwnershipChanged1() throws Exception {
183         roleManagerSpy.ownershipChanged(masterEntity);
184         inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
185         inOrder.verifyNoMoreInteractions();
186     }
187
188     @Test
189     public void testOwnershipChanged2() throws Exception {
190         Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
191         roleManagerSpy.ownershipChanged(masterEntity);
192         roleManagerSpy.ownershipChanged(masterTxEntity);
193         inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
194         inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
195         inOrder.verifyNoMoreInteractions();
196     }
197
198     @Test
199     public void testChangeOwnershipForMainEntity() throws Exception {
200         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
201         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
202         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
203     }
204
205     @Test
206     public void testChangeOwnershipForMainEntity2() throws Exception {
207         Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
208         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
209         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
210     }
211
212     @Test
213     public void testChangeOwnershipForTxEntity() throws Exception {
214         Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
215         roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
216         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
217         inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
218         inOrder.verify(roleContextSpy, Mockito.never()).close();
219         inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<NodeId>any());
220     }
221
222     @Test
223     public void testChangeOwnershipForTxEntity2() throws Exception {
224         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
225         roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
226         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
227         inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
228         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
229         inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
230     }
231
232     @Test
233     public void testChangeOwnershipForTxEntity3() throws Exception {
234         Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(false);
235         roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
236         verify(roleContextSpy).close();
237         verify(roleContextSpy).getNodeId();
238         verify(conductor).closeConnection(nodeId);
239     }
240
241     @Test
242     public void testChangeOwnershipForTxEntity4() throws Exception {
243         Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
244         roleManagerSpy.changeOwnershipForTxEntity(masterEntityNotOwner, roleContextSpy);
245         verify(roleContextSpy).close();
246         verify(conductor).closeConnection(nodeId);
247     }
248
249     @Test
250     public void testAddListener() throws Exception {
251         roleManager.addRoleChangeListener((new RoleChangeListener() {
252             @Override
253             public void roleInitializationDone(final NodeId nodeId, final boolean success) {
254                 Assert.assertTrue(nodeId.equals(nodeId));
255                 Assert.assertTrue(success);
256             }
257
258             @Override
259             public void roleChangeOnDevice(final NodeId nodeId_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
260                 Assert.assertTrue(nodeId.equals(nodeId_));
261                 Assert.assertTrue(success);
262                 Assert.assertFalse(initializationPhase);
263                 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
264             }
265         }));
266         roleManager.notifyListenersRoleInitializationDone(nodeId, true);
267         roleManager.notifyListenersRoleChangeOnDevice(nodeId, true, OfpRole.BECOMEMASTER, false);
268     }
269
270     @Test
271     public void testMakeDeviceRoleChange() throws Exception{
272         roleManagerSpy.makeDeviceRoleChange(OfpRole.BECOMEMASTER, roleContextSpy, true);
273         verify(roleManagerSpy, atLeastOnce()).sendRoleChangeToDevice(Mockito.<OfpRole>any(), Mockito.<RoleContext>any());
274         verify(roleManagerSpy, atLeastOnce()).notifyListenersRoleChangeOnDevice(Mockito.<NodeId>any(), eq(true), Mockito.<OfpRole>any(), eq(true));
275     }
276
277     @Test
278     public void testServicesChangeDone() throws Exception {
279         final NodeId nodeId2 = NodeId.getDefaultInstance("openflow:2");
280         roleManagerSpy.setRoleContext(nodeId2, roleContextSpy);
281         roleManagerSpy.servicesChangeDone(nodeId2, true);
282         verify(roleContextSpy).unregisterCandidate(Mockito.<Entity>any());
283     }
284
285     @Test
286     public void testServicesChangeDoneContextIsNull() throws Exception {
287         final NodeId nodeId2 = NodeId.getDefaultInstance("openflow:2");
288         roleManagerSpy.setRoleContext(nodeId, roleContextSpy);
289         roleManagerSpy.servicesChangeDone(nodeId2, true);
290         verify(roleContextSpy, never()).unregisterCandidate(Mockito.<Entity>any());
291     }
292 }