Optimize 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
43     private ImmutableNodes() {
44         throw new UnsupportedOperationException("Utilities class should not be instantiated");
45     }
46
47     public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder() {
48         return ImmutableMapNodeBuilder.create();
49     }
50
51     public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final QName name) {
52         return mapNodeBuilder(NodeIdentifier.create(name));
53     }
54
55     public static 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 MapNode mapNode(final QName name) {
66         return mapNodeBuilder(name).build();
67     }
68
69     private static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapNodeBuilder(final QName qname) {
70         return orderedMapNodeBuilder().withNodeIdentifier(NodeIdentifier.create(qname));
71     }
72
73     private static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> orderedMapNodeBuilder() {
74         return ImmutableOrderedMapNodeBuilder.create();
75     }
76
77     /**
78      * Create immutable ordered map node.
79      *
80      * @param name QName which will be used as node identifier
81      * @return An ordered Map node
82      */
83     public static OrderedMapNode orderedMapNode(final QName name) {
84         return orderedMapNodeBuilder(name).build();
85     }
86
87     /**
88      * Construct immutable leaf node.
89      *
90      * @param name Identifier of leaf node
91      * @param value Value of leaf node
92      * @param <T> Type of leaf node value
93      * @return Leaf node with supplied identifier and value
94      */
95     public static <T> LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
96         return ImmutableLeafNodeBuilder.<T>create()
97                 .withNodeIdentifier(name)
98                 .withValue(value)
99                 .build();
100     }
101
102     /**
103      * Construct immutable leaf node.
104      *
105      * @param name QName which will be used as node identifier
106      * @param value Value of leaf node.
107      * @param <T> Type of leaf node value
108      * @return Leaf node with supplied identifier and value
109      */
110     public static <T> LeafNode<T> leafNode(final QName name, final T value) {
111         return leafNode(NodeIdentifier.create(name), value);
112     }
113
114     public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
115             final QName nodeName, final QName keyName, final Object keyValue) {
116         return ImmutableMapEntryNodeBuilder.create()
117                 .withNodeIdentifier(new NodeIdentifierWithPredicates(nodeName, keyName, keyValue))
118                 .withChild(leafNode(keyName, keyValue));
119     }
120
121     public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
122         return ImmutableMapEntryNodeBuilder.create();
123     }
124
125     public static MapEntryNode mapEntry(final QName nodeName,final QName keyName,final Object keyValue) {
126         return mapEntryBuilder(nodeName, keyName, keyValue).build();
127     }
128
129     /**
130      * Create an immutable container node.
131      *
132      * @param name QName which will be used as node identifier
133      * @return A container node
134      */
135     public static ContainerNode containerNode(final QName name) {
136         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
137     }
138
139     /**
140      * Create an immutable choice node.
141      *
142      * @param name QName which will be used as node identifier
143      * @return A choice node
144      */
145     public static ChoiceNode choiceNode(final QName name) {
146         return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
147     }
148
149     /**
150      * Create an immutable list node.
151      *
152      * @param name QName which will be used as node identifier
153      * @return An unkeyed list node
154      */
155     public static UnkeyedListNode listNode(final QName name) {
156         return ImmutableUnkeyedListNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
157     }
158
159     /**
160      * Convert YangInstanceIdentifier into a normalized node structure.
161      *
162      * @param ctx schema context to used during serialization
163      * @param id instance identifier to convert to node structure starting from root
164      * @return serialized normalized node for provided instance Id
165      */
166     public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id) {
167         return fromInstanceId(ctx, id, Optional.empty(), Optional.empty());
168     }
169
170     /**
171      * Convert YangInstanceIdentifier into a normalized node structure.
172      *
173      * @param ctx schema context to used during serialization
174      * @param id instance identifier to convert to node structure starting from root
175      * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
176      *                       instance identifier
177      * @return serialized normalized node for provided instance Id with overridden last child.
178      */
179     public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
180             final NormalizedNode<?, ?> deepestElement) {
181         return fromInstanceId(ctx, id, Optional.of(deepestElement), Optional.empty());
182     }
183
184     /**
185      * Convert YangInstanceIdentifier into a normalized node structure.
186      *
187      * @param ctx schema context to used during serialization
188      * @param id instance identifier to convert to node structure starting from root
189      * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
190      *                       instance identifier
191      * @param operation modify operation attribute to be added to the deepest child. QName is the operation attribute
192      *                  key and ModifyAction is the value.
193      * @return serialized normalized node for provided instance Id with (optionally) overridden last child
194      *         and (optionally) marked with specific operation attribute.
195      */
196     public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
197             final Optional<NormalizedNode<?, ?>> deepestElement, final Optional<Entry<QName, ModifyAction>> operation) {
198         final Iterator<PathArgument> it = id.getPathArguments().iterator();
199         final PathArgument topLevelElement = it.next();
200         final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
201         checkNotNull(dataChildByName,
202             "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
203         final InstanceIdToNodes<?> instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx,
204             topLevelElement.getNodeType());
205         return instanceIdToNodes.create(topLevelElement, it, deepestElement, operation);
206     }
207 }