Merge "Clean wiring inital phase for new Device"
[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.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
24 import org.opendaylight.openflowplugin.impl.rpc.RpcContextImpl;
25 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
26 import org.opendaylight.openflowplugin.impl.services.SalFlowServiceImpl;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
32 import org.opendaylight.yangtools.yang.binding.RpcService;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34
35 public class RpcManagerImplTest {
36
37     private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 12;
38
39     private static final int AWAITED_NUM_OF_CALL_ADD_NEW_REQUEST = 1;
40
41     final ProviderContext mockedProviderContext = mock(ProviderContext.class);
42     final RpcManagerImpl rpcManager = new RpcManagerImpl(mockedProviderContext);
43     final RpcContext mockedRpcContext = mock(RpcContext.class);
44     final AddFlowInput mockedFlowInput = prepareTestingAddFlow();
45     final DeviceContext mockedDeviceContext = mock(DeviceContext.class);
46
47     @Ignore
48     @Test
49     public void deviceConnectedTest() {
50
51         rpcManager.onDeviceContextLevelUp(mockedDeviceContext);
52
53         verify(mockedProviderContext, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
54                 Matchers.any(Class.class), Matchers.any(RpcService.class));
55     }
56
57
58     /**
59      * Tests behavior of RpcContextImpl when calling rpc from MD-SAL
60      */
61     @Ignore
62     @Test
63     public void invokeRpcTestExistsCapacityTest() throws InterruptedException, ExecutionException {
64         final ConnectionContext mockedConnectionContext = mock(ConnectionContext.class);
65         final FeaturesReply mockedFeatures = mock(FeaturesReply.class);
66         final BigInteger dummyDatapathId = new BigInteger("1");
67         final Short dummyVersion = 1;
68         final ConnectionAdapter mockedConnectionAdapter = mock(ConnectionAdapter.class);
69
70         when(mockedFeatures.getDatapathId()).thenReturn(dummyDatapathId);
71         when(mockedFeatures.getVersion()).thenReturn(dummyVersion);
72         when(mockedConnectionContext.getFeatures()).thenReturn(mockedFeatures);
73         when(mockedConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
74         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedConnectionContext);
75         final Xid mockedXid = mock(Xid.class);
76         final Long dummyXid = 1l;
77         when(mockedXid.getValue()).thenReturn(dummyXid);
78         when(mockedDeviceContext.getNextXid()).thenReturn(mockedXid);
79
80         invokeRpcTestExistsCapacity(10, true);
81         invokeRpcTestExistsCapacity(0, false);
82     }
83
84     private void invokeRpcTestExistsCapacity(final int capacity, final boolean result) throws InterruptedException,
85             ExecutionException {
86         // TODO: how to invoke service remotely?
87         final RpcContextImpl rpcContext = new RpcContextImpl(mockedProviderContext, mockedDeviceContext);
88         when(mockedProviderContext.getRpcService(SalFlowService.class)).thenReturn(new SalFlowServiceImpl(rpcContext, mockedDeviceContext));
89         rpcContext.setRequestContextQuota(capacity);
90
91         final SalFlowService salFlowService = mockedProviderContext.getRpcService(SalFlowService.class);
92         final Future<RpcResult<AddFlowOutput>> addedFlow = salFlowService.addFlow(prepareTestingAddFlow());
93     }
94
95     /**
96      * @return
97      */
98     private AddFlowInput prepareTestingAddFlow() {
99         final AddFlowInputBuilder builder = new AddFlowInputBuilder();
100         builder.setFlowName("dummy flow");
101         builder.setHardTimeout(10000);
102
103         return builder.build();
104     }
105 }