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