260a28d93ddf83c2269932807cbf442733f43c90
[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.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;
39
40 public final class ImmutableNodes {
41     private static final NodeIdentifier SCHEMACONTEXT_NAME = NodeIdentifier.create(SchemaContext.NAME);
42
43     private ImmutableNodes() {
44         throw new UnsupportedOperationException("Utilities class should not be instantiated");
45     }
46
47     public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder() {
48         return ImmutableMapNodeBuilder.create();
49     }
50
51     public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final QName name) {
52         return mapNodeBuilder(NodeIdentifier.create(name));
53     }
54
55     public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final NodeIdentifier name) {
56         return ImmutableMapNodeBuilder.create().withNodeIdentifier(name);
57     }
58
59     /**
60      * Create an immutable map node.
61      *
62      * @param name QName which will be used as node identifier
63      * @return An unordered Map node
64      */
65     public static @NonNull MapNode mapNode(final QName name) {
66         return mapNode(NodeIdentifier.create(name));
67     }
68
69     /**
70      * Create an immutable map node.
71      *
72      * @param name QName which will be used as node identifier
73      * @return An unordered Map node
74      */
75     public static @NonNull MapNode mapNode(final NodeIdentifier name) {
76         return mapNodeBuilder(name).build();
77     }
78
79     /**
80      * Create immutable ordered map node.
81      *
82      * @param name QName which will be used as node identifier
83      * @return An ordered Map node
84      */
85     public static @NonNull OrderedMapNode orderedMapNode(final QName name) {
86         return orderedMapNode(NodeIdentifier.create(name));
87     }
88
89     /**
90      * Create immutable ordered map node.
91      *
92      * @param name Node identifier
93      * @return An ordered Map node
94      */
95     public static @NonNull OrderedMapNode orderedMapNode(final NodeIdentifier name) {
96         return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(name).build();
97     }
98
99     /**
100      * Construct immutable leaf node.
101      *
102      * @param name Identifier of leaf node
103      * @param value Value of leaf node
104      * @param <T> Type of leaf node value
105      * @return Leaf node with supplied identifier and value
106      */
107     public static <T> @NonNull LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
108         return ImmutableLeafNodeBuilder.<T>create()
109                 .withNodeIdentifier(name)
110                 .withValue(value)
111                 .build();
112     }
113
114     /**
115      * Construct immutable leaf node.
116      *
117      * @param name QName which will be used as node identifier
118      * @param value Value of leaf node.
119      * @param <T> Type of leaf node value
120      * @return Leaf node with supplied identifier and value
121      */
122     public static <T> @NonNull LeafNode<T> leafNode(final QName name, final T value) {
123         return leafNode(NodeIdentifier.create(name), value);
124     }
125
126     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
127             final QName nodeName, final QName keyName, final Object keyValue) {
128         return ImmutableMapEntryNodeBuilder.create()
129                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(nodeName, keyName, keyValue))
130                 .withChild(leafNode(keyName, keyValue));
131     }
132
133     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
134         return ImmutableMapEntryNodeBuilder.create();
135     }
136
137     public static @NonNull MapEntryNode mapEntry(final QName nodeName, final QName keyName, final Object keyValue) {
138         return mapEntryBuilder(nodeName, keyName, keyValue).build();
139     }
140
141     /**
142      * Create an immutable container node.
143      *
144      * @param name QName which will be used as node identifier
145      * @return A container node
146      */
147     public static @NonNull ContainerNode containerNode(final QName name) {
148         return containerNode(NodeIdentifier.create(name));
149     }
150
151     /**
152      * Create an immutable container node.
153      *
154      * @param name Node identifier
155      * @return A container node
156      */
157     public static @NonNull ContainerNode containerNode(final NodeIdentifier name) {
158         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(name).build();
159     }
160
161     /**
162      * Create an immutable choice node.
163      *
164      * @param name QName which will be used as node identifier
165      * @return A choice node
166      */
167     public static @NonNull ChoiceNode choiceNode(final QName name) {
168         return choiceNode(NodeIdentifier.create(name));
169     }
170
171     /**
172      * Create an immutable choice node.
173      *
174      * @param name Node identifier
175      * @return A choice node
176      */
177     public static @NonNull ChoiceNode choiceNode(final NodeIdentifier name) {
178         return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(name).build();
179     }
180
181     /**
182      * Create an immutable list node.
183      *
184      * @param name QName which will be used as node identifier
185      * @return An unkeyed list node
186      */
187     public static @NonNull UnkeyedListNode listNode(final QName name) {
188         return listNode(NodeIdentifier.create(name));
189     }
190
191     /**
192      * Create an immutable list node.
193      *
194      * @param name Node identifier
195      * @return An unkeyed list node
196      */
197     public static @NonNull UnkeyedListNode listNode(final NodeIdentifier name) {
198         return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(name).build();
199     }
200
201     /**
202      * Convert YangInstanceIdentifier into a normalized node structure.
203      *
204      * @param ctx schema context to used during serialization
205      * @param id instance identifier to convert to node structure starting from root
206      * @return serialized normalized node for provided instance Id
207      */
208     public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx,
209             final YangInstanceIdentifier id) {
210         return fromInstanceId(ctx, id, Optional.empty());
211     }
212
213     /**
214      * Convert YangInstanceIdentifier into a normalized node structure.
215      *
216      * @param ctx schema context to used during serialization
217      * @param id instance identifier to convert to node structure starting from root
218      * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
219      *                       instance identifier
220      * @return serialized normalized node for provided instance Id with overridden last child.
221      */
222     public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
223             final NormalizedNode<?, ?> deepestElement) {
224         return fromInstanceId(ctx, id, Optional.of(deepestElement));
225     }
226
227     /**
228      * Convert YangInstanceIdentifier into a normalized node structure.
229      *
230      * @param ctx schema context to used during serialization
231      * @param id instance identifier to convert to node structure starting from root
232      * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
233      *                       instance identifier
234      * @return serialized normalized node for provided instance Id with (optionally) overridden last child
235      *         and (optionally) marked with specific operation attribute.
236      */
237     public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
238             final Optional<NormalizedNode<?, ?>> deepestElement) {
239         final PathArgument topLevelElement;
240         final InstanceIdToNodes<?> instanceIdToNodes;
241         final Iterator<PathArgument> it = id.getPathArguments().iterator();
242         if (it.hasNext()) {
243             topLevelElement = it.next();
244             final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
245             checkNotNull(dataChildByName,
246                 "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
247             instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx, topLevelElement.getNodeType());
248         } else {
249             topLevelElement = SCHEMACONTEXT_NAME;
250             instanceIdToNodes = InstanceIdToNodes.fromDataSchemaNode(ctx);
251         }
252
253         return instanceIdToNodes.create(topLevelElement, it, deepestElement);
254     }
255 }