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.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.UserMapNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.api.schema.builder.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.ImmutableUnkeyedListNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUserMapNodeBuilder;
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, SystemMapNode> mapNodeBuilder() {
49 return ImmutableMapNodeBuilder.create();
52 public static @NonNull CollectionNodeBuilder<MapEntryNode, SystemMapNode> mapNodeBuilder(final QName name) {
53 return mapNodeBuilder(NodeIdentifier.create(name));
56 public static @NonNull CollectionNodeBuilder<MapEntryNode, SystemMapNode> mapNodeBuilder(
57 final NodeIdentifier name) {
58 return ImmutableMapNodeBuilder.create().withNodeIdentifier(name);
62 * Create an immutable map node.
64 * @param name QName which will be used as node identifier
65 * @return An unordered Map node
67 public static @NonNull SystemMapNode mapNode(final QName name) {
68 return mapNode(NodeIdentifier.create(name));
72 * Create an immutable map node.
74 * @param name QName which will be used as node identifier
75 * @return An unordered Map node
77 public static @NonNull SystemMapNode mapNode(final NodeIdentifier name) {
78 return mapNodeBuilder(name).build();
82 * Create immutable ordered map node.
84 * @param name QName which will be used as node identifier
85 * @return An ordered Map node
87 public static @NonNull UserMapNode orderedMapNode(final QName name) {
88 return orderedMapNode(NodeIdentifier.create(name));
92 * Create immutable ordered map node.
94 * @param name Node identifier
95 * @return An ordered Map node
97 public static @NonNull UserMapNode orderedMapNode(final NodeIdentifier name) {
98 return ImmutableUserMapNodeBuilder.create().withNodeIdentifier(name).build();
102 * Construct immutable leaf node.
104 * @param name Identifier of leaf node
105 * @param value Value of leaf node
106 * @param <T> Type of leaf node value
107 * @return Leaf node with supplied identifier and value
109 public static <T> @NonNull LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
110 return ImmutableLeafNodeBuilder.createNode(name, value);
114 * Construct immutable leaf node.
116 * @param name QName which will be used as node identifier
117 * @param value Value of leaf node.
118 * @param <T> Type of leaf node value
119 * @return Leaf node with supplied identifier and value
121 public static <T> @NonNull LeafNode<T> leafNode(final QName name, final T value) {
122 return leafNode(NodeIdentifier.create(name), value);
125 public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
126 final QName nodeName, final QName keyName, final Object keyValue) {
127 return ImmutableMapEntryNodeBuilder.create()
128 .withNodeIdentifier(NodeIdentifierWithPredicates.of(nodeName, keyName, keyValue))
129 .withChild(leafNode(keyName, keyValue));
132 public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
133 return ImmutableMapEntryNodeBuilder.create();
136 public static @NonNull MapEntryNode mapEntry(final QName nodeName, final QName keyName, final Object keyValue) {
137 return mapEntryBuilder(nodeName, keyName, keyValue).build();
141 * Create an immutable container node.
143 * @param name QName which will be used as node identifier
144 * @return A container node
146 public static @NonNull ContainerNode containerNode(final QName name) {
147 return containerNode(NodeIdentifier.create(name));
151 * Create an immutable container node.
153 * @param name Node identifier
154 * @return A container node
156 public static @NonNull ContainerNode containerNode(final NodeIdentifier name) {
157 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(name).build();
161 * Create an immutable choice node.
163 * @param name QName which will be used as node identifier
164 * @return A choice node
166 public static @NonNull ChoiceNode choiceNode(final QName name) {
167 return choiceNode(NodeIdentifier.create(name));
171 * Create an immutable choice node.
173 * @param name Node identifier
174 * @return A choice node
176 public static @NonNull ChoiceNode choiceNode(final NodeIdentifier name) {
177 return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(name).build();
181 * Create an immutable list node.
183 * @param name QName which will be used as node identifier
184 * @return An unkeyed list node
186 public static @NonNull UnkeyedListNode listNode(final QName name) {
187 return listNode(NodeIdentifier.create(name));
191 * Create an immutable list node.
193 * @param name Node identifier
194 * @return An unkeyed list node
196 public static @NonNull UnkeyedListNode listNode(final NodeIdentifier name) {
197 return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(name).build();
201 * Convert YangInstanceIdentifier into a normalized node structure.
203 * @param ctx schema context to used during serialization
204 * @param id instance identifier to convert to node structure starting from root
205 * @return serialized normalized node for provided instance Id
207 public static @NonNull NormalizedNode fromInstanceId(final SchemaContext ctx, 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);