Merge "BUG-4148: Improving the logging in flow/group/meter to provide more contextual...
[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.handlers.DeviceInitializationPhaseHandler;
40 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
41 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
42 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
43 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
47
48 /**
49  * Created by Jozef Bacigal
50  * Date: 19.4.2016.
51  * Time: 13:08
52  */
53 @RunWith(MockitoJUnitRunner.class)
54 public class RoleManagerImplTest {
55
56     @Mock
57     EntityOwnershipService entityOwnershipService;
58
59     @Mock
60     DataBroker dataBroker;
61
62     @Mock
63     DeviceContext deviceContext;
64
65     @Mock
66     DeviceManager deviceManager;
67
68     @Mock
69     EntityOwnershipListener entityOwnershipListener;
70
71     @Mock
72     EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
73
74     @Mock
75     EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
76
77     @Mock
78     ConnectionContext connectionContext;
79
80     @Mock
81     FeaturesReply featuresReply;
82
83     @Mock
84     DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
85
86     @Mock
87     DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
88
89     @Mock
90     WriteTransaction writeTransaction;
91
92     @Mock
93     LifecycleConductor conductor;
94
95     private RoleManagerImpl roleManager;
96     private RoleManagerImpl roleManagerSpy;
97     private RoleContext roleContextSpy;
98     private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
99
100     private final EntityOwnershipChange masterEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), false, true, true);
101     private final EntityOwnershipChange masterTxEntity = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), false, true, true);
102     private final EntityOwnershipChange slaveEntity = new EntityOwnershipChange(RoleManagerImpl.makeEntity(nodeId), true, false, true);
103     private final EntityOwnershipChange slaveTxEntityLast = new EntityOwnershipChange(RoleManagerImpl.makeTxEntity(nodeId), true, false, false);
104
105     private InOrder inOrder;
106
107     @Before
108     public void setUp() throws Exception {
109         CheckedFuture<Void, TransactionCommitFailedException> future = Futures.immediateCheckedFuture(null);
110         Mockito.when(entityOwnershipService.registerListener(Mockito.anyString(), Mockito.any(EntityOwnershipListener.class))).thenReturn(entityOwnershipListenerRegistration);
111         Mockito.when(entityOwnershipService.registerCandidate(Mockito.any(Entity.class))).thenReturn(entityOwnershipCandidateRegistration);
112         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
113         Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
114         Mockito.when(connectionContext.getNodeId()).thenReturn(nodeId);
115         Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
116         Mockito.when(featuresReply.getDatapathId()).thenReturn(new BigInteger("1"));
117         Mockito.when(featuresReply.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
118         Mockito.doNothing().when(deviceInitializationPhaseHandler).onDeviceContextLevelUp(Mockito.<NodeId>any());
119         Mockito.doNothing().when(deviceTerminationPhaseHandler).onDeviceContextLevelDown(Mockito.<DeviceContext>any());
120         Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
121         Mockito.when(writeTransaction.submit()).thenReturn(future);
122         Mockito.when(deviceManager.getDeviceContextFromNodeId(Mockito.<NodeId>any())).thenReturn(deviceContext);
123         roleManager = new RoleManagerImpl(entityOwnershipService, dataBroker, conductor);
124         roleManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
125         roleManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
126         Mockito.when(conductor.getDeviceContext(Mockito.<NodeId>any())).thenReturn(deviceContext);
127         roleManagerSpy = Mockito.spy(roleManager);
128         Mockito.doNothing().when(roleManagerSpy).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
129         roleManagerSpy.onDeviceContextLevelUp(nodeId);
130         roleContextSpy = Mockito.spy(roleManager.getRoleContext(nodeId));
131         inOrder = Mockito.inOrder(entityOwnershipListenerRegistration, roleManagerSpy, roleContextSpy);
132     }
133
134     @After
135     public void tearDown() throws Exception {
136     }
137
138     @Test(expected = VerifyException.class)
139     public void testOnDeviceContextLevelUp() throws Exception {
140         roleManagerSpy.onDeviceContextLevelUp(nodeId);
141         inOrder.verify(roleManagerSpy).onDeviceContextLevelUp(nodeId);
142         inOrder.verifyNoMoreInteractions();
143     }
144
145     @Test
146     public void testCloseMaster() throws Exception {
147         roleManagerSpy.ownershipChanged(masterEntity);
148         roleManagerSpy.ownershipChanged(masterTxEntity);
149         roleManagerSpy.close();
150         inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
151         inOrder.verify(roleManagerSpy).removeDeviceFromOperationalDS(nodeId);
152         inOrder.verifyNoMoreInteractions();
153     }
154
155     @Test
156     public void testCloseSlave() throws Exception {
157         roleManagerSpy.ownershipChanged(slaveEntity);
158         roleManagerSpy.close();
159         inOrder.verify(entityOwnershipListenerRegistration, Mockito.calls(2)).close();
160         inOrder.verify(roleManagerSpy, Mockito.never()).removeDeviceFromOperationalDS(nodeId);
161         inOrder.verifyNoMoreInteractions();
162     }
163
164     @Test
165     public void testOnDeviceContextLevelDown() throws Exception {
166         roleManagerSpy.onDeviceContextLevelDown(deviceContext);
167         inOrder.verify(roleManagerSpy).onDeviceContextLevelDown(deviceContext);
168         inOrder.verifyNoMoreInteractions();
169     }
170
171     @Test
172     public void testOwnershipChanged1() throws Exception {
173         roleManagerSpy.ownershipChanged(masterEntity);
174         inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForMainEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
175         inOrder.verifyNoMoreInteractions();
176     }
177
178     @Test
179     public void testOwnershipChanged2() throws Exception {
180         roleManagerSpy.ownershipChanged(masterEntity);
181         roleManagerSpy.ownershipChanged(masterTxEntity);
182         inOrder.verify(roleManagerSpy, Mockito.calls(1)).changeOwnershipForTxEntity(Mockito.<EntityOwnershipChange>any(),Mockito.<RoleContext>any());
183         inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
184         inOrder.verifyNoMoreInteractions();
185     }
186
187     @Test
188     public void testChangeOwnershipForMainEntity() throws Exception {
189         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
190         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
191         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).registerCandidate(Mockito.<Entity>any());
192     }
193
194     @Test
195     public void testChangeOwnershipForMainEntity2() throws Exception {
196         Mockito.when(roleContextSpy.isMainCandidateRegistered()).thenReturn(false);
197         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
198         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
199     }
200
201     @Test
202     public void testChangeOwnershipForTxEntity() throws Exception {
203         Mockito.when(roleContextSpy.isTxCandidateRegistered()).thenReturn(true);
204         roleManagerSpy.changeOwnershipForTxEntity(slaveTxEntityLast, roleContextSpy);
205         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
206         inOrder.verify(roleContextSpy, Mockito.calls(1)).unregisterCandidate(Mockito.<Entity>any());
207         inOrder.verify(roleContextSpy, Mockito.never()).close();
208         inOrder.verify(roleManagerSpy, Mockito.calls(1)).removeDeviceFromOperationalDS(Mockito.<NodeId>any());
209     }
210
211     @Test
212     public void testChangeOwnershipForTxEntity2() throws Exception {
213         roleManagerSpy.changeOwnershipForMainEntity(masterEntity, roleContextSpy);
214         roleManagerSpy.changeOwnershipForTxEntity(masterTxEntity, roleContextSpy);
215         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isMainCandidateRegistered();
216         inOrder.verify(roleContextSpy, Mockito.calls(1)).registerCandidate(Mockito.<Entity>any());
217         inOrder.verify(roleContextSpy, Mockito.atLeastOnce()).isTxCandidateRegistered();
218         inOrder.verify(roleManagerSpy, Mockito.calls(1)).makeDeviceRoleChange(Mockito.<OfpRole>any(), Mockito.<RoleContext>any(), Mockito.anyBoolean());
219     }
220
221     @Test
222     public void testAddListener() throws Exception {
223         roleManager.addRoleChangeListener((new RoleChangeListener() {
224             @Override
225             public void roleInitializationDone(final NodeId nodeId, final boolean success) {
226                 Assert.assertTrue(nodeId.equals(nodeId));
227                 Assert.assertTrue(success);
228             }
229
230             @Override
231             public void roleChangeOnDevice(final NodeId nodeId_, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
232                 Assert.assertTrue(nodeId.equals(nodeId_));
233                 Assert.assertTrue(success);
234                 Assert.assertFalse(initializationPhase);
235                 Assert.assertTrue(newRole.equals(OfpRole.BECOMEMASTER));
236             }
237         }));
238         roleManager.notifyListenersRoleInitializationDone(nodeId, true);
239         roleManager.notifyListenersRoleChangeOnDevice(nodeId, true, OfpRole.BECOMEMASTER, false);
240     }
241
242
243 }