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.eclipse.jdt.annotation.NonNull;
15 import org.kohsuke.MetaInfServices;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeFactory;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
33 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Deactivate;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * A factory for creating in-memory data trees.
50 @Component(immediate = true)
51 public final class InMemoryDataTreeFactory implements DataTreeFactory {
52 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTreeFactory.class);
53 // FIXME: YANGTOOLS-1074: we do not want this name
54 private static final @NonNull NormalizedNode<?, ?> ROOT_CONTAINER =
55 ImmutableNodes.containerNode(SchemaContext.NAME);
58 public DataTree create(final DataTreeConfiguration treeConfig) {
59 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
60 Version.initial()), treeConfig, null);
64 public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext) {
65 return createDataTree(treeConfig, initialSchemaContext, true);
69 public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
70 final NormalizedNodeContainer<?, ?, ?> initialRoot) throws DataValidationFailedException {
71 final DataTree ret = createDataTree(treeConfig, initialSchemaContext, false);
73 final DataTreeModification mod = ret.takeSnapshot().newModification();
74 mod.write(YangInstanceIdentifier.empty(), initialRoot);
78 final DataTreeCandidate candidate = ret.prepare(mod);
79 ret.commit(candidate);
84 @SuppressWarnings("static-method")
86 LOG.info("In-memory Data Tree activated");
90 @SuppressWarnings("static-method")
92 LOG.info("In-memory Data Tree deactivated");
95 private static @NonNull DataTree createDataTree(final DataTreeConfiguration treeConfig,
96 final SchemaContext initialSchemaContext, final boolean maskMandatory) {
97 final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
98 final NormalizedNode<?, ?> rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
99 treeConfig.getRootPath());
100 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(rootDataNode, Version.initial()), treeConfig,
101 initialSchemaContext, rootSchemaNode, maskMandatory);
104 private static @NonNull NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
105 final YangInstanceIdentifier path) {
106 if (path.isEmpty()) {
107 checkArgument(schemaNode instanceof ContainerSchemaNode,
108 "Conceptual tree root has to be a container, not %s", schemaNode);
109 return ROOT_CONTAINER;
112 final PathArgument arg = path.getLastPathArgument();
113 if (schemaNode instanceof ContainerSchemaNode) {
114 checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode, path);
115 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
116 } else if (schemaNode instanceof ListSchemaNode) {
117 // This can either be a top-level list or its individual entry
118 if (arg instanceof NodeIdentifierWithPredicates) {
119 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
121 checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
122 return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
124 throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
128 private static @NonNull NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
129 if (path.isEmpty()) {
130 return ROOT_CONTAINER;
133 final PathArgument arg = path.getLastPathArgument();
134 if (arg instanceof NodeIdentifier) {
135 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
137 if (arg instanceof NodeIdentifierWithPredicates) {
138 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
141 // FIXME: implement augmentations and leaf-lists
142 throw new IllegalArgumentException("Unsupported root node " + arg);
145 private static DataSchemaNode getRootSchemaNode(final SchemaContext schemaContext,
146 final YangInstanceIdentifier rootPath) {
147 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
148 final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(rootPath);
149 checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
151 final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
152 checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s",
153 rootPath, rootSchemaNode);
154 return rootSchemaNode;