fe01124d6c94ab26c7fd4ccd7e398da7e40eccb3
[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.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(deviceState.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(Mockito.<NodeId>any())).thenReturn(deviceContext);
120         Mockito.when(contexts.remove(nodeId)).thenReturn(removedContexts);
121     }
122
123     @Test
124     public void onDeviceContextLevelUp() throws Exception {
125         rpcManager.onDeviceContextLevelUp(nodeId);
126         verify(conductor).getDeviceContext(Mockito.<NodeId>any());
127     }
128
129     @Test
130     public void onDeviceContextLevelUpTwice() throws Exception {
131         rpcManager.onDeviceContextLevelUp(nodeId);
132         expectedException.expect(VerifyException.class);
133         rpcManager.onDeviceContextLevelUp(nodeId);
134     }
135
136     @Test
137     public void testOnDeviceContextLevelUpMaster() throws Exception {
138         rpcManager.onDeviceContextLevelUp(nodeId);
139         verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
140     }
141
142     @Test
143     public void testOnDeviceContextLevelUpSlave() throws Exception {
144         rpcManager.onDeviceContextLevelUp(nodeId);
145         verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
146     }
147
148     @Test
149     public void testOnDeviceContextLevelUpOther() throws Exception {
150         rpcManager.onDeviceContextLevelUp(nodeId);
151         verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
152     }
153
154     @Test
155     public void testOnDeviceContextLevelDown() throws Exception {
156         rpcManager.onDeviceContextLevelDown(deviceContext);
157         verify(deviceTerminationPhaseHandler).onDeviceContextLevelDown(deviceContext);
158     }
159
160     /**
161      * On non null context close and onDeviceContextLevelDown should be called
162      */
163     @Test
164     public void onDeviceContextLevelDown1() {
165         rpcManager.addRecordToContexts(nodeId,removedContexts);
166         rpcManager.onDeviceContextLevelDown(deviceContext);
167         verify(removedContexts,times(1)).close();
168         verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceContext);
169     }
170
171
172     /**
173      * On null context only onDeviceContextLevelDown should be called
174      */
175     @Test
176     public void onDeviceContextLevelDown2() {
177         rpcManager.onDeviceContextLevelDown(deviceContext);
178         verify(removedContexts,never()).close();
179         verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceContext);
180
181     }
182
183     @Test
184     public void close() {
185         rpcManager.addRecordToContexts(nodeId,removedContexts);
186         rpcManager.close();
187         verify(removedContexts,atLeastOnce()).close();
188     }
189 }