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.times;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Matchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
26 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.RpcService;
38 @RunWith(MockitoJUnitRunner.class)
39 public class RpcManagerImplTest {
41 private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 11;
43 private RpcManagerImpl rpcManager;
45 private ProviderContext rpcProviderRegistry;
47 private DeviceContext deviceContext;
49 private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
51 private ConnectionContext connectionContext;
53 private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
55 private DeviceState deviceState;
57 private ItemLifeCycleRegistry itemLifeCycleRegistry;
59 private KeyedInstanceIdentifier<Node, NodeKey> nodePath;
63 nodePath = KeyedInstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow-junit:1")));
64 rpcManager = new RpcManagerImpl(rpcProviderRegistry, 5);
65 rpcManager.setDeviceInitializationPhaseHandler(deviceINitializationPhaseHandler);
66 FeaturesReply features = new GetFeaturesOutputBuilder()
67 .setVersion(OFConstants.OFP_VERSION_1_3)
69 Mockito.when(connectionContext.getFeatures()).thenReturn(features);
70 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
71 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
72 Mockito.when(deviceContext.getDeviceState().getRole()).thenReturn(OfpRole.BECOMEMASTER);
73 Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
74 Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
78 public void testOnDeviceContextLevelUp() throws Exception {
80 Mockito.when(rpcProviderRegistry.addRoutedRpcImplementation(
81 Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class)))
82 .thenReturn(routedRpcRegistration);
84 rpcManager.onDeviceContextLevelUp(deviceContext);
86 Mockito.verify(rpcProviderRegistry, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
87 Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class));
88 Mockito.verify(routedRpcRegistration, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).registerPath(
89 NodeContext.class, nodePath);
90 Mockito.verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceContext);