553243f24d085ea01040b98fef197740908e5ad0
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / ImmutableNodes.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;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import java.util.Iterator;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeBuilder;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38
39 public final class ImmutableNodes {
40     // FIXME: YANGTOOLS-1074: we do not want this name
41     private static final NodeIdentifier SCHEMACONTEXT_NAME = NodeIdentifier.create(SchemaContext.NAME);
42
43     private ImmutableNodes() {
44         // Hidden on purpose
45     }
46
47     public static @NonNull CollectionNodeBuilder<MapEntryNode, SystemMapNode> mapNodeBuilder() {
48         return ImmutableMapNodeBuilder.create();
49     }
50
51     public static @NonNull CollectionNodeBuilder<MapEntryNode, SystemMapNode> mapNodeBuilder(final QName name) {
52         return mapNodeBuilder(NodeIdentifier.create(name));
53     }
54
55     public static @NonNull CollectionNodeBuilder<MapEntryNode, SystemMapNode> mapNodeBuilder(
56             final NodeIdentifier name) {
57         return ImmutableMapNodeBuilder.create().withNodeIdentifier(name);
58     }
59
60     /**
61      * Create an immutable map node.
62      *
63      * @param name QName which will be used as node identifier
64      * @return An unordered Map node
65      */
66     public static @NonNull SystemMapNode mapNode(final QName name) {
67         return mapNode(NodeIdentifier.create(name));
68     }
69
70     /**
71      * Create an immutable map node.
72      *
73      * @param name QName which will be used as node identifier
74      * @return An unordered Map node
75      */
76     public static @NonNull SystemMapNode mapNode(final NodeIdentifier name) {
77         return mapNodeBuilder(name).build();
78     }
79
80     /**
81      * Create immutable ordered map node.
82      *
83      * @param name QName which will be used as node identifier
84      * @return An ordered Map node
85      */
86     public static @NonNull UserMapNode orderedMapNode(final QName name) {
87         return orderedMapNode(NodeIdentifier.create(name));
88     }
89
90     /**
91      * Create immutable ordered map node.
92      *
93      * @param name Node identifier
94      * @return An ordered Map node
95      */
96     public static @NonNull UserMapNode orderedMapNode(final NodeIdentifier name) {
97         return ImmutableUserMapNodeBuilder.create().withNodeIdentifier(name).build();
98     }
99
100     /**
101      * Construct immutable leaf node.
102      *
103      * @param name Identifier of leaf node
104      * @param value Value of leaf node
105      * @param <T> Type of leaf node value
106      * @return Leaf node with supplied identifier and value
107      */
108     public static <T> @NonNull LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
109         return ImmutableLeafNodeBuilder.createNode(name, value);
110     }
111
112     /**
113      * Construct immutable leaf node.
114      *
115      * @param name QName which will be used as node identifier
116      * @param value Value of leaf node.
117      * @param <T> Type of leaf node value
118      * @return Leaf node with supplied identifier and value
119      */
120     public static <T> @NonNull LeafNode<T> leafNode(final QName name, final T value) {
121         return leafNode(NodeIdentifier.create(name), value);
122     }
123
124     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
125             final QName nodeName, final QName keyName, final Object keyValue) {
126         return ImmutableMapEntryNodeBuilder.create()
127                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(nodeName, keyName, keyValue))
128                 .withChild(leafNode(keyName, keyValue));
129     }
130
131     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
132         return ImmutableMapEntryNodeBuilder.create();
133     }
134
135     public static @NonNull MapEntryNode mapEntry(final QName nodeName, final QName keyName, final Object keyValue) {
136         return mapEntryBuilder(nodeName, keyName, keyValue).build();
137     }
138
139     /**
140      * Create an immutable container node.
141      *
142      * @param name QName which will be used as node identifier
143      * @return A container node
144      */
145     public static @NonNull ContainerNode containerNode(final QName name) {
146         return containerNode(NodeIdentifier.create(name));
147     }
148
149     /**
150      * Create an immutable container node.
151      *
152      * @param name Node identifier
153      * @return A container node
154      */
155     public static @NonNull ContainerNode containerNode(final NodeIdentifier name) {
156         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(name).build();
157     }
158
159     /**
160      * Create an immutable choice node.
161      *
162      * @param name QName which will be used as node identifier
163      * @return A choice node
164      */
165     public static @NonNull ChoiceNode choiceNode(final QName name) {
166         return choiceNode(NodeIdentifier.create(name));
167     }
168
169     /**
170      * Create an immutable choice node.
171      *
172      * @param name Node identifier
173      * @return A choice node
174      */
175     public static @NonNull ChoiceNode choiceNode(final NodeIdentifier name) {
176         return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(name).build();
177     }
178
179     /**
180      * Create an immutable list node.
181      *
182      * @param name QName which will be used as node identifier
183      * @return An unkeyed list node
184      */
185     public static @NonNull UnkeyedListNode listNode(final QName name) {
186         return listNode(NodeIdentifier.create(name));
187     }
188
189     /**
190      * Create an immutable list node.
191      *
192      * @param name Node identifier
193      * @return An unkeyed list node
194      */
195     public static @NonNull UnkeyedListNode listNode(final NodeIdentifier name) {
196         return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(name).build();
197     }
198
199     /**
200      * Convert YangInstanceIdentifier into a normalized node structure.
201      *
202      * @param ctx schema context to used during serialization
203      * @param id instance identifier to convert to node structure starting from root
204      * @return serialized normalized node for provided instance Id
205      */
206     public static @NonNull NormalizedNode fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id) {
207         final PathArgument topLevelElement;
208         final InstanceIdToNodes<?> instanceIdToNodes;
209         final Iterator<PathArgument> it = id.getPathArguments().iterator();
210         if (it.hasNext()) {
211             topLevelElement = it.next();
212             final DataSchemaNode dataChildByName = ctx.dataChildByName(topLevelElement.getNodeType());
213             checkNotNull(dataChildByName,
214                 "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
215             instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx, topLevelElement.getNodeType());
216         } else {
217             topLevelElement = SCHEMACONTEXT_NAME;
218             instanceIdToNodes = InstanceIdToNodes.fromDataSchemaNode(ctx);
219         }
220
221         return instanceIdToNodes.create(topLevelElement, it);
222     }
223 }