Implementation of Data Format serializer
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / dfserializer / XmlSerializer.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.DocumentException;
25 import org.dom4j.DocumentHelper;
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.PropertiesNode;
29 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeWalker;
30
31 import java.io.Writer;
32 import java.util.List;
33 import java.util.Map;
34
35 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormat.XML;
36 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_LIS_ERR;
37 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_TREE_ERR;
38
39 /**
40  * Representation of XML serializer which encodes properties to XML and
41  * decodes properties from XML with the data format serializer.
42  */
43 public class XmlSerializer extends DataFormatSerializer {
44
45     /**
46      * Creates an instance of XML serializer.
47      *
48      * @param serializerContext data format serializer context
49      */
50     protected XmlSerializer(DataFormatSerializerContext serializerContext) {
51         super(XML, serializerContext);
52     }
53
54     @Override
55     public String encode(Map<String, String> param,
56                          Map<String, List<Annotation>> annotations)
57             throws SvcLogicException {
58         PropertiesNode propNode = serializerContext().getPropNodeSerializer()
59                 .encode(param);
60         PropertiesNodeWalker nodeWalker = new DefaultPropertiesNodeWalker<>();
61         PropertiesNodeXmlListener xmlListener = new PropertiesNodeXmlListener();
62         nodeWalker.walk(xmlListener, propNode);
63         Writer writer = xmlListener.getWriter();
64         return writer.toString();
65     }
66
67     @Override
68     public Map<String, String> decode(String dataFormatBody)
69             throws SvcLogicException {
70         if (!(serializerContext().listener() instanceof XmlListener)) {
71             throw new SvcLogicException(XML_LIS_ERR);
72         }
73
74         XmlListener listener = (XmlListener) serializerContext().listener();
75         XmlWalker walker = new DefaultXmlWalker();
76         Document document;
77
78         try {
79             document = DocumentHelper.parseText(dataFormatBody);
80         } catch (DocumentException e) {
81             throw new SvcLogicException(XML_TREE_ERR, e);
82         }
83         walker.walk(listener, document.getRootElement());
84
85         return serializerContext().getPropNodeSerializer().decode(
86                 listener.serializerHelper().getPropertiesNode());
87     }
88 }