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.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableList;
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 javax.annotation.CheckForNull;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
30 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
31 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
32 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
33 import org.opendaylight.openflowplugin.impl.rpc.listener.ItemLifecycleListenerImpl;
34 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
35 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringOnTheFlyService;
36 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
44 public class StatisticsContextImpl implements StatisticsContext {
46 private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
47 private static final String CONNECTION_CLOSED = "Connection closed.";
49 private final ItemLifecycleListener itemLifeCycleListener;
50 private final Collection<RequestContext<?>> requestContexts = new HashSet<>();
51 private final DeviceContext deviceContext;
52 private final DeviceState devState;
53 private final ListenableFuture<Boolean> emptyFuture;
54 private final List<MultipartType> collectingStatType;
56 private StatisticsGatheringService statisticsGatheringService;
57 private StatisticsGatheringOnTheFlyService statisticsGatheringOnTheFlyService;
58 private Timeout pollTimeout;
60 public StatisticsContextImpl(@CheckForNull final DeviceContext deviceContext) {
61 this.deviceContext = Preconditions.checkNotNull(deviceContext);
62 devState = Preconditions.checkNotNull(deviceContext.getDeviceState());
63 emptyFuture = Futures.immediateFuture(new Boolean(false));
64 statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
65 statisticsGatheringOnTheFlyService = new StatisticsGatheringOnTheFlyService(this, deviceContext);
67 final List<MultipartType> statListForCollecting = new ArrayList<>();
68 if (devState.isTableStatisticsAvailable()) {
69 statListForCollecting.add(MultipartType.OFPMPTABLE);
71 if (devState.isFlowStatisticsAvailable()) {
72 statListForCollecting.add(MultipartType.OFPMPFLOW);
74 if (devState.isGroupAvailable()) {
75 statListForCollecting.add(MultipartType.OFPMPGROUPDESC);
76 statListForCollecting.add(MultipartType.OFPMPGROUP);
78 if (devState.isMetersAvailable()) {
79 statListForCollecting.add(MultipartType.OFPMPMETERCONFIG);
80 statListForCollecting.add(MultipartType.OFPMPMETER);
82 if (devState.isPortStatisticsAvailable()) {
83 statListForCollecting.add(MultipartType.OFPMPPORTSTATS);
85 if (devState.isQueueStatisticsAvailable()) {
86 statListForCollecting.add(MultipartType.OFPMPQUEUE);
88 collectingStatType = ImmutableList.<MultipartType>copyOf(statListForCollecting);
89 itemLifeCycleListener = new ItemLifecycleListenerImpl(deviceContext);
93 public ListenableFuture<Boolean> gatherDynamicData() {
94 final ListenableFuture<Boolean> errorResultFuture = deviceConnectionCheck();
95 if (errorResultFuture != null) {
96 return errorResultFuture;
98 final Iterator<MultipartType> statIterator = collectingStatType.iterator();
99 final SettableFuture<Boolean> settableStatResultFuture = SettableFuture.create();
100 statChainFuture(statIterator, settableStatResultFuture);
101 return settableStatResultFuture;
104 private ListenableFuture<Boolean> chooseStat(final MultipartType multipartType) {
105 switch (multipartType) {
107 return collectFlowStatistics(multipartType);
109 return collectTableStatistics(multipartType);
111 return collectPortStatistics(multipartType);
113 return collectQueueStatistics(multipartType);
115 return collectGroupDescStatistics(multipartType);
117 return collectGroupStatistics(multipartType);
118 case OFPMPMETERCONFIG:
119 return collectMeterConfigStatistics(multipartType);
121 return collectMeterStatistics(multipartType);
123 LOG.warn("Unsuported Statistics type {}", multipartType);
124 return Futures.immediateCheckedFuture(Boolean.TRUE);
129 public <T> RequestContext<T> createRequestContext() {
130 final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
132 public void close() {
133 requestContexts.remove(this);
136 requestContexts.add(ret);
141 public void close() {
142 for (final RequestContext<?> requestContext : requestContexts) {
143 RequestContextUtil.closeRequestContextWithRpcError(requestContext, CONNECTION_CLOSED);
145 if (null != pollTimeout && !pollTimeout.isExpired()) {
146 pollTimeout.cancel();
151 public void setPollTimeout(Timeout pollTimeout) {
152 this.pollTimeout = pollTimeout;
156 public Optional<Timeout> getPollTimeout() {
157 return Optional.fromNullable(pollTimeout);
160 void statChainFuture(final Iterator<MultipartType> iterator, final SettableFuture<Boolean> resultFuture) {
161 if ( ! iterator.hasNext()) {
162 resultFuture.set(Boolean.TRUE);
165 final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = chooseStat(iterator.next());
166 Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
168 public void onSuccess(final Boolean result) {
169 statChainFuture(iterator, resultFuture);
172 public void onFailure(final Throwable t) {
173 resultFuture.setException(t);
179 * Method checks a device state. It returns null for be able continue. Otherwise it returns immediateFuture
180 * which has to be returned from caller too
185 ListenableFuture<Boolean> deviceConnectionCheck() {
186 if (!ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
187 ListenableFuture<Boolean> resultingFuture = SettableFuture.create();
188 switch (deviceContext.getPrimaryConnectionContext().getConnectionState()) {
190 final String errMsg = String.format("Device connection doesn't exist anymore. Primary connection status : %s",
191 deviceContext.getPrimaryConnectionContext().getConnectionState());
192 resultingFuture = Futures.immediateFailedFuture(new Throwable(errMsg));
195 resultingFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
198 return resultingFuture;
203 private ListenableFuture<Boolean> collectFlowStatistics(final MultipartType multipartType) {
204 return devState.isFlowStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
205 statisticsGatheringOnTheFlyService, deviceContext, /*MultipartType.OFPMPFLOW*/ multipartType) : emptyFuture;
208 private ListenableFuture<Boolean> collectTableStatistics(final MultipartType multipartType) {
209 return devState.isTableStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
210 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPTABLE*/ multipartType) : emptyFuture;
213 private ListenableFuture<Boolean> collectPortStatistics(final MultipartType multipartType) {
214 return devState.isPortStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
215 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPPORTSTATS*/ multipartType) : emptyFuture;
218 private ListenableFuture<Boolean> collectQueueStatistics(final MultipartType multipartType) {
219 return devState.isQueueStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
220 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPQUEUE*/ multipartType) : emptyFuture;
223 private ListenableFuture<Boolean> collectGroupDescStatistics(final MultipartType multipartType) {
224 return devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(
225 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPGROUPDESC*/ multipartType) : emptyFuture;
228 private ListenableFuture<Boolean> collectGroupStatistics(final MultipartType multipartType) {
229 return devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(
230 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPGROUP*/ multipartType) : emptyFuture;
233 private ListenableFuture<Boolean> collectMeterConfigStatistics(final MultipartType multipartType) {
234 return devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(
235 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPMETERCONFIG*/ multipartType) : emptyFuture;
238 private ListenableFuture<Boolean> collectMeterStatistics(final MultipartType multipartType) {
239 return devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(
240 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPMETER*/ multipartType) : emptyFuture;
244 protected void setStatisticsGatheringService(StatisticsGatheringService statisticsGatheringService) {
245 this.statisticsGatheringService = statisticsGatheringService;
249 protected void setStatisticsGatheringOnTheFlyService(StatisticsGatheringOnTheFlyService
250 statisticsGatheringOnTheFlyService) {
251 this.statisticsGatheringOnTheFlyService = statisticsGatheringOnTheFlyService;
255 public ItemLifecycleListener getItemLifeCycleListener() {
256 return itemLifeCycleListener;