041210f22db790235177a2bf7c4f056f27243c3f
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / pnserializer / DefaultPropertiesNodeWalker.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.Map;
24
25 /**
26  * Implementation of properties node walker which helps in forming a new tree from properties node.
27  *
28  * @param <T> node child of properties node.
29  */
30 public class DefaultPropertiesNodeWalker<T extends NodeChild> implements PropertiesNodeWalker {
31
32     @Override
33     public void walk(PropertiesNodeListener listener,
34                      PropertiesNode propertiesNode) {
35         listener.start(propertiesNode);
36         walkChildNode(listener, propertiesNode);
37         listener.end(propertiesNode);
38     }
39
40     /**
41      * Walks the children node from the parent node.
42      *
43      * @param listener properties node listener
44      * @param propertiesNode properties node
45      */
46     public void walkChildNode(PropertiesNodeListener listener,
47                               PropertiesNode propertiesNode) {
48         Map<String, T> children = getChildren(propertiesNode);
49         if (children != null) {
50             for (Map.Entry<String, T> entry : children.entrySet()) {
51                 PropertiesNode node = ((PropertiesNode) entry.getValue());
52                 listener.enterPropertiesNode(node);
53                 walkChildNode(listener, node);
54                 listener.exitPropertiesNode(node);
55             }
56         }
57     }
58
59     /**
60      * Returns the children node according to the property node type.
61      *
62      * @param value property node
63      * @return property node children
64      */
65     private Map<String,T> getChildren(PropertiesNode value) {
66         if (value instanceof RootNode) {
67             return ((RootNode) value).children();
68         }
69         switch (value.nodeType()) {
70             case SINGLE_INSTANCE_NODE:
71                 return ((InnerNode) value).children();
72             case MULTI_INSTANCE_HOLDER_NODE:
73                 return ((Map<String, T>) ((ListHolderNode) value).children());
74             case MULTI_INSTANCE_NODE:
75                 return ((Map<String, T>) ((MultiInstanceNode) value)
76                         .children());
77             case MULTI_INSTANCE_LEAF_HOLDER_NODE:
78                 return ((Map<String, T>) ((LeafListHolderNode) value)
79                         .children());
80             case SINGLE_INSTANCE_LEAF_NODE:
81             case MULTI_INSTANCE_LEAF_NODE:
82                 return null;
83             default:
84                 throw new IllegalArgumentException("No more types allowed");
85         }
86     }
87 }