Merge "Bug 5925 - Reuse Threads using ThreadPool in ConnectionManagerImpl"
[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.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.Iterators;
14 import java.util.Iterator;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
18 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
21 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
22 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
23 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
24 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
25 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class RpcManagerImpl implements RpcManager {
30
31     private static final Logger LOG = LoggerFactory.getLogger(RpcManagerImpl.class);
32     private final RpcProviderRegistry rpcProviderRegistry;
33     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
34     private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
35     private final int maxRequestsQuota;
36     private final ConcurrentMap<DeviceInfo, RpcContext> contexts = new ConcurrentHashMap<>();
37     private boolean isStatisticsRpcEnabled;
38
39     private final LifecycleConductor conductor;
40
41     public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
42                           final int quotaValue,
43                           final LifecycleConductor lifecycleConductor) {
44         this.rpcProviderRegistry = rpcProviderRegistry;
45         maxRequestsQuota = quotaValue;
46         this.conductor = lifecycleConductor;
47     }
48
49     @Override
50     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
51         deviceInitPhaseHandler = handler;
52     }
53
54     @Override
55     public void onDeviceContextLevelUp(final DeviceInfo deviceInfo) throws Exception {
56
57         final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(deviceInfo));
58
59         final RpcContext rpcContext = new RpcContextImpl(
60                 rpcProviderRegistry,
61                 deviceContext,
62                 deviceContext.getMessageSpy(),
63                 maxRequestsQuota,
64                 deviceInfo.getNodeInstanceIdentifier());
65
66         Verify.verify(contexts.putIfAbsent(deviceInfo, rpcContext) == null, "RpcCtx still not closed for node {}", deviceInfo.getNodeId());
67
68         rpcContext.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
69
70         // finish device initialization cycle back to DeviceManager
71         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo);
72     }
73
74     @Override
75     public void close() {
76         for (final Iterator<RpcContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
77                 iterator.hasNext();) {
78             iterator.next().close();
79         }
80     }
81
82     @Override
83     public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
84         final RpcContext removedContext = contexts.remove(deviceInfo);
85         if (removedContext != null) {
86             LOG.info("Unregister RPCs services for device context closure");
87             removedContext.close();
88         }
89         deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
90     }
91
92     @Override
93     public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
94         this.deviceTerminPhaseHandler = handler;
95     }
96
97     /**
98      * This method is only for testing
99      */
100     @VisibleForTesting
101     void addRecordToContexts(DeviceInfo deviceInfo, RpcContext rpcContexts) {
102         if(!contexts.containsKey(deviceInfo)) {
103             this.contexts.put(deviceInfo,rpcContexts);
104         }
105     }
106
107     @Override
108     public <T extends OFPContext> T gainContext(DeviceInfo deviceInfo) {
109         return (T) contexts.get(deviceInfo);
110     }
111
112
113     @Override
114     public void setStatisticsRpcEnabled(boolean statisticsRpcEnabled) {
115         isStatisticsRpcEnabled = statisticsRpcEnabled;
116     }
117 }