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 {
42 private static final NodeIdentifier SCHEMACONTEXT_NAME = NodeIdentifier.create(SchemaContext.NAME);
44 private ImmutableNodes() {
45 throw new UnsupportedOperationException("Utilities class should not be instantiated");
48 public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder() {
49 return ImmutableMapNodeBuilder.create();
52 public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final QName name) {
53 return mapNodeBuilder(NodeIdentifier.create(name));
56 public static 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 MapNode mapNode(final QName name) {
67 return mapNodeBuilder(name).build();
70 private static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapNodeBuilder(final QName qname) {
71 return orderedMapNodeBuilder().withNodeIdentifier(NodeIdentifier.create(qname));
74 private static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapNodeBuilder() {
75 return ImmutableOrderedMapNodeBuilder.create();
79 * Create immutable ordered map node.
81 * @param name QName which will be used as node identifier
82 * @return An ordered Map node
84 public static OrderedMapNode orderedMapNode(final QName name) {
85 return orderedMapNodeBuilder(name).build();
89 * Construct immutable leaf node.
91 * @param name Identifier of leaf node
92 * @param value Value of leaf node
93 * @param <T> Type of leaf node value
94 * @return Leaf node with supplied identifier and value
96 public static <T> LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
97 return ImmutableLeafNodeBuilder.<T>create()
98 .withNodeIdentifier(name)
104 * Construct immutable leaf node.
106 * @param name QName which will be used as node identifier
107 * @param value Value of leaf node.
108 * @param <T> Type of leaf node value
109 * @return Leaf node with supplied identifier and value
111 public static <T> LeafNode<T> leafNode(final QName name, final T value) {
112 return leafNode(NodeIdentifier.create(name), value);
115 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
116 final QName nodeName, final QName keyName, final Object keyValue) {
117 return ImmutableMapEntryNodeBuilder.create()
118 .withNodeIdentifier(new NodeIdentifierWithPredicates(nodeName, keyName, keyValue))
119 .withChild(leafNode(keyName, keyValue));
122 public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
123 return ImmutableMapEntryNodeBuilder.create();
126 public static MapEntryNode mapEntry(final QName nodeName,final QName keyName,final Object keyValue) {
127 return mapEntryBuilder(nodeName, keyName, keyValue).build();
131 * Create an immutable container node.
133 * @param name QName which will be used as node identifier
134 * @return A container node
136 public static ContainerNode containerNode(final QName name) {
137 return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
141 * Create an immutable choice node.
143 * @param name QName which will be used as node identifier
144 * @return A choice node
146 public static ChoiceNode choiceNode(final QName name) {
147 return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
151 * Create an immutable list node.
153 * @param name QName which will be used as node identifier
154 * @return An unkeyed list node
156 public static UnkeyedListNode listNode(final QName name) {
157 return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
161 * Convert YangInstanceIdentifier into a normalized node structure.
163 * @param ctx schema context to used during serialization
164 * @param id instance identifier to convert to node structure starting from root
165 * @return serialized normalized node for provided instance Id
167 public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id) {
168 return fromInstanceId(ctx, id, Optional.empty(), Optional.empty());
172 * Convert YangInstanceIdentifier into a normalized node structure.
174 * @param ctx schema context to used during serialization
175 * @param id instance identifier to convert to node structure starting from root
176 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
177 * instance identifier
178 * @return serialized normalized node for provided instance Id with overridden last child.
180 public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
181 final NormalizedNode<?, ?> deepestElement) {
182 return fromInstanceId(ctx, id, Optional.of(deepestElement), Optional.empty());
186 * Convert YangInstanceIdentifier into a normalized node structure.
188 * @param ctx schema context to used during serialization
189 * @param id instance identifier to convert to node structure starting from root
190 * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
191 * instance identifier
192 * @param operation modify operation attribute to be added to the deepest child. QName is the operation attribute
193 * key and ModifyAction is the value.
194 * @return serialized normalized node for provided instance Id with (optionally) overridden last child
195 * and (optionally) marked with specific operation attribute.
197 public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
198 final Optional<NormalizedNode<?, ?>> deepestElement, final Optional<Entry<QName, ModifyAction>> operation) {
199 final PathArgument topLevelElement;
200 final InstanceIdToNodes<?> instanceIdToNodes;
201 final Iterator<PathArgument> it = id.getPathArguments().iterator();
203 topLevelElement = it.next();
204 final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
205 checkNotNull(dataChildByName,
206 "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
207 instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx, topLevelElement.getNodeType());
209 topLevelElement = SCHEMACONTEXT_NAME;
210 instanceIdToNodes = InstanceIdToNodes.fromDataSchemaNode(ctx);
213 return instanceIdToNodes.create(topLevelElement, it, deepestElement, operation);