UT and defect fixes for DF serializer
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / pnserializer / DefaultPropertiesNodeListener.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.pnserializer;
22
23 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
24
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Map;
28
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.SINGLE_INSTANCE_LEAF_NODE;
31
32 /**
33  * Represents implementation of PropertiesNodeListener.
34  */
35 public class DefaultPropertiesNodeListener implements PropertiesNodeListener {
36
37     private Map<String, String> params = new HashMap<>();
38
39     @Override
40     public void start(PropertiesNode node) {
41         // do nothing
42     }
43
44     @Override
45     public void end(PropertiesNode node) throws SvcLogicException {
46         exitPropertiesNode(node);
47     }
48
49     @Override
50     public void enterPropertiesNode(PropertiesNode node) {
51         /*
52          * Only if it is leaf node or leaf-list node,
53          * then create a property entry and add to map
54          */
55         if (node.nodeType() == SINGLE_INSTANCE_LEAF_NODE
56                 || node.nodeType() == MULTI_INSTANCE_LEAF_NODE) {
57             String val = ((LeafNode) node).value();
58             if (((LeafNode) node).valueNs() != null) {
59                 val = ((LeafNode) node).valueNs().moduleName() + ":" + val;
60             }
61             params.put(node.uri(), val);
62         }
63     }
64
65     @Override
66     public void exitPropertiesNode(PropertiesNode node) throws
67             SvcLogicException {
68         if (!node.augmentations().isEmpty()) {
69             for (Map.Entry<Object, Collection<PropertiesNode>> augmentationTochild
70                     : node.augmentations().asMap().entrySet()) {
71                 Collection<PropertiesNode> childsFromAugmentations = augmentationTochild
72                         .getValue();
73                 if (!childsFromAugmentations.isEmpty()) {
74                     PropertiesNodeWalker walker = new DefaultPropertiesNodeWalker<>();
75                     for (PropertiesNode pNode : childsFromAugmentations) {
76                         enterPropertiesNode(pNode);
77                         walker.walk(this, pNode);
78                         exitPropertiesNode(pNode);
79                     }
80                 }
81             }
82         }
83     }
84
85     /**
86      * Returns properties.
87      *
88      * @return properties
89      */
90     public Map<String, String> params() {
91         return params;
92     }
93
94     /**
95      * Sets properties.
96      *
97      * @param params properties
98      */
99     public void params(Map<String, String> params) {
100         this.params = params;
101     }
102 }