Merge "CHange log level from warn to debug in ProtocolSessionPromise when connection...
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / MeterStatsTracker.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 org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
11 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
12 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 final class MeterStatsTracker extends AbstractListeningStatsTracker<MeterStats, MeterStats> {
29     private static final Logger logger = LoggerFactory.getLogger(MeterStatsTracker.class);
30     private final OpendaylightMeterStatisticsService meterStatsService;
31
32     MeterStatsTracker(OpendaylightMeterStatisticsService meterStatsService, final FlowCapableContext context, long lifetimeNanos) {
33         super(context, lifetimeNanos);
34         this.meterStatsService = meterStatsService;
35     }
36
37     @Override
38     protected void cleanupSingleStat(DataModificationTransaction trans, MeterStats item) {
39         InstanceIdentifier<NodeMeterStatistics> meterRef = getNodeIdentifierBuilder()
40                             .augmentation(FlowCapableNode.class)
41                             .child(Meter.class,new MeterKey(item.getMeterId()))
42                             .augmentation(NodeMeterStatistics.class).build();
43         trans.removeOperationalData(meterRef);
44     }
45
46     @Override
47     protected MeterStats updateSingleStat(DataModificationTransaction trans, MeterStats item) {
48         MeterBuilder meterBuilder = new MeterBuilder();
49         MeterKey meterKey = new MeterKey(item.getMeterId());
50         meterBuilder.setKey(meterKey);
51
52         InstanceIdentifier<Meter> meterRef = getNodeIdentifierBuilder()
53                 .augmentation(FlowCapableNode.class).child(Meter.class,meterKey).build();
54
55         NodeMeterStatisticsBuilder meterStatsBuilder= new NodeMeterStatisticsBuilder();
56         meterStatsBuilder.setMeterStatistics(new MeterStatisticsBuilder(item).build());
57
58         //Update augmented data
59         meterBuilder.addAugmentation(NodeMeterStatistics.class, meterStatsBuilder.build());
60         trans.putOperationalData(meterRef, meterBuilder.build());
61         return item;
62     }
63
64     public void request() {
65         if (meterStatsService != null) {
66             GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder();
67             input.setNode(getNodeRef());
68
69             requestHelper(meterStatsService.getAllMeterStatistics(input.build()));
70         }
71     }
72
73     @Override
74     public void onDataChanged(DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
75         for (InstanceIdentifier<?> key : change.getCreatedConfigurationData().keySet()) {
76             if (Meter.class.equals(key.getTargetType())) {
77                 request();
78             }
79         }
80
81         final DataModificationTransaction trans = startTransaction();
82         for (InstanceIdentifier<?> key : change.getRemovedConfigurationData()) {
83             if (Meter.class.equals(key.getTargetType())) {
84                 @SuppressWarnings("unchecked")
85                 InstanceIdentifier<Meter> meter = (InstanceIdentifier<Meter>)key;
86
87                 InstanceIdentifier<?> nodeMeterStatisticsAugmentation =
88                         InstanceIdentifier.builder(meter).augmentation(NodeMeterStatistics.class).toInstance();
89                 trans.removeOperationalData(nodeMeterStatisticsAugmentation);
90             }
91         }
92         trans.commit();
93     }
94
95     @Override
96     protected InstanceIdentifier<?> listenPath() {
97         return getNodeIdentifierBuilder().augmentation(FlowCapableNode.class).child(Meter.class).build();
98     }
99
100     @Override
101     protected String statName() {
102         return "Meter";
103     }
104
105     @Override
106     public void start(final DataBrokerService dbs) {
107         if (meterStatsService == null) {
108             logger.debug("No Meter Statistics service, not subscribing to meters on node {}", getNodeIdentifier());
109             return;
110         }
111
112         super.start(dbs);
113     }
114 }