BUG-8291: expose additional DataTreeFactory methods
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTreeFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration.Builder;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeFactory;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
31 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
32 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37
38 /**
39  * A factory for creating in-memory data trees.
40  */
41 public final class InMemoryDataTreeFactory implements DataTreeFactory {
42     private static final InMemoryDataTreeFactory INSTANCE = new InMemoryDataTreeFactory();
43     private static final NormalizedNode<?, ?> ROOT_CONTAINER = ImmutableNodes.containerNode(SchemaContext.NAME);
44
45     private InMemoryDataTreeFactory() {
46         // Never instantiated externally
47     }
48
49     @Deprecated
50     @Override
51     public TipProducingDataTree create(final TreeType treeType) {
52         return create(DataTreeConfiguration.getDefault(treeType));
53     }
54
55     @Deprecated
56     @Override
57     public TipProducingDataTree create(final TreeType treeType, final YangInstanceIdentifier rootPath) {
58         if (rootPath.isEmpty()) {
59             return create(treeType);
60         }
61
62         final DataTreeConfiguration defConfig = DataTreeConfiguration.getDefault(treeType);
63         final DataTreeConfiguration config;
64         if (!rootPath.isEmpty()) {
65             config = new Builder(treeType).setMandatoryNodesValidation(defConfig.isMandatoryNodesValidationEnabled())
66                     .setRootPath(rootPath).setUniqueIndexes(defConfig.isUniqueIndexEnabled()).build();
67         } else {
68             config = defConfig;
69         }
70
71         return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(rootPath), Version.initial()), config,
72             null);
73     }
74
75     @Override
76     public TipProducingDataTree create(final DataTreeConfiguration treeConfig) {
77         return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
78             Version.initial()), treeConfig, null);
79     }
80
81     @Override
82     public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext) {
83         return create(treeConfig, initialSchemaContext, true);
84     }
85
86     @Override
87     public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
88             final NormalizedNodeContainer<?, ?, ?> initialRoot) throws DataValidationFailedException {
89         final DataTree ret = create(treeConfig, initialSchemaContext, false);
90
91         final DataTreeModification mod = ret.takeSnapshot().newModification();
92         mod.write(YangInstanceIdentifier.EMPTY, initialRoot);
93         mod.ready();
94
95         ret.validate(mod);
96         final DataTreeCandidate candidate = ret.prepare(mod);
97         ret.commit(candidate);
98         return ret;
99     }
100
101     private static DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
102             final boolean maskMandatory) {
103         final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
104         final NormalizedNode<?, ?> rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
105             treeConfig.getRootPath());
106         return new InMemoryDataTree(TreeNodeFactory.createTreeNode(rootDataNode, Version.initial()), treeConfig,
107             initialSchemaContext, rootSchemaNode, maskMandatory);
108     }
109
110     private static NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
111             final YangInstanceIdentifier path) {
112         if (path.isEmpty()) {
113             Preconditions.checkArgument(schemaNode instanceof ContainerSchemaNode,
114                 "Conceptual tree root has to be a container, not %s", schemaNode);
115             return ROOT_CONTAINER;
116         }
117
118         final PathArgument arg = path.getLastPathArgument();
119         if (schemaNode instanceof ContainerSchemaNode) {
120             Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode,
121                 path);
122             return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
123         } else if (schemaNode instanceof ListSchemaNode) {
124             // This can either be a top-level list or its individual entry
125             if (arg instanceof NodeIdentifierWithPredicates) {
126                 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
127             }
128             Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
129             return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
130         } else {
131             throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
132         }
133     }
134
135     private static DataSchemaNode getRootSchemaNode(final SchemaContext schemaContext,
136             final YangInstanceIdentifier rootPath) {
137         final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
138         final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(rootPath);
139         Preconditions.checkArgument(rootContextNode != null, "Failed to find root %s in schema context", rootPath);
140
141         final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
142         Preconditions.checkArgument(rootSchemaNode instanceof DataNodeContainer,
143             "Root %s resolves to non-container type %s", rootPath, rootSchemaNode);
144         return rootSchemaNode;
145     }
146
147     private static NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
148         if (path.isEmpty()) {
149             return ROOT_CONTAINER;
150         }
151
152         final PathArgument arg = path.getLastPathArgument();
153         if (arg instanceof NodeIdentifier) {
154             return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
155         }
156         if (arg instanceof NodeIdentifierWithPredicates) {
157             return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
158         }
159
160         // FIXME: implement augmentations and leaf-lists
161         throw new IllegalArgumentException("Unsupported root node " + arg);
162     }
163
164     /**
165      * Get an instance of this factory. This method cannot fail.
166      *
167      * @return Data tree factory instance.
168      */
169     public static InMemoryDataTreeFactory getInstance() {
170         return INSTANCE;
171     }
172 }