import java.util.Set;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
+import org.onap.cps.spi.exceptions.DataValidationException;
import org.onap.cps.utils.YangUtils;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
}
}
+ /**
+ * To build a {@link Collection} of {@link DataNode} objects.
+ *
+ * @return {@link DataNode} {@link Collection}
+ */
+ public Collection<DataNode> buildCollection() {
+ if (normalizedNodeTree != null) {
+ return buildCollectionFromNormalizedNodeTree();
+ } else {
+ return Set.of(buildFromAttributes());
+ }
+ }
+
private DataNode buildFromAttributes() {
- final DataNode dataNode = new DataNode();
+ final var dataNode = new DataNode();
dataNode.setXpath(xpath);
dataNode.setLeaves(leaves);
dataNode.setChildDataNodes(childDataNodes);
}
private DataNode buildFromNormalizedNodeTree() {
- final DataNode parentDataNode = new DataNodeBuilder().withXpath(parentNodeXpath).build();
+ final Collection<DataNode> dataNodeCollection = buildCollectionFromNormalizedNodeTree();
+ if (!dataNodeCollection.iterator().hasNext()) {
+ throw new DataValidationException(
+ "Unsupported xpath: ", "Unsupported xpath as it is referring to one element");
+ }
+ return dataNodeCollection.iterator().next();
+ }
+
+ private Collection<DataNode> buildCollectionFromNormalizedNodeTree() {
+ final var parentDataNode = new DataNodeBuilder().withXpath(parentNodeXpath).build();
addDataNodeFromNormalizedNode(parentDataNode, normalizedNodeTree);
- return parentDataNode.getChildDataNodes().iterator().next();
+ return parentDataNode.getChildDataNodes();
}
private static void addDataNodeFromNormalizedNode(final DataNode currentDataNode,
private static DataNode createAndAddChildDataNode(final DataNode parentDataNode, final String childXpath) {
- final DataNode newChildDataNode = new DataNodeBuilder()
+ final var newChildDataNode = new DataNodeBuilder()
.withXpath(parentDataNode.getXpath() + childXpath)
.build();
final Set<DataNode> allChildDataNodes = new ImmutableSet.Builder<DataNode>()