Merge "Bug 3051: Fixed pattern checks in generated DTOs"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTree.java
index dd1f00ed5e21a67cba5706e303ba90c43322c7df..fc9f744a6fe400ee83de27db5a7fa352341eeb51 100644 (file)
@@ -7,17 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import com.google.common.base.Optional;
+import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
-import java.util.Collections;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
@@ -26,8 +21,7 @@ import org.slf4j.LoggerFactory;
 /**
  * Read-only snapshot of the data tree.
  */
-final class InMemoryDataTree implements DataTree {
-    private static final YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.create(Collections.<PathArgument>emptyList());
+final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
     private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
             AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
@@ -52,7 +46,6 @@ final class InMemoryDataTree implements DataTree {
     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
         Preconditions.checkNotNull(newSchemaContext);
 
-        LOG.info("Attempting to install schema contexts");
         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
 
         final SchemaAwareApplyOperation operation = SchemaAwareApplyOperation.from(newSchemaContext);
@@ -69,32 +62,6 @@ final class InMemoryDataTree implements DataTree {
         return state.newSnapshot();
     }
 
-    @Override
-    public void validate(final DataTreeModification modification) throws DataValidationFailedException {
-        Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
-        final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
-
-        m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(state.getRoot()));
-    }
-
-    @Override
-    public DataTreeCandidate prepare(final DataTreeModification modification) {
-        Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
-
-        final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
-        final ModifiedNode root = m.getRootModification();
-
-        if (root.getType() == ModificationType.UNMODIFIED) {
-            return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
-        }
-
-        final TreeNode currentRoot = state.getRoot();
-        final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
-            Optional.<TreeNode>of(currentRoot), m.getVersion());
-        Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
-        return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot, newRoot.get());
-    }
-
     @Override
     public void commit(final DataTreeCandidate candidate) {
         if (candidate instanceof NoopDataTreeCandidate) {
@@ -105,10 +72,10 @@ final class InMemoryDataTree implements DataTree {
         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
 
         if (LOG.isTraceEnabled()) {
-            LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
+            LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
         }
 
-        final TreeNode newRoot = c.getAfterRoot();
+        final TreeNode newRoot = c.getTipRoot();
         DataTreeState currentState, newState;
         do {
             currentState = state;
@@ -122,4 +89,14 @@ final class InMemoryDataTree implements DataTree {
             LOG.trace("Updated state from {} to {}", currentState, newState);
         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
     }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this).add("object", super.toString()).add("state", state).toString();
+    }
+
+    @Override
+    protected TreeNode getTipRoot() {
+        return state.getRoot();
+    }
 }