2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.md.statistics.manager.impl;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
20 import org.opendaylight.controller.md.statistics.manager.StatisticsManager;
21 import org.opendaylight.controller.md.statistics.manager.StatisticsManager.StatDataStoreOperation;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterFeaturesUpdated;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStats;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterConfigStatsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeatures;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.MeterFeaturesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterConfigStatsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import com.google.common.base.Optional;
56 * org.opendaylight.controller.md.statistics.manager.impl
58 * StatListenCommitMeter
59 * Class is a NotifyListener for MeterStatistics and DataChangeListener for Config/DataStore for Meter node.
60 * All expected (registered) MeterStatistics will be builded and commit to Operational/DataStore.
61 * DataChangeEven should call create/delete Meter in Operational/DS
64 public class StatListenCommitMeter extends StatAbstractListenCommit<Meter, OpendaylightMeterStatisticsListener>
65 implements OpendaylightMeterStatisticsListener {
67 private static final Logger LOG = LoggerFactory.getLogger(StatListenCommitMeter.class);
69 public StatListenCommitMeter(final StatisticsManager manager, final DataBroker db,
70 final NotificationProviderService nps) {
71 super(manager, db, nps, Meter.class);
75 protected InstanceIdentifier<Meter> getWildCardedRegistrationPath() {
76 return InstanceIdentifier.create(Nodes.class).child(Node.class)
77 .augmentation(FlowCapableNode.class).child(Meter.class);
81 protected OpendaylightMeterStatisticsListener getStatNotificationListener() {
86 public void onMeterConfigStatsUpdated(final MeterConfigStatsUpdated notification) {
87 final TransactionId transId = notification.getTransactionId();
88 final NodeId nodeId = notification.getId();
89 if ( ! isExpectedStatistics(transId, nodeId)) {
90 LOG.debug("STAT-MANAGER - MeterConfigStatsUpdated: unregistred notification detect TransactionId {}", transId);
93 if (notification.isMoreReplies()) {
94 manager.getRpcMsgManager().addNotification(notification, nodeId);
97 final List<MeterConfigStats> meterConfStat = notification.getMeterConfigStats() != null
98 ? new ArrayList<>(notification.getMeterConfigStats()) : new ArrayList<MeterConfigStats>(10);
99 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
100 if (txContainer.isPresent()) {
101 final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
102 for (final TransactionAware notif : cacheNotifs) {
103 if (notif instanceof MeterConfigStatsUpdated) {
104 meterConfStat.addAll(((MeterConfigStatsUpdated) notif).getMeterConfigStats());
108 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
109 manager.enqueue(new StatDataStoreOperation() {
111 public void applyOperation(final ReadWriteTransaction tx) {
112 /* Notification for continue collecting statistics */
113 notifyToCollectNextStatistics(nodeIdent);
114 comitConfMeterStats(meterConfStat, nodeIdent, tx);
120 public void onMeterFeaturesUpdated(final MeterFeaturesUpdated notification) {
121 final TransactionId transId = notification.getTransactionId();
122 final NodeId nodeId = notification.getId();
123 if ( ! isExpectedStatistics(transId, nodeId)) {
124 LOG.debug("STAT-MANAGER - MeterFeaturesUpdated: unregistred notification detect TransactionId {}", transId);
127 if (notification.isMoreReplies()) {
128 manager.getRpcMsgManager().addNotification(notification, nodeId);
131 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
132 if ( ! txContainer.isPresent()) {
135 final MeterFeatures stats = new MeterFeaturesBuilder(notification).build();
136 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
137 .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
138 final InstanceIdentifier<MeterFeatures> meterFeatureIdent = nodeIdent
139 .augmentation(NodeMeterFeatures.class).child(MeterFeatures.class);
141 manager.enqueue(new StatDataStoreOperation() {
143 public void applyOperation(final ReadWriteTransaction tx) {
144 /* Notification for continue collecting statistics */
145 notifyToCollectNextStatistics(nodeIdent);
146 Optional<Node> node = Optional.absent();
148 node = tx.read(LogicalDatastoreType.OPERATIONAL, nodeIdent).checkedGet();
150 catch (final ReadFailedException e) {
151 LOG.debug("Read Operational/DS for Node fail! {}", nodeIdent, e);
153 if (node.isPresent()) {
154 tx.put(LogicalDatastoreType.OPERATIONAL, meterFeatureIdent, stats);
161 public void onMeterStatisticsUpdated(final MeterStatisticsUpdated notification) {
162 final TransactionId transId = notification.getTransactionId();
163 final NodeId nodeId = notification.getId();
164 if ( ! isExpectedStatistics(transId, nodeId)) {
165 LOG.debug("STAT-MANAGER - MeterStatisticsUpdated: unregistred notification detect TransactionId {}", transId);
168 if (notification.isMoreReplies()) {
169 manager.getRpcMsgManager().addNotification(notification, nodeId);
172 final List<MeterStats> meterStat = notification.getMeterStats() != null
173 ? new ArrayList<>(notification.getMeterStats()) : new ArrayList<MeterStats>(10);
174 final Optional<TransactionCacheContainer<?>> txContainer = getTransactionCacheContainer(transId, nodeId);
175 if (txContainer.isPresent()) {
176 final List<? extends TransactionAware> cacheNotifs = txContainer.get().getNotifications();
177 for (final TransactionAware notif : cacheNotifs) {
178 if (notif instanceof MeterConfigStatsUpdated) {
179 meterStat.addAll(((MeterStatisticsUpdated) notif).getMeterStats());
183 final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
184 manager.enqueue(new StatDataStoreOperation() {
186 public void applyOperation(final ReadWriteTransaction tx) {
187 statMeterCommit(meterStat, nodeIdent, tx);
188 /* Notification for continue collecting statistics */
189 notifyToCollectNextStatistics(nodeIdent);
194 private void statMeterCommit(final List<MeterStats> meterStats,
195 final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction trans) {
197 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
198 for (final MeterStats mStat : meterStats) {
199 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
201 final MeterKey mKey = new MeterKey(mStat.getMeterId());
202 final InstanceIdentifier<MeterStatistics> msIdent = fNodeIdent
203 .child(Meter.class, mKey).augmentation(NodeMeterStatistics.class)
204 .child(MeterStatistics.class);
205 /* Meter Statistics commit */
206 Optional<FlowCapableNode> fNode = Optional.absent();
208 fNode = trans.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
210 catch (final ReadFailedException e) {
211 LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
213 if (fNode.isPresent()) {
214 trans.put(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
219 private void comitConfMeterStats(final List<MeterConfigStats> meterConfStat,
220 final InstanceIdentifier<Node> nodeIdent, final ReadWriteTransaction trans) {
222 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
223 final List<MeterKey> deviceMeterKeys = new ArrayList<>();
225 for (final MeterConfigStats meterConf : meterConfStat) {
226 final MeterBuilder meterBuilder = new MeterBuilder(meterConf);
227 if (meterConf.getMeterId() != null) {
228 final MeterKey meterKey = new MeterKey(meterConf.getMeterId());
229 meterBuilder.setKey(meterKey);
230 final InstanceIdentifier<Meter> meterRef = nodeIdent
231 .augmentation(FlowCapableNode.class).child(Meter.class,meterKey);
232 final NodeMeterConfigStatsBuilder meterConfig = new NodeMeterConfigStatsBuilder();
233 meterConfig.setMeterConfigStats(new MeterConfigStatsBuilder(meterConf).build());
234 //Update augmented data
235 meterBuilder.addAugmentation(NodeMeterConfigStats.class, meterConfig.build());
236 deviceMeterKeys.add(meterKey);
237 Optional<FlowCapableNode> fNode = Optional.absent();
239 fNode = trans.read(LogicalDatastoreType.OPERATIONAL, fNodeIdent).checkedGet();
241 catch (final ReadFailedException e) {
242 LOG.debug("Read Operational/DS for FlowCapableNode fail! {}", fNodeIdent, e);
244 if (fNode.isPresent()) {
245 trans.put(LogicalDatastoreType.OPERATIONAL, meterRef, meterBuilder.build());
249 /* Delete all not presented Meter Nodes */
250 deleteAllNotPresentedNodes(fNodeIdent, trans, deviceMeterKeys);
253 private void deleteAllNotPresentedNodes(final InstanceIdentifier<FlowCapableNode> fNodeIdent,
254 final ReadWriteTransaction trans, final List<MeterKey> deviceMeterKeys) {
255 /* Delete all not presented meters */
256 final Optional<FlowCapableNode> fNode = readLatestConfiguration(fNodeIdent);
258 if ( ! fNode.isPresent()) {
259 LOG.trace("Read Operational/DS for FlowCapableNode fail! Node {} doesn't exist.", fNodeIdent);
262 final List<Meter> existMeters = fNode.get().getMeter() != null
263 ? fNode.get().getMeter() : Collections.<Meter> emptyList();
264 /* Add all existed groups paths - no updated paths has to be removed */
265 for (final Meter meter : existMeters) {
266 if (deviceMeterKeys.remove(meter.getKey())) {
267 break; // Meter still exist on device
269 final InstanceIdentifier<Meter> delMeterIdent = fNodeIdent.child(Meter.class, meter.getKey());
270 Optional<Meter> delMeter = Optional.absent();
272 delMeter = trans.read(LogicalDatastoreType.OPERATIONAL, delMeterIdent).checkedGet();
274 catch (final ReadFailedException e) {
275 // NOOP - probably another transaction delete that node
277 if (delMeter.isPresent()) {
278 trans.delete(LogicalDatastoreType.OPERATIONAL, delMeterIdent);