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