Cleanup DataTree interfaces and InMemmoryDataTreeFactory
[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.kohsuke.MetaInfServices;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
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.spi.TreeNodeFactory;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
29 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35
36 /**
37  * A factory for creating in-memory data trees.
38  */
39 @MetaInfServices
40 public final class InMemoryDataTreeFactory implements DataTreeFactory {
41     private static final NormalizedNode<?, ?> ROOT_CONTAINER = ImmutableNodes.containerNode(SchemaContext.NAME);
42
43     @Override
44     public DataTree create(final DataTreeConfiguration treeConfig) {
45         return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
46             Version.initial()), treeConfig, null);
47     }
48
49     @Override
50     public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext) {
51         return create(treeConfig, initialSchemaContext, true);
52     }
53
54     @Override
55     public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
56             final NormalizedNodeContainer<?, ?, ?> initialRoot) throws DataValidationFailedException {
57         final DataTree ret = create(treeConfig, initialSchemaContext, false);
58
59         final DataTreeModification mod = ret.takeSnapshot().newModification();
60         mod.write(YangInstanceIdentifier.EMPTY, initialRoot);
61         mod.ready();
62
63         ret.validate(mod);
64         final DataTreeCandidate candidate = ret.prepare(mod);
65         ret.commit(candidate);
66         return ret;
67     }
68
69     private static DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
70             final boolean maskMandatory) {
71         final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
72         final NormalizedNode<?, ?> rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
73             treeConfig.getRootPath());
74         return new InMemoryDataTree(TreeNodeFactory.createTreeNode(rootDataNode, Version.initial()), treeConfig,
75             initialSchemaContext, rootSchemaNode, maskMandatory);
76     }
77
78     private static DataSchemaNode getRootSchemaNode(final SchemaContext schemaContext,
79             final YangInstanceIdentifier rootPath) {
80         final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
81         final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(rootPath);
82         Preconditions.checkArgument(rootContextNode != null, "Failed to find root %s in schema context", rootPath);
83
84         final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
85         Preconditions.checkArgument(rootSchemaNode instanceof DataNodeContainer,
86             "Root %s resolves to non-container type %s", rootPath, rootSchemaNode);
87         return rootSchemaNode;
88     }
89
90     private static NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
91             final YangInstanceIdentifier path) {
92         if (path.isEmpty()) {
93             Preconditions.checkArgument(schemaNode instanceof ContainerSchemaNode,
94                 "Conceptual tree root has to be a container, not %s", schemaNode);
95             return ROOT_CONTAINER;
96         }
97
98         final PathArgument arg = path.getLastPathArgument();
99         if (schemaNode instanceof ContainerSchemaNode) {
100             Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode,
101                 path);
102             return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
103         } else if (schemaNode instanceof ListSchemaNode) {
104             // This can either be a top-level list or its individual entry
105             if (arg instanceof NodeIdentifierWithPredicates) {
106                 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
107             }
108             Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
109             return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
110         } else {
111             throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
112         }
113     }
114
115     private static NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
116         if (path.isEmpty()) {
117             return ROOT_CONTAINER;
118         }
119
120         final PathArgument arg = path.getLastPathArgument();
121         if (arg instanceof NodeIdentifier) {
122             return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
123         }
124         if (arg instanceof NodeIdentifierWithPredicates) {
125             return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
126         }
127
128         // FIXME: implement augmentations and leaf-lists
129         throw new IllegalArgumentException("Unsupported root node " + arg);
130     }
131 }