Merge "Bug 4957: OF Role processing changes"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcManagerImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.rpc;
9
10 import com.google.common.base.Verify;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
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;
24
25 public class RpcManagerImpl implements RpcManager {
26
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 ConcurrentMap<DeviceContext, RpcContext> contexts = new ConcurrentHashMap<>();
32     private boolean isStatisticsRpcEnabled;
33     private NotificationPublishService notificationPublishService;
34
35     public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
36                           final int quotaValue) {
37         this.rpcProviderRegistry = rpcProviderRegistry;
38         maxRequestsQuota = quotaValue;
39     }
40
41     @Override
42     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
43         deviceInitPhaseHandler = handler;
44     }
45
46     @Override
47     public void onDeviceContextLevelUp(final DeviceContext deviceContext) throws Exception {
48         final NodeId nodeId = deviceContext.getDeviceState().getNodeId();
49         final OfpRole ofpRole = deviceContext.getDeviceState().getRole();
50
51         LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", nodeId, ofpRole);
52         final RpcContext rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry,
53                 deviceContext, maxRequestsQuota, isStatisticsRpcEnabled, notificationPublishService);
54
55         Verify.verify(contexts.putIfAbsent(deviceContext, rpcContext) == null, "RpcCtx still not closed for node {}", nodeId);
56         deviceContext.addDeviceContextClosedHandler(this);
57
58         //FIXME : propagate isStatisticsRpcEnabled to DeviceContext
59
60         if (OfpRole.BECOMEMASTER.equals(ofpRole)) {
61             LOG.info("Registering Openflow RPCs for node:{}, role:{}", nodeId, ofpRole);
62             MdSalRegistratorUtils.registerMasterServices(rpcContext, deviceContext, ofpRole);
63
64         } else if(OfpRole.BECOMESLAVE.equals(ofpRole)) {
65             // if slave, we need to de-register rpcs if any have been registered, in case of master to slave
66             LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
67             MdSalRegistratorUtils.registerSlaveServices(rpcContext, ofpRole);
68         } else {
69             // if we don't know role, we need to unregister rpcs if any have been registered
70             LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
71             MdSalRegistratorUtils.unregisterServices(rpcContext);
72         }
73
74         // finish device initialization cycle back to DeviceManager
75         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
76     }
77
78     @Override
79     public void close() throws Exception {
80         for(final RpcContext ctx : contexts.values()) {
81             ctx.close();
82         }
83         contexts.clear();
84     }
85
86
87     @Override
88     public void onDeviceContextClosed(final DeviceContext deviceContext) {
89         final RpcContext removedContext = contexts.remove(deviceContext);
90         if (removedContext != null) {
91             LOG.info("Unregistering rpcs for device context closure");
92             removedContext.close();
93         }
94     }
95     @Override
96     public void setStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
97         this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
98     }
99
100     @Override
101     public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
102         this.notificationPublishService = notificationPublishService;
103     }
104 }