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

©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.