Tolerate empty instance identifier in ImmutableNodes.fromInstanceId()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / ImmutableNodes.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
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;
40
41 public final class ImmutableNodes {
42     private static final NodeIdentifier SCHEMACONTEXT_NAME = NodeIdentifier.create(SchemaContext.NAME);
43
44     private ImmutableNodes() {
45         throw new UnsupportedOperationException("Utilities class should not be instantiated");
46     }
47
48     public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder() {
49         return ImmutableMapNodeBuilder.create();
50     }
51
52     public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final QName name) {
53         return mapNodeBuilder(NodeIdentifier.create(name));
54     }
55
56     public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final NodeIdentifier name) {
57         return ImmutableMapNodeBuilder.create().withNodeIdentifier(name);
58     }
59
60     /**
61      * Create an immutable map node.
62      *
63      * @param name QName which will be used as node identifier
64      * @return An unordered Map node
65      */
66     public static MapNode mapNode(final QName name) {
67         return mapNodeBuilder(name).build();
68     }
69
70     private static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapNodeBuilder(final QName qname) {
71         return orderedMapNodeBuilder().withNodeIdentifier(NodeIdentifier.create(qname));
72     }
73
74     private static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapNodeBuilder() {
75         return ImmutableOrderedMapNodeBuilder.create();
76     }
77
78     /**
79      * Create immutable ordered map node.
80      *
81      * @param name QName which will be used as node identifier
82      * @return An ordered Map node
83      */
84     public static OrderedMapNode orderedMapNode(final QName name) {
85         return orderedMapNodeBuilder(name).build();
86     }
87
88     /**
89      * Construct immutable leaf node.
90      *
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
95      */
96     public static <T> LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
97         return ImmutableLeafNodeBuilder.<T>create()
98                 .withNodeIdentifier(name)
99                 .withValue(value)
100                 .build();
101     }
102
103     /**
104      * Construct immutable leaf node.
105      *
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
110      */
111     public static <T> LeafNode<T> leafNode(final QName name, final T value) {
112         return leafNode(NodeIdentifier.create(name), value);
113     }
114
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));
120     }
121
122     public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
123         return ImmutableMapEntryNodeBuilder.create();
124     }
125
126     public static MapEntryNode mapEntry(final QName nodeName,final QName keyName,final Object keyValue) {
127         return mapEntryBuilder(nodeName, keyName, keyValue).build();
128     }
129
130     /**
131      * Create an immutable container node.
132      *
133      * @param name QName which will be used as node identifier
134      * @return A container node
135      */
136     public static ContainerNode containerNode(final QName name) {
137         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
138     }
139
140     /**
141      * Create an immutable choice node.
142      *
143      * @param name QName which will be used as node identifier
144      * @return A choice node
145      */
146     public static ChoiceNode choiceNode(final QName name) {
147         return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
148     }
149
150     /**
151      * Create an immutable list node.
152      *
153      * @param name QName which will be used as node identifier
154      * @return An unkeyed list node
155      */
156     public static UnkeyedListNode listNode(final QName name) {
157         return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
158     }
159
160     /**
161      * Convert YangInstanceIdentifier into a normalized node structure.
162      *
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
166      */
167     public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id) {
168         return fromInstanceId(ctx, id, Optional.empty(), Optional.empty());
169     }
170
171     /**
172      * Convert YangInstanceIdentifier into a normalized node structure.
173      *
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.
179      */
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());
183     }
184
185     /**
186      * Convert YangInstanceIdentifier into a normalized node structure.
187      *
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.
196      */
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();
202         if (it.hasNext()) {
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());
208         } else {
209             topLevelElement = SCHEMACONTEXT_NAME;
210             instanceIdToNodes = InstanceIdToNodes.fromDataSchemaNode(ctx);
211         }
212
213         return instanceIdToNodes.create(topLevelElement, it, deepestElement, operation);
214     }
215 }