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