Merge "bug 3126 - shift to new notification service"
[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.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;
30
31 /**
32  * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
33  */
34 public class StatisticsContextImpl implements StatisticsContext {
35
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;
40
41
42     private final StatisticsGatheringService statisticsGatheringService;
43
44     public StatisticsContextImpl(final DeviceContext deviceContext) {
45         this.deviceContext = deviceContext;
46         statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
47
48     }
49
50     @Override
51     public ListenableFuture<Boolean> gatherDynamicData() {
52
53         final SettableFuture settableResultingFuture = SettableFuture.create();
54         ListenableFuture<Boolean> resultingFuture = settableResultingFuture;
55
56
57         if (ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
58             final DeviceState devState = deviceContext.getDeviceState();
59             ListenableFuture<Boolean> emptyFuture = Futures.immediateFuture(new Boolean(false));
60             final ListenableFuture<Boolean> flowStatistics = devState.isFlowStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPFLOW) : emptyFuture;
61
62             final ListenableFuture<Boolean> tableStatistics = devState.isTableStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPTABLE) : emptyFuture;
63
64             final ListenableFuture<Boolean> portStatistics = devState.isPortStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPPORTSTATS) : emptyFuture;
65
66             final ListenableFuture<Boolean> queueStatistics = devState.isQueueStatisticsAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPQUEUE) : emptyFuture;
67
68             final ListenableFuture<Boolean> groupDescStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUPDESC) : emptyFuture;
69             final ListenableFuture<Boolean> groupStatistics = devState.isGroupAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPGROUP) : emptyFuture;
70
71             final ListenableFuture<Boolean> meterConfigStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETERCONFIG) : emptyFuture;
72             final ListenableFuture<Boolean> meterStatistics = devState.isMetersAvailable() ? wrapLoggingOnStatisticsRequestCall(MultipartType.OFPMPMETER) : emptyFuture;
73
74
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>>() {
77                 @Override
78                 public void onSuccess(final List<Boolean> booleans) {
79                     boolean atLeastOneSuccess = false;
80                     for (Boolean bool : booleans) {
81                         atLeastOneSuccess |= bool.booleanValue();
82                     }
83                     settableResultingFuture.set(new Boolean(atLeastOneSuccess));
84                 }
85
86                 @Override
87                 public void onFailure(final Throwable throwable) {
88                     settableResultingFuture.setException(throwable);
89                 }
90             });
91         } else {
92             switch (deviceContext.getPrimaryConnectionContext().getConnectionState()) {
93                 case RIP:
94                     resultingFuture = Futures.immediateFailedFuture(new Throwable(String.format("Device connection doesn't exist anymore. Primary connection status : %s", deviceContext.getPrimaryConnectionContext().getConnectionState())));
95                     break;
96                 default:
97                     resultingFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
98                     break;
99             }
100
101
102         }
103         return resultingFuture;
104     }
105
106     private ListenableFuture<Boolean> wrapLoggingOnStatisticsRequestCall(final MultipartType type) {
107         final ListenableFuture<Boolean> future = StatisticsGatheringUtils.gatherStatistics(statisticsGatheringService, deviceContext, type);
108         Futures.addCallback(future, new FutureCallback() {
109             @Override
110             public void onSuccess(final Object o) {
111                 LOG.trace("Multipart response for {} was successful.", type);
112             }
113
114             @Override
115             public void onFailure(final Throwable throwable) {
116                 LOG.trace("Multipart response for {} FAILED.", type, throwable);
117             }
118         });
119         return future;
120     }
121
122     @Override
123     public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
124         requestContexts.remove(requestContexts);
125     }
126
127     @Override
128     public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
129         requestContexts.add(data);
130         return data.getFuture();
131     }
132
133     @Override
134     public <T> RequestContext<T> createRequestContext() {
135         return new RequestContextImpl<>(this);
136     }
137
138     @Override
139     public void close() throws Exception {
140         for (RequestContext requestContext : requestContexts) {
141             RequestContextUtil.closeRequestContextWithRpcError(requestContext, CONNECTION_CLOSED);
142         }
143     }
144 }