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;
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.rpc.RpcContext;
25 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
26 import org.opendaylight.openflowplugin.impl.rpc.RpcContextImpl;
27 import org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl;
28 import org.opendaylight.openflowplugin.impl.services.SalFlowServiceImpl;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.RpcService;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
43 public class RpcManagerImplTest {
45 private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 12;
47 private static final int AWAITED_NUM_OF_CALL_ADD_NEW_REQUEST = 1;
49 final ProviderContext mockedProviderContext = mock(ProviderContext.class);
50 final RpcManagerImpl rpcManager = new RpcManagerImpl(mockedProviderContext, 500l);
51 final RpcContext mockedRpcContext = mock(RpcContext.class);
52 final AddFlowInput mockedFlowInput = prepareTestingAddFlow();
53 final DeviceContext mockedDeviceContext = mock(DeviceContext.class);
55 private MessageSpy messageSpy;
59 public void deviceConnectedTest() {
61 rpcManager.onDeviceContextLevelUp(mockedDeviceContext);
63 verify(mockedProviderContext, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
64 Matchers.any(Class.class), Matchers.any(RpcService.class));
69 * Tests behavior of RpcContextImpl when calling rpc from MD-SAL
73 public void invokeRpcTestExistsCapacityTest() throws InterruptedException, ExecutionException {
74 final ConnectionContext mockedConnectionContext = mock(ConnectionContext.class);
75 final FeaturesReply mockedFeatures = mock(FeaturesReply.class);
76 final BigInteger dummyDatapathId = BigInteger.ONE;
77 final Short dummyVersion = 1;
78 final ConnectionAdapter mockedConnectionAdapter = mock(ConnectionAdapter.class);
80 when(mockedFeatures.getDatapathId()).thenReturn(dummyDatapathId);
81 when(mockedFeatures.getVersion()).thenReturn(dummyVersion);
82 when(mockedConnectionContext.getFeatures()).thenReturn(mockedFeatures);
83 when(mockedConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
84 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedConnectionContext);
85 final Xid mockedXid = mock(Xid.class);
86 final Long dummyXid = 1l;
87 when(mockedXid.getValue()).thenReturn(dummyXid);
88 when(mockedDeviceContext.getReservedXid()).thenReturn(dummyXid);
90 invokeRpcTestExistsCapacity(10, true);
91 invokeRpcTestExistsCapacity(0, false);
94 private void invokeRpcTestExistsCapacity(final int capacity, final boolean result) throws InterruptedException,
96 // TODO: how to invoke service remotely?
97 NodeId nodeId = new NodeId("openflow:1");
98 KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
99 final RpcContextImpl rpcContext = new RpcContextImpl(messageSpy, mockedProviderContext, nodeInstanceIdentifier);
100 when(mockedProviderContext.getRpcService(SalFlowService.class)).thenReturn(new SalFlowServiceImpl(rpcContext, mockedDeviceContext));
101 rpcContext.setRequestContextQuota(capacity);
103 final SalFlowService salFlowService = mockedProviderContext.getRpcService(SalFlowService.class);
104 final Future<RpcResult<AddFlowOutput>> addedFlow = salFlowService.addFlow(prepareTestingAddFlow());
110 private static AddFlowInput prepareTestingAddFlow() {
111 final AddFlowInputBuilder builder = new AddFlowInputBuilder();
112 builder.setFlowName("dummy flow");
113 builder.setHardTimeout(10000);
115 return builder.build();