UT and defect fixes for DF serializer
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / dfserializer / PropertiesNodeXmlListener.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.dfserializer;
22
23 import org.dom4j.Document;
24 import org.dom4j.DocumentHelper;
25 import org.dom4j.Element;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker;
28 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.LeafNode;
29 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.Namespace;
30 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode;
31 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeListener;
32 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.RootNode;
33
34 import java.io.Writer;
35 import java.net.URI;
36 import java.util.Collection;
37 import java.util.Map;
38 import java.util.Stack;
39
40 import static java.lang.String.format;
41 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.NODE_TYPE_ERR;
42 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.UTF_HEADER;
43 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_PREFIX;
44 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.getXmlWriter;
45 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_HOLDER_NODE;
46 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_HOLDER_NODE;
47
48 /**
49  * Representation of XML implementation of properties node listener.
50  */
51 public class PropertiesNodeXmlListener implements PropertiesNodeListener {
52
53     /**
54      * XML data from the element.
55      */
56     private String xmlData;
57
58     /**
59      * Root element of the XML document.
60      */
61     private Element rootElement;
62
63     /**
64      * Writer to write the XML.
65      */
66     private Writer writer;
67
68     /**
69      * XML element stack to store the elements.
70      */
71     private final Stack<Element> elementStack = new Stack<>();
72
73     /**
74      * Creates the properties node XML listener.
75      */
76     public PropertiesNodeXmlListener() {
77     }
78
79     @Override
80     public void start(PropertiesNode node) {
81         //Do Nothing.
82     }
83
84     @Override
85     public void end(PropertiesNode node) throws SvcLogicException {
86         xmlData = UTF_HEADER + xmlData;
87         writer = getXmlWriter(xmlData, "4");
88     }
89
90     @Override
91     public void enterPropertiesNode(PropertiesNode node)
92             throws SvcLogicException {
93         Element element = null;
94         String ns = getNodeNamespace(node);
95         switch (node.nodeType()) {
96             case MULTI_INSTANCE_LEAF_HOLDER_NODE:
97             case MULTI_INSTANCE_HOLDER_NODE:
98                 break;
99
100             case SINGLE_INSTANCE_NODE:
101             case MULTI_INSTANCE_NODE:
102                 element = addElement(ns, node);
103                 break;
104
105             case MULTI_INSTANCE_LEAF_NODE:
106             case SINGLE_INSTANCE_LEAF_NODE:
107                 element = addElement(ns, node);
108                 setValueWithNs(element, (LeafNode) node);
109                 break;
110
111             default:
112                 throw new SvcLogicException(format(
113                         NODE_TYPE_ERR, node.nodeType().toString()));
114         }
115         if (element != null) {
116             if (elementStack.isEmpty()) {
117                 rootElement = element;
118             }
119             elementStack.push(element);
120         }
121     }
122
123     @Override
124     public void exitPropertiesNode(PropertiesNode node)
125             throws SvcLogicException {
126         walkAugmentationNode(node);
127         switch (node.nodeType()) {
128             case MULTI_INSTANCE_LEAF_HOLDER_NODE:
129             case MULTI_INSTANCE_HOLDER_NODE:
130                 break;
131
132             case SINGLE_INSTANCE_NODE:
133             case MULTI_INSTANCE_NODE:
134             case MULTI_INSTANCE_LEAF_NODE:
135             case SINGLE_INSTANCE_LEAF_NODE:
136                 if (!elementStack.isEmpty() &&
137                         elementStack.peek().equals(rootElement)) {
138                     xmlData = rootElement.asXML();
139                 } else {
140                     elementStack.pop();
141                 }
142                 break;
143
144             default:
145                 throw new SvcLogicException(format(
146                         NODE_TYPE_ERR, node.nodeType().toString()));
147         }
148     }
149
150     /**
151      * Returns the writer.
152      *
153      * @return writer
154      */
155     public Writer getWriter() {
156         return writer;
157     }
158
159     /**
160      * Adds an XML element to the stack with namespace if present. If the
161      * stack is empty it creates new document and adds element else adds to
162      * the parent element.
163      *
164      * @param ns   namespace of the element
165      * @param node properties node
166      * @return new added element
167      */
168     private Element addElement(String ns, PropertiesNode node) {
169         Element element;
170         if (elementStack.isEmpty()) {
171             Document doc = DocumentHelper.createDocument();
172             if (ns != null) {
173                 element = doc.addElement(node.name(), ns);
174             } else {
175                 element = doc.addElement(node.name());
176             }
177         } else {
178             element = elementStack.peek();
179             if (ns != null) {
180                 element = element.addElement(node.name(), ns);
181             } else {
182                 element = element.addElement(node.name());
183             }
184         }
185
186         return element;
187     }
188
189     /**
190      * Returns the abstract XML namespace to be used in XML data format from
191      * the properties node.
192      *
193      * @param node properties node
194      * @return abstract XML namespace
195      */
196     private String getNodeNamespace(PropertiesNode node) {
197         PropertiesNode parent = node.parent();
198         if (parent.nodeType() == MULTI_INSTANCE_HOLDER_NODE ||
199                 parent.nodeType() == MULTI_INSTANCE_LEAF_HOLDER_NODE) {
200             parent = parent.parent();
201         }
202         if (parent instanceof RootNode || ! parent.namespace().moduleName()
203                 .equals(node.namespace().moduleName())) {
204             return node.namespace().moduleNs().toString();
205         }
206         return null;
207     }
208
209     /**
210      * Sets the value to the element for a leaf node and adds the value
211      * namespace if required.
212      *
213      * @param element XML element
214      * @param node leaf properties node
215      */
216     private void setValueWithNs(Element element, LeafNode node) {
217         Namespace valNs = node.valueNs();
218         URI modNs = (valNs == null) ? null : valNs.moduleNs();
219         String val = node.value();
220         if (modNs != null) {
221             element.addNamespace(XML_PREFIX, modNs.toString());
222             element.setText(XML_PREFIX + ":" + val);
223         } else {
224             element.setText(val);
225         }
226     }
227
228     /**
229      * Gets all the augmentation of the given node and walks through it.
230      *
231      * @param node properties node
232      * @throws SvcLogicException when walking the properties node fails
233      */
234     private void walkAugmentationNode(PropertiesNode node)
235             throws SvcLogicException {
236         for (Map.Entry<Object, Collection<PropertiesNode>>
237                 augToChild : node.augmentations().asMap().entrySet()) {
238             Collection<PropertiesNode> augChild = augToChild.getValue();
239             if (!augChild.isEmpty()) {
240                 DefaultPropertiesNodeWalker walker = new
241                         DefaultPropertiesNodeWalker();
242                 for (PropertiesNode p : augChild) {
243                     enterPropertiesNode(p);
244                     walker.walkChildNode(this, p);
245                     exitPropertiesNode(p);
246                 }
247             }
248         }
249     }
250 }