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.api.openflow.device;
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;
15 import java.math.BigInteger;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.mockito.Matchers;
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.rpc.RpcContext;
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.openflow.protocol.rev130731.FeaturesReply;
33 import org.opendaylight.yangtools.yang.binding.RpcService;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
36 public class RpcManagerImplTest {
38 private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 12;
40 private static final int AWAITED_NUM_OF_CALL_ADD_NEW_REQUEST = 1;
42 final ProviderContext mockedProviderContext = mock(ProviderContext.class);
43 final RpcManagerImpl rpcManager = new RpcManagerImpl(mockedProviderContext);
44 final DeviceContext mockedRequestContext = mock(DeviceContext.class);
48 public void deviceConnectedTest() {
50 rpcManager.deviceConnected(mockedRequestContext);
52 verify(mockedProviderContext, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
53 Matchers.any(Class.class), Matchers.any(RpcService.class));
56 final RpcContext mockedRpcContext = mock(RpcContext.class);
57 final AddFlowInput mockedFlowInput = prepareTestingAddFlow();
58 final DeviceContext mockedDeviceContext = mock(DeviceContext.class);
61 * Tests behavior of RpcContextImpl when calling rpc from MD-SAL
65 public void invokeRpcTestExistsCapacityTest() throws InterruptedException, ExecutionException {
66 final ConnectionContext mockedConnectionContext = mock(ConnectionContext.class);
67 final FeaturesReply mockedFeatures = mock(FeaturesReply.class);
68 final BigInteger dummyDatapathId = new BigInteger("1");
69 final Short dummyVersion = 1;
70 final ConnectionAdapter mockedConnectionAdapter = mock(ConnectionAdapter.class);
72 when(mockedFeatures.getDatapathId()).thenReturn(dummyDatapathId);
73 when(mockedFeatures.getVersion()).thenReturn(dummyVersion);
74 when(mockedConnectionContext.getFeatures()).thenReturn(mockedFeatures);
75 when(mockedConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
76 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedConnectionContext);
77 final Xid mockedXid = mock(Xid.class);
78 final Long dummyXid = 1l;
79 when(mockedXid.getValue()).thenReturn(dummyXid);
80 when(mockedDeviceContext.getNextXid()).thenReturn(mockedXid);
82 invokeRpcTestExistsCapacity(10, true);
83 invokeRpcTestExistsCapacity(0, false);
86 private void invokeRpcTestExistsCapacity(final int capacity, final boolean result) throws InterruptedException,
88 // TODO: how to invoke service remotely?
89 final RpcContextImpl rpcContext = new RpcContextImpl(mockedProviderContext, mockedDeviceContext);
90 when(mockedProviderContext.getRpcService(SalFlowService.class)).thenReturn(new SalFlowServiceImpl(rpcContext));
91 rpcContext.setRequestContextQuota(capacity);
93 final SalFlowService salFlowService = mockedProviderContext.getRpcService(SalFlowService.class);
94 final Future<RpcResult<AddFlowOutput>> addedFlow = salFlowService.addFlow(prepareTestingAddFlow());
100 private AddFlowInput prepareTestingAddFlow() {
101 final AddFlowInputBuilder builder = new AddFlowInputBuilder();
102 builder.setFlowName("dummy flow");
103 builder.setHardTimeout(10000);
105 return builder.build();