2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import java.util.Iterator;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.QName;
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.ChoiceNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 public final class ImmutableNodes {
41 private static final NodeIdentifier SCHEMACONTEXT_NAME = NodeIdentifier.create(SchemaContext.NAME);
43 private ImmutableNodes() {
44 throw new UnsupportedOperationException("Utilities class should not be instantiated");
47 public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder() {
48 return ImmutableMapNodeBuilder.create();
51 public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final QName name) {
52 return mapNodeBuilder(NodeIdentifier.create(name));
55 public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final NodeIdentifier name) {
56 return ImmutableMapNodeBuilder.create().withNodeIdentifier(name);
60 * Create an immutable map node.
62 * @param name QName which will be used as node identifier
63 * @return An unordered Map node
65 public static @NonNull MapNode mapNode(final QName name) {
66 return mapNode(NodeIdentifier.create(name));
70 * Create an immutable map node.
72 * @param name QName which will be used as node identifier
73 * @return An unordered Map node
75 public static @NonNull MapNode mapNode(final NodeIdentifier name) {
76 return mapNodeBuilder(name).build();
80 * Create immutable ordered map node.
82 * @param name QName which will be used as node identifier
83 * @return An ordered Map node
85 public static @NonNull OrderedMapNode orderedMapNode(final QName name) {
86 return orderedMapNode(NodeIdentifier.create(name));
90 * Create immutable ordered map node.
92 * @param name Node identifier
93 * @return An ordered Map node
95 public static @NonNull OrderedMapNode orderedMapNode(final NodeIdentifier name) {
96 return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(name).build();
100 * Construct immutable leaf node.
102 * @param name Identifier of leaf node
103 * @param value Value of leaf node
104 * @param <T> Type of leaf node value
105 * @return Leaf node with supplied identifier and value
107 public static <T> @NonNull LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
108 return ImmutableLeafNodeBuilder.<T>create()
109 .withNodeIdentifier(name)
115 * Construct immutable leaf node.
117 * @param name QName which will be used as node identifier
118 * @param value Value of leaf node.
119 * @param <T> Type of leaf node value
120 * @return Leaf node with supplied identifier and value
122 public static <T> @NonNull LeafNode<T> leafNode(final QName name, final T value) {
123 return leafNode(NodeIdentifier.create(name), value);
126 public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
127 final QName nodeName, final QName keyName, final Object keyValue) {
128 return ImmutableMapEntryNodeBuilder.create()
129 .withNodeIdentifier(NodeIdentifierWithPredicates.of(nodeName, keyName, keyValue))
130 .withChild(leafNode(keyName, keyValue));
133 public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
134 return ImmutableMapEntryNodeBuilder.create();
137 public static @NonNull MapEntryNode mapEntry(final QName nodeName, final QName keyName, final Object keyValue) {
138 return mapEntryBuilder(nodeName, keyName, keyValue).build();
142 * Create an immutable container node.
144 * @param name QName which will be used as node identifier
145 * @return A container node
147 public static @NonNull ContainerNode containerNode(final QName name) {
148 return containerNode(NodeIdentifier.create(name));
152 * Create an immutable container node.
154 * @param name Node identifier
155 * @return A container node
157 public static @NonNull ContainerNode containerNode(final NodeIdentifier name) {
158 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(name).build();
162 * Create an immutable choice node.
164 * @param name QName which will be used as node identifier
165 * @return A choice node
167 public static @NonNull ChoiceNode choiceNode(final QName name) {
168 return choiceNode(NodeIdentifier.create(name));
172 * Create an immutable choice node.
174 * @param name Node identifier
175 * @return A choice node
177 public static @NonNull ChoiceNode choiceNode(final NodeIdentifier name) {
178 return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(name).build();
182 * Create an immutable list node.
184 * @param name QName which will be used as node identifier
185 * @return An unkeyed list node
187 public static @NonNull UnkeyedListNode listNode(final QName name) {
188 return listNode(NodeIdentifier.create(name));
192 * Create an immutable list node.
194 * @param name Node identifier
195 * @return An unkeyed list node
197 public static @NonNull UnkeyedListNode listNode(final NodeIdentifier name) {
198 return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(name).build();
202 * Convert YangInstanceIdentifier into a normalized node structure.
204 * @param ctx schema context to used during serialization
205 * @param id instance identifier to convert to node structure starting from root
206 * @return serialized normalized node for provided instance Id
208 public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx,
209 final YangInstanceIdentifier id) {
210 return fromInstanceId(ctx, id, Optional.empty());
214 * Convert YangInstanceIdentifier into a normalized node structure.
216 * @param ctx schema context to used during serialization
217 * @param id instance identifier to convert to node structure starting from root
218 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
219 * instance identifier
220 * @return serialized normalized node for provided instance Id with overridden last child.
222 public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
223 final NormalizedNode<?, ?> deepestElement) {
224 return fromInstanceId(ctx, id, Optional.of(deepestElement));
228 * Convert YangInstanceIdentifier into a normalized node structure.
230 * @param ctx schema context to used during serialization
231 * @param id instance identifier to convert to node structure starting from root
232 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
233 * instance identifier
234 * @return serialized normalized node for provided instance Id with (optionally) overridden last child
235 * and (optionally) marked with specific operation attribute.
237 public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
238 final Optional<NormalizedNode<?, ?>> deepestElement) {
239 final PathArgument topLevelElement;
240 final InstanceIdToNodes<?> instanceIdToNodes;
241 final Iterator<PathArgument> it = id.getPathArguments().iterator();
243 topLevelElement = it.next();
244 final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
245 checkNotNull(dataChildByName,
246 "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
247 instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx, topLevelElement.getNodeType());
249 topLevelElement = SCHEMACONTEXT_NAME;
250 instanceIdToNodes = InstanceIdToNodes.fromDataSchemaNode(ctx);
253 return instanceIdToNodes.create(topLevelElement, it, deepestElement);