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