2 * Copyright (c) 2016 Red Hat, 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
8 package org.opendaylight.controller.md.sal.trace.dom.impl;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
14 import java.util.Objects;
15 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
30 import org.opendaylight.controller.md.sal.trace.api.TracingDOMDataBroker;
31 import org.opendaylight.controller.md.sal.trace.closetracker.impl.CloseTrackedRegistry;
32 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.Config;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 @SuppressWarnings("checkstyle:JavadocStyle")
42 //...because otherwise it whines about the elements in the @code block even though it's completely valid Javadoc
45 * TracingBroker logs "write" operations and listener registrations to the md-sal. It logs the instance identifier path,
46 * the objects themselves, as well as the stack trace of the call invoking the registration or write operation.
47 * It works by operating as a "bump on the stack" between the application and actual DataBroker, intercepting write
48 * and registration calls and writing to the log.
50 * TracingBroker is designed to be easy to use. In fact, for bundles using Blueprint to inject their DataBroker
51 * TracingBroker can be used without modifying your code at all in two simple steps:
54 * Simply add the dependency "mdsaltrace-features" to
59 * <groupId>org.opendaylight.controller</groupId>
60 * <artifactId>mdsal-trace-features</artifactId>
61 * <classifier>features</classifier>
63 * <scope>runtime</scope>
64 * <version>0.1.6-SNAPSHOT</version>
70 * Then just load the odl-mdsal-trace feature before your feature and you're done.
73 * This works because the mdsaltrace-impl bundle registers its service implementing DOMDataBroker with a higher
74 * rank than sal-binding-broker. As such, any OSGi service lookup for DataBroker will receive the TracingBroker.
76 * <h1>Avoiding log bloat:</h1>
77 * TracingBroker can be configured to only print registrations or write ops pertaining to certain subtrees of the
78 * md-sal. This can be done in the code via the methods of this class or via a config file. TracingBroker uses a more
79 * convenient but non-standard representation of the instance identifiers. Each instance identifier segment's
80 * class.getSimpleName() is used separated by a '/'.
82 * <h1>Known issues</h1>
85 * Filtering by paths. For some registrations the codec that converts back from the DOM to binding paths is
86 * busted. As such, an aproximated path is used in the output. For now it is recommended not to use
87 * watchRegistrations and allow all registrations to be logged.
92 public class TracingBroker implements TracingDOMDataBroker {
94 static final Logger LOG = LoggerFactory.getLogger(TracingBroker.class);
96 private static final int STACK_TRACE_FIRST_RELEVANT_FRAME = 2;
98 private final BindingNormalizedNodeSerializer codec;
99 private final DOMDataBroker delegate;
100 private final List<Watch> registrationWatches = new ArrayList<>();
101 private final List<Watch> writeWatches = new ArrayList<>();
103 private final boolean isDebugging;
104 private final CloseTrackedRegistry<TracingTransactionChain> transactionChainsRegistry;
105 private final CloseTrackedRegistry<TracingReadOnlyTransaction> readOnlyTransactionsRegistry;
106 private final CloseTrackedRegistry<TracingWriteTransaction> writeTransactionsRegistry;
107 private final CloseTrackedRegistry<TracingReadWriteTransaction> readWriteTransactionsRegistry;
109 private class Watch {
110 final String iidString;
111 final LogicalDatastoreType store;
113 Watch(String iidString, LogicalDatastoreType storeOrNull) {
114 this.store = storeOrNull;
115 this.iidString = iidString;
118 private String toIidCompString(YangInstanceIdentifier iid) {
119 StringBuilder builder = new StringBuilder();
120 toPathString(iid, builder);
122 return builder.toString();
125 private boolean isParent(String parent, String child) {
126 int parentOffset = 0;
127 if (parent.length() > 0 && parent.charAt(0) == '<') {
128 parentOffset = parent.indexOf('>') + 1;
132 if (child.length() > 0 && child.charAt(0) == '<') {
133 childOffset = child.indexOf('>') + 1;
136 return child.startsWith(parent.substring(parentOffset), childOffset);
139 public boolean subtreesOverlap(YangInstanceIdentifier iid, LogicalDatastoreType store,
140 AsyncDataBroker.DataChangeScope scope) {
141 if (this.store != null && !this.store.equals(store)) {
145 String otherIidString = toIidCompString(iid);
148 return isParent(iidString, otherIidString);
149 case ONE: //for now just treat like SUBTREE, even though it's not
151 return isParent(iidString, otherIidString) || isParent(otherIidString, iidString);
157 public boolean eventIsOfInterest(YangInstanceIdentifier iid, LogicalDatastoreType store) {
158 if (this.store != null && !this.store.equals(store)) {
162 return isParent(iidString, toPathString(iid));
166 public TracingBroker(DOMDataBroker delegate, Config config, BindingNormalizedNodeSerializer codec) {
167 this.delegate = Objects.requireNonNull(delegate);
168 this.codec = Objects.requireNonNull(codec);
171 if (config.isTransactionDebugContextEnabled() != null) {
172 this.isDebugging = config.isTransactionDebugContextEnabled();
174 this.isDebugging = false;
176 this.transactionChainsRegistry = new CloseTrackedRegistry<>(this, "createTransactionChain", isDebugging);
177 this.readOnlyTransactionsRegistry = new CloseTrackedRegistry<>(this, "newReadOnlyTransaction", isDebugging);
178 this.writeTransactionsRegistry = new CloseTrackedRegistry<>(this, "newWriteOnlyTransaction", isDebugging);
179 this.readWriteTransactionsRegistry = new CloseTrackedRegistry<>(this, "newReadWriteTransaction", isDebugging);
182 private void configure(Config config) {
183 registrationWatches.clear();
184 List<String> paths = config.getRegistrationWatches();
186 for (String path : paths) {
187 watchRegistrations(path, null);
191 writeWatches.clear();
192 paths = config.getWriteWatches();
194 for (String path : paths) {
195 watchWrites(path, null);
201 * Log registrations to this subtree of the md-sal.
202 * @param iidString the iid path of the root of the subtree
203 * @param store Which LogicalDataStore? or null for both
205 public void watchRegistrations(String iidString, LogicalDatastoreType store) {
206 LOG.info("Watching registrations to {} in {}", iidString, store);
207 registrationWatches.add(new Watch(iidString, store));
211 * Log writes to this subtree of the md-sal.
212 * @param iidString the iid path of the root of the subtree
213 * @param store Which LogicalDataStore? or null for both
215 public void watchWrites(String iidString, LogicalDatastoreType store) {
216 LOG.info("Watching writes to {} in {}", iidString, store);
217 Watch watch = new Watch(iidString, store);
218 writeWatches.add(watch);
221 private boolean isRegistrationWatched(YangInstanceIdentifier iid,
222 LogicalDatastoreType store, DataChangeScope scope) {
223 if (registrationWatches.isEmpty()) {
227 for (Watch regInterest : registrationWatches) {
228 if (regInterest.subtreesOverlap(iid, store, scope)) {
236 boolean isWriteWatched(YangInstanceIdentifier iid, LogicalDatastoreType store) {
237 if (writeWatches.isEmpty()) {
241 for (Watch watch : writeWatches) {
242 if (watch.eventIsOfInterest(iid, store)) {
250 static void toPathString(InstanceIdentifier<? extends DataObject> iid, StringBuilder builder) {
251 for (InstanceIdentifier.PathArgument pathArg : iid.getPathArguments()) {
252 builder.append('/').append(pathArg.getType().getSimpleName());
256 String toPathString(YangInstanceIdentifier yiid) {
257 StringBuilder sb = new StringBuilder();
258 toPathString(yiid, sb);
259 return sb.toString();
263 private void toPathString(YangInstanceIdentifier yiid, StringBuilder sb) {
264 InstanceIdentifier<?> iid = codec.fromYangInstanceIdentifier(yiid);
266 reconstructIidPathString(yiid, sb);
268 toPathString(iid, sb);
272 private void reconstructIidPathString(YangInstanceIdentifier yiid, StringBuilder sb) {
273 sb.append("<RECONSTRUCTED FROM: \"").append(yiid.toString()).append("\">");
274 for (YangInstanceIdentifier.PathArgument pathArg : yiid.getPathArguments()) {
275 if (pathArg instanceof YangInstanceIdentifier.AugmentationIdentifier) {
276 sb.append('/').append("AUGMENTATION");
279 sb.append('/').append(pathArg.getNodeType().getLocalName());
283 String getStackSummary() {
284 StackTraceElement[] stack = Thread.currentThread().getStackTrace();
286 StringBuilder sb = new StringBuilder();
287 for (int i = STACK_TRACE_FIRST_RELEVANT_FRAME; i < stack.length; i++) {
288 StackTraceElement frame = stack[i];
289 sb.append("\n\t(TracingBroker)\t").append(frame.getClassName()).append('.').append(frame.getMethodName());
292 return sb.toString();
296 public DOMDataReadWriteTransaction newReadWriteTransaction() {
297 return new TracingReadWriteTransaction(delegate.newReadWriteTransaction(), this, readWriteTransactionsRegistry);
301 public DOMDataWriteTransaction newWriteOnlyTransaction() {
302 return new TracingWriteTransaction(delegate.newWriteOnlyTransaction(), this, writeTransactionsRegistry);
306 public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(
307 LogicalDatastoreType store, YangInstanceIdentifier yiid,
308 DOMDataChangeListener listener, DataChangeScope scope) {
309 if (isRegistrationWatched(yiid, store, scope)) {
310 LOG.warn("Registration (registerDataChangeListener) for {} from {}",
311 toPathString(yiid), getStackSummary());
313 return delegate.registerDataChangeListener(store, yiid, listener, scope);
317 public DOMTransactionChain createTransactionChain(TransactionChainListener transactionChainListener) {
318 return new TracingTransactionChain(
319 delegate.createTransactionChain(transactionChainListener), this, transactionChainsRegistry);
323 public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
324 return new TracingReadOnlyTransaction(delegate.newReadOnlyTransaction(), this, readOnlyTransactionsRegistry);
329 public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
330 Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> res = delegate.getSupportedExtensions();
331 DOMDataTreeChangeService treeChangeSvc = (DOMDataTreeChangeService) res.get(DOMDataTreeChangeService.class);
332 if (treeChangeSvc == null) {
336 res = new HashMap<>(res);
338 res.put(DOMDataTreeChangeService.class, new DOMDataTreeChangeService() {
341 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerDataTreeChangeListener(
342 @Nonnull DOMDataTreeIdentifier domDataTreeIdentifier, @Nonnull L listener) {
343 if (isRegistrationWatched(domDataTreeIdentifier.getRootIdentifier(),
344 domDataTreeIdentifier.getDatastoreType(), DataChangeScope.SUBTREE)) {
345 LOG.warn("Registration (registerDataTreeChangeListener) for {} from {}",
346 toPathString(domDataTreeIdentifier.getRootIdentifier()), getStackSummary());
348 return treeChangeSvc.registerDataTreeChangeListener(domDataTreeIdentifier, listener);