BUG-4084: Li:Save flows in operational based on barrier success
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / rpc / RpcManagerImplTest.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.rpc;
9
10 import static org.mockito.Mockito.times;
11
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.yangtools.yang.binding.KeyedInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.RpcService;
36
37 @RunWith(MockitoJUnitRunner.class)
38 public class RpcManagerImplTest {
39
40     private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 9;
41
42     private RpcManagerImpl rpcManager;
43     @Mock
44     private ProviderContext rpcProviderRegistry;
45     @Mock
46     private DeviceContext deviceContext;
47     @Mock
48     private DeviceInitializationPhaseHandler deviceINitializationPhaseHandler;
49     @Mock
50     private ConnectionContext connectionContext;
51     @Mock
52     private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
53     @Mock
54     private DeviceState deviceState;
55     @Mock
56     private ItemLifeCycleRegistry itemLifeCycleRegistry;
57
58     private KeyedInstanceIdentifier<Node, NodeKey> nodePath;
59
60     @Before
61     public void setUp() {
62         nodePath = KeyedInstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow-junit:1")));
63         rpcManager = new RpcManagerImpl(rpcProviderRegistry, 5);
64         rpcManager.setDeviceInitializationPhaseHandler(deviceINitializationPhaseHandler);
65         FeaturesReply features = new GetFeaturesOutputBuilder()
66                 .setVersion(OFConstants.OFP_VERSION_1_3)
67                 .build();
68         Mockito.when(connectionContext.getFeatures()).thenReturn(features);
69         Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
70         Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
71         Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
72         Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodePath);
73     }
74
75     @Test
76     public void testOnDeviceContextLevelUp() {
77
78         Mockito.when(rpcProviderRegistry.addRoutedRpcImplementation(
79                 Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class)))
80                 .thenReturn(routedRpcRegistration);
81
82         rpcManager.onDeviceContextLevelUp(deviceContext);
83
84         Mockito.verify(rpcProviderRegistry, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
85                 Matchers.<Class<RpcService>>any(), Matchers.any(RpcService.class));
86         Mockito.verify(routedRpcRegistration, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).registerPath(
87                 NodeContext.class, nodePath);
88         Mockito.verify(deviceINitializationPhaseHandler).onDeviceContextLevelUp(deviceContext);
89     }
90 }