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