2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer;
23 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
25 import java.util.Collection;
29 * Implementation of properties node walker which helps in forming a new tree from properties node.
31 * @param <T> node child of properties node.
33 public class DefaultPropertiesNodeWalker<T extends NodeChild> implements PropertiesNodeWalker {
36 public void walk(PropertiesNodeListener listener,
37 PropertiesNode propertiesNode) throws SvcLogicException {
38 listener.start(propertiesNode);
39 walkChildNode(listener, propertiesNode);
40 listener.end(propertiesNode);
44 * Walks the children node from the parent node.
46 * @param listener properties node listener
47 * @param propertiesNode properties node
48 * @throws SvcLogicException when properties node walking fails
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);
62 if (propertiesNode instanceof RootNode) {
63 processAugments(propertiesNode, listener);
68 * Processes the augments present in the root node.
70 * @param node root node
71 * @param listener properties node listener
72 * @throws SvcLogicException when augment node walking fails
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);
91 * Returns the children node according to the property node type.
93 * @param value property node
94 * @return property node children
96 private Map<String,T> getChildren(PropertiesNode value) {
97 if (value instanceof RootNode) {
98 return ((RootNode) value).children();
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)
108 case MULTI_INSTANCE_LEAF_HOLDER_NODE:
109 return ((Map<String, T>) ((LeafListHolderNode) value)
111 case SINGLE_INSTANCE_LEAF_NODE:
112 case MULTI_INSTANCE_LEAF_NODE:
115 throw new IllegalArgumentException("No more types allowed");