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.osgi.service.component.annotations.RequireServiceComponentRuntime;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * A factory for creating in-memory data trees.
54 @Component(immediate = true)
55 @RequireServiceComponentRuntime
56 public final class InMemoryDataTreeFactory implements DataTreeFactory {
57 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTreeFactory.class);
58 // FIXME: YANGTOOLS-1074: we do not want this name
59 private static final @NonNull NormalizedNode ROOT_CONTAINER =
60 ImmutableNodes.containerNode(SchemaContext.NAME);
63 public InMemoryDataTreeFactory() {
68 public DataTree create(final DataTreeConfiguration treeConfig) {
69 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
70 Version.initial()), treeConfig, null);
74 public DataTree create(final DataTreeConfiguration treeConfig, final EffectiveModelContext initialSchemaContext) {
75 return createDataTree(treeConfig, initialSchemaContext, true);
79 public DataTree create(final DataTreeConfiguration treeConfig, final EffectiveModelContext initialSchemaContext,
80 final DistinctNodeContainer<?, ?> initialRoot) throws DataValidationFailedException {
81 final DataTree ret = createDataTree(treeConfig, initialSchemaContext, false);
83 final DataTreeModification mod = ret.takeSnapshot().newModification();
84 mod.write(YangInstanceIdentifier.empty(), initialRoot);
88 final DataTreeCandidate candidate = ret.prepare(mod);
89 ret.commit(candidate);
94 @SuppressWarnings("static-method")
96 LOG.info("In-memory Data Tree activated");
100 @SuppressWarnings("static-method")
102 LOG.info("In-memory Data Tree deactivated");
105 private static @NonNull DataTree createDataTree(final DataTreeConfiguration treeConfig,
106 final EffectiveModelContext initialSchemaContext, final boolean maskMandatory) {
107 final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
108 final NormalizedNode rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
109 treeConfig.getRootPath());
110 return new InMemoryDataTree(TreeNodeFactory.createTreeNode(rootDataNode, Version.initial()), treeConfig,
111 initialSchemaContext, rootSchemaNode, maskMandatory);
114 private static @NonNull NormalizedNode createRoot(final DataNodeContainer schemaNode,
115 final YangInstanceIdentifier path) {
116 if (path.isEmpty()) {
117 checkArgument(schemaNode instanceof ContainerLike,
118 "Conceptual tree root has to be a container, not %s", schemaNode);
119 return ROOT_CONTAINER;
122 final PathArgument arg = path.getLastPathArgument();
123 if (schemaNode instanceof ContainerSchemaNode) {
124 checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode, path);
125 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
126 } else if (schemaNode instanceof ListSchemaNode) {
127 // This can either be a top-level list or its individual entry
128 if (arg instanceof NodeIdentifierWithPredicates) {
129 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
131 checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
132 return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
134 throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
138 private static @NonNull NormalizedNode createRoot(final YangInstanceIdentifier path) {
139 if (path.isEmpty()) {
140 return ROOT_CONTAINER;
143 final PathArgument arg = path.getLastPathArgument();
144 if (arg instanceof NodeIdentifier) {
145 return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
147 if (arg instanceof NodeIdentifierWithPredicates) {
148 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
151 // FIXME: implement augmentations and leaf-lists
152 throw new IllegalArgumentException("Unsupported root node " + arg);
155 private static DataSchemaNode getRootSchemaNode(final EffectiveModelContext schemaContext,
156 final YangInstanceIdentifier rootPath) {
157 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
158 final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(rootPath);
159 checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
161 final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
162 checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s",
163 rootPath, rootSchemaNode);
164 return rootSchemaNode;