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.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
20 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
21 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
22 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
23 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
24 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
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<NodeId, RpcContext> contexts = new ConcurrentHashMap<>();
38 private final LifecycleConductor conductor;
40 public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
42 final LifecycleConductor lifecycleConductor) {
43 this.rpcProviderRegistry = rpcProviderRegistry;
44 maxRequestsQuota = quotaValue;
45 this.conductor = lifecycleConductor;
49 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
50 deviceInitPhaseHandler = handler;
54 public void onDeviceContextLevelUp(final DeviceInfo deviceInfo) throws Exception {
56 final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(deviceInfo.getNodeId()));
58 final RpcContext rpcContext = new RpcContextImpl(
61 deviceContext.getMessageSpy(),
63 deviceContext.getDeviceState().getNodeInstanceIdentifier());
65 deviceContext.setRpcContext(rpcContext);
67 Verify.verify(contexts.putIfAbsent(deviceInfo.getNodeId(), rpcContext) == null, "RpcCtx still not closed for node {}", deviceInfo.getNodeId());
69 // finish device initialization cycle back to DeviceManager
70 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo);
75 for (final Iterator<RpcContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
76 iterator.hasNext();) {
77 iterator.next().close();
82 public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
83 final RpcContext removedContext = contexts.remove(deviceInfo.getNodeId());
84 if (removedContext != null) {
85 LOG.info("Unregister RPCs services for device context closure");
86 removedContext.close();
88 deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
92 public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
93 this.deviceTerminPhaseHandler = handler;
97 * This method is only for testing
100 void addRecordToContexts(NodeId nodeId, RpcContext rpcContexts) {
101 if(!contexts.containsKey(nodeId)) {
102 this.contexts.put(nodeId,rpcContexts);