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