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.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
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 public static final String CONNECTION_CLOSED = "Connection closed.";
45 private final List<RequestContext> requestContexts = new ArrayList();
46 private final DeviceContext deviceContext;
49 private final StatisticsGatheringService statisticsGatheringService;
51 public StatisticsContextImpl(final DeviceContext deviceContext) {
52 this.deviceContext = deviceContext;
53 statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
57 private void pollFlowStatistics() {
58 final KeyedInstanceIdentifier<Node, NodeKey> nodeII = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
59 final NodeRef nodeRef = new NodeRef(nodeII);
60 final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder builder =
61 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
62 builder.setNode(nodeRef);
63 //TODO : process data from result
67 public ListenableFuture<Void> gatherDynamicData() {
69 final DeviceState devState = deviceContext.getDeviceState();
71 final ListenableFuture<Void> resultingFuture;
73 if (ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
74 final SettableFuture settableResultingFuture = SettableFuture.create();
75 resultingFuture = settableResultingFuture;
76 ListenableFuture<Boolean> emptyFuture = Futures.immediateFuture(null);
77 final ListenableFuture<Boolean> flowStatistics = devState.isFlowStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPFLOW) : emptyFuture;
79 final ListenableFuture<Boolean> tableStatistics = devState.isTableStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPTABLE) : emptyFuture;
81 final ListenableFuture<Boolean> portStatistics = devState.isPortStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPPORTSTATS) : emptyFuture;
83 final ListenableFuture<Boolean> queueStatistics = devState.isQueueStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPQUEUE) : emptyFuture;
85 final ListenableFuture<Boolean> groupDescStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUPDESC) : emptyFuture;
86 final ListenableFuture<Boolean> groupStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUP) : emptyFuture;
88 final ListenableFuture<Boolean> meterConfigStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETERCONFIG) : emptyFuture;
89 final ListenableFuture<Boolean> meterStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETER) : emptyFuture;
92 final ListenableFuture<List<Boolean>> allFutures = Futures.allAsList(Arrays.asList(flowStatistics, tableStatistics, groupDescStatistics, groupStatistics, meterConfigStatistics, meterStatistics, portStatistics, queueStatistics));
93 Futures.addCallback(allFutures, new FutureCallback<List<Boolean>>() {
95 public void onSuccess(final List<Boolean> booleans) {
96 settableResultingFuture.set(null);
100 public void onFailure(final Throwable throwable) {
101 settableResultingFuture.setException(throwable);
105 resultingFuture = Futures.immediateCancelledFuture();
107 return resultingFuture;
110 private ListenableFuture<Boolean> wrapLoggingOnStatisticsRequestCall(final MultipartType type) {
111 final ListenableFuture<Boolean> future = StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, type);
112 Futures.addCallback(future, new FutureCallback() {
114 public void onSuccess(final Object o) {
115 LOG.trace("Multipart response for {} was successful.", type);
119 public void onFailure(final Throwable throwable) {
120 LOG.trace("Multipart response for {} FAILED.", type, throwable);
127 public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
128 requestContexts.remove(requestContexts);
132 public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
133 requestContexts.add(data);
134 return data.getFuture();
138 public <T> RequestContext<T> createRequestContext() {
139 return new RequestContextImpl<>(this);
143 public void close() throws Exception {
144 for (RequestContext requestContext : requestContexts) {
145 RequestContextUtil.closeRequestContextWithRpcError(requestContext, CONNECTION_CLOSED);