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