X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-service%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Futils%2FDataMapUtils.java;h=4413c6b08b77cf0590bc0a552c04c2c74694423d;hb=3e81716f0bbbde2328cd0910bc25a05b73d3d48e;hp=71a95f1ca084b4b7daa4eb2b5186dd14cd5a1328;hpb=a79c9f1bdf335843c29a425da53c15b5e353e5a3;p=cps.git diff --git a/cps-service/src/main/java/org/onap/cps/utils/DataMapUtils.java b/cps-service/src/main/java/org/onap/cps/utils/DataMapUtils.java index 71a95f1ca..4413c6b08 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/DataMapUtils.java +++ b/cps-service/src/main/java/org/onap/cps/utils/DataMapUtils.java @@ -1,7 +1,8 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Pantheon.tech - * Modifications (C) 2021 Nordix Foundation + * Modifications (C) 2021-2022 Nordix Foundation + * Modifications Copyright (C) 2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,13 +37,24 @@ 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 toDataMapWithIdentifier(final DataNode dataNode) { + return ImmutableMap.builder() + .put(getNodeIdentifierWithPrefix(dataNode.getXpath(), dataNode.getModuleNamePrefix()), toDataMap(dataNode)) + .build(); + } + /** * Converts DataNode structure into a map for a JSON response. * * @param dataNode data node object * @return a map representing same data */ - public static Map toDataMap(final DataNode dataNode) { return ImmutableMap.builder() .putAll(dataNode.getLeaves()) @@ -79,10 +91,19 @@ public class DataMapUtils { )); } - 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) { + if (xpath.endsWith("]")) { + xpath = xpath.substring(0, xpath.lastIndexOf('[')); + } + 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) {