Fix unused import warnings
[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.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
20 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
21 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
22 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
23 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
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 DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
34     private final int maxRequestsQuota;
35     private final ConcurrentMap<NodeId, RpcContext> contexts = new ConcurrentHashMap<>();
36
37     private final LifecycleConductor conductor;
38
39     public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
40                           final int quotaValue,
41                           final LifecycleConductor lifecycleConductor) {
42         this.rpcProviderRegistry = rpcProviderRegistry;
43         maxRequestsQuota = quotaValue;
44         this.conductor = lifecycleConductor;
45     }
46
47     @Override
48     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
49         deviceInitPhaseHandler = handler;
50     }
51
52     @Override
53     public void onDeviceContextLevelUp(final NodeId nodeId) throws Exception {
54
55         final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(nodeId));
56
57         final RpcContext rpcContext = new RpcContextImpl(
58                 rpcProviderRegistry,
59                 deviceContext,
60                 deviceContext.getMessageSpy(),
61                 maxRequestsQuota,
62                 deviceContext.getDeviceState().getNodeInstanceIdentifier());
63
64         deviceContext.setRpcContext(rpcContext);
65
66         Verify.verify(contexts.putIfAbsent(nodeId, rpcContext) == null, "RpcCtx still not closed for node {}", nodeId);
67
68         // finish device initialization cycle back to DeviceManager
69         deviceInitPhaseHandler.onDeviceContextLevelUp(nodeId);
70     }
71
72     @Override
73     public void close() {
74         for (final Iterator<RpcContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
75                 iterator.hasNext();) {
76             iterator.next().close();
77         }
78     }
79
80     @Override
81     public void onDeviceContextLevelDown(final DeviceContext deviceContext) {
82         final RpcContext removedContext = contexts.remove(deviceContext.getDeviceState().getNodeId());
83         if (removedContext != null) {
84             LOG.info("Unregister RPCs services for device context closure");
85             removedContext.close();
86         }
87         deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceContext);
88     }
89
90     @Override
91     public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
92         this.deviceTerminPhaseHandler = handler;
93     }
94
95     /**
96      * This method is only for testing
97      */
98     @VisibleForTesting
99     void addRecordToContexts(NodeId nodeId, RpcContext rpcContexts) {
100         if(!contexts.containsKey(nodeId)) {
101             this.contexts.put(nodeId,rpcContexts);
102         }
103     }
104 }