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