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 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;
42 import java.util.concurrent.ConcurrentMap;
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;
50 @RunWith(MockitoJUnitRunner.class)
51 public class RpcManagerImplTest {
53 private static final int QUOTA_VALUE = 5;
54 private RpcManagerImpl rpcManager;
57 private ProviderContext rpcProviderRegistry;
59 private DeviceContext deviceContext;
61 private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
63 private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
65 private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
67 private DeviceState deviceState;
69 private MessageSpy mockMsgSpy;
71 private LifecycleConductor conductor;
73 private ConnectionContext connectionContext;
75 private ItemLifeCycleRegistry itemLifeCycleRegistry;
77 private MessageSpy messageSpy;
79 private RpcContext removedContexts;
81 private ConcurrentMap<NodeId, RpcContext> contexts;
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.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);
125 public void onDeviceContextLevelUp() throws Exception {
126 rpcManager.onDeviceContextLevelUp(nodeId);
127 verify(conductor).getDeviceContext(Mockito.<NodeId>any());
131 public void onDeviceContextLevelUpTwice() throws Exception {
132 rpcManager.onDeviceContextLevelUp(nodeId);
133 expectedException.expect(VerifyException.class);
134 rpcManager.onDeviceContextLevelUp(nodeId);
138 public void testOnDeviceContextLevelUpMaster() throws Exception {
139 Mockito.when(deviceState.getRole()).thenReturn(OfpRole.BECOMEMASTER);
140 rpcManager.onDeviceContextLevelUp(nodeId);
141 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
145 public void testOnDeviceContextLevelUpSlave() throws Exception {
146 Mockito.when(deviceState.getRole()).thenReturn(OfpRole.BECOMESLAVE);
147 rpcManager.onDeviceContextLevelUp(nodeId);
148 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
152 public void testOnDeviceContextLevelUpOther() throws Exception {
153 Mockito.when(deviceState.getRole()).thenReturn(OfpRole.NOCHANGE);
154 rpcManager.onDeviceContextLevelUp(nodeId);
155 verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(nodeId);
159 public void testOnDeviceContextLevelDown() throws Exception {
160 Mockito.when(deviceState.getRole()).thenReturn(OfpRole.NOCHANGE);
161 rpcManager.onDeviceContextLevelDown(deviceContext);
162 verify(deviceTerminationPhaseHandler).onDeviceContextLevelDown(deviceContext);
166 * On non null context close and onDeviceContextLevelDown should be called
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);
178 * On null context only onDeviceContextLevelDown should be called
181 public void onDeviceContextLevelDown2() {
182 rpcManager.onDeviceContextLevelDown(deviceContext);
183 verify(removedContexts,never()).close();
184 verify(deviceTerminationPhaseHandler,times(1)).onDeviceContextLevelDown(deviceContext);
189 public void close() {
190 rpcManager.addRecordToContexts(nodeId,removedContexts);
192 verify(removedContexts,atLeastOnce()).close();