Add inject annotations
[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     private static final NormalizedNode<?, ?> ROOT_CONTAINER = ImmutableNodes.containerNode(SchemaContext.NAME);
46
47     @Override
48     public DataTree create(final DataTreeConfiguration treeConfig) {
49         return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
50             Version.initial()), treeConfig, null);
51     }
52
53     @Override
54     public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext) {
55         return create(treeConfig, initialSchemaContext, true);
56     }
57
58     @Override
59     public DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
60             final NormalizedNodeContainer<?, ?, ?> initialRoot) throws DataValidationFailedException {
61         final DataTree ret = create(treeConfig, initialSchemaContext, false);
62
63         final DataTreeModification mod = ret.takeSnapshot().newModification();
64         mod.write(YangInstanceIdentifier.empty(), initialRoot);
65         mod.ready();
66
67         ret.validate(mod);
68         final DataTreeCandidate candidate = ret.prepare(mod);
69         ret.commit(candidate);
70         return ret;
71     }
72
73     private static DataTree create(final DataTreeConfiguration treeConfig, final SchemaContext initialSchemaContext,
74             final boolean maskMandatory) {
75         final DataSchemaNode rootSchemaNode = getRootSchemaNode(initialSchemaContext, treeConfig.getRootPath());
76         final NormalizedNode<?, ?> rootDataNode = createRoot((DataNodeContainer)rootSchemaNode,
77             treeConfig.getRootPath());
78         return new InMemoryDataTree(TreeNodeFactory.createTreeNode(rootDataNode, Version.initial()), treeConfig,
79             initialSchemaContext, rootSchemaNode, maskMandatory);
80     }
81
82     private static DataSchemaNode getRootSchemaNode(final SchemaContext schemaContext,
83             final YangInstanceIdentifier rootPath) {
84         final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
85         final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(rootPath);
86         checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
87
88         final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
89         checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s",
90             rootPath, rootSchemaNode);
91         return rootSchemaNode;
92     }
93
94     private static NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
95             final YangInstanceIdentifier path) {
96         if (path.isEmpty()) {
97             checkArgument(schemaNode instanceof ContainerSchemaNode,
98                 "Conceptual tree root has to be a container, not %s", schemaNode);
99             return ROOT_CONTAINER;
100         }
101
102         final PathArgument arg = path.getLastPathArgument();
103         if (schemaNode instanceof ContainerSchemaNode) {
104             checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode, path);
105             return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
106         } else if (schemaNode instanceof ListSchemaNode) {
107             // This can either be a top-level list or its individual entry
108             if (arg instanceof NodeIdentifierWithPredicates) {
109                 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
110             }
111             checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
112             return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
113         } else {
114             throw new IllegalArgumentException("Unsupported root schema " + schemaNode);
115         }
116     }
117
118     private static NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
119         if (path.isEmpty()) {
120             return ROOT_CONTAINER;
121         }
122
123         final PathArgument arg = path.getLastPathArgument();
124         if (arg instanceof NodeIdentifier) {
125             return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
126         }
127         if (arg instanceof NodeIdentifierWithPredicates) {
128             return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
129         }
130
131         // FIXME: implement augmentations and leaf-lists
132         throw new IllegalArgumentException("Unsupported root node " + arg);
133     }
134 }