c734ee84f041a5356cb1f709818505c8ff9d3e1d
[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     // 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, MapNode> mapNodeBuilder() {
49         return ImmutableMapNodeBuilder.create();
50     }
51
52     public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final QName name) {
53         return mapNodeBuilder(NodeIdentifier.create(name));
54     }
55
56     public static @NonNull 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 @NonNull MapNode mapNode(final QName name) {
67         return mapNode(NodeIdentifier.create(name));
68     }
69
70     /**
71      * Create an immutable map node.
72      *
73      * @param name QName which will be used as node identifier
74      * @return An unordered Map node
75      */
76     public static @NonNull MapNode mapNode(final NodeIdentifier name) {
77         return mapNodeBuilder(name).build();
78     }
79
80     /**
81      * Create immutable ordered map node.
82      *
83      * @param name QName which will be used as node identifier
84      * @return An ordered Map node
85      */
86     public static @NonNull OrderedMapNode orderedMapNode(final QName name) {
87         return orderedMapNode(NodeIdentifier.create(name));
88     }
89
90     /**
91      * Create immutable ordered map node.
92      *
93      * @param name Node identifier
94      * @return An ordered Map node
95      */
96     public static @NonNull OrderedMapNode orderedMapNode(final NodeIdentifier name) {
97         return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(name).build();
98     }
99
100     /**
101      * Construct immutable leaf node.
102      *
103      * @param name Identifier of leaf node
104      * @param value Value of leaf node
105      * @param <T> Type of leaf node value
106      * @return Leaf node with supplied identifier and value
107      */
108     public static <T> @NonNull LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
109         return ImmutableLeafNodeBuilder.createNode(name, value);
110     }
111
112     /**
113      * Construct immutable leaf node.
114      *
115      * @param name QName which will be used as node identifier
116      * @param value Value of leaf node.
117      * @param <T> Type of leaf node value
118      * @return Leaf node with supplied identifier and value
119      */
120     public static <T> @NonNull LeafNode<T> leafNode(final QName name, final T value) {
121         return leafNode(NodeIdentifier.create(name), value);
122     }
123
124     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
125             final QName nodeName, final QName keyName, final Object keyValue) {
126         return ImmutableMapEntryNodeBuilder.create()
127                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(nodeName, keyName, keyValue))
128                 .withChild(leafNode(keyName, keyValue));
129     }
130
131     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
132         return ImmutableMapEntryNodeBuilder.create();
133     }
134
135     public static @NonNull MapEntryNode mapEntry(final QName nodeName, final QName keyName, final Object keyValue) {
136         return mapEntryBuilder(nodeName, keyName, keyValue).build();
137     }
138
139     /**
140      * Create an immutable container node.
141      *
142      * @param name QName which will be used as node identifier
143      * @return A container node
144      */
145     public static @NonNull ContainerNode containerNode(final QName name) {
146         return containerNode(NodeIdentifier.create(name));
147     }
148
149     /**
150      * Create an immutable container node.
151      *
152      * @param name Node identifier
153      * @return A container node
154      */
155     public static @NonNull ContainerNode containerNode(final NodeIdentifier name) {
156         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(name).build();
157     }
158
159     /**
160      * Create an immutable choice node.
161      *
162      * @param name QName which will be used as node identifier
163      * @return A choice node
164      */
165     public static @NonNull ChoiceNode choiceNode(final QName name) {
166         return choiceNode(NodeIdentifier.create(name));
167     }
168
169     /**
170      * Create an immutable choice node.
171      *
172      * @param name Node identifier
173      * @return A choice node
174      */
175     public static @NonNull ChoiceNode choiceNode(final NodeIdentifier name) {
176         return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(name).build();
177     }
178
179     /**
180      * Create an immutable list node.
181      *
182      * @param name QName which will be used as node identifier
183      * @return An unkeyed list node
184      */
185     public static @NonNull UnkeyedListNode listNode(final QName name) {
186         return listNode(NodeIdentifier.create(name));
187     }
188
189     /**
190      * Create an immutable list node.
191      *
192      * @param name Node identifier
193      * @return An unkeyed list node
194      */
195     public static @NonNull UnkeyedListNode listNode(final NodeIdentifier name) {
196         return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(name).build();
197     }
198
199     /**
200      * Convert YangInstanceIdentifier into a normalized node structure.
201      *
202      * @param ctx schema context to used during serialization
203      * @param id instance identifier to convert to node structure starting from root
204      * @return serialized normalized node for provided instance Id
205      */
206     public static @NonNull NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx,
207             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.getDataChildByName(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 }