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.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
22 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
23 import org.opendaylight.openflowplugin.impl.rpc.RequestContextImpl;
24 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
25 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
34 public class StatisticsContextImpl implements StatisticsContext {
36 private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
37 public static final String CONNECTION_CLOSED = "Connection closed.";
38 private final List<RequestContext> requestContexts = new ArrayList();
39 private final DeviceContext deviceContext;
42 private final StatisticsGatheringService statisticsGatheringService;
44 public StatisticsContextImpl(final DeviceContext deviceContext) {
45 this.deviceContext = deviceContext;
46 statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
51 public ListenableFuture<Boolean> gatherDynamicData() {
53 final SettableFuture settableResultingFuture = SettableFuture.create();
54 ListenableFuture<Boolean> resultingFuture = settableResultingFuture;
57 if (ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
58 final DeviceState devState = deviceContext.getDeviceState();
59 final ListenableFuture<Boolean> emptyFuture = Futures.immediateFuture(new Boolean(false));
60 final ListenableFuture<Boolean> flowStatistics = devState.isFlowStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPFLOW) : emptyFuture;
62 final ListenableFuture<Boolean> tableStatistics = devState.isTableStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPTABLE) : emptyFuture;
64 final ListenableFuture<Boolean> portStatistics = devState.isPortStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPPORTSTATS) : emptyFuture;
66 final ListenableFuture<Boolean> queueStatistics = devState.isQueueStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPQUEUE) : emptyFuture;
68 final ListenableFuture<Boolean> groupDescStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUPDESC) : emptyFuture;
69 final ListenableFuture<Boolean> groupStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUP) : emptyFuture;
71 final ListenableFuture<Boolean> meterConfigStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETERCONFIG) : emptyFuture;
72 final ListenableFuture<Boolean> meterStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETER) : emptyFuture;
75 final ListenableFuture<List<Boolean>> allFutures = Futures.allAsList(Arrays.asList(flowStatistics, tableStatistics, groupDescStatistics, groupStatistics, meterConfigStatistics, meterStatistics, portStatistics, queueStatistics));
76 Futures.addCallback(allFutures, new FutureCallback<List<Boolean>>() {
78 public void onSuccess(final List<Boolean> booleans) {
79 boolean atLeastOneSuccess = false;
80 for (final Boolean bool : booleans) {
81 atLeastOneSuccess |= bool.booleanValue();
83 settableResultingFuture.set(new Boolean(atLeastOneSuccess));
87 public void onFailure(final Throwable throwable) {
88 settableResultingFuture.setException(throwable);
92 switch (deviceContext.getPrimaryConnectionContext().getConnectionState()) {
94 resultingFuture = Futures.immediateFailedFuture(new Throwable(String.format("Device connection doesn't exist anymore. Primary connection status : %s", deviceContext.getPrimaryConnectionContext().getConnectionState())));
97 resultingFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
103 return resultingFuture;
106 private ListenableFuture<Boolean> wrapLoggingOnStatisticsRequestCall(final MultipartType type) {
107 final ListenableFuture<Boolean> future = StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, type);
108 Futures.addCallback(future, new FutureCallback() {
110 public void onSuccess(final Object o) {
111 LOG.trace("Multipart response for {} was successful.", type);
115 public void onFailure(final Throwable throwable) {
116 LOG.trace("Multipart response for {} FAILED.", type, throwable);
123 public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
124 requestContexts.remove(requestContext);
128 public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
129 requestContexts.add(data);
130 return data.getFuture();
134 public <T> RequestContext<T> createRequestContext() {
135 return new RequestContextImpl<>(this);
139 public void close() throws Exception {
140 for (final RequestContext requestContext : requestContexts) {
141 RequestContextUtil.closeRequestContextWithRpcError(requestContext, CONNECTION_CLOSED);