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