Post "Clustering optimization" updates
[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 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.MdSalRegistrationUtils;
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;
27
28 public class RpcManagerImpl implements RpcManager {
29
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;
37
38     public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
39                           final int quotaValue) {
40         this.rpcProviderRegistry = rpcProviderRegistry;
41         maxRequestsQuota = quotaValue;
42     }
43
44     @Override
45     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
46         deviceInitPhaseHandler = handler;
47     }
48
49     @Override
50     public void onDeviceContextLevelUp(final DeviceContext deviceContext) throws Exception {
51         final NodeId nodeId = deviceContext.getDeviceState().getNodeId();
52         final OfpRole ofpRole = deviceContext.getDeviceState().getRole();
53
54         LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", nodeId, ofpRole);
55         final RpcContext rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry,
56                 deviceContext, maxRequestsQuota, isStatisticsRpcEnabled, notificationPublishService);
57
58         Verify.verify(contexts.putIfAbsent(deviceContext, rpcContext) == null, "RPC context still not closed for node {}", nodeId);
59         deviceContext.addDeviceContextClosedHandler(this);
60
61         if (OfpRole.BECOMEMASTER.equals(ofpRole)) {
62             LOG.info("Registering Openflow RPCs services for node:{}, role:{}", nodeId, ofpRole);
63             MdSalRegistrationUtils.registerMasterServices(rpcContext, deviceContext, ofpRole);
64
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("Unregister RPC services (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
68             MdSalRegistrationUtils.registerSlaveServices(rpcContext, ofpRole);
69         } else {
70             // if we don't know role, we need to unregister rpcs if any have been registered
71             LOG.info("Unregister RPC services (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
72             MdSalRegistrationUtils.unregisterServices(rpcContext);
73         }
74
75         // finish device initialization cycle back to DeviceManager
76         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
77     }
78
79     @Override
80     public void close() {
81         for (final Iterator<Entry<DeviceContext, RpcContext>> iterator = Iterators
82                 .consumingIterator(contexts.entrySet().iterator()); iterator.hasNext();) {
83             iterator.next().getValue().close();
84         }
85     }
86
87
88     @Override
89     public void onDeviceContextClosed(final DeviceContext deviceContext) {
90         final RpcContext removedContext = contexts.remove(deviceContext);
91         if (removedContext != null) {
92             LOG.info("Unregister RPCs services for device context closure");
93             removedContext.close();
94         }
95     }
96     @Override
97     public void setStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
98         this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
99     }
100
101     @Override
102     public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
103         this.notificationPublishService = notificationPublishService;
104     }
105 }