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