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