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