/*
* ============LICENSE_START=======================================================
* Copyright (C) 2021 Pantheon.tech
- * Modifications (C) 2021 Nordix Foundation
+ * Modifications (C) 2021-2023 Nordix Foundation
+ * Modifications Copyright (C) 2022 Bell Canada
+ * Modifications Copyright (C) 2022-2023 TechMahindra Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import static java.util.stream.Collectors.toUnmodifiableMap;
import com.google.common.collect.ImmutableMap;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.onap.cps.cpspath.parser.CpsPathQuery;
+import org.onap.cps.cpspath.parser.CpsPathUtil;
import org.onap.cps.spi.model.DataNode;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DataMapUtils {
+ /**
+ * Converts DataNode structure into a map including the root node identifier for a JSON response.
+ *
+ * @param dataNode data node object
+ * @return a map representing same data with the root node identifier
+ */
+ public static Map<String, Object> toDataMapWithIdentifier(final DataNode dataNode, final String prefix) {
+ final String nodeIdentifierWithPrefix = getNodeIdentifierWithPrefix(dataNode.getXpath(), prefix);
+ return ImmutableMap.<String, Object>builder().put(nodeIdentifierWithPrefix, toDataMap(dataNode)).build();
+ }
+
+ /**
+ * Converts list of DataNode structure into a map including the root node identifier for a JSON response.
+ * @param dataNodeList list of data nodes for a given anchor name
+ * @param anchorName anchor name
+ * @param prefix prefix
+ * @return a map representing same list of data for given anchor with the root node identifier
+ */
+ public static Map<String, Object> toDataMapWithIdentifierAndAnchor(final List<DataNode> dataNodeList,
+ final String anchorName, final String prefix) {
+ final List<Map<String, Object>> dataMaps = toDataNodesWithIdentifier(dataNodeList, prefix);
+ return ImmutableMap.<String, Object>builder().put("anchorName", anchorName)
+ .put("dataNodes", dataMaps).build();
+ }
+
+ private static List<Map<String, Object>> toDataNodesWithIdentifier(final List<DataNode> dataNodeList,
+ final String prefix) {
+ final List<Map<String, Object>> dataMaps = new ArrayList<>(dataNodeList.size());
+ for (final DataNode dataNode: dataNodeList) {
+ final String nodeIdentifierWithPrefix = getNodeIdentifierWithPrefix(dataNode.getXpath(), prefix);
+ final Map<String, Object> dataMap = ImmutableMap.<String, Object>builder()
+ .put(nodeIdentifierWithPrefix, toDataMap(dataNode)).build();
+ dataMaps.add(dataMap);
+ }
+ return dataMaps;
+ }
+
/**
* Converts DataNode structure into a map for a JSON response.
*
* @param dataNode data node object
* @return a map representing same data
*/
-
public static Map<String, Object> toDataMap(final DataNode dataNode) {
return ImmutableMap.<String, Object>builder()
.putAll(dataNode.getLeaves())
));
}
- private static String getNodeIdentifier(final String xpath) {
- final int fromIndex = xpath.lastIndexOf("/") + 1;
- final int toIndex = xpath.indexOf("[", fromIndex);
- return toIndex > 0 ? xpath.substring(fromIndex, toIndex) : xpath.substring(fromIndex);
+ private static String getNodeIdentifier(String xpath) {
+ final CpsPathQuery cpsPathQuery = CpsPathUtil.getCpsPathQuery(xpath);
+ if (cpsPathQuery.isPathToListElement()) {
+ xpath = cpsPathQuery.getXpathPrefix();
+ }
+ final int fromIndex = xpath.lastIndexOf('/') + 1;
+ return xpath.substring(fromIndex);
+ }
+
+ private static String getNodeIdentifierWithPrefix(final String xpath, final String moduleNamePrefix) {
+ if (moduleNamePrefix != null) {
+ return moduleNamePrefix + ":" + getNodeIdentifier(xpath);
+ }
+ return getNodeIdentifier(xpath);
}
private static boolean isContainerNode(final String xpath) {