Bug fix to add anyxml node.
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / dfserializer / JsonSerializer.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 com.fasterxml.jackson.databind.JsonNode;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25
26 import java.io.IOException;
27 import java.io.Writer;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker;
33 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode;
34 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeWalker;
35
36 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormat.JSON;
37 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.JSON_LIS_ERR;
38 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.JSON_TREE_ERR;
39
40 /**
41  * Representation of JSON serializer which encodes properties to JSON and
42  * decodes properties from JSON with the data format serializer.
43  */
44 public class JsonSerializer extends DataFormatSerializer {
45
46     /**
47      * Creates an instance of data format serializer.
48      *
49      * @param serializerContext data format serializer context
50      */
51     protected JsonSerializer(DataFormatSerializerContext serializerContext) {
52         super(JSON, serializerContext);
53     }
54
55     @Override
56     public String encode(Map<String, String> param,
57                          Map<String, List<Annotation>> annotations)
58             throws SvcLogicException {
59         PropertiesNode propNode = serializerContext().getPropNodeSerializer()
60                 .encode(param);
61         PropertiesNodeWalker nodeWalker = new DefaultPropertiesNodeWalker<>();
62         PropertiesNodeJsonListener jsonLis = new PropertiesNodeJsonListener();
63         nodeWalker.walk(jsonLis, propNode);
64         Writer writer = jsonLis.getWriter();
65         return writer.toString();
66     }
67
68     @Override
69     public Map<String, String> decode(String dataFormatBody)
70             throws SvcLogicException {
71         if (!(serializerContext().listener() instanceof JsonListener)) {
72             throw new SvcLogicException(JSON_LIS_ERR);
73         }
74
75         JsonListener listener = (JsonListener) serializerContext().listener();
76         JsonWalker walker = new DefaultJsonWalker();
77         ObjectMapper mapper = new ObjectMapper();
78         JsonNode jsonNode;
79
80         try {
81             jsonNode = mapper.readTree(dataFormatBody);
82         } catch (IOException e) {
83             throw new SvcLogicException(JSON_TREE_ERR, e);
84         }
85
86         walker.walk(listener, jsonNode);
87
88         return serializerContext().getPropNodeSerializer().decode(
89                 listener.serializerHelper().getPropertiesNode());
90     }
91 }