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;
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;
48 @RunWith(MockitoJUnitRunner.class)
49 public class RpcManagerImplTest {
51 private static final int QUOTA_VALUE = 5;
52 private RpcManagerImpl rpcManager;
55 private ProviderContext rpcProviderRegistry;
57 private DeviceContext deviceContext;
59 private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
61 private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
63 private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
65 private DeviceState deviceState;
67 private MessageSpy mockMsgSpy;
69 private LifecycleConductor conductor;
71 private ConnectionContext connectionContext;
73 private ItemLifeCycleRegistry itemLifeCycleRegistry;
75 private MessageSpy messageSpy;
77 private RpcContext removedContexts;
79 private ConcurrentMap<DeviceInfo, RpcContext> contexts;
81 private DeviceInfo deviceInfo;
84 public ExpectedException expectedException = ExpectedException.none();
86 private KeyedInstanceIdentifier<Node, NodeKey> nodePath;
88 private NodeId nodeId = new NodeId("openflow-junit:1");
92 final NodeKey nodeKey = new NodeKey(nodeId);
93 rpcManager = new RpcManagerImpl(rpcProviderRegistry, QUOTA_VALUE, conductor);
94 rpcManager.setDeviceInitializationPhaseHandler(deviceINitializationPhaseHandler);
96 GetFeaturesOutput featuresOutput = new GetFeaturesOutputBuilder()
97 .setVersion(OFConstants.OFP_VERSION_1_3)
100 FeaturesReply features = featuresOutput;
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(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
107 rpcManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
108 Mockito.when(connectionContext.getFeatures()).thenReturn(features);
109 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
110 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
111 Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
112 Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
113 Mockito.when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
114 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeKey.getId());
115 Mockito.when(rpcProviderRegistry.addRoutedRpcImplementation(
116 Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class)))
117 .thenReturn(routedRpcRegistration);
118 Mockito.when(conductor.getDeviceContext(deviceInfo)).thenReturn(deviceContext);
119 Mockito.when(contexts.remove(deviceInfo)).thenReturn(removedContexts);
123 public void onDeviceContextLevelUp() throws Exception {
124 rpcManager.onDeviceContextLevelUp(deviceInfo);
125 verify(conductor).getDeviceContext(deviceInfo);
129 public void onDeviceContextLevelUpTwice() throws Exception {
130 rpcManager.onDeviceContextLevelUp(deviceInfo);
131 expectedException.expect(VerifyException.class);
132 rpcManager.onDeviceContextLevelUp(deviceInfo);
136 public void testOnDeviceContextLevelUpMaster() throws Exception {
137 rpcManager.onDeviceContextLevelUp(deviceInfo);
138 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo);
142 public void testOnDeviceContextLevelUpSlave() throws Exception {
143 rpcManager.onDeviceContextLevelUp(deviceInfo);
144 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo);
148 public void testOnDeviceContextLevelUpOther() throws Exception {
149 rpcManager.onDeviceContextLevelUp(deviceInfo);
150 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceInfo);
154 public void testOnDeviceContextLevelDown() throws Exception {
155 rpcManager.onDeviceContextLevelDown(deviceInfo);
156 verify(deviceTerminationPhaseHandler).onDeviceContextLevelDown(deviceInfo);
160 * On non null context close and onDeviceContextLevelDown should be called
163 public void onDeviceContextLevelDown1() {
164 rpcManager.addRecordToContexts(deviceInfo,removedContexts);
165 rpcManager.onDeviceContextLevelDown(deviceInfo);
166 verify(removedContexts,times(1)).close();
167 verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceInfo);
172 * On null context only onDeviceContextLevelDown should be called
175 public void onDeviceContextLevelDown2() {
176 rpcManager.onDeviceContextLevelDown(deviceInfo);
177 verify(removedContexts,never()).close();
178 verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceInfo);
183 public void close() {
184 rpcManager.addRecordToContexts(deviceInfo,removedContexts);
186 verify(removedContexts,atLeastOnce()).close();