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.createNode(name, value);
112 * Construct immutable leaf node.
114 * @param name QName which will be used as node identifier
115 * @param value Value of leaf node.
116 * @param <T> Type of leaf node value
117 * @return Leaf node with supplied identifier and value
119 public static <T> @NonNull LeafNode<T> leafNode(final QName name, final T value) {
120 return leafNode(NodeIdentifier.create(name), value);
123 public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
124 final QName nodeName, final QName keyName, final Object keyValue) {
125 return ImmutableMapEntryNodeBuilder.create()
126 .withNodeIdentifier(NodeIdentifierWithPredicates.of(nodeName, keyName, keyValue))
127 .withChild(leafNode(keyName, keyValue));
130 public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
131 return ImmutableMapEntryNodeBuilder.create();
134 public static @NonNull MapEntryNode mapEntry(final QName nodeName, final QName keyName, final Object keyValue) {
135 return mapEntryBuilder(nodeName, keyName, keyValue).build();
139 * Create an immutable container node.
141 * @param name QName which will be used as node identifier
142 * @return A container node
144 public static @NonNull ContainerNode containerNode(final QName name) {
145 return containerNode(NodeIdentifier.create(name));
149 * Create an immutable container node.
151 * @param name Node identifier
152 * @return A container node
154 public static @NonNull ContainerNode containerNode(final NodeIdentifier name) {
155 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(name).build();
159 * Create an immutable choice node.
161 * @param name QName which will be used as node identifier
162 * @return A choice node
164 public static @NonNull ChoiceNode choiceNode(final QName name) {
165 return choiceNode(NodeIdentifier.create(name));
169 * Create an immutable choice node.
171 * @param name Node identifier
172 * @return A choice node
174 public static @NonNull ChoiceNode choiceNode(final NodeIdentifier name) {
175 return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(name).build();
179 * Create an immutable list node.
181 * @param name QName which will be used as node identifier
182 * @return An unkeyed list node
184 public static @NonNull UnkeyedListNode listNode(final QName name) {
185 return listNode(NodeIdentifier.create(name));
189 * Create an immutable list node.
191 * @param name Node identifier
192 * @return An unkeyed list node
194 public static @NonNull UnkeyedListNode listNode(final NodeIdentifier name) {
195 return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(name).build();
199 * Convert YangInstanceIdentifier into a normalized node structure.
201 * @param ctx schema context to used during serialization
202 * @param id instance identifier to convert to node structure starting from root
203 * @return serialized normalized node for provided instance Id
205 public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx,
206 final YangInstanceIdentifier id) {
207 return fromInstanceId(ctx, id, Optional.empty());
211 * Convert YangInstanceIdentifier into a normalized node structure.
213 * @param ctx schema context to used during serialization
214 * @param id instance identifier to convert to node structure starting from root
215 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
216 * instance identifier
217 * @return serialized normalized node for provided instance Id with overridden last child.
219 public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
220 final NormalizedNode<?, ?> deepestElement) {
221 return fromInstanceId(ctx, id, Optional.of(deepestElement));
225 * Convert YangInstanceIdentifier into a normalized node structure.
227 * @param ctx schema context to used during serialization
228 * @param id instance identifier to convert to node structure starting from root
229 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
230 * instance identifier
231 * @return serialized normalized node for provided instance Id with (optionally) overridden last child
232 * and (optionally) marked with specific operation attribute.
234 public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
235 final Optional<NormalizedNode<?, ?>> deepestElement) {
236 final PathArgument topLevelElement;
237 final InstanceIdToNodes<?> instanceIdToNodes;
238 final Iterator<PathArgument> it = id.getPathArguments().iterator();
240 topLevelElement = it.next();
241 final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
242 checkNotNull(dataChildByName,
243 "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
244 instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx, topLevelElement.getNodeType());
246 topLevelElement = SCHEMACONTEXT_NAME;
247 instanceIdToNodes = InstanceIdToNodes.fromDataSchemaNode(ctx);
250 return instanceIdToNodes.create(topLevelElement, it, deepestElement);