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.ContainerLike;
35 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
39 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Deactivate;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * A factory for creating in-memory data trees.
52 @Component(immediate = true)
53 public final class InMemoryDataTreeFactory implements DataTreeFactory {
54 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTreeFactory.class);
55 // FIXME: YANGTOOLS-1074: we do not want this name
56 private static final @NonNull NormalizedNode<?, ?> ROOT_CONTAINER =
57 ImmutableNodes.containerNode(SchemaContext.NAME);
60 public DataTree create(final DataTreeConfiguration treeConfig) {
61 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
62 Version.initial()), treeConfig, null);
66 public DataTree create(final DataTreeConfiguration treeConfig, final EffectiveModelContext initialSchemaContext) {
67 return createDataTree(treeConfig, initialSchemaContext, true);
71 public DataTree create(final DataTreeConfiguration treeConfig, final EffectiveModelContext initialSchemaContext,
72 final NormalizedNodeContainer<?, ?, ?> initialRoot) throws DataValidationFailedException {
73 final DataTree ret = createDataTree(treeConfig, initialSchemaContext, false);
75 final DataTreeModification mod = ret.takeSnapshot().newModification();
76 mod.write(YangInstanceIdentifier.empty(), initialRoot);
80 final DataTreeCandidate candidate = ret.prepare(mod);
81 ret.commit(candidate);
86 @SuppressWarnings("static-method")
88 LOG.info("In-memory Data Tree activated");
92 @SuppressWarnings("static-method")
94 LOG.info("In-memory Data Tree deactivated");
97 private static @NonNull DataTree createDataTree(final DataTreeConfiguration treeConfig,
98 final EffectiveModelContext initialSchemaContext, final boolean maskMandatory) {
99 final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
100 final NormalizedNode<?, ?> rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
101 treeConfig.getRootPath());
102 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(rootDataNode, Version.initial()), treeConfig,
103 initialSchemaContext, rootSchemaNode, maskMandatory);
106 private static @NonNull NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
107 final YangInstanceIdentifier path) {
108 if (path.isEmpty()) {
109 checkArgument(schemaNode instanceof ContainerLike,
110 "Conceptual tree root has to be a container, not %s", schemaNode);
111 return ROOT_CONTAINER;
114 final PathArgument arg = path.getLastPathArgument();
115 if (schemaNode instanceof ContainerSchemaNode) {
116 checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode, path);
117 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
118 } else if (schemaNode instanceof ListSchemaNode) {
119 // This can either be a top-level list or its individual entry
120 if (arg instanceof NodeIdentifierWithPredicates) {
121 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
123 checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
124 return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
126 throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
130 private static @NonNull NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
131 if (path.isEmpty()) {
132 return ROOT_CONTAINER;
135 final PathArgument arg = path.getLastPathArgument();
136 if (arg instanceof NodeIdentifier) {
137 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
139 if (arg instanceof NodeIdentifierWithPredicates) {
140 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
143 // FIXME: implement augmentations and leaf-lists
144 throw new IllegalArgumentException("Unsupported root node " + arg);
147 private static DataSchemaNode getRootSchemaNode(final EffectiveModelContext schemaContext,
148 final YangInstanceIdentifier rootPath) {
149 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
150 final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(rootPath);
151 checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
153 final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
154 checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s",
155 rootPath, rootSchemaNode);
156 return rootSchemaNode;