Fetching data node by xpath - persistence layer
[cps.git] / cps-service / src / main / java / org / onap / cps / spi / model / DataNodeBuilder.java
1 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.spi.model;
22
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;
28 import java.util.Map;
29 import java.util.Set;
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;
40
41 @Slf4j
42 public class DataNodeBuilder {
43
44     private NormalizedNode<?, ?> normalizedNodeTree;
45     private String xpath;
46     private Map<String, Object> leaves = Collections.emptyMap();
47     private Collection<DataNode> childDataNodes = Collections.emptySet();
48
49
50     /**
51      * To use {@link NormalizedNode} for creating {@link DataNode}.
52      *
53      * @param normalizedNodeTree used for creating the Data Node
54      * @return this {@link DataNodeBuilder} object
55      */
56     public DataNodeBuilder withNormalizedNodeTree(final NormalizedNode<?, ?> normalizedNodeTree) {
57         this.normalizedNodeTree = normalizedNodeTree;
58         return this;
59     }
60
61     /**
62      * To use xpath for creating {@link DataNode}.
63      *
64      * @param xpath for the data node
65      * @return DataNodeBuilder
66      */
67     public DataNodeBuilder withXpath(final String xpath) {
68         this.xpath = xpath;
69         return this;
70     }
71
72     /**
73      * To use attributes for creating {@link DataNode}.
74      *
75      * @param leaves for the data node
76      * @return DataNodeBuilder
77      */
78     public DataNodeBuilder withLeaves(final Map<String, Object> leaves) {
79         this.leaves = leaves;
80         return this;
81     }
82
83     /**
84      * To specify child nodes needs to be used while creating {@link DataNode}.
85      *
86      * @param childDataNodes to be added to the dataNode
87      * @return DataNodeBuilder
88      */
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;
93         return this;
94     }
95
96     /**
97      * To create the {@link DataNode}.
98      *
99      * @return {@link DataNode}
100      */
101     public DataNode build() {
102         if (normalizedNodeTree != null) {
103             return buildFromNormalizedNodeTree();
104         } else {
105             return buildFromAttributes();
106         }
107     }
108
109     private DataNode buildFromAttributes() {
110         final DataNode dataNode = new DataNode();
111         dataNode.setXpath(xpath);
112         dataNode.setLeaves(leaves);
113         dataNode.setChildDataNodes(childDataNodes);
114         return dataNode;
115     }
116
117     private DataNode buildFromNormalizedNodeTree() {
118         final DataNode formalRootDataNode = new DataNodeBuilder().withXpath("").build();
119         addDataNodeFromNormalizedNode(formalRootDataNode, normalizedNodeTree);
120         return formalRootDataNode.getChildDataNodes().iterator().next();
121     }
122
123     private static void addDataNodeFromNormalizedNode(final DataNode currentDataNode,
124         final NormalizedNode<?, ?> normalizedNode) {
125
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);
135         } else {
136             log.warn("Unsupported NormalizedNode type detected: {}", normalizedNode.getClass());
137         }
138     }
139
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);
146         }
147     }
148
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)
153             .build();
154         currentDataNode.setLeaves(leaves);
155     }
156
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())
160             .stream()
161             .map(normalizedNode -> ((ValueNode<?, ?>) normalizedNode).getValue())
162             .collect(Collectors.toUnmodifiableList());
163         addYangLeaf(currentDataNode, leafListName, leafListValues);
164     }
165
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);
170         }
171     }
172
173     private static DataNode createAndAddChildDataNode(final DataNode parentDataNode, final String childXpath) {
174
175         final DataNode newChildDataNode = new DataNodeBuilder()
176             .withXpath(parentDataNode.getXpath() + childXpath)
177             .build();
178         final Set<DataNode> allChildDataNodes = new ImmutableSet.Builder<DataNode>()
179             .addAll(parentDataNode.getChildDataNodes())
180             .add(newChildDataNode)
181             .build();
182         parentDataNode.setChildDataNodes(allChildDataNodes);
183         return newChildDataNode;
184     }
185
186 }