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 static com.google.common.base.Preconditions.checkArgument;
12 import java.util.Optional;
13 import javax.inject.Singleton;
14 import org.kohsuke.MetaInfServices;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeFactory;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
32 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 * A factory for creating in-memory data trees.
44 public final class InMemoryDataTreeFactory implements DataTreeFactory {
45 // FIXME: YANGTOOLS-1074: we do not want this name
46 private static final NormalizedNode<?, ?> ROOT_CONTAINER = ImmutableNodes.containerNode(SchemaContext.NAME);
49 public DataTree create(final DataTreeConfiguration treeConfig) {
50 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
51 Version.initial()), treeConfig, null);
55 public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext) {
56 return create(treeConfig, initialSchemaContext, true);
60 public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
61 final NormalizedNodeContainer<?, ?, ?> initialRoot) throws DataValidationFailedException {
62 final DataTree ret = create(treeConfig, initialSchemaContext, false);
64 final DataTreeModification mod = ret.takeSnapshot().newModification();
65 mod.write(YangInstanceIdentifier.empty(), initialRoot);
69 final DataTreeCandidate candidate = ret.prepare(mod);
70 ret.commit(candidate);
74 private static DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
75 final boolean maskMandatory) {
76 final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
77 final NormalizedNode<?, ?> rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
78 treeConfig.getRootPath());
79 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(rootDataNode, Version.initial()), treeConfig,
80 initialSchemaContext, rootSchemaNode, maskMandatory);
83 private static DataSchemaNode getRootSchemaNode(final SchemaContext schemaContext,
84 final YangInstanceIdentifier rootPath) {
85 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
86 final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(rootPath);
87 checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
89 final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
90 checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s",
91 rootPath, rootSchemaNode);
92 return rootSchemaNode;
95 private static NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
96 final YangInstanceIdentifier path) {
98 checkArgument(schemaNode instanceof ContainerSchemaNode,
99 "Conceptual tree root has to be a container, not %s", schemaNode);
100 return ROOT_CONTAINER;
103 final PathArgument arg = path.getLastPathArgument();
104 if (schemaNode instanceof ContainerSchemaNode) {
105 checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode, path);
106 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
107 } else if (schemaNode instanceof ListSchemaNode) {
108 // This can either be a top-level list or its individual entry
109 if (arg instanceof NodeIdentifierWithPredicates) {
110 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
112 checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
113 return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
115 throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
119 private static NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
120 if (path.isEmpty()) {
121 return ROOT_CONTAINER;
124 final PathArgument arg = path.getLastPathArgument();
125 if (arg instanceof NodeIdentifier) {
126 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
128 if (arg instanceof NodeIdentifierWithPredicates) {
129 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
132 // FIXME: implement augmentations and leaf-lists
133 throw new IllegalArgumentException("Unsupported root node " + arg);