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