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.Inject;
14 import javax.inject.Singleton;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.kohsuke.MetaInfServices;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeFactory;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
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.spi.tree.TreeNodeFactory;
32 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
33 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
34 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
35 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
36 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.osgi.service.component.annotations.Activate;
43 import org.osgi.service.component.annotations.Component;
44 import org.osgi.service.component.annotations.Deactivate;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * A factory for creating in-memory data trees.
53 @Component(immediate = true)
54 public final class InMemoryDataTreeFactory implements DataTreeFactory {
55 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTreeFactory.class);
56 // FIXME: YANGTOOLS-1074: we do not want this name
57 private static final @NonNull NormalizedNode ROOT_CONTAINER =
58 ImmutableNodes.containerNode(SchemaContext.NAME);
61 public InMemoryDataTreeFactory() {
66 public DataTree create(final DataTreeConfiguration treeConfig) {
67 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
68 Version.initial()), treeConfig, null);
72 public DataTree create(final DataTreeConfiguration treeConfig, final EffectiveModelContext initialSchemaContext) {
73 return createDataTree(treeConfig, initialSchemaContext, true);
77 public DataTree create(final DataTreeConfiguration treeConfig, final EffectiveModelContext initialSchemaContext,
78 final DistinctNodeContainer<?, ?> initialRoot) throws DataValidationFailedException {
79 final DataTree ret = createDataTree(treeConfig, initialSchemaContext, false);
81 final DataTreeModification mod = ret.takeSnapshot().newModification();
82 mod.write(YangInstanceIdentifier.empty(), initialRoot);
86 final DataTreeCandidate candidate = ret.prepare(mod);
87 ret.commit(candidate);
92 @SuppressWarnings("static-method")
94 LOG.info("In-memory Data Tree activated");
98 @SuppressWarnings("static-method")
100 LOG.info("In-memory Data Tree deactivated");
103 private static @NonNull DataTree createDataTree(final DataTreeConfiguration treeConfig,
104 final EffectiveModelContext initialSchemaContext, final boolean maskMandatory) {
105 final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
106 final NormalizedNode rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
107 treeConfig.getRootPath());
108 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(rootDataNode, Version.initial()), treeConfig,
109 initialSchemaContext, rootSchemaNode, maskMandatory);
112 private static @NonNull NormalizedNode createRoot(final DataNodeContainer schemaNode,
113 final YangInstanceIdentifier path) {
114 if (path.isEmpty()) {
115 checkArgument(schemaNode instanceof ContainerLike,
116 "Conceptual tree root has to be a container, not %s", schemaNode);
117 return ROOT_CONTAINER;
120 final PathArgument arg = path.getLastPathArgument();
121 if (schemaNode instanceof ContainerSchemaNode) {
122 checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode, path);
123 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
124 } else if (schemaNode instanceof ListSchemaNode) {
125 // This can either be a top-level list or its individual entry
126 if (arg instanceof NodeIdentifierWithPredicates) {
127 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
129 checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
130 return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
132 throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
136 private static @NonNull NormalizedNode createRoot(final YangInstanceIdentifier path) {
137 if (path.isEmpty()) {
138 return ROOT_CONTAINER;
141 final PathArgument arg = path.getLastPathArgument();
142 if (arg instanceof NodeIdentifier) {
143 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
145 if (arg instanceof NodeIdentifierWithPredicates) {
146 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
149 // FIXME: implement augmentations and leaf-lists
150 throw new IllegalArgumentException("Unsupported root node " + arg);
153 private static DataSchemaNode getRootSchemaNode(final EffectiveModelContext schemaContext,
154 final YangInstanceIdentifier rootPath) {
155 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
156 final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(rootPath);
157 checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
159 final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
160 checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s",
161 rootPath, rootSchemaNode);
162 return rootSchemaNode;