*/
package org.opendaylight.yangtools.yang.data.impl.schema.tree;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
import com.google.common.base.MoreObjects;
-import com.google.common.base.Preconditions;
+import java.util.Optional;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
-import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Read-only snapshot of the data tree.
*/
-final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
+final class InMemoryDataTree extends AbstractDataTreeTip implements DataTree {
private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
final SchemaContext schemaContext) {
- this.treeConfig = Preconditions.checkNotNull(treeConfig, "treeConfig");
+ this.treeConfig = requireNonNull(treeConfig, "treeConfig");
maskMandatory = true;
state = DataTreeState.createInitial(rootNode);
if (schemaContext != null) {
}
}
- InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig, final SchemaContext schemaContext,
- final DataSchemaNode rootSchemaNode, final boolean maskMandatory) {
- this.treeConfig = Preconditions.checkNotNull(treeConfig, "treeConfig");
+ InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
+ final SchemaContext schemaContext, final DataSchemaNode rootSchemaNode, final boolean maskMandatory) {
+ this.treeConfig = requireNonNull(treeConfig, "treeConfig");
this.maskMandatory = maskMandatory;
state = DataTreeState.createInitial(rootNode).withSchemaContext(schemaContext, getOperation(rootSchemaNode));
}
private ModificationApplyOperation getOperation(final DataSchemaNode rootSchemaNode) {
- if (maskMandatory && rootSchemaNode instanceof ContainerSchemaNode) {
+ if (rootSchemaNode instanceof ContainerSchemaNode && maskMandatory) {
return new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeConfig);
}
+ if (rootSchemaNode instanceof ListSchemaNode) {
+ final PathArgument arg = treeConfig.getRootPath().getLastPathArgument();
+ if (arg instanceof NodeIdentifierWithPredicates) {
+ return maskMandatory ? new ListEntryModificationStrategy((ListSchemaNode) rootSchemaNode, treeConfig)
+ : ListEntryModificationStrategy.of((ListSchemaNode) rootSchemaNode, treeConfig);
+ }
+ }
return SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
}
*/
@Override
public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
- Preconditions.checkNotNull(newSchemaContext);
+ requireNonNull(newSchemaContext);
LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
- final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(getRootPath());
- if (rootContextNode == null) {
+ final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(getRootPath());
+ if (!rootContextNode.isPresent()) {
LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
return;
}
- final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
+ final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
if (!(rootSchemaNode instanceof DataNodeContainer)) {
LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
return;
}
final ModificationApplyOperation rootNode = getOperation(rootSchemaNode);
- DataTreeState currentState, newState;
+ DataTreeState currentState;
+ DataTreeState newState;
do {
currentState = state;
newState = currentState.withSchemaContext(newSchemaContext, rootNode);
if (candidate instanceof NoopDataTreeCandidate) {
return;
}
- Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
+ checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s",
+ candidate.getClass());
final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
if (LOG.isTraceEnabled()) {
}
final TreeNode newRoot = c.getTipRoot();
- DataTreeState currentState, newState;
+ DataTreeState currentState;
+ DataTreeState newState;
do {
currentState = state;
final TreeNode currentRoot = currentState.getRoot();
if (oldRoot != currentRoot) {
final String oldStr = simpleToString(oldRoot);
final String currentStr = simpleToString(currentRoot);
- throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr + " differ.");
+ throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr
+ + " differ.");
}
newState = currentState.withRoot(newRoot);
} while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
}
- private static String simpleToString(final Object o) {
- return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
+ private static String simpleToString(final Object obj) {
+ return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
}
@Override
@Override
public String toString() {
- return MoreObjects.toStringHelper(this).
- add("object", super.toString()).
- add("config", treeConfig).
- add("state", state).
- toString();
+ return MoreObjects.toStringHelper(this)
+ .add("object", super.toString())
+ .add("config", treeConfig)
+ .add("state", state)
+ .toString();
}
@Override
- @Nonnull
protected TreeNode getTipRoot() {
return state.getRoot();
}