2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.spi.shard;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
17 import org.opendaylight.yangtools.concepts.Identifiable;
18 import org.opendaylight.yangtools.concepts.Mutable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * A handle for a transaction being done on a different shard. This class is not thread-safe.
26 public final class ForeignShardModificationContext implements Identifiable<DOMDataTreeIdentifier>, Mutable {
27 private static final Logger LOG = LoggerFactory.getLogger(ForeignShardModificationContext.class);
29 private final DOMDataTreeIdentifier identifier;
30 private final DOMDataTreeShardProducer producer;
32 private DOMDataTreeShardWriteTransaction tx;
33 private DOMDataTreeWriteCursor cursor;
35 private volatile boolean ready = false;
37 public ForeignShardModificationContext(final DOMDataTreeIdentifier identifier,
38 final DOMDataTreeShardProducer producer) {
39 this.identifier = requireNonNull(identifier);
40 this.producer = requireNonNull(producer);
43 public DOMDataTreeWriteCursor getCursor() {
44 checkState(!ready, "Context %s has been readied", this);
48 tx = producer.createTransaction();
50 cursor = tx.createCursor(getIdentifier());
55 public boolean isModified() {
61 // Idempotent, but emit a debug
62 LOG.debug("Duplicate ready() of context {}", this);
73 // TODO: it would be nice if we could clear this reference
79 public DOMDataTreeIdentifier getIdentifier() {
83 public DOMDataTreeShardProducer getProducer() {
87 public ListenableFuture<Boolean> validate() {
91 public ListenableFuture<Void> prepare() {
95 public ListenableFuture<Void> submit() {
96 checkState(ready, "Modification context %s has to be ready before submit", this);
97 final ListenableFuture<Void> commit = tx.commit();
103 public void closeForeignTransaction() {
104 if (cursor != null) {