BUG-3363: code optimization and cleanup - Fix eclipse warnings
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / api / openflow / device / 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.api.openflow.device;
9
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.times;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14 import java.math.BigInteger;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
23 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
24 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
25 import org.opendaylight.openflowplugin.impl.rpc.RpcContextImpl;
26 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
27 import org.opendaylight.openflowplugin.impl.services.SalFlowServiceImpl;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.RpcService;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41
42 public class RpcManagerImplTest {
43
44     private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 12;
45
46
47     final ProviderContext mockedProviderContext = mock(ProviderContext.class);
48     final RpcManagerImpl rpcManager = new RpcManagerImpl(mockedProviderContext, 500);
49     final DeviceContext mockedDeviceContext = mock(DeviceContext.class);
50     @Mock
51     private MessageSpy messageSpy;
52
53     @Ignore
54     @Test
55     public void deviceConnectedTest() {
56
57         rpcManager.onDeviceContextLevelUp(mockedDeviceContext);
58
59         verify(mockedProviderContext, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
60                 Matchers.any(Class.class), Matchers.any(RpcService.class));
61     }
62
63
64     /**
65      * Tests behavior of RpcContextImpl when calling rpc from MD-SAL
66      */
67     @Ignore
68     @Test
69     public void invokeRpcTestExistsCapacityTest() throws InterruptedException, ExecutionException {
70         final ConnectionContext mockedConnectionContext = mock(ConnectionContext.class);
71         final FeaturesReply mockedFeatures = mock(FeaturesReply.class);
72         final BigInteger dummyDatapathId = BigInteger.ONE;
73         final Short dummyVersion = 1;
74         final ConnectionAdapter mockedConnectionAdapter = mock(ConnectionAdapter.class);
75
76         when(mockedFeatures.getDatapathId()).thenReturn(dummyDatapathId);
77         when(mockedFeatures.getVersion()).thenReturn(dummyVersion);
78         when(mockedConnectionContext.getFeatures()).thenReturn(mockedFeatures);
79         when(mockedConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
80         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedConnectionContext);
81         final Xid mockedXid = mock(Xid.class);
82         final Long dummyXid = 1l;
83         when(mockedXid.getValue()).thenReturn(dummyXid);
84         when(mockedDeviceContext.getReservedXid()).thenReturn(dummyXid);
85
86         invokeRpcTestExistsCapacity(10, true);
87         invokeRpcTestExistsCapacity(0, false);
88     }
89
90     private void invokeRpcTestExistsCapacity(final int capacity, final boolean result) throws InterruptedException,
91             ExecutionException {
92         // TODO: how to invoke service remotely?
93         NodeId nodeId = new NodeId("openflow:1");
94         KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
95         final RpcContextImpl rpcContext = new RpcContextImpl(messageSpy, mockedProviderContext, mockedDeviceContext, capacity);
96         when(mockedProviderContext.getRpcService(SalFlowService.class)).thenReturn(new SalFlowServiceImpl(rpcContext, mockedDeviceContext));
97
98         final SalFlowService salFlowService = mockedProviderContext.getRpcService(SalFlowService.class);
99         final Future<RpcResult<AddFlowOutput>> addedFlow = salFlowService.addFlow(prepareTestingAddFlow());
100     }
101
102     /**
103      * @return
104      */
105     private static AddFlowInput prepareTestingAddFlow() {
106         final AddFlowInputBuilder builder = new AddFlowInputBuilder();
107         builder.setFlowName("dummy flow");
108         builder.setHardTimeout(10000);
109
110         return builder.build();
111     }
112 }