2 * Copyright (c) 2014 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.yangtools.yang.data.impl.schema.tree;
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration.Builder;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeFactory;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
31 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
32 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 * A factory for creating in-memory data trees.
41 public final class InMemoryDataTreeFactory implements DataTreeFactory {
42 private static final InMemoryDataTreeFactory INSTANCE = new InMemoryDataTreeFactory();
43 private static final NormalizedNode<?, ?> ROOT_CONTAINER = ImmutableNodes.containerNode(SchemaContext.NAME);
45 private InMemoryDataTreeFactory() {
46 // Never instantiated externally
51 public TipProducingDataTree create(final TreeType treeType) {
52 return create(DataTreeConfiguration.getDefault(treeType));
57 public TipProducingDataTree create(final TreeType treeType, final YangInstanceIdentifier rootPath) {
58 if (rootPath.isEmpty()) {
59 return create(treeType);
62 final DataTreeConfiguration defConfig = DataTreeConfiguration.getDefault(treeType);
63 final DataTreeConfiguration config;
64 if (!rootPath.isEmpty()) {
65 config = new Builder(treeType).setMandatoryNodesValidation(defConfig.isMandatoryNodesValidationEnabled())
66 .setRootPath(rootPath).setUniqueIndexes(defConfig.isUniqueIndexEnabled()).build();
71 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(rootPath), Version.initial()), config,
76 public TipProducingDataTree create(final DataTreeConfiguration treeConfig) {
77 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
78 Version.initial()), treeConfig, null);
82 public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext) {
83 return create(treeConfig, initialSchemaContext, true);
87 public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
88 final NormalizedNodeContainer<?, ?, ?> initialRoot) throws DataValidationFailedException {
89 final DataTree ret = create(treeConfig, initialSchemaContext, false);
91 final DataTreeModification mod = ret.takeSnapshot().newModification();
92 mod.write(YangInstanceIdentifier.EMPTY, initialRoot);
96 final DataTreeCandidate candidate = ret.prepare(mod);
97 ret.commit(candidate);
101 private static DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
102 final boolean maskMandatory) {
103 final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
104 final NormalizedNode<?, ?> rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
105 treeConfig.getRootPath());
106 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(rootDataNode, Version.initial()), treeConfig,
107 initialSchemaContext, rootSchemaNode, maskMandatory);
110 private static NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
111 final YangInstanceIdentifier path) {
112 if (path.isEmpty()) {
113 Preconditions.checkArgument(schemaNode instanceof ContainerSchemaNode,
114 "Conceptual tree root has to be a container, not %s", schemaNode);
115 return ROOT_CONTAINER;
118 final PathArgument arg = path.getLastPathArgument();
119 if (schemaNode instanceof ContainerSchemaNode) {
120 Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode,
122 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
123 } else if (schemaNode instanceof ListSchemaNode) {
124 // This can either be a top-level list or its individual entry
125 if (arg instanceof NodeIdentifierWithPredicates) {
126 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
128 Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
129 return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
131 throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
135 private static DataSchemaNode getRootSchemaNode(final SchemaContext schemaContext,
136 final YangInstanceIdentifier rootPath) {
137 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
138 final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(rootPath);
139 Preconditions.checkArgument(rootContextNode != null, "Failed to find root %s in schema context", rootPath);
141 final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
142 Preconditions.checkArgument(rootSchemaNode instanceof DataNodeContainer,
143 "Root %s resolves to non-container type %s", rootPath, rootSchemaNode);
144 return rootSchemaNode;
147 private static NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
148 if (path.isEmpty()) {
149 return ROOT_CONTAINER;
152 final PathArgument arg = path.getLastPathArgument();
153 if (arg instanceof NodeIdentifier) {
154 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
156 if (arg instanceof NodeIdentifierWithPredicates) {
157 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
160 // FIXME: implement augmentations and leaf-lists
161 throw new IllegalArgumentException("Unsupported root node " + arg);
165 * Get an instance of this factory. This method cannot fail.
167 * @return Data tree factory instance.
169 public static InMemoryDataTreeFactory getInstance() {