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