Unit test and decode implementation
[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     protected RootNode(String name, Namespace namespace,
48                        Object appInfo, String uri) {
49         super(name, namespace, uri, null, appInfo, null);
50     }
51
52     /**
53      * Returns children.
54      *
55      * @return children
56      */
57     public Map<String, T> children() {
58         return children;
59     }
60
61     /**
62      * Sets children.
63      *
64      * @param children child nodes
65      */
66     public void children(Map<String, T> children) {
67         this.children = children;
68     }
69
70     @Override
71     public PropertiesNode addChild(String name, Namespace namespace,
72                                    NodeType type,
73                                    Object appInfo) throws SvcLogicException {
74         PropertiesNode node = ((PropertiesNode) children.get(name));
75         if (node != null) {
76             return node;
77         }
78
79         // get augment schema, if it is augmented node
80         AugmentationSchemaNode augSchema = null;
81         if (((DataSchemaNode) appInfo).isAugmenting()) {
82             augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
83                                                  ((DataSchemaNode) appInfo));
84             node = getAugmentationNode(augSchema, this, name);
85         }
86
87         // create node based on type, this api will be invoked only for these three types
88         if (node == null) {
89             String uri = getUri(this, name, namespace);
90             node = createNode(name, namespace, uri, this, appInfo, type);
91         }
92
93         // If namespace is not same as parent then it is augmented node
94         if (augSchema != null && !isNamespaceAsParent(this, node)) {
95             addToAugmentations(augSchema, this, node);
96         } else {
97             children.put(name, ((T) node));
98         }
99         return node;
100     }
101
102     @Override
103     public PropertiesNode addChild(String name, Namespace namespace,
104                                    NodeType type, String value,
105                                    Namespace valuens,
106                                    Object appInfo) throws SvcLogicException {
107         LeafNode node = ((LeafNode) children.get(name));
108         if (node != null) {
109             return  node;
110         }
111
112         AugmentationSchemaNode augSchema = null;
113         if (((DataSchemaNode) appInfo).isAugmenting()) {
114             augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
115                                                  ((DataSchemaNode) appInfo));
116         }
117
118         String uri = getUri(this, name, namespace);
119         node = new LeafNode(name, namespace, uri, this,
120                             appInfo, type, value);
121
122         if (augSchema != null && !isNamespaceAsParent(this, node)) {
123             addToAugmentations(augSchema, this, node);
124         } else {
125             children.put(name, ((T) node));
126         }
127         return node;
128     }
129
130     @Override
131     public PropertiesNode addChild(String index, String name,
132                                    Namespace namespace, NodeType type,
133                                    Object appInfo) throws SvcLogicException {
134         String localname = resolveName(name);
135         PropertiesNode node = ((PropertiesNode) children.get(localname));
136         if (node == null) {
137             String uri = getUri(this, name, namespace);
138             AugmentationSchemaNode augSchema = null;
139             if (((DataSchemaNode) appInfo).isAugmenting()) {
140                 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
141                                                      ((DataSchemaNode) appInfo));
142                 node = getAugmentationNode(augSchema, this, localname);
143             }
144
145             if (node == null) {
146                 node = new ListHolderNode(localname, namespace, uri,
147                                           this, appInfo, MULTI_INSTANCE_HOLDER_NODE);
148             }
149
150             if (augSchema != null && !isNamespaceAsParent(this, node)) {
151                 addToAugmentations(augSchema, this, node);
152             } else {
153                 children.put(localname, ((T) node));
154             }
155             node = node.addChild(index, localname, namespace, type, appInfo);
156         } else if (node instanceof ListHolderNode) {
157             ListHolderChild child = ((ListHolderNode) node).child(index);
158             node = (child != null ? ((MultiInstanceNode) child) :
159                     node.addChild(index, localname, namespace, type, appInfo));
160         } else {
161             throw new SvcLogicException("Duplicate node exist with same node");
162         }
163         return node;
164     }
165
166     @Override
167     public PropertiesNode addChild(String index, String name,
168                                    Namespace namespace, NodeType type,
169                                    String value, Namespace valueNs,
170                                    Object appInfo) throws SvcLogicException {
171         String localName = resolveName(name);
172         PropertiesNode node = ((PropertiesNode) children.get(localName));
173         if (node == null) {
174             String uri = getUri(this, name, namespace);
175             AugmentationSchemaNode augSchema = null;
176             if (((DataSchemaNode) appInfo).isAugmenting()) {
177                 augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()),
178                                                      ((DataSchemaNode) appInfo));
179                 node = getAugmentationNode(augSchema, this, localName);
180             }
181
182             if (node == null) {
183                 node = new LeafListHolderNode(localName, namespace, uri, this,
184                                               appInfo, MULTI_INSTANCE_LEAF_HOLDER_NODE);
185             }
186
187             if (augSchema != null && !isNamespaceAsParent(this, node)) {
188                 addToAugmentations(augSchema, this, node);
189             } else {
190                 children.put(localName, ((T) node));
191             }
192             node = node.addChild(index, localName, namespace, type, value, valueNs, appInfo);
193         } else if (node instanceof LeafListHolderNode) {
194             LeafNode child = ((LeafNode) ((HolderNode) node).child(index));
195             node = (child != null ? child : node.addChild(index, localName,
196                                                           namespace, type,
197                                                           value, valueNs,
198                                                           appInfo));
199         } else {
200             throw new SvcLogicException("Duplicate node exist with same node");
201         }
202         return node;
203     }
204 }