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.impl.rpc;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.atLeastOnce;
12 import static org.mockito.Mockito.verify;
14 import java.util.concurrent.ConcurrentMap;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Matchers;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
30 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
31 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
33 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
34 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint16Type;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.RpcService;
46 @RunWith(MockitoJUnitRunner.class)
47 public class RpcManagerImplTest {
49 private static final int QUOTA_VALUE = 5;
50 private RpcManagerImpl rpcManager;
53 private ProviderContext rpcProviderRegistry;
55 private DeviceContext deviceContext;
57 private BindingAwareBroker.RoutedRpcRegistration<RpcService> routedRpcRegistration;
59 private DeviceState deviceState;
61 private ConnectionContext connectionContext;
63 private ItemLifeCycleRegistry itemLifeCycleRegistry;
65 private MessageSpy messageSpy;
67 private RpcContext removedContexts;
69 private ConcurrentMap<DeviceInfo, RpcContext> contexts;
71 private DeviceInfo deviceInfo;
73 private ExtensionConverterProvider extensionConverterProvider;
75 private ConvertorExecutor convertorExecutor;
77 private NotificationPublishService notificationPublishService;
79 private KeyedInstanceIdentifier<Node, NodeKey> nodePath;
81 private NodeId nodeId = new NodeId("openflow-junit:1");
85 final NodeKey nodeKey = new NodeKey(nodeId);
86 rpcManager = new RpcManagerImpl(new OpenflowProviderConfigBuilder()
87 .setRpcRequestsQuota(new NonZeroUint16Type(QUOTA_VALUE))
88 .setIsStatisticsRpcEnabled(false)
90 rpcProviderRegistry, extensionConverterProvider, convertorExecutor, notificationPublishService);
92 FeaturesReply features = new GetFeaturesOutputBuilder()
93 .setVersion(OFConstants.OFP_VERSION_1_3)
96 Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeKey.getId());
97 Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
98 Mockito.when(connectionContext.getFeatures()).thenReturn(features);
99 Mockito.when(deviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
100 Mockito.when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
101 Mockito.when(deviceContext.getDeviceState()).thenReturn(deviceState);
102 Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
103 Mockito.when(deviceContext.getMessageSpy()).thenReturn(messageSpy);
104 Mockito.when(rpcProviderRegistry.addRoutedRpcImplementation(
105 Matchers.any(), Matchers.any(RpcService.class)))
106 .thenReturn(routedRpcRegistration);
107 Mockito.when(contexts.remove(deviceInfo)).thenReturn(removedContexts);
111 public void createContext() throws Exception {
112 final RpcContext context = rpcManager.createContext(deviceContext);
113 assertEquals(deviceInfo, context.getDeviceInfo());
117 public void close() {
118 rpcManager.addRecordToContexts(deviceInfo,removedContexts);
120 verify(removedContexts,atLeastOnce()).close();