Implementation of Data Format serializer
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / pnserializer / RootNode.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 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;
26
27 import java.util.HashMap;
28 import java.util.Map;
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 node representing properties data tree.
42  */
43 public class RootNode<T extends NodeChild> extends PropertiesNode {
44
45     private Map<String, T> children = new HashMap<String, T>();
46
47     /**
48      * Creates an instance of the root node to build the properties.
49      *
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
54      */
55     public RootNode(String name, Namespace namespace,
56                        Object appInfo, String uri) {
57         super(name, namespace, uri, null, appInfo, null);
58     }
59
60     /**
61      * Returns children.
62      *
63      * @return children
64      */
65     public Map<String, T> children() {
66         return children;
67     }
68
69     /**
70      * Sets children.
71      *
72      * @param children child nodes
73      */
74     public void children(Map<String, T> children) {
75         this.children = children;
76     }
77
78     @Override
79     public PropertiesNode addChild(String name, Namespace namespace,
80                                    NodeType type,
81                                    Object appInfo) throws SvcLogicException {
82         PropertiesNode node = ((PropertiesNode) children.get(name));
83         if (node != null) {
84             return node;
85         }
86
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);
93         }
94
95         // create node based on type, this api will be invoked only for these three types
96         if (node == null) {
97             String uri = getUri(this, name, namespace);
98             node = createNode(name, namespace, uri, this, appInfo, type);
99         }
100
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);
104         } else {
105             children.put(name, ((T) node));
106         }
107         return node;
108     }
109
110     @Override
111     public PropertiesNode addChild(String name, Namespace namespace,
112                                    NodeType type, String value,
113                                    Namespace valuens,
114                                    Object appInfo) throws SvcLogicException {
115         LeafNode node = ((LeafNode) children.get(name));
116         if (node != null) {
117             return  node;
118         }
119
120         AugmentationSchemaNode augSchema = null;
121         if (((DataSchemaNode) appInfo).isAugmenting()) {
122             augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
123                                                  ((DataSchemaNode) appInfo));
124         }
125
126         String uri = getUri(this, name, namespace);
127         node = new LeafNode(name, namespace, uri, this,
128                             appInfo, type, value);
129
130         if (augSchema != null && !isNamespaceAsParent(this, node)) {
131             addToAugmentations(augSchema, this, node);
132         } else {
133             children.put(name, ((T) node));
134         }
135         return node;
136     }
137
138     @Override
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));
144         if (node == null) {
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);
151             }
152
153             if (node == null) {
154                 node = new ListHolderNode(localname, namespace, uri,
155                                           this, appInfo, MULTI_INSTANCE_HOLDER_NODE);
156             }
157
158             if (augSchema != null && !isNamespaceAsParent(this, node)) {
159                 addToAugmentations(augSchema, this, node);
160             } else {
161                 children.put(localname, ((T) node));
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         if (node == null) {
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);
188             }
189
190             if (node == null) {
191                 node = new LeafListHolderNode(localName, namespace, uri, this,
192                                               appInfo, MULTI_INSTANCE_LEAF_HOLDER_NODE);
193             }
194
195             if (augSchema != null && !isNamespaceAsParent(this, node)) {
196                 addToAugmentations(augSchema, this, node);
197             } else {
198                 children.put(localName, ((T) node));
199             }
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,
204                                                           namespace, type,
205                                                           value, valueNs,
206                                                           appInfo));
207         } else {
208             throw new SvcLogicException("Duplicate node exist with same node");
209         }
210         return node;
211     }
212 }