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.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.ValueNode;
43 public class DataNodeBuilder {
45 private NormalizedNode<?, ?> normalizedNodeTree;
47 private String parentNodeXpath = "";
48 private Map<String, Object> leaves = Collections.emptyMap();
49 private Collection<DataNode> childDataNodes = Collections.emptySet();
52 * To use parent node xpath for creating {@link DataNode}.
54 * @param parentNodeXpath xpath of a parent node
55 * @return this {@link DataNodeBuilder} object
57 public DataNodeBuilder withParentNodeXpath(final String parentNodeXpath) {
58 this.parentNodeXpath = parentNodeXpath;
64 * To use {@link NormalizedNode} for creating {@link DataNode}.
66 * @param normalizedNodeTree used for creating the Data Node
67 * @return this {@link DataNodeBuilder} object
69 public DataNodeBuilder withNormalizedNodeTree(final NormalizedNode<?, ?> normalizedNodeTree) {
70 this.normalizedNodeTree = normalizedNodeTree;
75 * To use xpath for creating {@link DataNode}.
77 * @param xpath for the data node
78 * @return DataNodeBuilder
80 public DataNodeBuilder withXpath(final String xpath) {
86 * To use attributes for creating {@link DataNode}.
88 * @param leaves for the data node
89 * @return DataNodeBuilder
91 public DataNodeBuilder withLeaves(final Map<String, Object> leaves) {
97 * To specify child nodes needs to be used while creating {@link DataNode}.
99 * @param childDataNodes to be added to the dataNode
100 * @return DataNodeBuilder
102 public DataNodeBuilder withChildDataNodes(final Collection<DataNode> childDataNodes) {
103 // Added as this is being set from test cases .
104 // Open for suggestions
105 this.childDataNodes = childDataNodes;
110 * To create the {@link DataNode}.
112 * @return {@link DataNode}
114 public DataNode build() {
115 if (normalizedNodeTree != null) {
116 return buildFromNormalizedNodeTree();
118 return buildFromAttributes();
123 * To build a {@link Collection} of {@link DataNode} objects.
125 * @return {@link DataNode} {@link Collection}
127 public Collection<DataNode> buildCollection() {
128 if (normalizedNodeTree != null) {
129 return buildCollectionFromNormalizedNodeTree();
131 return Set.of(buildFromAttributes());
135 private DataNode buildFromAttributes() {
136 final var dataNode = new DataNode();
137 dataNode.setXpath(xpath);
138 dataNode.setLeaves(leaves);
139 dataNode.setChildDataNodes(childDataNodes);
143 private DataNode buildFromNormalizedNodeTree() {
144 final Collection<DataNode> dataNodeCollection = buildCollectionFromNormalizedNodeTree();
145 return dataNodeCollection.iterator().next();
148 private Collection<DataNode> buildCollectionFromNormalizedNodeTree() {
149 final var parentDataNode = new DataNodeBuilder().withXpath(parentNodeXpath).build();
150 addDataNodeFromNormalizedNode(parentDataNode, normalizedNodeTree);
151 return parentDataNode.getChildDataNodes();
154 private static void addDataNodeFromNormalizedNode(final DataNode currentDataNode,
155 final NormalizedNode<?, ?> normalizedNode) {
157 if (normalizedNode instanceof DataContainerNode) {
158 addYangContainer(currentDataNode, (DataContainerNode<?>) normalizedNode);
159 } else if (normalizedNode instanceof MapNode) {
160 addDataNodeForEachListElement(currentDataNode, (MapNode) normalizedNode);
161 } else if (normalizedNode instanceof ValueNode) {
162 final ValueNode<?, ?> valuesNode = (ValueNode<?, ?>) normalizedNode;
163 addYangLeaf(currentDataNode, valuesNode.getNodeType().getLocalName(), valuesNode.getValue());
164 } else if (normalizedNode instanceof LeafSetNode) {
165 addYangLeafList(currentDataNode, (LeafSetNode<?>) normalizedNode);
167 log.warn("Unsupported NormalizedNode type detected: {}", normalizedNode.getClass());
171 private static void addYangContainer(final DataNode currentDataNode, final DataContainerNode<?> dataContainerNode) {
172 final DataNode dataContainerDataNode =
173 (dataContainerNode.getIdentifier() instanceof YangInstanceIdentifier.AugmentationIdentifier)
175 : createAndAddChildDataNode(currentDataNode, YangUtils.buildXpath(dataContainerNode.getIdentifier()));
176 final Collection<DataContainerChild<?, ?>> normalizedChildNodes = dataContainerNode.getValue();
177 for (final NormalizedNode<?, ?> normalizedNode : normalizedChildNodes) {
178 addDataNodeFromNormalizedNode(dataContainerDataNode, normalizedNode);
182 private static void addYangLeaf(final DataNode currentDataNode, final String leafName, final Object leafValue) {
183 final Map<String, Object> leaves = new ImmutableMap.Builder<String, Object>()
184 .putAll(currentDataNode.getLeaves())
185 .put(leafName, leafValue)
187 currentDataNode.setLeaves(leaves);
190 private static void addYangLeafList(final DataNode currentDataNode, final LeafSetNode<?> leafSetNode) {
191 final String leafListName = leafSetNode.getNodeType().getLocalName();
192 final List<?> leafListValues = ((Collection<? extends NormalizedNode<?, ?>>) leafSetNode.getValue())
194 .map(normalizedNode -> ((ValueNode<?, ?>) normalizedNode).getValue())
195 .collect(Collectors.toUnmodifiableList());
196 addYangLeaf(currentDataNode, leafListName, leafListValues);
199 private static void addDataNodeForEachListElement(final DataNode currentDataNode, final MapNode mapNode) {
200 final Collection<MapEntryNode> mapEntryNodes = mapNode.getValue();
201 for (final MapEntryNode mapEntryNode : mapEntryNodes) {
202 addDataNodeFromNormalizedNode(currentDataNode, mapEntryNode);
206 private static DataNode createAndAddChildDataNode(final DataNode parentDataNode, final String childXpath) {
208 final var newChildDataNode = new DataNodeBuilder()
209 .withXpath(parentDataNode.getXpath() + childXpath)
211 final Set<DataNode> allChildDataNodes = new ImmutableSet.Builder<DataNode>()
212 .addAll(parentDataNode.getChildDataNodes())
213 .add(newChildDataNode)
215 parentDataNode.setChildDataNodes(allChildDataNodes);
216 return newChildDataNode;