Fix modernization issues
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractClientHandle.java
1 /*
2  * Copyright (c) 2016 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.cluster.databroker.actors.dds;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
19 import java.util.function.Function;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.yangtools.concepts.Identifiable;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Abstract superclass of both ClientSnapshot and ClientTransaction. Provided for convenience.
29  *
30  * @author Robert Varga
31  */
32 @Beta
33 public abstract class AbstractClientHandle<T extends AbstractProxyTransaction> extends LocalAbortable
34         implements Identifiable<TransactionIdentifier> {
35     /*
36      * Our state consist of the the proxy map, hence we just subclass ConcurrentHashMap directly.
37      */
38     private static final class State<T> extends ConcurrentHashMap<Long, T> {
39         private static final long serialVersionUID = 1L;
40     }
41
42     private static final Logger LOG = LoggerFactory.getLogger(AbstractClientHandle.class);
43     @SuppressWarnings("rawtypes")
44     private static final AtomicReferenceFieldUpdater<AbstractClientHandle, State> STATE_UPDATER =
45             AtomicReferenceFieldUpdater.newUpdater(AbstractClientHandle.class, State.class, "state");
46
47     private final TransactionIdentifier transactionId;
48     private final AbstractClientHistory parent;
49
50     private volatile State<T> state = new State<>();
51
52     // Hidden to prevent outside instantiation
53     AbstractClientHandle(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
54         this.transactionId = requireNonNull(transactionId);
55         this.parent = requireNonNull(parent);
56     }
57
58     @Override
59     public TransactionIdentifier getIdentifier() {
60         return transactionId;
61     }
62
63     /**
64      * Release all state associated with this transaction.
65      *
66      * @return True if this transaction became closed during this call
67      */
68     public boolean abort() {
69         if (commonAbort()) {
70             parent.onTransactionAbort(this);
71             return true;
72         }
73
74         return false;
75     }
76
77     private boolean commonAbort() {
78         final Collection<T> toClose = ensureClosed();
79         if (toClose == null) {
80             return false;
81         }
82
83         toClose.forEach(AbstractProxyTransaction::abort);
84         return true;
85     }
86
87     @Override
88     final void localAbort(final Throwable cause) {
89         LOG.debug("Local abort of transaction {}", getIdentifier(), cause);
90         commonAbort();
91     }
92
93     /**
94      * Make sure this snapshot is closed. If it became closed as the effect of this call, return a collection of
95      * {@link AbstractProxyTransaction} handles which need to be closed, too.
96      *
97      * @return null if this snapshot has already been closed, otherwise a collection of proxies, which need to be
98      *         closed, too.
99      */
100     final @Nullable Collection<T> ensureClosed() {
101         @SuppressWarnings("unchecked")
102         final State<T> local = STATE_UPDATER.getAndSet(this, null);
103         return local == null ? null : local.values();
104     }
105
106     final T ensureProxy(final YangInstanceIdentifier path, final Function<Long, T> createProxy) {
107         final Map<Long, T> local = getState();
108         final Long shard = parent.resolveShardForPath(path);
109
110         return local.computeIfAbsent(shard, createProxy);
111     }
112
113     final AbstractClientHistory parent() {
114         return parent;
115     }
116
117     private State<T> getState() {
118         final State<T> local = state;
119         checkState(local != null, "Transaction %s is closed", transactionId);
120         return local;
121     }
122
123     @Override
124     public final String toString() {
125         return MoreObjects.toStringHelper(this).omitNullValues().add("identifier", transactionId).add("state", state)
126                 .toString();
127     }
128 }