fd5ba565bb72382b7b290a6c3a2d62b4a55fe67e
[controller.git] / opendaylight / md-sal / mdsal-trace / dom-impl / src / main / java / org / opendaylight / controller / md / sal / trace / dom / impl / AbstractTracingWriteTransaction.java
1 /*
2  * Copyright (c) 2016 Red Hat, 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 package org.opendaylight.controller.md.sal.trace.dom.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22
23 @Deprecated
24 abstract class AbstractTracingWriteTransaction implements DOMDataWriteTransaction {
25
26     private final DOMDataWriteTransaction delegate;
27     private final TracingBroker tracingBroker;
28     private final List<String> logs = new ArrayList<>();
29
30     AbstractTracingWriteTransaction(DOMDataWriteTransaction delegate, TracingBroker tracingBroker) {
31         this.delegate = requireNonNull(delegate);
32         this.tracingBroker = requireNonNull(tracingBroker);
33         recordOp(null, null, "instantiate", null);
34     }
35
36     private void recordOp(LogicalDatastoreType store, YangInstanceIdentifier yiid, String method,
37             NormalizedNode<?, ?> node) {
38         if (yiid != null && !tracingBroker.isWriteWatched(yiid, store)) {
39             return;
40         }
41
42         final Object value = node != null ? node.getValue() : null;
43
44         if (value != null && value instanceof ImmutableSet && ((Set<?>)value).isEmpty()) {
45             if (TracingBroker.LOG.isDebugEnabled()) {
46                 TracingBroker.LOG.debug("Empty data set write to {}", tracingBroker.toPathString(yiid));
47             }
48         } else {
49             StringBuilder sb = new StringBuilder();
50             sb.append("Method \"").append(method).append('"');
51             if (store != null) {
52                 sb.append(" to ").append(store);
53             }
54             if (yiid != null) {
55                 sb.append(" at ").append(tracingBroker.toPathString(yiid));
56             }
57             sb.append('.');
58             if (yiid != null) {
59                 // If we don’t have an id, we don’t expect data either
60                 sb.append(" Data: ");
61                 if (node != null) {
62                     sb.append(node.getValue());
63                 } else {
64                     sb.append("null");
65                 }
66             }
67             sb.append(" Stack:").append(tracingBroker.getStackSummary());
68             synchronized (this) {
69                 logs.add(sb.toString());
70             }
71         }
72     }
73
74     private synchronized void logOps() {
75         synchronized (this) {
76             if (TracingBroker.LOG.isWarnEnabled()) {
77                 TracingBroker.LOG.warn("Transaction {} contains the following operations:", getIdentifier());
78                 logs.forEach(TracingBroker.LOG::warn);
79             }
80             logs.clear();
81         }
82     }
83
84     @Override
85     public void put(LogicalDatastoreType store, YangInstanceIdentifier yiid, NormalizedNode<?, ?> node) {
86         recordOp(store, yiid, "put", node);
87         delegate.put(store, yiid, node);
88     }
89
90     @Override
91     public void merge(LogicalDatastoreType store, YangInstanceIdentifier yiid, NormalizedNode<?, ?> node) {
92         recordOp(store, yiid, "merge", node);
93         delegate.merge(store, yiid, node);
94     }
95
96     @Override
97     public boolean cancel() {
98         synchronized (this) {
99             logs.clear();
100         }
101         return delegate.cancel();
102     }
103
104     @Override
105     public void delete(LogicalDatastoreType store, YangInstanceIdentifier yiid) {
106         recordOp(store, yiid, "delete", null);
107         delegate.delete(store, yiid);
108     }
109
110     @Override
111     public FluentFuture<? extends CommitInfo> commit() {
112         recordOp(null, null, "commit", null);
113         logOps();
114         return delegate.commit();
115     }
116
117     @Override
118     public Object getIdentifier() {
119         return delegate.getIdentifier();
120     }
121
122     // https://jira.opendaylight.org/browse/CONTROLLER-1792
123
124     @Override
125     public final boolean equals(Object object) {
126         return object == this || delegate.equals(object);
127     }
128
129     @Override
130     public final int hashCode() {
131         return delegate.hashCode();
132     }
133
134     @Override
135     public final String toString() {
136         return getClass().getName() + "; delegate=" + delegate;
137     }
138 }

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