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.onap.ccsdk.sli.core.sli.SvcLogicException;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import java.util.HashMap;
30 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.addToAugmentations;
31 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.createNode;
32 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getAugmentationNode;
33 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getUri;
34 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.isNamespaceAsParent;
35 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName;
36 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_HOLDER_NODE;
37 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_HOLDER_NODE;
38 import static org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils.findCorrespondingAugment;
41 * Abstraction of node representing properties data tree.
43 public class RootNode<T extends NodeChild> extends PropertiesNode {
45 private Map<String, T> children = new HashMap<String, T>();
48 * Creates an instance of the root node to build the properties.
50 * @param name name of the node
51 * @param namespace namespace of the node
52 * @param appInfo application info
53 * @param uri URI of the node
55 public RootNode(String name, Namespace namespace,
56 Object appInfo, String uri) {
57 super(name, namespace, uri, null, appInfo, null);
65 public Map<String, T> children() {
72 * @param children child nodes
74 public void children(Map<String, T> children) {
75 this.children = children;
79 public PropertiesNode addChild(String name, Namespace namespace,
81 Object appInfo) throws SvcLogicException {
82 PropertiesNode node = ((PropertiesNode) children.get(name));
87 // get augment schema, if it is augmented node
88 AugmentationSchemaNode augSchema = null;
89 if (((DataSchemaNode) appInfo).isAugmenting()) {
90 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
91 ((DataSchemaNode) appInfo));
92 node = getAugmentationNode(augSchema, this, name);
95 // create node based on type, this api will be invoked only for these three types
97 String uri = getUri(this, name, namespace);
98 node = createNode(name, namespace, uri, this, appInfo, type);
101 // If namespace is not same as parent then it is augmented node
102 if (augSchema != null && !isNamespaceAsParent(this, node)) {
103 addToAugmentations(augSchema, this, node);
105 children.put(name, ((T) node));
111 public PropertiesNode addChild(String name, Namespace namespace,
112 NodeType type, String value,
114 Object appInfo) throws SvcLogicException {
115 LeafNode node = ((LeafNode) children.get(name));
120 AugmentationSchemaNode augSchema = null;
121 if (((DataSchemaNode) appInfo).isAugmenting()) {
122 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
123 ((DataSchemaNode) appInfo));
126 String uri = getUri(this, name, namespace);
127 node = new LeafNode(name, namespace, uri, this,
128 appInfo, type, value);
130 if (augSchema != null && !isNamespaceAsParent(this, node)) {
131 addToAugmentations(augSchema, this, node);
133 children.put(name, ((T) node));
139 public PropertiesNode addChild(String index, String name,
140 Namespace namespace, NodeType type,
141 Object appInfo) throws SvcLogicException {
142 String localname = resolveName(name);
143 PropertiesNode node = ((PropertiesNode) children.get(localname));
145 String uri = getUri(this, name, namespace);
146 AugmentationSchemaNode augSchema = null;
147 if (((DataSchemaNode) appInfo).isAugmenting()) {
148 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
149 ((DataSchemaNode) appInfo));
150 node = getAugmentationNode(augSchema, this, localname);
154 node = new ListHolderNode(localname, namespace, uri,
155 this, appInfo, MULTI_INSTANCE_HOLDER_NODE);
158 if (augSchema != null && !isNamespaceAsParent(this, node)) {
159 addToAugmentations(augSchema, this, node);
161 children.put(localname, ((T) node));
163 node = node.addChild(index, localname, namespace, type, appInfo);
164 } else if (node instanceof ListHolderNode) {
165 ListHolderChild child = ((ListHolderNode) node).child(index);
166 node = (child != null ? ((MultiInstanceNode) child) :
167 node.addChild(index, localname, namespace, type, appInfo));
169 throw new SvcLogicException("Duplicate node exist with same node");
175 public PropertiesNode addChild(String index, String name,
176 Namespace namespace, NodeType type,
177 String value, Namespace valueNs,
178 Object appInfo) throws SvcLogicException {
179 String localName = resolveName(name);
180 PropertiesNode node = ((PropertiesNode) children.get(localName));
182 String uri = getUri(this, name, namespace);
183 AugmentationSchemaNode augSchema = null;
184 if (((DataSchemaNode) appInfo).isAugmenting()) {
185 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
186 ((DataSchemaNode) appInfo));
187 node = getAugmentationNode(augSchema, this, localName);
191 node = new LeafListHolderNode(localName, namespace, uri, this,
192 appInfo, MULTI_INSTANCE_LEAF_HOLDER_NODE);
195 if (augSchema != null && !isNamespaceAsParent(this, node)) {
196 addToAugmentations(augSchema, this, node);
198 children.put(localName, ((T) node));
200 node = node.addChild(index, localName, namespace, type, value, valueNs, appInfo);
201 } else if (node instanceof LeafListHolderNode) {
202 LeafNode child = ((LeafNode) ((HolderNode) node).child(index));
203 node = (child != null ? child : node.addChild(index, localName,
208 throw new SvcLogicException("Duplicate node exist with same node");