Bug 5596 Created lifecycle service
[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(
42             final RpcProviderRegistry rpcProviderRegistry,
43             final int quotaValue,
44             final LifecycleConductor lifecycleConductor) {
45         this.rpcProviderRegistry = rpcProviderRegistry;
46         maxRequestsQuota = quotaValue;
47         this.conductor = lifecycleConductor;
48     }
49
50     @Override
51     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
52         deviceInitPhaseHandler = handler;
53     }
54
55     @Override
56     public void onDeviceContextLevelUp(final DeviceInfo deviceInfo) throws Exception {
57
58         final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(deviceInfo));
59
60         final RpcContext rpcContext = new RpcContextImpl(
61                 deviceInfo,
62                 rpcProviderRegistry,
63                 deviceContext,
64                 deviceContext.getMessageSpy(),
65                 maxRequestsQuota,
66                 deviceInfo.getNodeInstanceIdentifier());
67
68         Verify.verify(contexts.putIfAbsent(deviceInfo, rpcContext) == null, "RpcCtx still not closed for node {}", deviceInfo.getNodeId());
69
70         rpcContext.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
71
72         // finish device initialization cycle back to DeviceManager
73         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo);
74     }
75
76     @Override
77     public void close() {
78         for (final Iterator<RpcContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
79                 iterator.hasNext();) {
80             iterator.next().close();
81         }
82     }
83
84     @Override
85     public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
86         final RpcContext removedContext = contexts.remove(deviceInfo);
87         if (removedContext != null) {
88             LOG.info("Unregister RPCs services for device context closure");
89             removedContext.close();
90         }
91         deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
92     }
93
94     @Override
95     public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
96         this.deviceTerminPhaseHandler = handler;
97     }
98
99     /**
100      * This method is only for testing
101      */
102     @VisibleForTesting
103     void addRecordToContexts(DeviceInfo deviceInfo, RpcContext rpcContexts) {
104         if(!contexts.containsKey(deviceInfo)) {
105             this.contexts.put(deviceInfo,rpcContexts);
106         }
107     }
108
109     @Override
110     public <T extends OFPContext> T gainContext(DeviceInfo deviceInfo) {
111         return (T) contexts.get(deviceInfo);
112     }
113
114
115     @Override
116     public void setStatisticsRpcEnabled(boolean statisticsRpcEnabled) {
117         isStatisticsRpcEnabled = statisticsRpcEnabled;
118     }
119 }