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.DeviceState;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
32 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
33 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
34 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
35 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
36 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
43 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.RpcService;
47 @RunWith(MockitoJUnitRunner.class)
48 public class RpcManagerImplTest {
50 private static final int QUOTA_VALUE = 5;
51 private RpcManagerImpl rpcManager;
54 private ProviderContext rpcProviderRegistry;
56 private DeviceContext deviceContext;
58 private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
60 private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
62 private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
64 private DeviceState deviceState;
66 private MessageSpy mockMsgSpy;
68 private LifecycleConductor conductor;
70 private ConnectionContext connectionContext;
72 private ItemLifeCycleRegistry itemLifeCycleRegistry;
74 private MessageSpy messageSpy;
76 private RpcContext removedContexts;
78 private ConcurrentMap<NodeId, RpcContext> contexts;
81 public ExpectedException expectedException = ExpectedException.none();
83 private KeyedInstanceIdentifier<Node, NodeKey> nodePath;
85 private NodeId nodeId = new NodeId("openflow-junit:1");
89 final NodeKey nodeKey = new NodeKey(nodeId);
90 rpcManager = new RpcManagerImpl(rpcProviderRegistry, QUOTA_VALUE, conductor);
91 rpcManager.setDeviceInitializationPhaseHandler(deviceINitializationPhaseHandler);
93 GetFeaturesOutput featuresOutput = new GetFeaturesOutputBuilder()
94 .setVersion(OFConstants.OFP_VERSION_1_3)
97 FeaturesReply features = featuresOutput;
99 Mockito.when(connectionContext.getFeatures()).thenReturn(features);
100 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
101 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
102 Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
103 Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
104 Mockito.when(deviceState.getFeatures()).thenReturn(featuresOutput);
105 rpcManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
106 Mockito.when(connectionContext.getFeatures()).thenReturn(features);
107 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
108 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
109 Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
110 Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
111 Mockito.when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
112 Mockito.when(deviceState.getNodeId()).thenReturn(nodeKey.getId());
113 Mockito.when(rpcProviderRegistry.addRoutedRpcImplementation(
114 Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class)))
115 .thenReturn(routedRpcRegistration);
116 Mockito.when(conductor.getDeviceContext(Mockito.<NodeId>any())).thenReturn(deviceContext);
117 Mockito.when(contexts.remove(nodeId)).thenReturn(removedContexts);
121 public void onDeviceContextLevelUp() throws Exception {
122 rpcManager.onDeviceContextLevelUp(nodeId);
123 verify(conductor).getDeviceContext(Mockito.<NodeId>any());
127 public void onDeviceContextLevelUpTwice() throws Exception {
128 rpcManager.onDeviceContextLevelUp(nodeId);
129 expectedException.expect(VerifyException.class);
130 rpcManager.onDeviceContextLevelUp(nodeId);
134 public void testOnDeviceContextLevelUpMaster() throws Exception {
135 rpcManager.onDeviceContextLevelUp(nodeId);
136 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
140 public void testOnDeviceContextLevelUpSlave() throws Exception {
141 rpcManager.onDeviceContextLevelUp(nodeId);
142 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
146 public void testOnDeviceContextLevelUpOther() throws Exception {
147 rpcManager.onDeviceContextLevelUp(nodeId);
148 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
152 public void testOnDeviceContextLevelDown() throws Exception {
153 rpcManager.onDeviceContextLevelDown(deviceContext);
154 verify(deviceTerminationPhaseHandler).onDeviceContextLevelDown(deviceContext);
158 * On non null context close and onDeviceContextLevelDown should be called
161 public void onDeviceContextLevelDown1() {
162 rpcManager.addRecordToContexts(nodeId,removedContexts);
163 rpcManager.onDeviceContextLevelDown(deviceContext);
164 verify(removedContexts,times(1)).close();
165 verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceContext);
170 * On null context only onDeviceContextLevelDown should be called
173 public void onDeviceContextLevelDown2() {
174 rpcManager.onDeviceContextLevelDown(deviceContext);
175 verify(removedContexts,never()).close();
176 verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceContext);
181 public void close() {
182 rpcManager.addRecordToContexts(nodeId,removedContexts);
184 verify(removedContexts,atLeastOnce()).close();