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