Merge "Bug 6181 - DeviceFlowRegistry performance issue when reading from datastore"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / LifecycleConductorImplTest.java
1 /**
2  * Copyright (c) 2015 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 package org.opendaylight.openflowplugin.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.util.concurrent.ListenableFuture;
18 import io.netty.util.HashedWheelTimer;
19 import io.netty.util.TimerTask;
20 import java.math.BigInteger;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.TimeUnit;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
35 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
36 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
37 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
38 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
39 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
40 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
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 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class LifecycleConductorImplTest {
52
53     private LifecycleConductorImpl lifecycleConductor;
54
55     @Mock
56     private MessageIntelligenceAgency messageIntelligenceAgency;
57     @Mock
58     private ServiceChangeListener serviceChangeListener;
59     @Mock
60     private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners;
61     @Mock
62     private DeviceContext deviceContext;
63     @Mock
64     private DeviceManager deviceManager;
65     @Mock
66     private DeviceState deviceState;
67     @Mock
68     private ConnectionContext connectionContext;
69     @Mock
70     private FeaturesReply featuresReply;
71     @Mock
72     private TimerTask timerTask;
73     @Mock
74     private TimeUnit timeUnit;
75     @Mock
76     private HashedWheelTimer hashedWheelTimer;
77     @Mock
78     private ListenableFuture<Void> listenableFuture;
79     @Mock
80     private StatisticsManager statisticsManager;
81     @Mock
82     private RpcManager rpcManager;
83     @Mock
84     private RpcContext rpcContext;
85     @Mock
86     private DeviceInfo deviceInfo;
87
88     private NodeId nodeId = new NodeId("openflow-junit:1");
89     private OfpRole ofpRole = OfpRole.NOCHANGE;
90
91     @Before
92     public void setUp() {
93         final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
94
95         lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
96         lifecycleConductor.setSafelyManager(deviceManager);
97         lifecycleConductor.setSafelyManager(statisticsManager);
98         lifecycleConductor.setSafelyManager(rpcManager);
99
100         when(deviceManager.gainContext(Mockito.any())).thenReturn(deviceContext);
101         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
102         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
103         when(rpcManager.gainContext(Mockito.any())).thenReturn(rpcContext);
104         when(deviceInfo.getNodeId()).thenReturn(nodeId);
105         when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
106         when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
107         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
108         when(rpcManager.gainContext(Mockito.any())).thenReturn(rpcContext);
109     }
110
111     @Test
112     public void addOneTimeListenerWhenServicesChangesDoneTest() {
113         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
114         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
115     }
116
117
118     /**
119      * If serviceChangeListeners is empty NOTHING should happen
120      */
121     @Test
122     public void notifyServiceChangeListenersTest1() {
123         lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
124         when(serviceChangeListeners.size()).thenReturn(0);
125         verify(serviceChangeListeners,times(0)).remove(deviceInfo);
126     }
127
128     /**
129      * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
130      */
131     @Test
132     public void notifyServiceChangeListenersTest2() {
133         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
134         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
135         lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
136         assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
137     }
138
139
140     /**
141      * When success flag is set to FALSE nodeID connection should be closed
142      */
143     @Test
144     public void roleInitializationDoneTest1() {
145         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
146         lifecycleConductor.roleInitializationDone(deviceInfo,false);
147         verify(deviceContext,times(1)).shutdownConnection();
148     }
149
150     /**
151      * When success flag is set to TRUE LOG should be printed
152      */
153     @Test
154     public void roleInitializationDoneTest2() {
155         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
156         lifecycleConductor.roleInitializationDone(deviceInfo,true);
157         verify(deviceContext,times(0)).shutdownConnection();
158     }
159
160     /**
161      * When getDeviceContext returns null raise exception
162      */
163     @Test(expected = NullPointerException.class)
164     public void roleChangeOnDeviceTest1() {
165         when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
166         lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
167         verify(deviceContext,times(0)).shutdownConnection();
168         lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
169         verify(deviceContext,times(0)).shutdownConnection();
170     }
171
172     /**
173      * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
174      */
175     @Test
176     public void roleChangeOnDeviceTest4() {
177         final DataBroker dataBroker = mock(DataBroker.class);
178
179         when(deviceContext.getDeviceState()).thenReturn(deviceState);
180         when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker));
181         when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
182         when(deviceManager.onClusterRoleChange(deviceInfo, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
183         lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMEMASTER);
184         verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
185     }
186
187     /**
188      * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
189      */
190     @Test
191     public void roleChangeOnDeviceTest5() {
192         final DataBroker dataBroker = mock(DataBroker.class);
193
194         when(deviceContext.getDeviceState()).thenReturn(deviceState);
195         when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker));
196         when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
197         when(deviceManager.onClusterRoleChange(deviceInfo, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
198
199         lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMESLAVE);
200         verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
201     }
202
203     /**
204      * If getDeviceContext return null then null should be returned
205      */
206     @Test
207     public void gainConnectionStateSafelyTest1() {
208         when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
209         assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
210     }
211
212     /**
213      * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
214      */
215     @Test
216     public void gainConnectionStateSafelyTest2() {
217         lifecycleConductor.gainConnectionStateSafely(deviceInfo);
218         verify(deviceContext,times(1)).getPrimaryConnectionContext();
219     }
220
221     /**
222      * If getDeviceContext returns null then null should be returned
223      */
224     @Test
225     public void reserveXidForDeviceMessageTest1() {
226         when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
227         assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
228     }
229
230     /**
231      * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
232      */
233     @Test
234     public void reserveXidForDeviceMessageTest2() {
235         lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
236         verify(deviceContext,times(1)).reserveXidForDeviceMessage();
237     }
238
239     /**
240      * When succes flag is set to FALSE connection should be closed
241      */
242     @Test
243     public void deviceStartInitializationDoneTest() {
244         lifecycleConductor.deviceStartInitializationDone(deviceInfo, false);
245         verify(deviceContext,times(1)).shutdownConnection();
246     }
247
248     /**
249      * When succes flag is set to FALSE connection should be closed
250      */
251     @Test
252     public void deviceInitializationDoneTest() {
253         lifecycleConductor.deviceInitializationDone(deviceInfo, false);
254         verify(deviceContext,times(1)).shutdownConnection();
255     }
256 }