Bug-4957: Double candidate, clean up
[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 java.util.concurrent.ConcurrentHashMap;
11 import java.util.concurrent.atomic.AtomicLong;
12
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 ConcurrentHashMap<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         NodeId nodeId = deviceContext.getDeviceState().getNodeId();
49         OfpRole ofpRole = deviceContext.getDeviceState().getRole();
50
51         LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", nodeId, ofpRole);
52
53         RpcContext rpcContext = contexts.get(deviceContext);
54         if (rpcContext == null) {
55             rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry, deviceContext, maxRequestsQuota);
56             contexts.put(deviceContext, rpcContext);
57         }
58
59
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());
63             try {
64                 MdSalRegistratorUtils.unregisterServices(rpcContext);
65             } catch (Exception e) {
66                 LOG.error("Exception while unregistering rpcs for slave role for node:{}. But continuing.", nodeId, e);
67             }
68
69         } else {
70             LOG.info("Registering Openflow RPCs for node:{}, role:{}", nodeId, ofpRole);
71             MdSalRegistratorUtils.registerServices(rpcContext, deviceContext, ofpRole);
72
73             if (isStatisticsRpcEnabled) {
74                 MdSalRegistratorUtils.registerStatCompatibilityServices(rpcContext, deviceContext,
75                         notificationPublishService, new AtomicLong());
76             }
77         }
78
79         deviceContext.addDeviceContextClosedHandler(this);
80
81         // finish device initialization cycle back to DeviceManager
82         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
83     }
84
85     @Override
86     public void close() throws Exception {
87
88     }
89
90
91     @Override
92     public void onDeviceContextClosed(DeviceContext deviceContext) {
93         RpcContext removedContext = contexts.remove(deviceContext);
94         if (removedContext != null) {
95             try {
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);
101             }
102         }
103     }
104     public void setStatisticsRpcEnabled(boolean isStatisticsRpcEnabled) {
105         this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
106     }
107
108     @Override
109     public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
110         this.notificationPublishService = notificationPublishService;
111     }
112 }