2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Iterators;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.SettableFuture;
19 import io.netty.util.Timeout;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.concurrent.ExecutionException;
27 import javax.annotation.Nonnull;
28 import javax.annotation.Nullable;
29 import javax.annotation.concurrent.GuardedBy;
30 import org.opendaylight.mdsal.common.api.TransactionChainClosedException;
31 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
32 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
36 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
37 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
38 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
39 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
40 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
41 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
42 import org.opendaylight.openflowplugin.impl.rpc.listener.ItemLifecycleListenerImpl;
43 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
44 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringOnTheFlyService;
45 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
46 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 class StatisticsContextImpl implements StatisticsContext {
53 private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
54 private static final String CONNECTION_CLOSED = "Connection closed.";
56 private final ItemLifecycleListener itemLifeCycleListener;
57 private final Collection<RequestContext<?>> requestContexts = new HashSet<>();
58 private final DeviceContext deviceContext;
59 private final DeviceState devState;
60 private final ListenableFuture<Boolean> emptyFuture;
61 private final boolean shuttingDownStatisticsPolling;
62 private final Object COLLECTION_STAT_TYPE_LOCK = new Object();
63 private final SinglePurposeMultipartReplyTranslator multipartReplyTranslator;
64 @GuardedBy("COLLECTION_STAT_TYPE_LOCK")
65 private List<MultipartType> collectingStatType;
67 private StatisticsGatheringService statisticsGatheringService;
68 private StatisticsGatheringOnTheFlyService statisticsGatheringOnTheFlyService;
69 private Timeout pollTimeout;
70 private final DeviceInfo deviceInfo;
71 private final StatisticsManager myManager;
72 private final LifecycleService lifecycleService;
74 private volatile boolean schedulingEnabled;
75 private volatile CONTEXT_STATE state;
77 StatisticsContextImpl(@Nonnull final DeviceInfo deviceInfo,
78 final boolean shuttingDownStatisticsPolling,
79 @Nonnull final LifecycleService lifecycleService,
80 @Nonnull final ConvertorExecutor convertorExecutor,
81 @Nonnull final StatisticsManager myManager) {
82 this.lifecycleService = lifecycleService;
83 this.deviceContext = lifecycleService.getDeviceContext();
84 this.devState = Preconditions.checkNotNull(deviceContext.getDeviceState());
85 this.shuttingDownStatisticsPolling = shuttingDownStatisticsPolling;
86 multipartReplyTranslator = new SinglePurposeMultipartReplyTranslator(convertorExecutor);
87 emptyFuture = Futures.immediateFuture(false);
88 statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
89 statisticsGatheringOnTheFlyService = new StatisticsGatheringOnTheFlyService(this, deviceContext, convertorExecutor);
90 itemLifeCycleListener = new ItemLifecycleListenerImpl(deviceContext);
91 statListForCollectingInitialization();
92 setState(CONTEXT_STATE.INITIALIZATION);
93 this.deviceInfo = deviceInfo;
94 this.myManager = myManager;
98 public void statListForCollectingInitialization() {
99 synchronized (COLLECTION_STAT_TYPE_LOCK) {
100 final List<MultipartType> statListForCollecting = new ArrayList<>();
101 if (devState.isTableStatisticsAvailable()) {
102 statListForCollecting.add(MultipartType.OFPMPTABLE);
104 if (devState.isFlowStatisticsAvailable()) {
105 statListForCollecting.add(MultipartType.OFPMPFLOW);
107 if (devState.isGroupAvailable()) {
108 statListForCollecting.add(MultipartType.OFPMPGROUPDESC);
109 statListForCollecting.add(MultipartType.OFPMPGROUP);
111 if (devState.isMetersAvailable()) {
112 statListForCollecting.add(MultipartType.OFPMPMETERCONFIG);
113 statListForCollecting.add(MultipartType.OFPMPMETER);
115 if (devState.isPortStatisticsAvailable()) {
116 statListForCollecting.add(MultipartType.OFPMPPORTSTATS);
118 if (devState.isQueueStatisticsAvailable()) {
119 statListForCollecting.add(MultipartType.OFPMPQUEUE);
121 collectingStatType = ImmutableList.<MultipartType>copyOf(statListForCollecting);
127 public ListenableFuture<Boolean> initialGatherDynamicData() {
128 return gatherDynamicData(true);
132 public ListenableFuture<Boolean> gatherDynamicData(){
133 return gatherDynamicData(false);
136 private ListenableFuture<Boolean> gatherDynamicData(final boolean initial) {
137 if (shuttingDownStatisticsPolling) {
138 LOG.debug("Statistics for device {} is not enabled.", getDeviceInfo().getNodeId().getValue());
139 return Futures.immediateFuture(Boolean.TRUE);
141 final ListenableFuture<Boolean> errorResultFuture = deviceConnectionCheck();
142 if (errorResultFuture != null) {
143 return errorResultFuture;
145 synchronized (COLLECTION_STAT_TYPE_LOCK) {
146 final Iterator<MultipartType> statIterator = collectingStatType.iterator();
147 final SettableFuture<Boolean> settableStatResultFuture = SettableFuture.create();
149 // write start timestamp to state snapshot container
150 StatisticsGatheringUtils.markDeviceStateSnapshotStart(deviceContext);
152 statChainFuture(statIterator, settableStatResultFuture, initial);
154 // write end timestamp to state snapshot container
155 Futures.addCallback(settableStatResultFuture, new FutureCallback<Boolean>() {
157 public void onSuccess(@Nullable final Boolean result) {
158 StatisticsGatheringUtils.markDeviceStateSnapshotEnd(deviceContext, true);
161 public void onFailure(final Throwable t) {
162 if (!(t instanceof TransactionChainClosedException)) {
163 StatisticsGatheringUtils.markDeviceStateSnapshotEnd(deviceContext, false);
167 return settableStatResultFuture;
171 private ListenableFuture<Boolean> chooseStat(final MultipartType multipartType, final boolean initial){
172 switch (multipartType) {
174 return collectFlowStatistics(multipartType, initial);
176 return collectTableStatistics(multipartType);
178 return collectPortStatistics(multipartType);
180 return collectQueueStatistics(multipartType);
182 return collectGroupDescStatistics(multipartType);
184 return collectGroupStatistics(multipartType);
185 case OFPMPMETERCONFIG:
186 return collectMeterConfigStatistics(multipartType);
188 return collectMeterStatistics(multipartType);
190 LOG.warn("Unsuported Statistics type {}", multipartType);
191 return Futures.immediateCheckedFuture(Boolean.TRUE);
197 public <T> RequestContext<T> createRequestContext() {
198 final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceInfo.reserveXidForDeviceMessage()) {
200 public void close() {
201 requestContexts.remove(this);
204 requestContexts.add(ret);
209 public void close() {
210 if (CONTEXT_STATE.TERMINATION.equals(getState())) {
211 if (LOG.isDebugEnabled()) {
212 LOG.debug("Statistics context is already in state TERMINATION.");
215 setState(CONTEXT_STATE.TERMINATION);
216 schedulingEnabled = false;
217 for (final Iterator<RequestContext<?>> iterator = Iterators.consumingIterator(requestContexts.iterator());
218 iterator.hasNext(); ) {
219 RequestContextUtil.closeRequestContextWithRpcError(iterator.next(), CONNECTION_CLOSED);
221 if (null != pollTimeout && !pollTimeout.isExpired()) {
222 pollTimeout.cancel();
228 public void setSchedulingEnabled(final boolean schedulingEnabled) {
229 this.schedulingEnabled = schedulingEnabled;
233 public boolean isSchedulingEnabled() {
234 return schedulingEnabled;
238 public void setPollTimeout(final Timeout pollTimeout) {
239 this.pollTimeout = pollTimeout;
243 public Optional<Timeout> getPollTimeout() {
244 return Optional.ofNullable(pollTimeout);
247 private void statChainFuture(final Iterator<MultipartType> iterator, final SettableFuture<Boolean> resultFuture, final boolean initial) {
248 if (ConnectionContext.CONNECTION_STATE.RIP.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
249 final String errMsg = String.format("Device connection is closed for Node : %s.",
250 getDeviceInfo().getNodeId());
252 resultFuture.setException(new IllegalStateException(errMsg));
255 if ( ! iterator.hasNext()) {
256 resultFuture.set(Boolean.TRUE);
257 LOG.debug("Stats collection successfully finished for node {}", getDeviceInfo().getNodeId());
261 final MultipartType nextType = iterator.next();
262 LOG.debug("Stats iterating to next type for node {} of type {}", getDeviceInfo().getNodeId(), nextType);
264 final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = chooseStat(nextType, initial);
265 Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
267 public void onSuccess(final Boolean result) {
268 statChainFuture(iterator, resultFuture, initial);
271 public void onFailure(@Nonnull final Throwable t) {
272 resultFuture.setException(t);
278 * Method checks a device state. It returns null for be able continue. Otherwise it returns immediateFuture
279 * which has to be returned from caller too
284 ListenableFuture<Boolean> deviceConnectionCheck() {
285 if (!ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
286 ListenableFuture<Boolean> resultingFuture;
287 switch (deviceContext.getPrimaryConnectionContext().getConnectionState()) {
289 final String errMsg = String.format("Device connection doesn't exist anymore. Primary connection status : %s",
290 deviceContext.getPrimaryConnectionContext().getConnectionState());
291 resultingFuture = Futures.immediateFailedFuture(new Throwable(errMsg));
294 resultingFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
297 return resultingFuture;
302 //TODO: Refactor twice sending deviceContext into gatheringStatistics
303 private ListenableFuture<Boolean> collectFlowStatistics(final MultipartType multipartType, final boolean initial) {
304 return devState.isFlowStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
305 statisticsGatheringOnTheFlyService,
307 /*MultipartType.OFPMPFLOW*/ multipartType,
310 initial, multipartReplyTranslator) : emptyFuture;
313 private ListenableFuture<Boolean> collectTableStatistics(final MultipartType multipartType) {
314 return devState.isTableStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
315 statisticsGatheringService,
317 /*MultipartType.OFPMPTABLE*/ multipartType,
320 false, multipartReplyTranslator) : emptyFuture;
323 private ListenableFuture<Boolean> collectPortStatistics(final MultipartType multipartType) {
324 return devState.isPortStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
325 statisticsGatheringService,
327 /*MultipartType.OFPMPPORTSTATS*/ multipartType,
330 false, multipartReplyTranslator) : emptyFuture;
333 private ListenableFuture<Boolean> collectQueueStatistics(final MultipartType multipartType) {
334 return !devState.isQueueStatisticsAvailable() ? emptyFuture : StatisticsGatheringUtils.gatherStatistics(
335 statisticsGatheringService,
337 /*MultipartType.OFPMPQUEUE*/ multipartType,
340 false, multipartReplyTranslator);
343 private ListenableFuture<Boolean> collectGroupDescStatistics(final MultipartType multipartType) {
344 return devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(
345 statisticsGatheringService,
347 /*MultipartType.OFPMPGROUPDESC*/ multipartType,
350 false, multipartReplyTranslator) : emptyFuture;
353 private ListenableFuture<Boolean> collectGroupStatistics(final MultipartType multipartType) {
354 return devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(
355 statisticsGatheringService,
357 /*MultipartType.OFPMPGROUP*/ multipartType,
360 false, multipartReplyTranslator) : emptyFuture;
363 private ListenableFuture<Boolean> collectMeterConfigStatistics(final MultipartType multipartType) {
364 return devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(
365 statisticsGatheringService,
367 /*MultipartType.OFPMPMETERCONFIG*/ multipartType,
370 false, multipartReplyTranslator) : emptyFuture;
373 private ListenableFuture<Boolean> collectMeterStatistics(final MultipartType multipartType) {
374 return devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(
375 statisticsGatheringService,
377 /*MultipartType.OFPMPMETER*/ multipartType,
380 false, multipartReplyTranslator) : emptyFuture;
384 void setStatisticsGatheringService(final StatisticsGatheringService statisticsGatheringService) {
385 this.statisticsGatheringService = statisticsGatheringService;
389 void setStatisticsGatheringOnTheFlyService(final StatisticsGatheringOnTheFlyService
390 statisticsGatheringOnTheFlyService) {
391 this.statisticsGatheringOnTheFlyService = statisticsGatheringOnTheFlyService;
395 public ItemLifecycleListener getItemLifeCycleListener () {
396 return itemLifeCycleListener;
400 public CONTEXT_STATE getState() {
405 public void setState(CONTEXT_STATE state) {
410 public ServiceGroupIdentifier getServiceIdentifier() {
411 return this.deviceInfo.getServiceIdentifier();
415 public DeviceInfo getDeviceInfo() {
416 return this.deviceInfo;
420 public void startupClusterServices() throws ExecutionException, InterruptedException {
421 if (!this.shuttingDownStatisticsPolling) {
422 this.statListForCollectingInitialization();
423 this.initialGatherDynamicData();
424 myManager.startScheduling(deviceInfo);
429 public ListenableFuture<Void> stopClusterServices() {
430 myManager.stopScheduling(deviceInfo);
431 return Futures.immediateFuture(null);
435 public LifecycleService getLifecycleService() {
436 return lifecycleService;