DOMDataTree APIs 98/14998/12
authorRobert Varga <rovarga@cisco.com>
Thu, 5 Feb 2015 17:30:28 +0000 (18:30 +0100)
committerRobert Varga <rovarga@cisco.com>
Mon, 23 Feb 2015 09:51:23 +0000 (10:51 +0100)
This patch introduces the end-user visible API model for interacting
with the conceptual data tree. Unlike our previous APIs, these revolve
around applications explicitly binding their inputs (former data change
listeners) and outputs (former explicit writes). This allows efficient
inter-datastore interactions to be maintained by understanding what
sort of output applications make and what sort of indexes are required
to feed them the data they require. Beryllium evolution will see the
DOMDataTreeListener being subscribed to an XQuery expression, thus
providing a dense definition of what data the applciation actually
extracts out of the subtree.

Change-Id: I11446ce7f8dbd7cf61540f72e463460b483ff15f
Signed-off-by: Robert Varga <rovarga@cisco.com>
14 files changed:
opendaylight/md-sal/sal-dom-api/pom.xml
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeInaccessibleException.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeListener.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeListeningException.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeLoopException.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducer.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducerBusyException.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducerException.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducerFactory.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeService.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeShard.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeShardingConflictException.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeShardingService.java [new file with mode: 0644]
opendaylight/md-sal/sal-dom-api/src/test/java/org/opendaylight/controller/md/sal/dom/api/AbstractDOMDataTreeServiceTestSuite.java [new file with mode: 0644]

index 0302a7d920bc248a2566569dbaa7680f286df1fc..89fca50354a04f44983b2e5e8126209a3aae4a34 100644 (file)
       <groupId>org.osgi</groupId>
       <artifactId>org.osgi.core</artifactId>
     </dependency>
       <groupId>org.osgi</groupId>
       <artifactId>org.osgi.core</artifactId>
     </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.controller</groupId>
+      <artifactId>sal-test-model</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>yang-data-impl</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
   </dependencies>
   <build>
     <plugins>
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeInaccessibleException.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeInaccessibleException.java
new file mode 100644 (file)
index 0000000..eea1be5
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import com.google.common.base.Preconditions;
+
+
+/**
+ * Failure reported when a data tree is no longer accessible.
+ */
+public class DOMDataTreeInaccessibleException extends DOMDataTreeListeningException {
+    private static final long serialVersionUID = 1L;
+    private final DOMDataTreeIdentifier treeIdentifier;
+
+    public DOMDataTreeInaccessibleException(final DOMDataTreeIdentifier treeIdentifier, final String message) {
+        super(message);
+        this.treeIdentifier = Preconditions.checkNotNull(treeIdentifier);
+    }
+
+    public DOMDataTreeInaccessibleException(final DOMDataTreeIdentifier treeIdentifier, final String message, final Throwable cause) {
+        super(message);
+        this.treeIdentifier = Preconditions.checkNotNull(treeIdentifier);
+    }
+
+    public final DOMDataTreeIdentifier getTreeIdentifier() {
+        return treeIdentifier;
+    }
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeListener.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeListener.java
new file mode 100644 (file)
index 0000000..083cd10
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import java.util.Collection;
+import java.util.EventListener;
+import java.util.Map;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
+
+/**
+ * Interface implemented by data consumers, e.g. processes wanting to act on data
+ * after it has been introduced to the conceptual data tree.
+ */
+public interface DOMDataTreeListener extends EventListener {
+    /**
+     * Invoked whenever one or more registered subtrees change. The logical changes are reported,
+     * as well as the roll up of new state for all subscribed subtrees.
+     *
+     * @param changes The set of changes being reported. Each subscribed subtree may be present
+     *                at most once.
+     * @param subtrees Per-subtree state as visible after the reported changes have been applied.
+     *                 This includes all the subtrees this listener is subscribed to, even those
+     *                 which have not changed.
+     */
+    void onDataTreeChanged(@Nonnull Collection<DataTreeCandidate> changes, @Nonnull Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtrees);
+
+    /**
+     * Invoked when a subtree listening failure occurs. This can be triggered, for example, when
+     * a connection to external subtree source is broken. The listener will not receive any other
+     * callbacks, but its registration still needs to be closed to prevent resource leak.
+     *
+     * @param cause Collection of failure causes, may not be null or empty.
+     */
+    void onDataTreeFailed(@Nonnull Collection<DOMDataTreeListeningException> causes);
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeListeningException.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeListeningException.java
new file mode 100644 (file)
index 0000000..e0d9b42
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+/**
+ * Base exception for various causes why and {@link DOMDataTreeListener}
+ * may be terminated by the {@link DOMDataTreeService} implementation.
+ */
+public class DOMDataTreeListeningException extends Exception {
+    private static final long serialVersionUID = 1L;
+
+    public DOMDataTreeListeningException(final String message) {
+        super(message);
+    }
+
+    public DOMDataTreeListeningException(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeLoopException.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeLoopException.java
new file mode 100644 (file)
index 0000000..8f498a8
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import javax.annotation.Nonnull;
+
+/**
+ * Exception thrown when a loop is detected in the way {@link DOMDataTreeListener}
+ * and {@link DOMDataTreeProducer} instances would be connected.
+ */
+public class DOMDataTreeLoopException extends Exception {
+    private static final long serialVersionUID = 1L;
+
+    public DOMDataTreeLoopException(final @Nonnull String message) {
+        super(message);
+    }
+
+    public DOMDataTreeLoopException(final @Nonnull String message, final @Nonnull Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducer.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducer.java
new file mode 100644 (file)
index 0000000..cbfa012
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import java.util.Collection;
+import javax.annotation.Nonnull;
+
+/**
+ * A data producer context. It allows transactions to be submitted to the subtrees
+ * specified at instantiation time. At any given time there may be a single transaction
+ * open. It needs to be either submitted or cancelled before another one can be open.
+ * Once a transaction is submitted, it will proceed to be committed asynchronously.
+ *
+ * Each instance has  an upper bound on the number of transactions which can be in-flight,
+ * once that capacity is exceeded, an attempt to create a new transaction will block
+ * until some transactions complete.
+ *
+ * Each {@link DOMDataTreeProducer} can be in two logical states, bound and unbound,
+ * which define the lifecycle rules for when is it legal to create and submit transactions
+ * in relationship with {@link DOMDataTreeListener} callbacks.
+ *
+ * When a producer is first created, it is unbound. In this state the producer can be
+ * accessed by any application thread to allocate or submit transactions, as long as
+ * the 'single open transaction' rule is maintained. The producer and any transaction
+ * object MUST NOT be accessed, directly or indirectly, from a {@link DOMDataTreeListener}
+ * callback.
+ *
+ * When a producer is referenced in a call to {@link DOMDataTreeService#registerListener(DOMDataTreeListener, java.util.Collection, boolean, java.util.Collection)},
+ * an attempt will be made to bind the producer to the specified {@link DOMDataTreeListener}.
+ * Such an attempt will fail the producer is already bound, or it has an open transaction.
+ * Once bound, the producer can only be accessed from within the {@link DOMDataTreeListener}
+ * callback on that particular instance. Any transaction which is not submitted by the
+ * time the callback returns will be implicitly cancelled. A producer becomes unbound
+ * when the listener it is bound to becomes unregistered.
+ */
+public interface DOMDataTreeProducer extends DOMDataTreeProducerFactory, AutoCloseable {
+    /**
+     * Allocate a new open transaction on this producer. Any and all transactions
+     * previously allocated must have been either submitted or cancelled by the
+     * time this method is invoked.
+     *
+     * @param barrier Indicates whether this transaction should be a barrier. A barrier
+     *                transaction is processed separately from any preceding transactions.
+     *                Non-barrier transactions may be merged and processed in a batch,
+     *                such that any observers see the modifications contained in them as
+     *                if the modifications were made in a single transaction.
+     * @return A new {@link DOMDataWriteTransaction}
+     * @throws {@link IllegalStateException} if a previous transaction was not closed.
+     * @throws {@link IllegalThreadStateException} if the calling thread context does not
+     *         match the lifecycle rules enforced by the producer state (e.g. bound or unbound).
+     *         This exception is thrown on a best effort basis and programs should not rely
+     *         on it for correct operation.
+     */
+    @Nonnull DOMDataWriteTransaction createTransaction(boolean isolated);
+
+    /**
+     * {@inheritDoc}
+     *
+     * When invoked on a {@link DOMDataTreeProducer}, this method has additional restrictions.
+     * There may not be an open transaction from this producer. The method needs to be
+     * invoked in appropriate context, e.g. bound or unbound.
+     *
+     * Specified subtrees must be accessible by this producer. Accessible means they are a subset
+     * of the subtrees specified when the producer is instantiated. The set is further reduced as
+     * child producers are instantiated -- if you create a producer for /a and then a child for
+     * /a/b, /a/b is not accessible from the first producer.
+     *
+     * Once this method returns successfully, this (parent) producer loses the ability to
+     * access the specified paths until the resulting (child) producer is shut down.
+     *
+     * @throws {@link IllegalStateException} if there is an open transaction
+     * @throws {@link IllegalArgumentException} if subtrees contains a subtree which is not
+     *         accessible by this producer
+     * @throws {@link IllegalThreadStateException} if the calling thread context does not
+     *         match the lifecycle rules enforced by the producer state (e.g. bound or unbound).
+     *         This exception is thrown on a best effort basis and programs should not rely
+     *         on it for correct operation.
+     */
+    @Override
+    @Nonnull DOMDataTreeProducer createProducer(@Nonnull Collection<DOMDataTreeIdentifier> subtrees);
+
+    /**
+     * {@inheritDoc}
+     *
+     * @throws DOMDataTreeProducerBusyException when there is an open transaction.
+     */
+    @Override
+    void close() throws DOMDataTreeProducerException;
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducerBusyException.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducerBusyException.java
new file mode 100644 (file)
index 0000000..a83a5ca
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+/**
+ * Exception indicating that the {@link DOMDataTreeProducer} has an open user
+ * transaction and cannot be closed.
+ */
+public class DOMDataTreeProducerBusyException extends DOMDataTreeProducerException {
+    private static final long serialVersionUID = 1L;
+
+    public DOMDataTreeProducerBusyException(final String message) {
+        super(message);
+    }
+
+    public DOMDataTreeProducerBusyException(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducerException.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducerException.java
new file mode 100644 (file)
index 0000000..16c2676
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+/**
+ * Base exception for all exceptions related to {@link DOMDataTreeProducer}s.
+ */
+public class DOMDataTreeProducerException extends Exception {
+    private static final long serialVersionUID = 1L;
+
+    public DOMDataTreeProducerException(final String message) {
+        super(message);
+    }
+
+    public DOMDataTreeProducerException(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducerFactory.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeProducerFactory.java
new file mode 100644 (file)
index 0000000..89ac8d1
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import java.util.Collection;
+import javax.annotation.Nonnull;
+
+/**
+ * Base source of {@link DOMDataTreeProducer}s. This interface is usually not used directly,
+ * but rather through one of its sub-interfaces.
+ */
+public interface DOMDataTreeProducerFactory {
+    /**
+     * Create a producer, which is able to access to a set of trees.
+     *
+     * @param subtrees The collection of subtrees the resulting producer should have access to.
+     * @return A {@link DOMDataTreeProducer} instance.
+     * @throws {@link IllegalArgumentException} if subtrees is empty.
+     */
+    @Nonnull DOMDataTreeProducer createProducer(@Nonnull Collection<DOMDataTreeIdentifier> subtrees);
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeService.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeService.java
new file mode 100644 (file)
index 0000000..21ff44c
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import java.util.Collection;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+
+/**
+ * A {@link DOMService} providing access to the conceptual data tree. Interactions
+ * with the data tree are split into data producers and consumers (listeners). Each
+ * of them operate on a set of subtrees, which need to be declared at instantiation time.
+ *
+ * Returned instances are not thread-safe and expected to be used by a single thread
+ * at a time. Furthermore, producers may not be accessed from consumer callbacks
+ * unless they were specified when the listener is registered.
+ *
+ * The service maintains a loop-free topology of producers and consumers. What this means
+ * is that a consumer is not allowed to access a producer, which affects any of the
+ * subtrees it is subscribed to. This restriction is in place to ensure the system does
+ * not go into a feedback loop, where it is impossible to block either a producer or
+ * a consumer without accumulating excess work in the backlog stemming from its previous
+ * activity.
+ */
+public interface DOMDataTreeService extends DOMDataTreeProducerFactory, DOMService {
+    /**
+     * Register a {@link DOMDataTreeListener} instance. Once registered, the listener
+     * will start receiving changes on the selected subtrees. If the listener cannot
+     * keep up with the rate of changes, and allowRxMerges is set to true, this service
+     * is free to merge the changes, so that a smaller number of them will be reported,
+     * possibly hiding some data transitions (like flaps).
+     *
+     * If the listener wants to write into any producer, that producer has to be mentioned
+     * in the call to this method. Those producers will be bound exclusively to the
+     * registration, so that accessing them outside of this listener's callback will trigger
+     * an error. Any producers mentioned must be idle, e.g. they may not have an open
+     * transaction at the time this method is invoked.
+     *
+     * Each listener instance can be registered at most once. Implementations of this
+     * interface have to guarantee that the listener's methods will not be invoked
+     * concurrently from multiple threads.
+     *
+     * @param listener {@link DOMDataTreeListener} that is being registered
+     * @param subtrees Conceptual subtree identifier of subtrees which should be monitored
+     *                 for changes. May not be null or empty.
+     * @param allowRxMerges True if the backend may perform ingress state compression.
+     * @param producers {@link DOMDataTreeProducer} instances to bind to the listener.
+     * @return A listener registration. Once closed, the listener will no longer be
+     *         invoked and the producers will be unbound.
+     * @throws IllegalArgumentException if subtrees is empty or the listener is already bound
+     * @throws DOMDataTreeLoopException if the registration of the listener to the specified
+     *                                  subtrees with specified producers would form a
+     *                                  feedback loop
+     */
+    @Nonnull <T extends DOMDataTreeListener> ListenerRegistration<T> registerListener(@Nonnull T listener,
+        @Nonnull Collection<DOMDataTreeIdentifier> subtrees, boolean allowRxMerges, @Nonnull Collection<DOMDataTreeProducer> producers);
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeShard.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeShard.java
new file mode 100644 (file)
index 0000000..e9ef003
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import java.util.EventListener;
+import javax.annotation.Nonnull;
+
+/**
+ * A single shard of the conceptual data tree. This interface defines the basic notifications
+ * a shard can receive. Each shard implementation is expected to also implement some of the
+ * datastore-level APIs. Which interfaces are required depends on the {@link DOMDataTreeShardingService}
+ * implementation.
+ */
+public interface DOMDataTreeShard extends EventListener {
+    /**
+     * Invoked whenever a child is getting attached as a more specific prefix under this shard.
+     *
+     * @param prefix Child's prefix
+     * @param child Child shard
+     */
+    void onChildAttached(@Nonnull DOMDataTreeIdentifier prefix, @Nonnull DOMDataTreeShard child);
+
+    /**
+     * Invoked whenever a child is getting detached as a more specific prefix under this shard.
+     *
+     * @param prefix Child's prefix
+     * @param child Child shard
+     */
+    void onChildDetached(@Nonnull DOMDataTreeIdentifier prefix, @Nonnull DOMDataTreeShard child);
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeShardingConflictException.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeShardingConflictException.java
new file mode 100644 (file)
index 0000000..d44316d
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import javax.annotation.Nonnull;
+
+/**
+ * Exception thrown when an attempt to attach a conflicting shard to the global
+ * table.
+ */
+public class DOMDataTreeShardingConflictException extends Exception {
+    private static final long serialVersionUID = 1L;
+
+    public DOMDataTreeShardingConflictException(final @Nonnull String message) {
+        super(message);
+    }
+
+    public DOMDataTreeShardingConflictException(final @Nonnull String message, final @Nonnull Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeShardingService.java b/opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMDataTreeShardingService.java
new file mode 100644 (file)
index 0000000..c087224
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+
+/**
+ * A {@link DOMService} providing access to details on how the conceptual data tree
+ * is distributed among providers (also known as shards). Each shard is tied to a
+ * single {@link DOMDataTreeIdentifier}. Based on those data tree identifiers, the
+ * shards are organized in a tree, where there is a logical parent/child relationship.
+ *
+ * It is not allowed to attach two shards to the same data tree identifier, which means
+ * the mapping of each piece of information has an unambiguous home. When accessing
+ * the information, the shard with the longest matching data tree identifier is used,
+ * which is why this interface treats it is a prefix.
+ *
+ * Whenever a parent/child relationship is changed, the parent is notified, so it can
+ * understand that a logical child has been attached.
+ */
+public interface DOMDataTreeShardingService extends DOMService {
+    /**
+     * Register a shard as responsible for a particular subtree prefix.
+     *
+     * @param prefix Data tree identifier, may not be null.
+     * @param shard Responsible shard instance
+     * @return A registration. To remove the shard's binding, close the registration.
+     * @throws DOMDataTreeShardingConflictException if the prefix is already bound
+     */
+    @Nonnull <T extends DOMDataTreeShard> ListenerRegistration<T> registerDataTreeShard(@Nonnull DOMDataTreeIdentifier prefix, @Nonnull T shard) throws DOMDataTreeShardingConflictException;
+}
diff --git a/opendaylight/md-sal/sal-dom-api/src/test/java/org/opendaylight/controller/md/sal/dom/api/AbstractDOMDataTreeServiceTestSuite.java b/opendaylight/md-sal/sal-dom-api/src/test/java/org/opendaylight/controller/md/sal/dom/api/AbstractDOMDataTreeServiceTestSuite.java
new file mode 100644 (file)
index 0000000..896c606
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.dom.api;
+
+import static org.junit.Assert.assertNotNull;
+import com.google.common.util.concurrent.CheckedFuture;
+import java.net.URI;
+import java.util.Collections;
+import javax.annotation.Nonnull;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+
+/**
+ * Abstract test suite demonstrating various access patterns on how a {@link DOMDataTreeService}
+ * can be used.
+ */
+public abstract class AbstractDOMDataTreeServiceTestSuite {
+    protected static final QNameModule TEST_MODULE = QNameModule.create(URI.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:store"), null);
+
+    protected static final YangInstanceIdentifier UNORDERED_CONTAINER_IID = YangInstanceIdentifier.create(
+        new NodeIdentifier(QName.create(TEST_MODULE, "lists")),
+        new NodeIdentifier(QName.create(TEST_MODULE, "unordered-container")));
+    protected static final DOMDataTreeIdentifier UNORDERED_CONTAINER_TREE = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, UNORDERED_CONTAINER_IID);
+
+    /**
+     * Return a reference to the service used in this test. The instance
+     * needs to be reused within the same test and must be isolated between
+     * tests.
+     *
+     * @return {@link DOMDataTreeService} instance.
+     */
+    protected abstract @Nonnull DOMDataTreeService service();
+
+    /**
+     * A simple unbound producer. It write some basic things into the data store based on the
+     * test model.
+     * @throws DOMDataTreeProducerException
+     * @throws TransactionCommitFailedException
+     */
+    @Test
+    public final void testBasicProducer() throws DOMDataTreeProducerException, TransactionCommitFailedException {
+        // Create a producer. It is an AutoCloseable resource, hence the try-with pattern
+        try (final DOMDataTreeProducer prod = service().createProducer(Collections.singleton(UNORDERED_CONTAINER_TREE))) {
+            assertNotNull(prod);
+
+            final DOMDataWriteTransaction tx = prod.createTransaction(true);
+            assertNotNull(tx);
+
+            tx.put(LogicalDatastoreType.OPERATIONAL, UNORDERED_CONTAINER_IID, ImmutableContainerNodeBuilder.create().build());
+
+            final CheckedFuture<Void, TransactionCommitFailedException> f = tx.submit();
+            assertNotNull(f);
+
+            f.checkedGet();
+        }
+    }
+
+    // TODO: simple listener
+}