Merge "BUG-2633 - Netconf northbound mapping."
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / AbstractDOMStoreTransaction.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 package org.opendaylight.controller.md.sal.dom.store.impl;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
14 import org.slf4j.Logger;
15
16 /**
17  * Abstract DOM Store Transaction
18  *
19  * Convenience super implementation of DOM Store transaction which provides
20  * common implementation of {@link #toString()} and {@link #getIdentifier()}.
21  */
22 abstract class AbstractDOMStoreTransaction implements DOMStoreTransaction {
23     private final Throwable debugContext;
24     private final Object identifier;
25
26     protected AbstractDOMStoreTransaction(final Object identifier, final boolean debug) {
27         this.identifier = Preconditions.checkNotNull(identifier, "Identifier must not be null.");
28         this.debugContext = debug ? new Throwable().fillInStackTrace() : null;
29     }
30
31     @Override
32     public final Object getIdentifier() {
33         return identifier;
34     }
35
36     protected final void warnDebugContext(final Logger logger) {
37         if (debugContext != null) {
38             logger.warn("Transaction {} has been allocated in the following context", identifier, debugContext);
39         }
40     }
41
42     @Override
43     public final String toString() {
44         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
45     }
46
47     /**
48      * Add class-specific toString attributes.
49      *
50      * @param toStringHelper
51      *            ToStringHelper instance
52      * @return ToStringHelper instance which was passed in
53      */
54     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
55         return toStringHelper.add("id", identifier);
56     }
57 }