2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.rpc;
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;
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;
49 @RunWith(MockitoJUnitRunner.class)
50 public class RpcManagerImplTest {
52 private static final int QUOTA_VALUE = 5;
53 private RpcManagerImpl rpcManager;
56 private ProviderContext rpcProviderRegistry;
58 private DeviceContext deviceContext;
60 private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
62 private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
64 private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
66 private DeviceState deviceState;
68 private MessageSpy mockMsgSpy;
70 private LifecycleConductor conductor;
72 private ConnectionContext connectionContext;
74 private ItemLifeCycleRegistry itemLifeCycleRegistry;
76 private MessageSpy messageSpy;
78 private RpcContext removedContexts;
80 private ConcurrentMap<DeviceInfo, RpcContext> contexts;
82 private DeviceInfo deviceInfo;
85 public ExpectedException expectedException = ExpectedException.none();
87 private KeyedInstanceIdentifier<Node, NodeKey> nodePath;
89 private NodeId nodeId = new NodeId("openflow-junit:1");
93 final NodeKey nodeKey = new NodeKey(nodeId);
94 rpcManager = new RpcManagerImpl(rpcProviderRegistry, QUOTA_VALUE, conductor);
95 rpcManager.setDeviceInitializationPhaseHandler(deviceINitializationPhaseHandler);
97 GetFeaturesOutput featuresOutput = new GetFeaturesOutputBuilder()
98 .setVersion(OFConstants.OFP_VERSION_1_3)
101 FeaturesReply features = featuresOutput;
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);
124 public void onDeviceContextLevelUp() throws Exception {
125 rpcManager.onDeviceContextLevelUp(deviceInfo);
126 verify(conductor).getDeviceContext(deviceInfo);
130 public void onDeviceContextLevelUpTwice() throws Exception {
131 rpcManager.onDeviceContextLevelUp(deviceInfo);
132 expectedException.expect(VerifyException.class);
133 rpcManager.onDeviceContextLevelUp(deviceInfo);
137 public void testOnDeviceContextLevelUpMaster() throws Exception {
138 rpcManager.onDeviceContextLevelUp(deviceInfo);
139 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo);
143 public void testOnDeviceContextLevelUpSlave() throws Exception {
144 rpcManager.onDeviceContextLevelUp(deviceInfo);
145 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo);
149 public void testOnDeviceContextLevelUpOther() throws Exception {
150 rpcManager.onDeviceContextLevelUp(deviceInfo);
151 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo);
155 public void testOnDeviceContextLevelDown() throws Exception {
156 rpcManager.onDeviceContextLevelDown(deviceInfo);
157 verify(deviceTerminationPhaseHandler).onDeviceContextLevelDown(deviceInfo);
161 * On non null context close and onDeviceContextLevelDown should be called
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);
173 * On null context only onDeviceContextLevelDown should be called
176 public void onDeviceContextLevelDown2() {
177 rpcManager.onDeviceContextLevelDown(deviceInfo);
178 verify(removedContexts,never()).close();
179 verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceInfo);
184 public void close() {
185 rpcManager.addRecordToContexts(deviceInfo,removedContexts);
187 verify(removedContexts,atLeastOnce()).close();