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.Map.Entry;
14 import java.util.Optional;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 public final class ImmutableNodes {
43 private ImmutableNodes() {
44 throw new UnsupportedOperationException("Utilities class should not be instantiated");
47 public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder() {
48 return ImmutableMapNodeBuilder.create();
51 public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final QName name) {
52 return mapNodeBuilder(NodeIdentifier.create(name));
55 public static 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 MapNode mapNode(final QName name) {
66 return mapNodeBuilder(name).build();
69 private static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapNodeBuilder(final QName qname) {
70 return orderedMapNodeBuilder().withNodeIdentifier(NodeIdentifier.create(qname));
73 private static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapNodeBuilder() {
74 return ImmutableOrderedMapNodeBuilder.create();
78 * Create immutable ordered map node.
80 * @param name QName which will be used as node identifier
81 * @return An ordered Map node
83 public static OrderedMapNode orderedMapNode(final QName name) {
84 return orderedMapNodeBuilder(name).build();
88 * Construct immutable leaf node.
90 * @param name Identifier of leaf node
91 * @param value Value of leaf node
92 * @param <T> Type of leaf node value
93 * @return Leaf node with supplied identifier and value
95 public static <T> LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
96 return ImmutableLeafNodeBuilder.<T>create()
97 .withNodeIdentifier(name)
103 * Construct immutable leaf node.
105 * @param name QName which will be used as node identifier
106 * @param value Value of leaf node.
107 * @param <T> Type of leaf node value
108 * @return Leaf node with supplied identifier and value
110 public static <T> LeafNode<T> leafNode(final QName name, final T value) {
111 return leafNode(NodeIdentifier.create(name), value);
114 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
115 final QName nodeName, final QName keyName, final Object keyValue) {
116 return ImmutableMapEntryNodeBuilder.create()
117 .withNodeIdentifier(new NodeIdentifierWithPredicates(nodeName, keyName, keyValue))
118 .withChild(leafNode(keyName, keyValue));
121 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
122 return ImmutableMapEntryNodeBuilder.create();
125 public static MapEntryNode mapEntry(final QName nodeName,final QName keyName,final Object keyValue) {
126 return mapEntryBuilder(nodeName, keyName, keyValue).build();
130 * Create an immutable container node.
132 * @param name QName which will be used as node identifier
133 * @return A container node
135 public static ContainerNode containerNode(final QName name) {
136 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
140 * Create an immutable choice node.
142 * @param name QName which will be used as node identifier
143 * @return A choice node
145 public static ChoiceNode choiceNode(final QName name) {
146 return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
150 * Create an immutable list node.
152 * @param name QName which will be used as node identifier
153 * @return An unkeyed list node
155 public static UnkeyedListNode listNode(final QName name) {
156 return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
160 * Convert YangInstanceIdentifier into a normalized node structure.
162 * @param ctx schema context to used during serialization
163 * @param id instance identifier to convert to node structure starting from root
164 * @return serialized normalized node for provided instance Id
166 public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id) {
167 return fromInstanceId(ctx, id, Optional.empty(), Optional.empty());
171 * Convert YangInstanceIdentifier into a normalized node structure.
173 * @param ctx schema context to used during serialization
174 * @param id instance identifier to convert to node structure starting from root
175 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
176 * instance identifier
177 * @return serialized normalized node for provided instance Id with overridden last child.
179 public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
180 final NormalizedNode<?, ?> deepestElement) {
181 return fromInstanceId(ctx, id, Optional.of(deepestElement), Optional.empty());
185 * Convert YangInstanceIdentifier into a normalized node structure.
187 * @param ctx schema context to used during serialization
188 * @param id instance identifier to convert to node structure starting from root
189 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
190 * instance identifier
191 * @param operation modify operation attribute to be added to the deepest child. QName is the operation attribute
192 * key and ModifyAction is the value.
193 * @return serialized normalized node for provided instance Id with (optionally) overridden last child
194 * and (optionally) marked with specific operation attribute.
196 public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
197 final Optional<NormalizedNode<?, ?>> deepestElement, final Optional<Entry<QName, ModifyAction>> operation) {
198 final Iterator<PathArgument> it = id.getPathArguments().iterator();
199 final PathArgument topLevelElement = it.next();
200 final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
201 checkNotNull(dataChildByName,
202 "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
203 final InstanceIdToNodes<?> instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx,
204 topLevelElement.getNodeType());
205 return instanceIdToNodes.create(topLevelElement, it, deepestElement, operation);