ca32f6c1532167b43efb1bdcf35f6a99a260d756
[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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration.Builder;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeFactory;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNodeFactory;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25
26 /**
27  * A factory for creating in-memory data trees.
28  */
29 public final class InMemoryDataTreeFactory implements DataTreeFactory {
30     private static final InMemoryDataTreeFactory INSTANCE = new InMemoryDataTreeFactory();
31     private static final NormalizedNode<?, ?> ROOT_CONTAINER = ImmutableNodes.containerNode(SchemaContext.NAME);
32
33     private InMemoryDataTreeFactory() {
34         // Never instantiated externally
35     }
36
37     @Override
38     public TipProducingDataTree create(final TreeType treeType) {
39         return create(DataTreeConfiguration.getDefault(treeType));
40     }
41
42     @Override
43     public TipProducingDataTree create(final DataTreeConfiguration treeConfig) {
44         return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(treeConfig.getRootPath()),
45             Version.initial()), treeConfig, null);
46     }
47
48     private static NormalizedNode<?, ?> createRoot(final YangInstanceIdentifier path) {
49         if (path.isEmpty()) {
50             return ROOT_CONTAINER;
51         }
52
53         final PathArgument arg = path.getLastPathArgument();
54         if (arg instanceof NodeIdentifier) {
55             return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
56         }
57         if (arg instanceof NodeIdentifierWithPredicates) {
58             return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
59         }
60
61         // FIXME: implement augmentations and leaf-lists
62         throw new IllegalArgumentException("Unsupported root node " + arg);
63     }
64
65     @Override
66     public TipProducingDataTree create(final TreeType treeType, final YangInstanceIdentifier rootPath) {
67         if (rootPath.isEmpty()) {
68             return create(treeType);
69         }
70
71         final DataTreeConfiguration defConfig = DataTreeConfiguration.getDefault(treeType);
72         final DataTreeConfiguration config;
73         if (!rootPath.isEmpty()) {
74             config = new Builder(treeType).setMandatoryNodesValidation(defConfig.isMandatoryNodesValidationEnabled())
75                     .setRootPath(rootPath).setUniqueIndexes(defConfig.isUniqueIndexEnabled()).build();
76         } else {
77             config = defConfig;
78         }
79
80         return new InMemoryDataTree(TreeNodeFactory.createTreeNode(createRoot(rootPath), Version.initial()), config,
81             null);
82     }
83
84     /**
85      * Get an instance of this factory. This method cannot fail.
86      *
87      * @return Data tree factory instance.
88      */
89     public static InMemoryDataTreeFactory getInstance() {
90         return INSTANCE;
91     }
92 }