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
9 package org.opendaylight.mdsal.dom.spi.shard;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.annotations.Beta;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
19 import org.opendaylight.yangtools.concepts.Identifiable;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
25 public final class ForeignShardModificationContext implements Identifiable<DOMDataTreeIdentifier> {
26 private static final Logger LOG = LoggerFactory.getLogger(ForeignShardModificationContext.class);
28 private final DOMDataTreeIdentifier identifier;
29 private final DOMDataTreeShardProducer producer;
31 private DOMDataTreeShardWriteTransaction tx;
32 private DOMDataTreeWriteCursor cursor;
34 private volatile boolean ready = false;
36 public ForeignShardModificationContext(final DOMDataTreeIdentifier identifier,
37 final DOMDataTreeShardProducer producer) {
38 this.identifier = requireNonNull(identifier);
39 this.producer = requireNonNull(producer);
42 public DOMDataTreeWriteCursor getCursor() {
43 checkState(!ready, "Context %s has been readied", this);
47 tx = producer.createTransaction();
49 cursor = tx.createCursor(getIdentifier());
54 public boolean isModified() {
60 // Idempotent, but emit a debug
61 LOG.debug("Duplicate ready() of context {}", this);
72 // TODO: it would be nice if we could clear this reference
78 public DOMDataTreeIdentifier getIdentifier() {
82 public DOMDataTreeShardProducer getProducer() {
86 public ListenableFuture<Boolean> validate() {
90 public ListenableFuture<Void> prepare() {
94 public ListenableFuture<Void> submit() {
95 checkState(ready, "Modification context %s has to be ready before submit", this);
96 final ListenableFuture<Void> commit = tx.commit();
102 public void closeForeignTransaction() {
103 if (cursor != null) {