/*
 * Copyright (c) 2016 Red Hat, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.md.sal.trace.dom.impl;

import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.FluentFuture;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
import org.opendaylight.mdsal.common.api.CommitInfo;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;

abstract class AbstractTracingWriteTransaction implements DOMDataWriteTransaction {

    private final DOMDataWriteTransaction delegate;
    private final TracingBroker tracingBroker;
    private final List<String> logs = new ArrayList<>();

    AbstractTracingWriteTransaction(DOMDataWriteTransaction delegate, TracingBroker tracingBroker) {
        this.delegate = Objects.requireNonNull(delegate);
        this.tracingBroker = Objects.requireNonNull(tracingBroker);
    }

    private void recordOp(LogicalDatastoreType store, YangInstanceIdentifier yiid, String method,
            NormalizedNode<?, ?> node) {
        if (!tracingBroker.isWriteWatched(yiid, store)) {
            return;
        }

        final Object value = node != null ? node.getValue() : null;

        if (value != null && value instanceof ImmutableSet && ((Set<?>)value).isEmpty()) {
            if (TracingBroker.LOG.isDebugEnabled()) {
                TracingBroker.LOG.debug("Empty data set write to {}", tracingBroker.toPathString(yiid));
            }
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append("Method \"").append(method);
            sb.append("\" to ").append(store);
            sb.append(" at ").append(tracingBroker.toPathString(yiid)).append('.');
            sb.append(" Data: ");
            if (node != null) {
                sb.append(node.getValue());
            } else {
                sb.append("null");
            }
            sb.append(" Stack:").append(tracingBroker.getStackSummary());
            synchronized (this) {
                logs.add(sb.toString());
            }
        }
    }

    private synchronized void logOps() {
        synchronized (this) {
            logs.forEach(log -> TracingBroker.LOG.warn(log));
            logs.clear();
        }
    }

    @Override
    public void put(LogicalDatastoreType store, YangInstanceIdentifier yiid, NormalizedNode<?, ?> node) {
        recordOp(store, yiid, "put", node);
        delegate.put(store, yiid, node);
    }

    @Override
    public void merge(LogicalDatastoreType store, YangInstanceIdentifier yiid, NormalizedNode<?, ?> node) {
        recordOp(store, yiid, "merge", node);
        delegate.merge(store, yiid, node);
    }

    @Override
    public boolean cancel() {
        synchronized (this) {
            logs.clear();
        }
        boolean result = delegate.cancel();
        return result;
    }

    @Override
    public void delete(LogicalDatastoreType store, YangInstanceIdentifier yiid) {
        recordOp(store, yiid, "delete", null);
        delegate.delete(store, yiid);
    }

    @Override
    public FluentFuture<? extends CommitInfo> commit() {
        logOps();
        return delegate.commit();
    }

    @Override
    public Object getIdentifier() {
        return delegate.getIdentifier();
    }

    // https://jira.opendaylight.org/browse/CONTROLLER-1792

    @Override
    public final boolean equals(Object object) {
        return object == this || delegate.equals(object);
    }

    @Override
    public final int hashCode() {
        return delegate.hashCode();
    }

    @Override
    public final String toString() {
        return getClass().getName() + "; delegate=" + delegate;
    }
}

@@ -552,24 +552,57 @@
 a.util.Set;%0A
+import javax.annotation.Nonnull;%0A
 import org.o
@@ -669,16 +669,16 @@
 reType;%0A
-
 import o
@@ -1410,16 +1410,67 @@
 roker);%0A
+        recordOp(null, null, %22instantiate%22, null);%0A
     %7D%0A%0A 
@@ -2147,18 +2147,67 @@
 (method)
+.append('%22')
 ;%0A
+            if (store != null) %7B%0A    
         
@@ -2221,18 +2221,16 @@
 append(%22
-%5C%22
  to %22).a
@@ -2239,24 +2239,74 @@
 end(store);%0A
+            %7D%0A            if (yiid != null) %7B%0A    
             
@@ -2367,22 +2367,159 @@
 id))
-.append('.');%0A
+;%0A            %7D%0A            sb.append('.');%0A            if (yiid != null) %7B%0A                // If we don%E2%80%99t have an id, we don%E2%80%99t expect data either%0A    
     
@@ -2556,24 +2556,28 @@
             
+    
 if (node != 
@@ -2576,32 +2576,36 @@
 node != null) %7B%0A
+    
                 
@@ -2636,32 +2636,36 @@
 ));%0A            
+ 
+   
 %7D else %7B%0A       
@@ -2649,32 +2649,36 @@
        %7D else %7B%0A
+    
                 
@@ -2692,24 +2692,42 @@
 nd(%22null%22);%0A
+                %7D%0A
             
@@ -2995,57 +2995,227 @@
     
-logs.forEach(log -%3E TracingBroker.LOG.warn(log));
+if (TracingBroker.LOG.isWarnEnabled()) %7B%0A                TracingBroker.LOG.warn(%22Transaction %7B%7D contains the following operations:%22, getIdentifier());%0A                logs.forEach(TracingBroker.LOG::warn);%0A            %7D
 %0A   
@@ -3806,24 +3806,14 @@
     
-boolean result =
+return
  del
@@ -3832,31 +3832,8 @@
 ();%0A
-        return result;%0A
     
@@ -4090,24 +4090,70 @@
  commit() %7B%0A
+        recordOp(null, null, %22commit%22, null);%0A
         logO
@@ -4191,32 +4191,32 @@
 ommit();%0A    %7D%0A%0A
-
     @Override%0A  
@@ -4205,32 +4205,45 @@
 %0A%0A    @Override%0A
+    @Nonnull%0A
     public Objec

Add transaction level traces