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 java.util.concurrent.atomic.AtomicLong;
11 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
12 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
15 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
16 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
18 import org.opendaylight.openflowplugin.impl.util.MdSalRegistratorUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import java.util.concurrent.ConcurrentHashMap;
26 public class RpcManagerImpl implements RpcManager {
28 private static final Logger LOG = LoggerFactory.getLogger(RpcManagerImpl.class);
29 private final RpcProviderRegistry rpcProviderRegistry;
30 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
31 private final int maxRequestsQuota;
32 private final ConcurrentHashMap<DeviceContext, RpcContext> contexts = new ConcurrentHashMap<>();
33 private boolean isStatisticsRpcEnabled;
34 private NotificationPublishService notificationPublishService;
36 public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
37 final int quotaValue) {
38 this.rpcProviderRegistry = rpcProviderRegistry;
39 maxRequestsQuota = quotaValue;
43 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
44 deviceInitPhaseHandler = handler;
48 public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
49 NodeId nodeId = deviceContext.getDeviceState().getNodeId();
50 OfpRole ofpRole = deviceContext.getDeviceState().getRole();
52 LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", nodeId, ofpRole);
54 RpcContext rpcContext = contexts.get(deviceContext);
55 if (rpcContext == null) {
56 rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry, deviceContext, maxRequestsQuota);
57 contexts.put(deviceContext, rpcContext);
61 if (ofpRole == OfpRole.BECOMESLAVE) {
62 // if slave, we need to de-register rpcs if any have been registered, in case of master to slave
63 LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
65 MdSalRegistratorUtils.unregisterServices(rpcContext);
66 } catch (Exception e) {
67 LOG.error("Exception while unregistering rpcs for slave role for node:{}. But continuing.", nodeId, e);
71 LOG.info("Registering Openflow RPCs for node:{}, role:{}", nodeId, ofpRole);
72 MdSalRegistratorUtils.registerServices(rpcContext, deviceContext);
74 if (isStatisticsRpcEnabled) {
75 MdSalRegistratorUtils.registerStatCompatibilityServices(rpcContext, deviceContext,
76 notificationPublishService, new AtomicLong());
80 deviceContext.addDeviceContextClosedHandler(this);
82 // finish device initialization cycle back to DeviceManager
83 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
87 public void close() throws Exception {
93 public void onDeviceContextClosed(DeviceContext deviceContext) {
94 RpcContext removedContext = contexts.remove(deviceContext);
95 if (removedContext != null) {
97 LOG.info("Unregistering rpcs for device context closure");
98 removedContext.close();
99 } catch (Exception e) {
100 LOG.error("Exception while unregistering rpcs onDeviceContextClosed handler for node:{}. But continuing.",
101 deviceContext.getDeviceState().getNodeId(), e);
105 public void setStatisticsRpcEnabled(boolean isStatisticsRpcEnabled) {
106 this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
110 public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
111 this.notificationPublishService = notificationPublishService;