Bug fix to add anyxml node.
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / pnserializer / InnerNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29
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;
39
40 /**
41  * Abstraction of an entity that represents an inner node to properties data
42  * tree.
43  *
44  * @param <T> type of child
45  */
46 public abstract class InnerNode<T extends NodeChild> extends PropertiesNode {
47
48     private Map<String, T> children = new HashMap<String, T>();
49
50     protected InnerNode(String name, Namespace namespace, String uri,
51                         PropertiesNode parent, Object appInfo, NodeType nodeType) {
52         super(name, namespace, uri, parent, appInfo, nodeType);
53     }
54
55     /**
56      * Returns children.
57      *
58      * @return children
59      */
60     public Map<String, T> children() {
61         return children;
62     }
63
64     /**
65      * Sets children.
66      *
67      * @param children child nodes
68      */
69     public void children(Map<String, T> children) {
70         this.children = children;
71     }
72
73     @Override
74     public PropertiesNode addChild(String name, Namespace namespace,
75                                    NodeType type,
76                                    Object appInfo) throws SvcLogicException {
77         PropertiesNode node = ((PropertiesNode) children.get(name));
78         if (node != null) {
79             return node;
80         }
81
82         // get augment schema, if it is augmented node
83         AugmentationSchemaNode augSchema = null;
84         if (((DataSchemaNode) appInfo).isAugmenting()) {
85             augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()), ((DataSchemaNode) appInfo));
86             node = getAugmentationNode(augSchema, this, name);
87         }
88
89         // create node based on type
90         if (node == null) {
91             String uri = getUri(this, name, namespace);
92             node = createNode(name, namespace, uri, this, appInfo, type);
93         }
94
95         // If namespace is not same as parent then it is augmented node
96         if (augSchema != null && !isNamespaceAsParent(this, node)) {
97             addToAugmentations(augSchema, this, node);
98         } else {
99             children.put(name, ((T) node));
100         }
101         return node;
102     }
103
104     @Override
105     public PropertiesNode addChild(String name, Namespace namespace,
106                                    NodeType type, String value,
107                                    Namespace valueNs,
108                                    Object appInfo) throws SvcLogicException {
109         LeafNode node = ((LeafNode) children.get(name));
110         if (node != null) {
111             return  node;
112         }
113
114         AugmentationSchemaNode augSchema = null;
115         String uri = getUri(this, name, namespace);
116         node = new LeafNode(name, namespace, uri, this,
117                             appInfo, type, value);
118
119         if (type != NodeType.ANY_XML_NODE) {
120             if (((DataSchemaNode) appInfo).isAugmenting()) {
121                 augSchema = findCorrespondingAugment(
122                         ((DataSchemaNode) this.appInfo()),
123                         ((DataSchemaNode) appInfo));
124             }
125             node.valueNs(valueNs);
126         }
127
128         if (augSchema != null && !isNamespaceAsParent(this, node)) {
129             addToAugmentations(augSchema, this, node);
130         } else {
131             children.put(name, ((T) node));
132         }
133         return node;
134     }
135
136     @Override
137     public PropertiesNode addChild(String index, String name,
138                                    Namespace namespace, NodeType type,
139                                    Object appInfo) throws SvcLogicException {
140         String localname = resolveName(name);
141         PropertiesNode node = ((PropertiesNode) children.get(localname));
142
143         if (node == null) {
144             AugmentationSchemaNode augSchema = null;
145             if (((DataSchemaNode) appInfo).isAugmenting()) {
146                 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
147                                                      ((DataSchemaNode) appInfo));
148                 node = getAugmentationNode(augSchema, this, localname);
149             }
150
151             if (node == null) {
152                 String uri = getUri(this, name, namespace);
153                 node = new ListHolderNode(localname, namespace, uri,
154                                           this, appInfo, MULTI_INSTANCE_HOLDER_NODE);
155             }
156
157             if (augSchema != null && !isNamespaceAsParent(this, node)) {
158                 addToAugmentations(augSchema, this, node);
159             } else {
160                 children.put(localname, ((T) node));
161             }
162
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));
168         } else {
169             throw new SvcLogicException("Duplicate node exist with same node");
170         }
171         return node;
172     }
173
174     @Override
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));
181
182         if (node == null) {
183
184             AugmentationSchemaNode augSchema = null;
185             if (((DataSchemaNode) appInfo).isAugmenting()) {
186                 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
187                                                      ((DataSchemaNode) appInfo));
188                 node = getAugmentationNode(augSchema, this, localName);
189             }
190
191             if (node == null) {
192                 String uri = getUri(this, name, namespace);
193                 node = new LeafListHolderNode(localName, namespace, uri, this,
194                                               appInfo, MULTI_INSTANCE_LEAF_HOLDER_NODE);
195             }
196
197             if (augSchema != null && !isNamespaceAsParent(this, node)) {
198                 addToAugmentations(augSchema, this, node);
199             } else {
200                 children.put(localName, ((T) node));
201             }
202
203             node = node.addChild(index, localName, namespace, type, value, valueNs, appInfo);
204         } else if (node instanceof LeafListHolderNode) {
205             LeafNode child = ((LeafNode) ((HolderNode) node).child(index));
206             node = (child != null ? child : node.addChild(index, localName,
207                                                           namespace, type,
208                                                           value, valueNs,
209                                                           appInfo));
210         } else {
211             throw new SvcLogicException("Duplicate node exist with same node");
212         }
213         return node;
214     }
215 }