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