Implementation of Data Format serializer
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / dfserializer / DefaultJsonListener.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 org.onap.ccsdk.sli.core.sli.SvcLogicException;
25 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType;
26
27 import static java.lang.String.format;
28 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.NODE_TYPE_ERR;
29 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE;
30 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE;
31 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE;
32 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_NODE;
33
34
35 /**
36  * Representation of default implementation of JSON listener.
37  */
38 public class DefaultJsonListener implements JsonListener {
39
40     /**
41      * Serializer helper to convert to properties node.
42      */
43     private SerializerHelper serializerHelper;
44
45     /**
46      * Name of the current JSON node.
47      */
48     private String name;
49
50     /**
51      * Module name of the current JSON node.
52      */
53     private String modName;
54
55     /**
56      * Creates an instance of default json listener with its serializer helper.
57      *
58      * @param serializerHelper serializer helper
59      */
60     public DefaultJsonListener(SerializerHelper serializerHelper) {
61         this.serializerHelper = serializerHelper;
62     }
63
64     @Override
65     public void enterJsonNode(String nodeName, JsonNode node,
66                               NodeType nodeType) throws SvcLogicException {
67         getNodeName(nodeName);
68
69         switch (nodeType) {
70             case SINGLE_INSTANCE_LEAF_NODE:
71                 serializerHelper.addNode(name, modName, node.asText(), null,
72                                          SINGLE_INSTANCE_LEAF_NODE);
73                 break;
74
75             case MULTI_INSTANCE_LEAF_NODE:
76                 serializerHelper.addNode(name, modName, node.asText(), null,
77                                          MULTI_INSTANCE_LEAF_NODE);
78                 break;
79
80             case SINGLE_INSTANCE_NODE:
81                 serializerHelper.addNode(name, modName, null, null,
82                                          SINGLE_INSTANCE_NODE);
83                 break;
84
85             case MULTI_INSTANCE_NODE:
86                 serializerHelper.addNode(name, modName, null, null,
87                                          MULTI_INSTANCE_NODE);
88                 break;
89
90             default:
91                 throw new SvcLogicException(format(NODE_TYPE_ERR,
92                                                    nodeType.toString()));
93         }
94     }
95
96     @Override
97     public void exitJsonNode(JsonNode node) throws SvcLogicException {
98         serializerHelper.exitNode();
99     }
100
101     @Override
102     public SerializerHelper serializerHelper() {
103         return serializerHelper;
104     }
105
106     /**
107      * Parses the abstract JSON name and fills the node name and node
108      * namespace of the current JSON node.
109      *
110      * @param abstractName abstract JSON name
111      */
112     private void getNodeName(String abstractName) {
113         String[] val = abstractName.split(":");
114         if (val.length == 2) {
115             modName = val[0];
116             name = val[1];
117         } else {
118             name = val[0];
119         }
120     }
121
122 }