7b8988d064950a9a5f0b7ceb92213fea7053ee93
[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.impl.schema.builder.api.CollectionNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33
34 public final class ImmutableNodes {
35
36     private ImmutableNodes() {
37         throw new UnsupportedOperationException("Utilities class should not be instantiated");
38     }
39
40     public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder() {
41         return ImmutableMapNodeBuilder.create();
42     }
43
44     public static CollectionNodeBuilder<MapEntryNode, MapNode> mapNodeBuilder(final QName name) {
45         return ImmutableMapNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name));
46     }
47
48     /**
49      * Construct immutable leaf node.
50      *
51      * @param name Identifier of leaf node
52      * @param value Value of leaf node
53      * @param <T> Type of leaf node value
54      * @return Leaf node with supplied identifier and value
55      */
56     public static <T> LeafNode<T> leafNode(final NodeIdentifier name, final T value) {
57         return ImmutableLeafNodeBuilder.<T>create()
58                 .withNodeIdentifier(name)
59                 .withValue(value)
60                 .build();
61     }
62
63     /**
64      * Construct immutable leaf node.
65      *
66      * @param name QName which will be used as node identifier
67      * @param value Value of leaf node.
68      * @param <T> Type of leaf node value
69      * @return Leaf node with supplied identifier and value
70      */
71     public static <T> LeafNode<T> leafNode(final QName name,final T value) {
72         return leafNode(NodeIdentifier.create(name), value);
73     }
74
75     public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder(
76             final QName nodeName, final QName keyName, final Object keyValue) {
77         return ImmutableMapEntryNodeBuilder.create()
78                 .withNodeIdentifier(new NodeIdentifierWithPredicates(nodeName, keyName, keyValue))
79                 .withChild(leafNode(keyName, keyValue));
80     }
81
82     public static DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder() {
83         return ImmutableMapEntryNodeBuilder.create();
84     }
85
86     public static MapEntryNode mapEntry(final QName nodeName,final QName keyName,final Object keyValue) {
87         return mapEntryBuilder(nodeName, keyName, keyValue).build();
88     }
89
90     public static ContainerNode containerNode(final QName name) {
91         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
92     }
93
94     public static ChoiceNode choiceNode(final QName name) {
95         return ImmutableChoiceNodeBuilder.create().withNodeIdentifier(NodeIdentifier.create(name)).build();
96     }
97
98     /**
99      * Convert YangInstanceIdentifier into a normalized node structure.
100      *
101      * @param ctx schema context to used during serialization
102      * @param id instance identifier to convert to node structure starting from root
103      * @return serialized normalized node for provided instance Id
104      */
105     public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id) {
106         return fromInstanceId(ctx, id, Optional.empty(), Optional.empty());
107     }
108
109     /**
110      * Convert YangInstanceIdentifier into a normalized node structure.
111      *
112      * @param ctx schema context to used during serialization
113      * @param id instance identifier to convert to node structure starting from root
114      * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
115      *                       instance identifier
116      * @return serialized normalized node for provided instance Id with overridden last child.
117      */
118     public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
119             final NormalizedNode<?, ?> deepestElement) {
120         return fromInstanceId(ctx, id, Optional.of(deepestElement), Optional.empty());
121     }
122
123     /**
124      * Convert YangInstanceIdentifier into a normalized node structure.
125      *
126      * @param ctx schema context to used during serialization
127      * @param id instance identifier to convert to node structure starting from root
128      * @param deepestElement pre-built deepest child that will be inserted at the last path argument of provided
129      *                       instance identifier
130      * @param operation modify operation attribute to be added to the deepest child. QName is the operation attribute
131      *                  key and ModifyAction is the value.
132      * @return serialized normalized node for provided instance Id with (optionally) overridden last child
133      *         and (optionally) marked with specific operation attribute.
134      */
135     public static NormalizedNode<?, ?> fromInstanceId(final SchemaContext ctx, final YangInstanceIdentifier id,
136             final Optional<NormalizedNode<?, ?>> deepestElement, final Optional<Entry<QName, ModifyAction>> operation) {
137         final YangInstanceIdentifier.PathArgument topLevelElement = id.getPathArguments().get(0);
138         final DataSchemaNode dataChildByName = ctx.getDataChildByName(topLevelElement.getNodeType());
139         Preconditions.checkNotNull(dataChildByName,
140             "Cannot find %s node in schema context. Instance identifier has to start from root", topLevelElement);
141         final InstanceIdToNodes<?> instanceIdToNodes = InstanceIdToNodes.fromSchemaAndQNameChecked(ctx,
142             topLevelElement.getNodeType());
143         return instanceIdToNodes.create(id, deepestElement, operation);
144     }
145 }