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