2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
26 import org.opendaylight.yangtools.yang.data.util.ParserStreamUtils;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
34 import java.util.Collection;
35 import java.util.Deque;
36 import java.util.Iterator;
37 import java.util.Optional;
40 * Represents utilities for properties node tree.
42 public final class MdsalPropertiesNodeUtils {
44 private MdsalPropertiesNodeUtils() {
48 * Returns the index from multi instance property name.
50 * @param name name of the property
51 * @return index from multi instance property name
53 public static String getIndex(String name) {
54 return name.substring(name.indexOf("[") + 1,
59 * Returns the multi instance property name.
61 * @param name name of the property
62 * @return the multi instance property name
64 public static String getListName(String name) {
65 String[] s = name.split("\\[");
70 * Returns true if property is multi instance.
72 * @param name name of the property
73 * @return true if property is multi instance
75 public static boolean isListEntry(String name) {
76 String s[] = name.split("\\[");
81 * Returns name of the property after pruning namespace and
82 * index if the property is multi instance.
84 * @param name name of the property
85 * @return name of the property
87 public static String resolveName(String name) {
88 String localName = getListName(name);
89 final int lastIndexOfColon = localName.lastIndexOf(":");
90 if (lastIndexOfColon != -1) {
91 localName = localName.substring(lastIndexOfColon + 1);
97 * Adds current node to parent's augmentation map.
99 * @param augSchema augment schema
100 * @param parent parent property node
101 * @param curNode current property node
103 public static void addToAugmentations(AugmentationSchemaNode augSchema,
104 PropertiesNode parent,
105 PropertiesNode curNode) {
106 Collection<PropertiesNode> childsFromAugmentation = parent
107 .augmentations().get(augSchema);
108 if (!childsFromAugmentation.isEmpty()) {
109 for (PropertiesNode pNode : childsFromAugmentation) {
110 if (pNode.name().equals(curNode.name())) {
115 parent.augmentations().put(augSchema, curNode);
120 * Returns augmented properties node if it is already
121 * added in properties tree.
123 * @param augSchema augmented schema node
124 * @param parent parent properties node
125 * @param name name of the properties
126 * @return augmented properties node if it is already added
128 public static PropertiesNode getAugmentationNode(
129 AugmentationSchemaNode augSchema,
130 PropertiesNode parent, String name) {
131 if (augSchema != null) {
132 Collection<PropertiesNode> childsFromAugmentation = parent
133 .augmentations().get(augSchema);
134 if (!childsFromAugmentation.isEmpty()) {
135 for (PropertiesNode pNode : childsFromAugmentation) {
136 if (pNode.name().equals(name)) {
146 * Creates uri with specified name and namespace.
148 * @param parent parent properties node
149 * @param name name of the node
150 * @param ns namespace of the node
151 * @return uri with specified name and namespace
153 public static String getUri(PropertiesNode parent, String name,
156 if (!(parent.namespace().moduleNs().equals(ns.moduleNs()))) {
157 uri = ns.moduleName() + ":" + name;
159 return parent.uri() + "." + uri;
163 * Creates new properties with specified parameters.
165 * @param name name of the properties node
166 * @param namespace namespace of the properties node
167 * @param uri uri of the properties node
168 * @param parent parent node
169 * @param appInfo application info
170 * @param type node type
171 * @return new properties node
173 public static PropertiesNode createNode(String name, Namespace namespace,
174 String uri, PropertiesNode parent,
175 Object appInfo, NodeType type) {
177 case SINGLE_INSTANCE_NODE:
178 return new SingleInstanceNode(name, namespace, uri, parent, appInfo, type);
179 case MULTI_INSTANCE_HOLDER_NODE:
180 return new ListHolderNode(name, namespace, uri, parent, appInfo, type);
181 case MULTI_INSTANCE_LEAF_HOLDER_NODE:
182 return new LeafListHolderNode(name, namespace, uri, parent, appInfo, type);
184 throw new RuntimeException("Invalid node type");
189 * Returns true if namespace is same as parent's namespace.
191 * @param parent parent property node
192 * @param curNode current property node
193 * @return true if namespace is same as parent namespace
195 public static boolean isNamespaceAsParent(PropertiesNode parent,
196 PropertiesNode curNode) {
197 return parent.namespace().moduleNs().equals(curNode.namespace().moduleNs());
203 * @param childName name of the property
204 * @param ctx schema context
205 * @param parent parent property node
208 public static Namespace getNamespace(String childName,
210 PropertiesNode parent) {
211 int lastIndexOfColon = childName.lastIndexOf(":");
212 if (lastIndexOfColon != -1) {
213 String moduleName = childName.substring(0, lastIndexOfColon);
214 Iterator<Module> it = ctx.findModules(moduleName).iterator();
216 // module is not present in context
219 Module m = it.next();
220 return new Namespace(moduleName, m.getQNameModule().getNamespace(),
221 getRevision(m.getRevision()));
223 Namespace parentNs = parent.namespace();
224 return new Namespace(parentNs.moduleName(), parentNs.moduleNs(),
225 parentNs.revision());
229 * Returns child schema node.
231 * @param curSchema current schema node
232 * @param name name of the property
233 * @param namespace namespace of the property
234 * @return child schema node
236 public static SchemaNode getChildSchemaNode(SchemaNode curSchema,
238 Namespace namespace) {
239 if (namespace == null) {
243 QName qname = QName.create(namespace.moduleNs(),
244 Revision.of(namespace.revision()), name);
246 // YANG RPC will not be instance of DataSchemaNode
247 if (curSchema instanceof DataSchemaNode) {
248 Deque<DataSchemaNode> schemaNodeDeque = ParserStreamUtils.
249 findSchemaNodeByNameAndNamespace(((DataSchemaNode)
250 curSchema), name, namespace.moduleNs());
251 if (schemaNodeDeque.isEmpty()) {
252 // could not find schema node
256 DataSchemaNode schemaNode = schemaNodeDeque.pop();
257 if (schemaNodeDeque.isEmpty()){
262 // node is child of Choice/case
263 return SchemaUtils.findSchemaForChild(((ChoiceSchemaNode) schemaNode),
266 return SchemaUtils.findDataChildSchemaByQName(curSchema, qname);
271 * Returns the property node type.
273 * @param index current index
274 * @param length length of the properties
275 * @param name name of the property
276 * @return the property node type
278 public static NodeType getNodeType(int index, int length, String name) {
279 if (index == length-1) {
280 return (isListEntry(name) ? NodeType.MULTI_INSTANCE_LEAF_NODE :
281 NodeType.SINGLE_INSTANCE_LEAF_NODE);
283 return (isListEntry(name) ? NodeType.MULTI_INSTANCE_NODE :
284 NodeType.SINGLE_INSTANCE_NODE);
289 * Returns revision in string.
291 * @param r YANG revision
292 * @return revision in string
294 public static String getRevision(Optional<Revision> r) {
295 return (r.isPresent()) ? r.get().toString() : null;