Merge branch 'master' of ../controller
[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.createNode(name, value);
109     }
110
111     /**
112      * Construct immutable leaf node.
113      *
114      * @param name QName which will be used as node identifier
115      * @param value Value of leaf node.
116      * @param <T> Type of leaf node value
117      * @return Leaf node with supplied identifier and value
118      */
119     public static <T> @NonNull LeafNode<T> leafNode(final QName name, final T value) {
120         return leafNode(NodeIdentifier.create(name), value);
121     }
122
123     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
124             final QName nodeName, final QName keyName, final Object keyValue) {
125         return ImmutableMapEntryNodeBuilder.create()
126                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(nodeName, keyName, keyValue))
127                 .withChild(leafNode(keyName, keyValue));
128     }
129
130     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
131         return ImmutableMapEntryNodeBuilder.create();
132     }
133
134     public static @NonNull MapEntryNode mapEntry(final QName nodeName, final QName keyName, final Object keyValue) {
135         return mapEntryBuilder(nodeName, keyName, keyValue).build();
136     }
137
138     /**
139      * Create an immutable container node.
140      *
141      * @param name QName which will be used as node identifier
142      * @return A container node
143      */
144     public static @NonNull ContainerNode containerNode(final QName name) {
145         return containerNode(NodeIdentifier.create(name));
146     }
147
148     /**
149      * Create an immutable container node.
150      *
151      * @param name Node identifier
152      * @return A container node
153      */
154     public static @NonNull ContainerNode containerNode(final NodeIdentifier name) {
155         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(name).build();
156     }
157
158     /**
159      * Create an immutable choice node.
160      *
161      * @param name QName which will be used as node identifier
162      * @return A choice node
163      */
164     public static @NonNull ChoiceNode choiceNode(final QName name) {
165         return choiceNode(NodeIdentifier.create(name));
166     }
167
168     /**
169      * Create an immutable choice node.
170      *
171      * @param name Node identifier
172      * @return A choice node
173      */
174     public static @NonNull ChoiceNode choiceNode(final NodeIdentifier name) {
175         return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(name).build();
176     }
177
178     /**
179      * Create an immutable list node.
180      *
181      * @param name QName which will be used as node identifier
182      * @return An unkeyed list node
183      */
184     public static @NonNull UnkeyedListNode listNode(final QName name) {
185         return listNode(NodeIdentifier.create(name));
186     }
187
188     /**
189      * Create an immutable list node.
190      *
191      * @param name Node identifier
192      * @return An unkeyed list node
193      */
194     public static @NonNull UnkeyedListNode listNode(final NodeIdentifier name) {
195         return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(name).build();
196     }
197
198     /**
199      * Convert YangInstanceIdentifier into a normalized node structure.
200      *
201      * @param ctx schema context to used during serialization
202      * @param id instance identifier to convert to node structure starting from root
203      * @return serialized normalized node for provided instance Id
204      */
205     public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx,
206             final YangInstanceIdentifier id) {
207         return fromInstanceId(ctx, id, Optional.empty());
208     }
209
210     /**
211      * Convert YangInstanceIdentifier into a normalized node structure.
212      *
213      * @param ctx schema context to used during serialization
214      * @param id instance identifier to convert to node structure starting from root
215      * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
216      *                       instance identifier
217      * @return serialized normalized node for provided instance Id with overridden last child.
218      */
219     public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
220             final NormalizedNode<?, ?> deepestElement) {
221         return fromInstanceId(ctx, id, Optional.of(deepestElement));
222     }
223
224     /**
225      * Convert YangInstanceIdentifier into a normalized node structure.
226      *
227      * @param ctx schema context to used during serialization
228      * @param id instance identifier to convert to node structure starting from root
229      * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
230      *                       instance identifier
231      * @return serialized normalized node for provided instance Id with (optionally) overridden last child
232      *         and (optionally) marked with specific operation attribute.
233      */
234     public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
235             final Optional<NormalizedNode<?, ?>> deepestElement) {
236         final PathArgument topLevelElement;
237         final InstanceIdToNodes<?> instanceIdToNodes;
238         final Iterator<PathArgument> it = id.getPathArguments().iterator();
239         if (it.hasNext()) {
240             topLevelElement = it.next();
241             final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
242             checkNotNull(dataChildByName,
243                 "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
244             instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx, topLevelElement.getNodeType());
245         } else {
246             topLevelElement = SCHEMACONTEXT_NAME;
247             instanceIdToNodes = InstanceIdToNodes.fromDataSchemaNode(ctx);
248         }
249
250         return instanceIdToNodes.create(topLevelElement, it, deepestElement);
251     }
252 }