11f30f644f79ddbd2d73700d4211a69e11a64de2
[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.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.LifecycleService;
24 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
25 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
26 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class RpcManagerImpl implements RpcManager {
32
33     private static final Logger LOG = LoggerFactory.getLogger(RpcManagerImpl.class);
34     private final RpcProviderRegistry rpcProviderRegistry;
35     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
36     private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
37     private final int maxRequestsQuota;
38     private final ConcurrentMap<DeviceInfo, RpcContext> contexts = new ConcurrentHashMap<>();
39     private boolean isStatisticsRpcEnabled;
40     private final ExtensionConverterProvider extensionConverterProvider;
41     private final ConvertorExecutor convertorExecutor;
42     private final NotificationPublishService notificationPublishService;
43
44
45     public RpcManagerImpl(
46             final RpcProviderRegistry rpcProviderRegistry,
47             final int quotaValue,
48             final ExtensionConverterProvider extensionConverterProvider,
49                 final ConvertorExecutor convertorExecutor,
50             final NotificationPublishService notificationPublishService) {
51         this.rpcProviderRegistry = rpcProviderRegistry;
52         maxRequestsQuota = quotaValue;
53         this.extensionConverterProvider = extensionConverterProvider;
54         this.convertorExecutor = convertorExecutor;
55         this.notificationPublishService = notificationPublishService;
56     }
57
58     @Override
59     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
60         deviceInitPhaseHandler = handler;
61     }
62
63     @Override
64     public void onDeviceContextLevelUp(final DeviceInfo deviceInfo, final LifecycleService lifecycleService) throws Exception {
65
66         final DeviceContext deviceContext = Preconditions.checkNotNull(lifecycleService.getDeviceContext());
67
68         final RpcContext rpcContext = new RpcContextImpl(
69                 deviceInfo,
70                 rpcProviderRegistry,
71                 deviceContext.getMessageSpy(),
72                 maxRequestsQuota,
73                 deviceInfo.getNodeInstanceIdentifier(),
74                 deviceContext,
75                 extensionConverterProvider,
76                 convertorExecutor,
77                 notificationPublishService);
78
79         Verify.verify(contexts.putIfAbsent(deviceInfo, rpcContext) == null, "RpcCtx still not closed for node {}", deviceInfo.getNodeId());
80         lifecycleService.setRpcContext(rpcContext);
81         rpcContext.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
82
83         // finish device initialization cycle back to DeviceManager
84         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo, lifecycleService);
85     }
86
87     @Override
88     public void close() {
89         for (final Iterator<RpcContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
90                 iterator.hasNext();) {
91             iterator.next().close();
92         }
93     }
94
95     @Override
96     public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
97         final RpcContext removedContext = contexts.remove(deviceInfo);
98         if (removedContext != null) {
99             LOG.debug("Unregister RPCs services for  node {}", deviceInfo.getLOGValue());
100             removedContext.close();
101         }
102         deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
103     }
104
105     @Override
106     public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
107         this.deviceTerminPhaseHandler = handler;
108     }
109
110     /**
111      * This method is only for testing
112      */
113     @VisibleForTesting
114     void addRecordToContexts(DeviceInfo deviceInfo, RpcContext rpcContexts) {
115         if(!contexts.containsKey(deviceInfo)) {
116             this.contexts.put(deviceInfo,rpcContexts);
117         }
118     }
119
120
121     @Override
122     public void setStatisticsRpcEnabled(boolean statisticsRpcEnabled) {
123         isStatisticsRpcEnabled = statisticsRpcEnabled;
124     }
125 }