Fix unused import warnings
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / rpc / RpcManagerImplTest.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.rpc;
9
10 import static org.mockito.Mockito.atLeastOnce;
11 import static org.mockito.Mockito.never;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14 import com.google.common.base.VerifyException;
15 import java.util.concurrent.ConcurrentMap;
16 import org.junit.Before;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.ExpectedException;
20 import org.junit.runner.RunWith;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
32 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
33 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
34 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
35 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
36 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
43 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.RpcService;
45
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class RpcManagerImplTest {
49
50     private static final int QUOTA_VALUE = 5;
51     private RpcManagerImpl rpcManager;
52
53     @Mock
54     private ProviderContext rpcProviderRegistry;
55     @Mock
56     private DeviceContext deviceContext;
57     @Mock
58     private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
59     @Mock
60     private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
61     @Mock
62     private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
63     @Mock
64     private DeviceState deviceState;
65     @Mock
66     private MessageSpy mockMsgSpy;
67     @Mock
68     private LifecycleConductor conductor;
69     @Mock
70     private ConnectionContext connectionContext;
71     @Mock
72     private ItemLifeCycleRegistry itemLifeCycleRegistry;
73     @Mock
74     private MessageSpy messageSpy;
75     @Mock
76     private RpcContext removedContexts;
77     @Mock
78     private ConcurrentMap<NodeId, RpcContext> contexts;
79
80     @Rule
81     public ExpectedException expectedException = ExpectedException.none();
82
83     private KeyedInstanceIdentifier<Node, NodeKey> nodePath;
84
85     private NodeId nodeId = new NodeId("openflow-junit:1");
86
87     @Before
88     public void setUp() {
89         final NodeKey nodeKey = new NodeKey(nodeId);
90         rpcManager = new RpcManagerImpl(rpcProviderRegistry, QUOTA_VALUE, conductor);
91         rpcManager.setDeviceInitializationPhaseHandler(deviceINitializationPhaseHandler);
92
93         GetFeaturesOutput featuresOutput = new GetFeaturesOutputBuilder()
94                 .setVersion(OFConstants.OFP_VERSION_1_3)
95                 .build();
96
97         FeaturesReply features = featuresOutput;
98
99         Mockito.when(connectionContext.getFeatures()).thenReturn(features);
100         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
101         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
102         Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
103         Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
104         Mockito.when(deviceState.getFeatures()).thenReturn(featuresOutput);
105         rpcManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
106         Mockito.when(connectionContext.getFeatures()).thenReturn(features);
107         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
108         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
109         Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
110         Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
111         Mockito.when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
112         Mockito.when(deviceState.getNodeId()).thenReturn(nodeKey.getId());
113         Mockito.when(rpcProviderRegistry.addRoutedRpcImplementation(
114                 Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class)))
115                 .thenReturn(routedRpcRegistration);
116         Mockito.when(conductor.getDeviceContext(Mockito.<NodeId>any())).thenReturn(deviceContext);
117         Mockito.when(contexts.remove(nodeId)).thenReturn(removedContexts);
118     }
119
120     @Test
121     public void onDeviceContextLevelUp() throws Exception {
122         rpcManager.onDeviceContextLevelUp(nodeId);
123         verify(conductor).getDeviceContext(Mockito.<NodeId>any());
124     }
125
126     @Test
127     public void onDeviceContextLevelUpTwice() throws Exception {
128         rpcManager.onDeviceContextLevelUp(nodeId);
129         expectedException.expect(VerifyException.class);
130         rpcManager.onDeviceContextLevelUp(nodeId);
131     }
132
133     @Test
134     public void testOnDeviceContextLevelUpMaster() throws Exception {
135         rpcManager.onDeviceContextLevelUp(nodeId);
136         verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
137     }
138
139     @Test
140     public void testOnDeviceContextLevelUpSlave() throws Exception {
141         rpcManager.onDeviceContextLevelUp(nodeId);
142         verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
143     }
144
145     @Test
146     public void testOnDeviceContextLevelUpOther() throws Exception {
147         rpcManager.onDeviceContextLevelUp(nodeId);
148         verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
149     }
150
151     @Test
152     public void testOnDeviceContextLevelDown() throws Exception {
153         rpcManager.onDeviceContextLevelDown(deviceContext);
154         verify(deviceTerminationPhaseHandler).onDeviceContextLevelDown(deviceContext);
155     }
156
157     /**
158      * On non null context close and onDeviceContextLevelDown should be called
159      */
160     @Test
161     public void onDeviceContextLevelDown1() {
162         rpcManager.addRecordToContexts(nodeId,removedContexts);
163         rpcManager.onDeviceContextLevelDown(deviceContext);
164         verify(removedContexts,times(1)).close();
165         verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceContext);
166     }
167
168
169     /**
170      * On null context only onDeviceContextLevelDown should be called
171      */
172     @Test
173     public void onDeviceContextLevelDown2() {
174         rpcManager.onDeviceContextLevelDown(deviceContext);
175         verify(removedContexts,never()).close();
176         verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceContext);
177
178     }
179
180     @Test
181     public void close() {
182         rpcManager.addRecordToContexts(nodeId,removedContexts);
183         rpcManager.close();
184         verify(removedContexts,atLeastOnce()).close();
185     }
186 }