Resolving root level augment nodes
[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.Collection;
26 import java.util.Map;
27
28 /**
29  * Implementation of properties node walker which helps in forming a new tree from properties node.
30  *
31  * @param <T> node child of properties node.
32  */
33 public class DefaultPropertiesNodeWalker<T extends NodeChild> implements PropertiesNodeWalker {
34
35     @Override
36     public void walk(PropertiesNodeListener listener,
37                      PropertiesNode propertiesNode) throws SvcLogicException {
38         listener.start(propertiesNode);
39         walkChildNode(listener, propertiesNode);
40         listener.end(propertiesNode);
41     }
42
43     /**
44      * Walks the children node from the parent node.
45      *
46      * @param listener properties node listener
47      * @param propertiesNode properties node
48      * @throws SvcLogicException when properties node walking fails
49      */
50     public void walkChildNode(PropertiesNodeListener listener,
51                               PropertiesNode propertiesNode)
52             throws SvcLogicException {
53         Map<String, T> children = getChildren(propertiesNode);
54         if (children != null) {
55             for (Map.Entry<String, T> entry : children.entrySet()) {
56                 PropertiesNode node = ((PropertiesNode) entry.getValue());
57                 listener.enterPropertiesNode(node);
58                 walkChildNode(listener, node);
59                 listener.exitPropertiesNode(node);
60             }
61         }
62         if (propertiesNode instanceof RootNode) {
63             processAugments(propertiesNode, listener);
64         }
65     }
66
67     /**
68      * Processes the augments present in the root node.
69      *
70      * @param node     root node
71      * @param listener properties node listener
72      * @throws SvcLogicException when augment node walking fails
73      */
74     private void processAugments(PropertiesNode node,
75                                  PropertiesNodeListener listener)
76             throws SvcLogicException {
77         for (Map.Entry<Object, Collection<PropertiesNode>>
78                 augToChild : node.augmentations().asMap().entrySet()) {
79             Collection<PropertiesNode> child = augToChild.getValue();
80             if (!child.isEmpty()) {
81                 for (PropertiesNode p : child) {
82                     listener.enterPropertiesNode(p);
83                     walkChildNode(listener, p);
84                     listener.exitPropertiesNode(p);
85                 }
86             }
87         }
88     }
89
90     /**
91      * Returns the children node according to the property node type.
92      *
93      * @param value property node
94      * @return property node children
95      */
96     private Map<String,T> getChildren(PropertiesNode value) {
97         if (value instanceof RootNode) {
98             return ((RootNode) value).children();
99         }
100         switch (value.nodeType()) {
101             case SINGLE_INSTANCE_NODE:
102                 return ((InnerNode) value).children();
103             case MULTI_INSTANCE_HOLDER_NODE:
104                 return ((Map<String, T>) ((ListHolderNode) value).children());
105             case MULTI_INSTANCE_NODE:
106                 return ((Map<String, T>) ((MultiInstanceNode) value)
107                         .children());
108             case MULTI_INSTANCE_LEAF_HOLDER_NODE:
109                 return ((Map<String, T>) ((LeafListHolderNode) value)
110                         .children());
111             case SINGLE_INSTANCE_LEAF_NODE:
112             case MULTI_INSTANCE_LEAF_NODE:
113                 return null;
114             default:
115                 throw new IllegalArgumentException("No more types allowed");
116         }
117     }
118 }