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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.Iterators;
14 import java.util.Iterator;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
18 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
21 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
22 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
23 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
24 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
25 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class RpcManagerImpl implements RpcManager {
31 private static final Logger LOG = LoggerFactory.getLogger(RpcManagerImpl.class);
32 private final RpcProviderRegistry rpcProviderRegistry;
33 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
34 private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
35 private final int maxRequestsQuota;
36 private final ConcurrentMap<DeviceInfo, RpcContext> contexts = new ConcurrentHashMap<>();
37 private boolean isStatisticsRpcEnabled;
39 private final LifecycleConductor conductor;
41 public RpcManagerImpl(
42 final RpcProviderRegistry rpcProviderRegistry,
44 final LifecycleConductor lifecycleConductor) {
45 this.rpcProviderRegistry = rpcProviderRegistry;
46 maxRequestsQuota = quotaValue;
47 this.conductor = lifecycleConductor;
51 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
52 deviceInitPhaseHandler = handler;
56 public void onDeviceContextLevelUp(final DeviceInfo deviceInfo) throws Exception {
58 final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(deviceInfo));
60 final RpcContext rpcContext = new RpcContextImpl(
64 deviceContext.getMessageSpy(),
66 deviceInfo.getNodeInstanceIdentifier());
68 Verify.verify(contexts.putIfAbsent(deviceInfo, rpcContext) == null, "RpcCtx still not closed for node {}", deviceInfo.getNodeId());
70 rpcContext.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
72 // finish device initialization cycle back to DeviceManager
73 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo);
78 for (final Iterator<RpcContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
79 iterator.hasNext();) {
80 iterator.next().close();
85 public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
86 final RpcContext removedContext = contexts.remove(deviceInfo);
87 if (removedContext != null) {
88 LOG.info("Unregister RPCs services for device context closure");
89 removedContext.close();
91 deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
95 public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
96 this.deviceTerminPhaseHandler = handler;
100 * This method is only for testing
103 void addRecordToContexts(DeviceInfo deviceInfo, RpcContext rpcContexts) {
104 if(!contexts.containsKey(deviceInfo)) {
105 this.contexts.put(deviceInfo,rpcContexts);
110 public <T extends OFPContext> T gainContext(DeviceInfo deviceInfo) {
111 return (T) contexts.get(deviceInfo);
116 public void setStatisticsRpcEnabled(boolean statisticsRpcEnabled) {
117 isStatisticsRpcEnabled = statisticsRpcEnabled;