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.base.Verify;
11 import com.google.common.collect.Iterators;
12 import java.util.Iterator;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
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.handlers.DeviceInitializationPhaseHandler;
20 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
21 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
22 import org.opendaylight.openflowplugin.impl.util.MdSalRegistratorUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class RpcManagerImpl implements RpcManager {
30 private static final Logger LOG = LoggerFactory.getLogger(RpcManagerImpl.class);
31 private final RpcProviderRegistry rpcProviderRegistry;
32 private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
33 private final int maxRequestsQuota;
34 private final ConcurrentMap<DeviceContext, RpcContext> contexts = new ConcurrentHashMap<>();
35 private boolean isStatisticsRpcEnabled;
36 private NotificationPublishService notificationPublishService;
38 public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
39 final int quotaValue) {
40 this.rpcProviderRegistry = rpcProviderRegistry;
41 maxRequestsQuota = quotaValue;
45 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
46 deviceInitPhaseHandler = handler;
50 public void onDeviceContextLevelUp(final DeviceContext deviceContext) throws Exception {
51 final NodeId nodeId = deviceContext.getDeviceState().getNodeId();
52 final OfpRole ofpRole = deviceContext.getDeviceState().getRole();
54 LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", nodeId, ofpRole);
55 final RpcContext rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry,
56 deviceContext, maxRequestsQuota, isStatisticsRpcEnabled, notificationPublishService);
58 Verify.verify(contexts.putIfAbsent(deviceContext, rpcContext) == null, "RpcCtx still not closed for node {}", nodeId);
59 deviceContext.addDeviceContextClosedHandler(this);
61 if (OfpRole.BECOMEMASTER.equals(ofpRole)) {
62 LOG.info("Registering Openflow RPCs for node:{}, role:{}", nodeId, ofpRole);
63 MdSalRegistratorUtils.registerMasterServices(rpcContext, deviceContext, ofpRole);
65 } else if(OfpRole.BECOMESLAVE.equals(ofpRole)) {
66 // if slave, we need to de-register rpcs if any have been registered, in case of master to slave
67 LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
68 MdSalRegistratorUtils.registerSlaveServices(rpcContext, ofpRole);
70 // if we don't know role, we need to unregister rpcs if any have been registered
71 LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
72 MdSalRegistratorUtils.unregisterServices(rpcContext);
75 // finish device initialization cycle back to DeviceManager
76 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
81 for (final Iterator<Entry<DeviceContext, RpcContext>> iterator = Iterators
82 .consumingIterator(contexts.entrySet().iterator()); iterator.hasNext();) {
83 iterator.next().getValue().close();
89 public void onDeviceContextClosed(final DeviceContext deviceContext) {
90 final RpcContext removedContext = contexts.remove(deviceContext);
91 if (removedContext != null) {
92 LOG.info("Unregistering rpcs for device context closure");
93 removedContext.close();
97 public void setStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
98 this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
102 public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
103 this.notificationPublishService = notificationPublishService;