adb58709865d7310dc41aeaf759d7695c50be7e7
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / impl / StatNodeRegistrationImpl.java
1 /**
2  * Copyright (c) 2014 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.controller.md.statistics.manager.impl;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
17 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.controller.md.statistics.manager.StatNodeRegistration;
21 import org.opendaylight.controller.md.statistics.manager.StatPermCollector.StatCapabTypes;
22 import org.opendaylight.controller.md.statistics.manager.StatisticsManager;
23 import org.opendaylight.controller.md.statistics.manager.StatisticsManager.StatDataStoreOperation;
24 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FeatureCapability;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityGroupStats;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityTableStats;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterFeatures;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.google.common.base.Optional;
47 import com.google.common.base.Preconditions;
48
49 /**
50  * statistics-manager
51  * org.opendaylight.controller.md.statistics.manager.impl
52  *
53  * StatNodeRegistrationImpl
54  * {@link FlowCapableNode} Registration Implementation contains two method for registration/unregistration
55  * {@link FeatureCapability} for every connect/disconnect {@link FlowCapableNode}. Process of connection/disconnection
56  * is substituted by listening Operation/DS for add/delete {@link FeatureCapability}.
57  * All statistic capabilities are reading from new Node directly without contacting device or DS.
58  *
59  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
60  *
61  * Created: Aug 28, 2014
62  */
63 public class StatNodeRegistrationImpl implements StatNodeRegistration {
64
65     private static final Logger LOG = LoggerFactory.getLogger(StatNodeRegistrationImpl.class);
66
67     private final StatisticsManager manager;
68     private ListenerRegistration<DataChangeListener> listenerRegistration;
69     private ListenerRegistration<?> notifListenerRegistration;
70
71     public StatNodeRegistrationImpl(final StatisticsManager manager, final DataBroker db,
72             final NotificationProviderService notificationService) {
73         this.manager = Preconditions.checkNotNull(manager, "StatisticManager can not be null!");
74         Preconditions.checkArgument(db != null, "DataBroker can not be null!");
75         Preconditions.checkArgument(notificationService != null, "NotificationProviderService can not be null!");
76         notifListenerRegistration = notificationService.registerNotificationListener(this);
77     }
78
79     @Override
80     public void close() throws Exception {
81
82         if (notifListenerRegistration != null) {
83             try {
84                 notifListenerRegistration.close();
85             }
86             catch (final Exception e) {
87                 LOG.warn("Error by stop FlowCapableNode Notification StatNodeRegistration.");
88             }
89             notifListenerRegistration = null;
90         }
91
92         if (listenerRegistration != null) {
93             try {
94                 listenerRegistration.close();
95             } catch (final Exception e) {
96                 LOG.warn("Error by stop FlowCapableNode DataChange StatListeningCommiter.", e);
97             }
98             listenerRegistration = null;
99         }
100     }
101
102     @Override
103     public void connectFlowCapableNode(final InstanceIdentifier<SwitchFeatures> keyIdent,
104             final SwitchFeatures data, final InstanceIdentifier<Node> nodeIdent) {
105         Preconditions.checkNotNull(keyIdent, "InstanceIdentifier can not be null!");
106         Preconditions.checkNotNull(data, "SwitchFeatures data for {} can not be null!", keyIdent);
107         Preconditions.checkArgument(( ! keyIdent.isWildcarded()), "InstanceIdentifier is WildCarded!");
108
109         manager.enqueue(new StatDataStoreOperation() {
110             @Override
111             public void applyOperation(final ReadWriteTransaction tx) {
112
113                 final List<StatCapabTypes> statCapabTypes = new ArrayList<>();
114                 Short maxCapTables = Short.valueOf("1");
115
116                 final List<Class<? extends FeatureCapability>> capabilities = data.getCapabilities() != null
117                         ? data.getCapabilities() : Collections.<Class<? extends FeatureCapability>> emptyList();
118                 for (final Class<? extends FeatureCapability> capability : capabilities) {
119                     if (capability == FlowFeatureCapabilityTableStats.class) {
120                         statCapabTypes.add(StatCapabTypes.TABLE_STATS);
121                     } else if (capability == FlowFeatureCapabilityFlowStats.class) {
122                         statCapabTypes.add(StatCapabTypes.FLOW_STATS);
123                     } else if (capability == FlowFeatureCapabilityGroupStats.class) {
124                         statCapabTypes.add(StatCapabTypes.GROUP_STATS);
125                     } else if (capability == FlowFeatureCapabilityPortStats.class) {
126                         statCapabTypes.add(StatCapabTypes.PORT_STATS);
127                     } else if (capability == FlowFeatureCapabilityQueueStats.class) {
128                         statCapabTypes.add(StatCapabTypes.QUEUE_STATS);
129                     }
130                 }
131                 maxCapTables = data.getMaxTables();
132
133                 final Optional<Short> maxTables = Optional.<Short> of(maxCapTables);
134
135                 /* Meters management */
136                 final InstanceIdentifier<NodeMeterFeatures> meterFeaturesIdent = nodeIdent.augmentation(NodeMeterFeatures.class);
137
138
139                 Optional<NodeMeterFeatures> meterFeatures = Optional.absent();
140                 try {
141                     meterFeatures = tx.read(LogicalDatastoreType.OPERATIONAL, meterFeaturesIdent).checkedGet();
142                 }
143                 catch (final ReadFailedException e) {
144                     LOG.warn("Read NodeMeterFeatures {} fail!", meterFeaturesIdent, e);
145                 }
146                 if (meterFeatures.isPresent()) {
147                     statCapabTypes.add(StatCapabTypes.METER_STATS);
148                 }
149                 manager.connectedNodeRegistration(nodeIdent,
150                         Collections.unmodifiableList(statCapabTypes), maxTables.get());
151             }
152         });
153     }
154
155     @Override
156     public void disconnectFlowCapableNode(final InstanceIdentifier<Node> nodeIdent) {
157         Preconditions.checkArgument(nodeIdent != null, "InstanceIdentifier can not be NULL!");
158         Preconditions.checkArgument(( ! nodeIdent.isWildcarded()),
159                 "InstanceIdentifier {} is WildCarded!", nodeIdent);
160         manager.enqueue(new StatDataStoreOperation() {
161             @Override
162             public void applyOperation(final ReadWriteTransaction tx) {
163                 manager.disconnectedNodeUnregistration(nodeIdent);
164             }
165         });
166     }
167
168
169     @Override
170     public void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
171         // NOOP
172     }
173
174     @Override
175     public void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
176         // NOOP
177     }
178
179     @Override
180     public void onNodeRemoved(final NodeRemoved notification) {
181         final NodeRef nodeRef = notification.getNodeRef();
182         final InstanceIdentifier<?> nodeRefIdent = nodeRef.getValue();
183         final InstanceIdentifier<Node> nodeIdent =
184                 nodeRefIdent.firstIdentifierOf(Node.class);
185         if (nodeIdent != null) {
186             disconnectFlowCapableNode(nodeIdent);
187         }
188     }
189
190     @Override
191     public void onNodeUpdated(final NodeUpdated notification) {
192         final FlowCapableNodeUpdated newFlowNode =
193                 notification.getAugmentation(FlowCapableNodeUpdated.class);
194         if (newFlowNode != null && newFlowNode.getSwitchFeatures() != null) {
195             final NodeRef nodeRef = notification.getNodeRef();
196             final InstanceIdentifier<?> nodeRefIdent = nodeRef.getValue();
197             final InstanceIdentifier<Node> nodeIdent =
198                     nodeRefIdent.firstIdentifierOf(Node.class);
199
200             final InstanceIdentifier<SwitchFeatures> swichFeaturesIdent =
201                     nodeIdent.augmentation(FlowCapableNode.class).child(SwitchFeatures.class);
202             final SwitchFeatures switchFeatures = newFlowNode.getSwitchFeatures();
203             connectFlowCapableNode(swichFeaturesIdent, switchFeatures, nodeIdent);
204         }
205     }
206 }
207

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.