79d9f70d44ee441801888f30dab97de7c431431d
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsContextImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.statistics;
10
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.Collection;
18 import java.util.HashSet;
19 import java.util.List;
20 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
23 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
24 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
25 import org.opendaylight.openflowplugin.impl.rpc.RequestContextImpl;
26 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
27 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
35  */
36 public class StatisticsContextImpl implements StatisticsContext {
37
38     private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
39     public static final String CONNECTION_CLOSED = "Connection closed.";
40     private final Collection<RequestContext> requestContexts = new HashSet<>();
41     private final DeviceContext deviceContext;
42
43
44     private final StatisticsGatheringService statisticsGatheringService;
45
46     public StatisticsContextImpl(final DeviceContext deviceContext) {
47         this.deviceContext = deviceContext;
48         statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
49
50     }
51
52     @Override
53     public ListenableFuture<Boolean> gatherDynamicData() {
54
55         final SettableFuture settableResultingFuture = SettableFuture.create();
56         ListenableFuture<Boolean> resultingFuture = settableResultingFuture;
57
58
59         if (ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
60             final DeviceState devState = deviceContext.getDeviceState();
61             final ListenableFuture<Boolean> emptyFuture = Futures.immediateFuture(new Boolean(false));
62             final ListenableFuture<Boolean> flowStatistics = devState.isFlowStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, MultipartType.OFPMPFLOW) : emptyFuture;
63
64             final ListenableFuture<Boolean> tableStatistics = devState.isTableStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, MultipartType.OFPMPTABLE) : emptyFuture;
65
66             final ListenableFuture<Boolean> portStatistics = devState.isPortStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, MultipartType.OFPMPPORTSTATS) : emptyFuture;
67
68             final ListenableFuture<Boolean> queueStatistics = devState.isQueueStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, MultipartType.OFPMPQUEUE) : emptyFuture;
69
70             final ListenableFuture<Boolean> groupDescStatistics = devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, MultipartType.OFPMPGROUPDESC) : emptyFuture;
71             final ListenableFuture<Boolean> groupStatistics = devState.isGroupAvailable() ? StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, MultipartType.OFPMPGROUP) : emptyFuture;
72
73             final ListenableFuture<Boolean> meterConfigStatistics = devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, MultipartType.OFPMPMETERCONFIG) : emptyFuture;
74             final ListenableFuture<Boolean> meterStatistics = devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, MultipartType.OFPMPMETER) : emptyFuture;
75
76
77             final ListenableFuture<List<Boolean>> allFutures = Futures.allAsList(Arrays.asList(flowStatistics, tableStatistics, groupDescStatistics, groupStatistics, meterConfigStatistics, meterStatistics, portStatistics, queueStatistics));
78             Futures.addCallback(allFutures, new FutureCallback<List<Boolean>>() {
79                 @Override
80                 public void onSuccess(final List<Boolean> booleans) {
81                     boolean atLeastOneSuccess = false;
82                     for (final Boolean bool : booleans) {
83                         atLeastOneSuccess |= bool.booleanValue();
84                     }
85                     settableResultingFuture.set(new Boolean(atLeastOneSuccess));
86                 }
87
88                 @Override
89                 public void onFailure(final Throwable throwable) {
90                     settableResultingFuture.setException(throwable);
91                 }
92             });
93         } else {
94             switch (deviceContext.getPrimaryConnectionContext().getConnectionState()) {
95                 case RIP:
96                     resultingFuture = Futures.immediateFailedFuture(new Throwable(String.format("Device connection doesn't exist anymore. Primary connection status : %s", deviceContext.getPrimaryConnectionContext().getConnectionState())));
97                     break;
98                 default:
99                     resultingFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
100                     break;
101             }
102
103
104         }
105         return resultingFuture;
106     }
107
108     @Override
109     public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
110         requestContexts.remove(requestContext);
111     }
112
113     @Override
114     public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
115         requestContexts.add(data);
116         return data.getFuture();
117     }
118
119     @Override
120     public <T> RequestContext<T> createRequestContext() {
121         return new RequestContextImpl<>(this);
122     }
123
124     @Override
125     public void close() throws Exception {
126         for (final RequestContext requestContext : requestContexts) {
127             RequestContextUtil.closeRequestContextWithRpcError(requestContext, CONNECTION_CLOSED);
128         }
129     }
130 }