2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.model;
23 import com.google.common.collect.ImmutableMap;
24 import com.google.common.collect.ImmutableSet;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
30 import java.util.stream.Collectors;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.utils.YangUtils;
33 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.ValueNode;
42 public class DataNodeBuilder {
44 private NormalizedNode<?, ?> normalizedNodeTree;
46 private Map<String, Object> leaves = Collections.emptyMap();
47 private Collection<DataNode> childDataNodes = Collections.emptySet();
51 * To use {@link NormalizedNode} for creating {@link DataNode}.
53 * @param normalizedNodeTree used for creating the Data Node
54 * @return this {@link DataNodeBuilder} object
56 public DataNodeBuilder withNormalizedNodeTree(final NormalizedNode<?, ?> normalizedNodeTree) {
57 this.normalizedNodeTree = normalizedNodeTree;
62 * To use xpath for creating {@link DataNode}.
64 * @param xpath for the data node
65 * @return DataNodeBuilder
67 public DataNodeBuilder withXpath(final String xpath) {
73 * To use attributes for creating {@link DataNode}.
75 * @param leaves for the data node
76 * @return DataNodeBuilder
78 public DataNodeBuilder withLeaves(final Map<String, Object> leaves) {
84 * To specify child nodes needs to be used while creating {@link DataNode}.
86 * @param childDataNodes to be added to the dataNode
87 * @return DataNodeBuilder
89 public DataNodeBuilder withChildDataNodes(final Collection<DataNode> childDataNodes) {
90 // Added as this is being set from test cases .
91 // Open for suggestions
92 this.childDataNodes = childDataNodes;
97 * To create the {@link DataNode}.
99 * @return {@link DataNode}
101 public DataNode build() {
102 if (normalizedNodeTree != null) {
103 return buildFromNormalizedNodeTree();
105 return buildFromAttributes();
109 private DataNode buildFromAttributes() {
110 final DataNode dataNode = new DataNode();
111 dataNode.setXpath(xpath);
112 dataNode.setLeaves(leaves);
113 dataNode.setChildDataNodes(childDataNodes);
117 private DataNode buildFromNormalizedNodeTree() {
118 final DataNode formalRootDataNode = new DataNodeBuilder().withXpath("").build();
119 addDataNodeFromNormalizedNode(formalRootDataNode, normalizedNodeTree);
120 return formalRootDataNode.getChildDataNodes().iterator().next();
123 private static void addDataNodeFromNormalizedNode(final DataNode currentDataNode,
124 final NormalizedNode<?, ?> normalizedNode) {
126 if (normalizedNode instanceof DataContainerNode) {
127 addYangContainer(currentDataNode, (DataContainerNode<?>) normalizedNode);
128 } else if (normalizedNode instanceof MapNode) {
129 addDataNodeForEachListElement(currentDataNode, (MapNode) normalizedNode);
130 } else if (normalizedNode instanceof ValueNode) {
131 final ValueNode<?, ?> valuesNode = (ValueNode<?, ?>) normalizedNode;
132 addYangLeaf(currentDataNode, valuesNode.getNodeType().getLocalName(), valuesNode.getValue());
133 } else if (normalizedNode instanceof LeafSetNode) {
134 addYangLeafList(currentDataNode, (LeafSetNode<?>) normalizedNode);
136 log.warn("Unsupported NormalizedNode type detected: {}", normalizedNode.getClass());
140 private static void addYangContainer(final DataNode currentDataNode, final DataContainerNode<?> dataContainerNode) {
141 final DataNode dataContainerDataNode = createAndAddChildDataNode(currentDataNode,
142 YangUtils.buildXpath(dataContainerNode.getIdentifier()));
143 final Collection<DataContainerChild<?, ?>> normalizedChildNodes = dataContainerNode.getValue();
144 for (final NormalizedNode<?, ?> normalizedNode : normalizedChildNodes) {
145 addDataNodeFromNormalizedNode(dataContainerDataNode, normalizedNode);
149 private static void addYangLeaf(final DataNode currentDataNode, final String leafName, final Object leafValue) {
150 final Map<String, Object> leaves = new ImmutableMap.Builder<String, Object>()
151 .putAll(currentDataNode.getLeaves())
152 .put(leafName, leafValue)
154 currentDataNode.setLeaves(leaves);
157 private static void addYangLeafList(final DataNode currentDataNode, final LeafSetNode<?> leafSetNode) {
158 final String leafListName = leafSetNode.getNodeType().getLocalName();
159 final List<?> leafListValues = ((Collection<? extends NormalizedNode<?, ?>>) leafSetNode.getValue())
161 .map(normalizedNode -> ((ValueNode<?, ?>) normalizedNode).getValue())
162 .collect(Collectors.toUnmodifiableList());
163 addYangLeaf(currentDataNode, leafListName, leafListValues);
166 private static void addDataNodeForEachListElement(final DataNode currentDataNode, final MapNode mapNode) {
167 final Collection<MapEntryNode> mapEntryNodes = mapNode.getValue();
168 for (final MapEntryNode mapEntryNode : mapEntryNodes) {
169 addDataNodeFromNormalizedNode(currentDataNode, mapEntryNode);
173 private static DataNode createAndAddChildDataNode(final DataNode parentDataNode, final String childXpath) {
175 final DataNode newChildDataNode = new DataNodeBuilder()
176 .withXpath(parentDataNode.getXpath() + childXpath)
178 final Set<DataNode> allChildDataNodes = new ImmutableSet.Builder<DataNode>()
179 .addAll(parentDataNode.getChildDataNodes())
180 .add(newChildDataNode)
182 parentDataNode.setChildDataNodes(allChildDataNodes);
183 return newChildDataNode;