2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
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;
34 import java.io.Writer;
36 import java.util.Collection;
38 import java.util.Stack;
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;
49 * Representation of XML implementation of properties node listener.
51 public class PropertiesNodeXmlListener implements PropertiesNodeListener {
54 * XML data from the element.
56 private String xmlData;
59 * Root element of the XML document.
61 private Element rootElement;
64 * Writer to write the XML.
66 private Writer writer;
69 * XML element stack to store the elements.
71 private final Stack<Element> elementStack = new Stack<>();
74 * Creates the properties node XML listener.
76 public PropertiesNodeXmlListener() {
80 public void start(PropertiesNode node) {
81 rootElement = addElement(null, node);
82 elementStack.push(rootElement);
86 public void end(PropertiesNode node) throws SvcLogicException {
87 xmlData = rootElement.asXML();
88 xmlData = UTF_HEADER + xmlData;
89 writer = getXmlWriter(xmlData, "4");
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:
102 case SINGLE_INSTANCE_NODE:
103 case MULTI_INSTANCE_NODE:
104 element = addElement(ns, node);
107 case MULTI_INSTANCE_LEAF_NODE:
108 case SINGLE_INSTANCE_LEAF_NODE:
109 element = addElement(ns, node);
110 setValueWithNs(element, (LeafNode) node);
114 throw new SvcLogicException(format(
115 NODE_TYPE_ERR, node.nodeType().toString()));
117 if (element != null) {
118 elementStack.push(element);
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:
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()) {
141 throw new SvcLogicException(format(
142 NODE_TYPE_ERR, node.nodeType().toString()));
147 * Returns the writer.
151 public Writer getWriter() {
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.
160 * @param ns namespace of the element
161 * @param node properties node
162 * @return new added element
164 private Element addElement(String ns, PropertiesNode node) {
166 if (elementStack.isEmpty()) {
167 Document doc = DocumentHelper.createDocument();
169 element = doc.addElement(node.name(), ns);
171 element = doc.addElement(node.name());
174 element = elementStack.peek();
176 element = element.addElement(node.name(), ns);
178 element = element.addElement(node.name());
186 * Returns the abstract XML namespace to be used in XML data format from
187 * the properties node.
189 * @param node properties node
190 * @return abstract XML namespace
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();
198 if (parent instanceof RootNode || ! parent.namespace().moduleName()
199 .equals(node.namespace().moduleName())) {
200 return node.namespace().moduleNs().toString();
206 * Sets the value to the element for a leaf node and adds the value
207 * namespace if required.
209 * @param element XML element
210 * @param node leaf properties node
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();
217 element.addNamespace(XML_PREFIX, modNs.toString());
218 element.setText(XML_PREFIX + ":" + val);
220 element.setText(val);
225 * Gets all the augmentation of the given node and walks through it.
227 * @param node properties node
228 * @throws SvcLogicException when walking the properties node fails
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);