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 org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
13 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
14 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
15 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
16 import org.opendaylight.openflowplugin.impl.util.MdSalRegistratorUtils;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import java.util.concurrent.ConcurrentHashMap;
24 public class RpcManagerImpl implements RpcManager {
26 private static final Logger LOG = LoggerFactory.getLogger(RpcManagerImpl.class);
27 private final RpcProviderRegistry rpcProviderRegistry;
28 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
29 private final int maxRequestsQuota;
30 private final ConcurrentHashMap<DeviceContext, RpcContext> contexts = new ConcurrentHashMap<>();
32 public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
33 final int quotaValue) {
34 this.rpcProviderRegistry = rpcProviderRegistry;
35 maxRequestsQuota = quotaValue;
39 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
40 deviceInitPhaseHandler = handler;
44 public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
45 NodeId nodeId = deviceContext.getDeviceState().getNodeId();
46 OfpRole ofpRole = deviceContext.getDeviceState().getRole();
48 LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", nodeId, ofpRole);
50 RpcContext rpcContext = contexts.get(deviceContext);
51 if (rpcContext == null) {
52 rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry, deviceContext, maxRequestsQuota);
53 contexts.put(deviceContext, rpcContext);
57 if (ofpRole == OfpRole.BECOMESLAVE) {
58 // if slave, we need to de-register rpcs if any have been registered, in case of master to slave
59 LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
61 MdSalRegistratorUtils.unregisterServices(rpcContext);
62 } catch (Exception e) {
63 LOG.error("Exception while unregistering rpcs for slave role for node:{}. But continuing.", nodeId, e);
67 LOG.info("Registering Openflow RPCs for node:{}, role:{}", nodeId, ofpRole);
68 MdSalRegistratorUtils.registerServices(rpcContext, deviceContext);
71 deviceContext.addDeviceContextClosedHandler(this);
73 // finish device initialization cycle back to DeviceManager
74 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
78 public void close() throws Exception {
84 public void onDeviceContextClosed(DeviceContext deviceContext) {
85 RpcContext removedContext = contexts.remove(deviceContext);
86 if (removedContext != null) {
88 LOG.info("Unregistering rpcs for device context closure");
89 removedContext.close();
90 } catch (Exception e) {
91 LOG.error("Exception while unregistering rpcs onDeviceContextClosed handler for node:{}. But continuing.",
92 deviceContext.getDeviceState().getNodeId(), e);