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