UT and defect fixes for DF 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      * Value of the current JSON node.
57      */
58     private String value;
59
60     /**
61      * Value namespace of the current JSON node.
62      */
63     private String valueNs;
64
65     /**
66      * Creates an instance of default json listener with its serializer helper.
67      *
68      * @param serializerHelper serializer helper
69      */
70     public DefaultJsonListener(SerializerHelper serializerHelper) {
71         this.serializerHelper = serializerHelper;
72     }
73
74     @Override
75     public void enterJsonNode(String nodeName, JsonNode node,
76                               NodeType nodeType) throws SvcLogicException {
77         getNodeName(nodeName, false);
78
79         switch (nodeType) {
80             case SINGLE_INSTANCE_LEAF_NODE:
81                 getNodeName(node.asText(), true);
82                 serializerHelper.addNode(name, modName, value, valueNs,
83                                          SINGLE_INSTANCE_LEAF_NODE);
84                 break;
85
86             case MULTI_INSTANCE_LEAF_NODE:
87                 getNodeName(node.asText(), true);
88                 serializerHelper.addNode(name, modName, value, valueNs,
89                                          MULTI_INSTANCE_LEAF_NODE);
90                 break;
91
92             case SINGLE_INSTANCE_NODE:
93                 serializerHelper.addNode(name, modName, null, null,
94                                          SINGLE_INSTANCE_NODE);
95                 break;
96
97             case MULTI_INSTANCE_NODE:
98                 serializerHelper.addNode(name, modName, null, null,
99                                          MULTI_INSTANCE_NODE);
100                 break;
101
102             default:
103                 throw new SvcLogicException(format(NODE_TYPE_ERR,
104                                                    nodeType.toString()));
105         }
106     }
107
108     @Override
109     public void exitJsonNode(JsonNode node) throws SvcLogicException {
110         serializerHelper.exitNode();
111     }
112
113     @Override
114     public SerializerHelper serializerHelper() {
115         return serializerHelper;
116     }
117
118     /**
119      * Parses the abstract JSON name and fills the node name and node
120      * namespace or value and value namespace of the current JSON node .
121      *
122      * @param abstractName full name value
123      * @param isVal if it is for value parsing
124      */
125     private void getNodeName(String abstractName, boolean isVal) {
126         String[] val = abstractName.split(":");
127         if (val.length == 2) {
128             if (isVal) {
129                 valueNs = val[0];
130                 value = val[1];
131             } else {
132                 modName = val[0];
133                 name = val[1];
134             }
135         } else {
136             if (isVal) {
137                 value = val[0];
138                 valueNs = null;
139             } else {
140                 name = val[0];
141                 modName = null;
142             }
143         }
144     }
145 }