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 // FIXME: YANGTOOLS-1074: we do not want this name
42 private static final NodeIdentifier SCHEMACONTEXT_NAME = NodeIdentifier.create(SchemaContext.NAME);
44 private ImmutableNodes() {
48 public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder() {
49 return ImmutableMapNodeBuilder.create();
52 public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final QName name) {
53 return mapNodeBuilder(NodeIdentifier.create(name));
56 public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final NodeIdentifier name) {
57 return ImmutableMapNodeBuilder.create().withNodeIdentifier(name);
61 * Create an immutable map node.
63 * @param name QName which will be used as node identifier
64 * @return An unordered Map node
66 public static @NonNull MapNode mapNode(final QName name) {
67 return mapNode(NodeIdentifier.create(name));
71 * Create an immutable map node.
73 * @param name QName which will be used as node identifier
74 * @return An unordered Map node
76 public static @NonNull MapNode mapNode(final NodeIdentifier name) {
77 return mapNodeBuilder(name).build();
81 * Create immutable ordered map node.
83 * @param name QName which will be used as node identifier
84 * @return An ordered Map node
86 public static @NonNull OrderedMapNode orderedMapNode(final QName name) {
87 return orderedMapNode(NodeIdentifier.create(name));
91 * Create immutable ordered map node.
93 * @param name Node identifier
94 * @return An ordered Map node
96 public static @NonNull OrderedMapNode orderedMapNode(final NodeIdentifier name) {
97 return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(name).build();
101 * Construct immutable leaf node.
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
108 public static <T> @NonNull LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
109 return ImmutableLeafNodeBuilder.createNode(name, value);
113 * Construct immutable leaf node.
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
120 public static <T> @NonNull LeafNode<T> leafNode(final QName name, final T value) {
121 return leafNode(NodeIdentifier.create(name), value);
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));
131 public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
132 return ImmutableMapEntryNodeBuilder.create();
135 public static @NonNull MapEntryNode mapEntry(final QName nodeName, final QName keyName, final Object keyValue) {
136 return mapEntryBuilder(nodeName, keyName, keyValue).build();
140 * Create an immutable container node.
142 * @param name QName which will be used as node identifier
143 * @return A container node
145 public static @NonNull ContainerNode containerNode(final QName name) {
146 return containerNode(NodeIdentifier.create(name));
150 * Create an immutable container node.
152 * @param name Node identifier
153 * @return A container node
155 public static @NonNull ContainerNode containerNode(final NodeIdentifier name) {
156 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(name).build();
160 * Create an immutable choice node.
162 * @param name QName which will be used as node identifier
163 * @return A choice node
165 public static @NonNull ChoiceNode choiceNode(final QName name) {
166 return choiceNode(NodeIdentifier.create(name));
170 * Create an immutable choice node.
172 * @param name Node identifier
173 * @return A choice node
175 public static @NonNull ChoiceNode choiceNode(final NodeIdentifier name) {
176 return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(name).build();
180 * Create an immutable list node.
182 * @param name QName which will be used as node identifier
183 * @return An unkeyed list node
185 public static @NonNull UnkeyedListNode listNode(final QName name) {
186 return listNode(NodeIdentifier.create(name));
190 * Create an immutable list node.
192 * @param name Node identifier
193 * @return An unkeyed list node
195 public static @NonNull UnkeyedListNode listNode(final NodeIdentifier name) {
196 return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(name).build();
200 * Convert YangInstanceIdentifier into a normalized node structure.
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
206 public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx,
207 final YangInstanceIdentifier id) {
208 return fromInstanceId(ctx, id, Optional.empty());
212 * Convert YangInstanceIdentifier into a normalized node structure.
214 * @param ctx schema context to used during serialization
215 * @param id instance identifier to convert to node structure starting from root
216 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
217 * instance identifier
218 * @return serialized normalized node for provided instance Id with overridden last child.
220 public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
221 final NormalizedNode<?, ?> deepestElement) {
222 return fromInstanceId(ctx, id, Optional.of(deepestElement));
226 * Convert YangInstanceIdentifier into a normalized node structure.
228 * @param ctx schema context to used during serialization
229 * @param id instance identifier to convert to node structure starting from root
230 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
231 * instance identifier
232 * @return serialized normalized node for provided instance Id with (optionally) overridden last child
233 * and (optionally) marked with specific operation attribute.
235 public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
236 final Optional<NormalizedNode<?, ?>> deepestElement) {
237 final PathArgument topLevelElement;
238 final InstanceIdToNodes<?> instanceIdToNodes;
239 final Iterator<PathArgument> it = id.getPathArguments().iterator();
241 topLevelElement = it.next();
242 final DataSchemaNode dataChildByName = ctx.dataChildByName(topLevelElement.getNodeType());
243 checkNotNull(dataChildByName,
244 "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
245 instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx, topLevelElement.getNodeType());
247 topLevelElement = SCHEMACONTEXT_NAME;
248 instanceIdToNodes = InstanceIdToNodes.fromDataSchemaNode(ctx);
251 return instanceIdToNodes.create(topLevelElement, it, deepestElement);