3fea20faaf7bb15825b23829c8928cd0e7722bdc
[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.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;
42
43 public class RpcManagerImplTest {
44
45     private static final int AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC = 12;
46
47     private static final int AWAITED_NUM_OF_CALL_ADD_NEW_REQUEST = 1;
48
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);
54     @Mock
55     private MessageSpy messageSpy;
56
57     @Ignore
58     @Test
59     public void deviceConnectedTest() {
60
61         rpcManager.onDeviceContextLevelUp(mockedDeviceContext);
62
63         verify(mockedProviderContext, times(AWAITED_NUM_OF_CALL_ADD_ROUTED_RPC)).addRoutedRpcImplementation(
64                 Matchers.any(Class.class), Matchers.any(RpcService.class));
65     }
66
67
68     /**
69      * Tests behavior of RpcContextImpl when calling rpc from MD-SAL
70      */
71     @Ignore
72     @Test
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);
79
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);
89
90         invokeRpcTestExistsCapacity(10, true);
91         invokeRpcTestExistsCapacity(0, false);
92     }
93
94     private void invokeRpcTestExistsCapacity(final int capacity, final boolean result) throws InterruptedException,
95             ExecutionException {
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, capacity);
100         when(mockedProviderContext.getRpcService(SalFlowService.class)).thenReturn(new SalFlowServiceImpl(rpcContext, mockedDeviceContext));
101
102         final SalFlowService salFlowService = mockedProviderContext.getRpcService(SalFlowService.class);
103         final Future<RpcResult<AddFlowOutput>> addedFlow = salFlowService.addFlow(prepareTestingAddFlow());
104     }
105
106     /**
107      * @return
108      */
109     private static AddFlowInput prepareTestingAddFlow() {
110         final AddFlowInputBuilder builder = new AddFlowInputBuilder();
111         builder.setFlowName("dummy flow");
112         builder.setHardTimeout(10000);
113
114         return builder.build();
115     }
116 }