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