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