Move statistics request functions into trackers
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / FlowTableStatsTracker.java
1 /*
2  * Copyright IBM Corporation, 2013.  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 package org.opendaylight.controller.md.statistics.manager;
9
10 import java.util.Collections;
11 import java.util.Set;
12 import java.util.concurrent.ConcurrentSkipListSet;
13
14 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.GetFlowTablesStatisticsInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28
29 import com.google.common.base.Preconditions;
30 import com.google.common.util.concurrent.ListenableFuture;
31
32 final class FlowTableStatsTracker extends AbstractStatsTracker<FlowTableAndStatisticsMap, FlowTableAndStatisticsMap> {
33     private final Set<TableKey> privateTables = new ConcurrentSkipListSet<>();
34     private final Set<TableKey> tables = Collections.unmodifiableSet(privateTables);
35     private final OpendaylightFlowTableStatisticsService flowTableStatsService;
36
37     FlowTableStatsTracker(OpendaylightFlowTableStatisticsService flowTableStatsService, final FlowCapableContext context, long lifetimeNanos) {
38         super(context, lifetimeNanos);
39         this.flowTableStatsService = Preconditions.checkNotNull(flowTableStatsService);
40     }
41
42     Set<TableKey> getTables() {
43         return tables;
44     }
45
46     @Override
47     protected void cleanupSingleStat(DataModificationTransaction trans, FlowTableAndStatisticsMap item) {
48         // TODO: do we want to do this?
49     }
50
51     @Override
52     protected FlowTableAndStatisticsMap updateSingleStat(DataModificationTransaction trans, FlowTableAndStatisticsMap item) {
53
54         InstanceIdentifier<Table> tableRef = getNodeIdentifierBuilder()
55                 .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(item.getTableId().getValue())).build();
56
57         FlowTableStatisticsDataBuilder statisticsDataBuilder = new FlowTableStatisticsDataBuilder();
58         final FlowTableStatistics stats = new FlowTableStatisticsBuilder(item).build();
59         statisticsDataBuilder.setFlowTableStatistics(stats);
60
61         TableBuilder tableBuilder = new TableBuilder();
62         tableBuilder.setKey(new TableKey(item.getTableId().getValue()));
63         tableBuilder.addAugmentation(FlowTableStatisticsData.class, statisticsDataBuilder.build());
64         trans.putOperationalData(tableRef, tableBuilder.build());
65         return item;
66     }
67
68     public ListenableFuture<TransactionId> request() {
69         final GetFlowTablesStatisticsInputBuilder input = new GetFlowTablesStatisticsInputBuilder();
70         input.setNode(getNodeRef());
71
72         return requestHelper(flowTableStatsService.getFlowTablesStatistics(input.build()));
73     }
74 }