Appending dummy root node to XML data format
[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         rootElement = addElement(null, node);
82         elementStack.push(rootElement);
83     }
84
85     @Override
86     public void end(PropertiesNode node) throws SvcLogicException {
87         xmlData = rootElement.asXML();
88         xmlData = UTF_HEADER + xmlData;
89         writer = getXmlWriter(xmlData, "4");
90     }
91
92     @Override
93     public void enterPropertiesNode(PropertiesNode node)
94             throws SvcLogicException {
95         Element element = null;
96         String ns = getNodeNamespace(node);
97         switch (node.nodeType()) {
98             case MULTI_INSTANCE_LEAF_HOLDER_NODE:
99             case MULTI_INSTANCE_HOLDER_NODE:
100                 break;
101
102             case SINGLE_INSTANCE_NODE:
103             case MULTI_INSTANCE_NODE:
104                 element = addElement(ns, node);
105                 break;
106
107             case MULTI_INSTANCE_LEAF_NODE:
108             case SINGLE_INSTANCE_LEAF_NODE:
109                 element = addElement(ns, node);
110                 setValueWithNs(element, (LeafNode) node);
111                 break;
112
113             default:
114                 throw new SvcLogicException(format(
115                         NODE_TYPE_ERR, node.nodeType().toString()));
116         }
117         if (element != null) {
118             elementStack.push(element);
119         }
120     }
121
122     @Override
123     public void exitPropertiesNode(PropertiesNode node)
124             throws SvcLogicException {
125         walkAugmentationNode(node);
126         switch (node.nodeType()) {
127             case MULTI_INSTANCE_LEAF_HOLDER_NODE:
128             case MULTI_INSTANCE_HOLDER_NODE:
129                 break;
130
131             case SINGLE_INSTANCE_NODE:
132             case MULTI_INSTANCE_NODE:
133             case MULTI_INSTANCE_LEAF_NODE:
134             case SINGLE_INSTANCE_LEAF_NODE:
135                 if (!elementStack.isEmpty()) {
136                     elementStack.pop();
137                 }
138                 break;
139
140             default:
141                 throw new SvcLogicException(format(
142                         NODE_TYPE_ERR, node.nodeType().toString()));
143         }
144     }
145
146     /**
147      * Returns the writer.
148      *
149      * @return writer
150      */
151     public Writer getWriter() {
152         return writer;
153     }
154
155     /**
156      * Adds an XML element to the stack with namespace if present. If the
157      * stack is empty it creates new document and adds element else adds to
158      * the parent element.
159      *
160      * @param ns   namespace of the element
161      * @param node properties node
162      * @return new added element
163      */
164     private Element addElement(String ns, PropertiesNode node) {
165         Element element;
166         if (elementStack.isEmpty()) {
167             Document doc = DocumentHelper.createDocument();
168             if (ns != null) {
169                 element = doc.addElement(node.name(), ns);
170             } else {
171                 element = doc.addElement(node.name());
172             }
173         } else {
174             element = elementStack.peek();
175             if (ns != null) {
176                 element = element.addElement(node.name(), ns);
177             } else {
178                 element = element.addElement(node.name());
179             }
180         }
181
182         return element;
183     }
184
185     /**
186      * Returns the abstract XML namespace to be used in XML data format from
187      * the properties node.
188      *
189      * @param node properties node
190      * @return abstract XML namespace
191      */
192     private String getNodeNamespace(PropertiesNode node) {
193         PropertiesNode parent = node.parent();
194         if (parent.nodeType() == MULTI_INSTANCE_HOLDER_NODE ||
195                 parent.nodeType() == MULTI_INSTANCE_LEAF_HOLDER_NODE) {
196             parent = parent.parent();
197         }
198         if (parent instanceof RootNode || ! parent.namespace().moduleName()
199                 .equals(node.namespace().moduleName())) {
200             return node.namespace().moduleNs().toString();
201         }
202         return null;
203     }
204
205     /**
206      * Sets the value to the element for a leaf node and adds the value
207      * namespace if required.
208      *
209      * @param element XML element
210      * @param node leaf properties node
211      */
212     private void setValueWithNs(Element element, LeafNode node) {
213         Namespace valNs = node.valueNs();
214         URI modNs = (valNs == null) ? null : valNs.moduleNs();
215         String val = node.value();
216         if (modNs != null) {
217             element.addNamespace(XML_PREFIX, modNs.toString());
218             element.setText(XML_PREFIX + ":" + val);
219         } else {
220             element.setText(val);
221         }
222     }
223
224     /**
225      * Gets all the augmentation of the given node and walks through it.
226      *
227      * @param node properties node
228      * @throws SvcLogicException when walking the properties node fails
229      */
230     private void walkAugmentationNode(PropertiesNode node)
231             throws SvcLogicException {
232         for (Map.Entry<Object, Collection<PropertiesNode>>
233                 augToChild : node.augmentations().asMap().entrySet()) {
234             Collection<PropertiesNode> augChild = augToChild.getValue();
235             if (!augChild.isEmpty()) {
236                 DefaultPropertiesNodeWalker walker = new
237                         DefaultPropertiesNodeWalker();
238                 for (PropertiesNode p : augChild) {
239                     enterPropertiesNode(p);
240                     walker.walkChildNode(this, p);
241                     exitPropertiesNode(p);
242                 }
243             }
244         }
245     }
246 }