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.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.SettableFuture;
18 import io.netty.util.Timeout;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import javax.annotation.CheckForNull;
25 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
30 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
31 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
32 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringOnTheFlyService;
33 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
41 public class StatisticsContextImpl implements StatisticsContext {
43 private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
44 private static final String CONNECTION_CLOSED = "Connection closed.";
45 private final Collection<RequestContext<?>> requestContexts = new HashSet<>();
46 private final DeviceContext deviceContext;
47 private final DeviceState devState;
48 private final ListenableFuture<Boolean> emptyFuture;
49 private final List<MultipartType> collectingStatType;
51 private StatisticsGatheringService statisticsGatheringService;
52 private StatisticsGatheringOnTheFlyService statisticsGatheringOnTheFlyService;
53 private Timeout pollTimeout;
55 public StatisticsContextImpl(@CheckForNull final DeviceContext deviceContext) {
56 this.deviceContext = Preconditions.checkNotNull(deviceContext);
57 devState = Preconditions.checkNotNull(deviceContext.getDeviceState());
58 emptyFuture = Futures.immediateFuture(new Boolean(false));
59 statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
60 statisticsGatheringOnTheFlyService = new StatisticsGatheringOnTheFlyService(this, deviceContext);
62 final List<MultipartType> statListForCollecting = new ArrayList<>();
63 if (devState.isTableStatisticsAvailable()) {
64 statListForCollecting.add(MultipartType.OFPMPTABLE);
66 if (devState.isFlowStatisticsAvailable()) {
67 statListForCollecting.add(MultipartType.OFPMPFLOW);
69 if (devState.isGroupAvailable()) {
70 statListForCollecting.add(MultipartType.OFPMPGROUPDESC);
71 statListForCollecting.add(MultipartType.OFPMPGROUP);
73 if (devState.isMetersAvailable()) {
74 statListForCollecting.add(MultipartType.OFPMPMETERCONFIG);
75 statListForCollecting.add(MultipartType.OFPMPMETER);
77 if (devState.isPortStatisticsAvailable()) {
78 statListForCollecting.add(MultipartType.OFPMPPORTSTATS);
80 if (devState.isQueueStatisticsAvailable()) {
81 statListForCollecting.add(MultipartType.OFPMPQUEUE);
83 collectingStatType = ImmutableList.<MultipartType>copyOf(statListForCollecting);
87 public ListenableFuture<Boolean> gatherDynamicData() {
88 final ListenableFuture<Boolean> errorResultFuture = deviceConnectionCheck();
89 if (errorResultFuture != null) {
90 return errorResultFuture;
92 final Iterator<MultipartType> statIterator = collectingStatType.iterator();
93 final SettableFuture<Boolean> settableStatResultFuture = SettableFuture.create();
94 statChainFuture(statIterator, settableStatResultFuture);
95 return settableStatResultFuture;
98 private ListenableFuture<Boolean> chooseStat(final MultipartType multipartType) {
99 switch (multipartType) {
101 return collectFlowStatistics(multipartType);
103 return collectTableStatistics(multipartType);
105 return collectPortStatistics(multipartType);
107 return collectQueueStatistics(multipartType);
109 return collectGroupDescStatistics(multipartType);
111 return collectGroupStatistics(multipartType);
112 case OFPMPMETERCONFIG:
113 return collectMeterConfigStatistics(multipartType);
115 return collectMeterStatistics(multipartType);
117 LOG.warn("Unsuported Statistics type {}", multipartType);
118 return Futures.immediateCheckedFuture(Boolean.TRUE);
123 public <T> RequestContext<T> createRequestContext() {
124 final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
126 public void close() {
127 requestContexts.remove(this);
130 requestContexts.add(ret);
135 public void close() {
136 for (final RequestContext<?> requestContext : requestContexts) {
137 RequestContextUtil.closeRequestContextWithRpcError(requestContext, CONNECTION_CLOSED);
139 if (null != pollTimeout && !pollTimeout.isExpired()) {
140 pollTimeout.cancel();
145 public void setPollTimeout(Timeout pollTimeout) {
146 this.pollTimeout = pollTimeout;
149 void statChainFuture(final Iterator<MultipartType> iterator, final SettableFuture<Boolean> resultFuture) {
150 if ( ! iterator.hasNext()) {
151 resultFuture.set(Boolean.TRUE);
154 final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = chooseStat(iterator.next());
155 Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
157 public void onSuccess(final Boolean result) {
158 statChainFuture(iterator, resultFuture);
161 public void onFailure(final Throwable t) {
162 resultFuture.setException(t);
168 * Method checks a device state. It returns null for be able continue. Otherwise it returns immediateFuture
169 * which has to be returned from caller too
173 private ListenableFuture<Boolean> deviceConnectionCheck() {
174 if (!ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
175 ListenableFuture<Boolean> resultingFuture = SettableFuture.create();
176 switch (deviceContext.getPrimaryConnectionContext().getConnectionState()) {
178 final String errMsg = String.format("Device connection doesn't exist anymore. Primary connection status : %s",
179 deviceContext.getPrimaryConnectionContext().getConnectionState());
180 resultingFuture = Futures.immediateFailedFuture(new Throwable(errMsg));
183 resultingFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
186 return resultingFuture;
191 private ListenableFuture<Boolean> collectFlowStatistics(final MultipartType multipartType) {
192 return devState.isFlowStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
193 statisticsGatheringOnTheFlyService, deviceContext, /*MultipartType.OFPMPFLOW*/ multipartType) : emptyFuture;
196 private ListenableFuture<Boolean> collectTableStatistics(final MultipartType multipartType) {
197 return devState.isTableStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
198 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPTABLE*/ multipartType) : emptyFuture;
201 private ListenableFuture<Boolean> collectPortStatistics(final MultipartType multipartType) {
202 return devState.isPortStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
203 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPPORTSTATS*/ multipartType) : emptyFuture;
206 private ListenableFuture<Boolean> collectQueueStatistics(final MultipartType multipartType) {
207 return devState.isQueueStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
208 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPQUEUE*/ multipartType) : emptyFuture;
211 private ListenableFuture<Boolean> collectGroupDescStatistics(final MultipartType multipartType) {
212 return devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(
213 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPGROUPDESC*/ multipartType) : emptyFuture;
216 private ListenableFuture<Boolean> collectGroupStatistics(final MultipartType multipartType) {
217 return devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(
218 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPGROUP*/ multipartType) : emptyFuture;
221 private ListenableFuture<Boolean> collectMeterConfigStatistics(final MultipartType multipartType) {
222 return devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(
223 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPMETERCONFIG*/ multipartType) : emptyFuture;
226 private ListenableFuture<Boolean> collectMeterStatistics(final MultipartType multipartType) {
227 return devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(
228 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPMETER*/ multipartType) : emptyFuture;
232 protected void setStatisticsGatheringService(StatisticsGatheringService statisticsGatheringService) {
233 this.statisticsGatheringService = statisticsGatheringService;
237 protected void setStatisticsGatheringOnTheFlyService(StatisticsGatheringOnTheFlyService
238 statisticsGatheringOnTheFlyService) {
239 this.statisticsGatheringOnTheFlyService = statisticsGatheringOnTheFlyService;