statistics context implements dedicated services
[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.List;
17 import java.util.concurrent.Future;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
21 import org.opendaylight.openflowplugin.impl.rpc.RequestContextImpl;
22 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowStatisticsServiceImpl;
23 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowTableStatisticsServiceImpl;
24 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightGroupStatisticsServiceImpl;
25 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightMeterStatisticsServiceImpl;
26 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightPortStatisticsServiceImpl;
27 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightQueueStatisticsServiceImpl;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.FlowStatisticsService;
43 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.FlowTableStatisticsService;
44 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.GroupStatisticsService;
45 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.MeterStatisticsService;
46 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.PortStatisticsService;
47 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.QueueStatisticsService;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import java.util.Arrays;
50
51 /**
52  * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
53  */
54 public class StatisticsContextImpl implements StatisticsContext {
55
56     private final List<RequestContext> requestContexts = new ArrayList();
57     private final DeviceContext deviceContext;
58
59
60     private final FlowStatisticsService flowStatisticsService;
61     private final FlowTableStatisticsService flowTableStatisticsService;
62     private final GroupStatisticsService groupStatisticsService;
63     private final MeterStatisticsService meterStatisticsService;
64     private final PortStatisticsService portStatisticsService;
65     private final QueueStatisticsService queueStatisticsService;
66
67
68     public StatisticsContextImpl(DeviceContext deviceContext) {
69         this.deviceContext = deviceContext;
70
71         flowStatisticsService = new FlowStatisticsService(this, deviceContext);
72         flowTableStatisticsService = new FlowTableStatisticsService(this, deviceContext);
73         groupStatisticsService = new GroupStatisticsService(this, deviceContext);
74         meterStatisticsService = new MeterStatisticsService(this, deviceContext);
75         portStatisticsService = new PortStatisticsService(this, deviceContext);
76         queueStatisticsService = new QueueStatisticsService(this, deviceContext);
77
78     }
79
80     private void pollFlowStatistics() {
81         final KeyedInstanceIdentifier<Node, NodeKey> nodeII = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
82         final NodeRef nodeRef = new NodeRef(nodeII);
83         final GetAllFlowsStatisticsFromAllFlowTablesInputBuilder builder =
84                 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder();
85         builder.setNode(nodeRef);
86         //TODO : process data from result
87     }
88
89     @Override
90     public ListenableFuture<Void> gatherDynamicData() {
91         ListenableFuture<Boolean> flowStatistics = StatisticsGatheringUtils.gatherFlowStatistics(flowStatisticsService, deviceContext);
92         ListenableFuture<Boolean> tableStatistics = StatisticsGatheringUtils.gatherTableStatistics(flowTableStatisticsService, deviceContext);
93         ListenableFuture<Boolean> groupStatistics = StatisticsGatheringUtils.gatherGroupStatistics(groupStatisticsService, deviceContext);
94         ListenableFuture<Boolean> meterStatistics = StatisticsGatheringUtils.gatherMeterStatistics(meterStatisticsService, deviceContext);
95         ListenableFuture<Boolean> portStatistics = StatisticsGatheringUtils.gatherPortStatistics(portStatisticsService, deviceContext);
96         ListenableFuture<Boolean> queueStatistics = StatisticsGatheringUtils.gatherQueueStatistics(queueStatisticsService, deviceContext);
97
98         ListenableFuture<List<Boolean>> allFutures = Futures.allAsList(Arrays.asList(flowStatistics, tableStatistics, groupStatistics, meterStatistics, portStatistics, queueStatistics));
99         final SettableFuture<Void> resultingFuture = SettableFuture.create();
100         Futures.addCallback(allFutures, new FutureCallback<List<Boolean>>() {
101             @Override
102             public void onSuccess(final List<Boolean> booleans) {
103                 resultingFuture.set(null);
104             }
105
106             @Override
107             public void onFailure(final Throwable throwable) {
108                 resultingFuture.setException(throwable);
109             }
110         });
111         return resultingFuture;
112     }
113
114     @Override
115     public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
116         requestContexts.remove(requestContexts);
117     }
118
119     @Override
120     public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> data) {
121         requestContexts.add(data);
122         return data.getFuture();
123     }
124
125     @Override
126     public <T> RequestContext<T> createRequestContext() {
127         return new RequestContextImpl<>(this);
128     }
129 }