Use getDataNodes (plural version) into NCMP to get CM handles
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / InventoryPersistenceImpl.java
index 29712f4..c2578cc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022-2023 Nordix Foundation
+ *  Copyright (C) 2022 Nordix Foundation
  *  Modifications Copyright (C) 2022 Bell Canada
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -33,6 +33,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.api.CpsAdminService;
@@ -41,6 +42,7 @@ import org.onap.cps.api.CpsModuleService;
 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
 import org.onap.cps.spi.FetchDescendantsOption;
+import org.onap.cps.spi.exceptions.DataValidationException;
 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
 import org.onap.cps.spi.model.DataNode;
 import org.onap.cps.spi.model.ModuleDefinition;
@@ -75,8 +77,8 @@ public class InventoryPersistenceImpl implements InventoryPersistence {
     @Override
     public CompositeState getCmHandleState(final String cmHandleId) {
         final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
-            String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId) + "/state",
-            FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
+                String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId) + "/state",
+                FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
         cpsValidator.validateNameCharacters(cmHandleId);
         return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
     }
@@ -106,6 +108,21 @@ public class InventoryPersistenceImpl implements InventoryPersistence {
         return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
     }
 
+    @Override
+    public Collection<YangModelCmHandle> getYangModelCmHandles(final Collection<String> cmHandleIds) {
+        final Collection<String> validCmHandleIds = new ArrayList<>();
+        cmHandleIds.forEach(cmHandleId -> {
+            try {
+                cpsValidator.validateNameCharacters(cmHandleId);
+                validCmHandleIds.add(cmHandleId);
+            } catch (final DataValidationException dataValidationException) {
+                log.error("DataValidationException in CmHandleId {} to be ignored",
+                        dataValidationException.getMessage());
+            }
+        });
+        return YangDataConverter.convertDataNodesToYangModelCmHandles(getCmHandleDataNodes(validCmHandleIds));
+    }
+
     @Override
     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
         return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
@@ -142,7 +159,7 @@ public class InventoryPersistenceImpl implements InventoryPersistence {
 
     @Override
     @Timed(value = "cps.ncmp.inventory.persistence.schemaset.delete",
-        description = "Time taken to delete a schemaset")
+            description = "Time taken to delete a schemaset")
     public void deleteSchemaSetWithCascade(final String schemaSetName) {
         try {
             cpsValidator.validateNameCharacters(schemaSetName);
@@ -154,23 +171,45 @@ public class InventoryPersistenceImpl implements InventoryPersistence {
     }
 
     @Override
+    @Timed(value = "cps.ncmp.inventory.persistence.datanode.get",
+            description = "Time taken to get a data node (from ncmp dmi registry)")
     public DataNode getDataNode(final String xpath) {
         return getDataNode(xpath, INCLUDE_ALL_DESCENDANTS);
     }
 
     @Override
     @Timed(value = "cps.ncmp.inventory.persistence.datanode.get",
-        description = "Time taken to get a data node (from ncmp dmi registry)")
+            description = "Time taken to get a data node (from ncmp dmi registry)")
     public DataNode getDataNode(final String xpath, final FetchDescendantsOption fetchDescendantsOption) {
         return cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
                 xpath, fetchDescendantsOption);
     }
 
+    @Override
+    public Collection<DataNode> getDataNodes(final Collection<String> xpaths) {
+        return getDataNodes(xpaths, INCLUDE_ALL_DESCENDANTS);
+    }
+
+    @Override
+    public Collection<DataNode> getDataNodes(final Collection<String> xpaths,
+                                             final FetchDescendantsOption fetchDescendantsOption) {
+        return cpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
+                xpaths, fetchDescendantsOption);
+    }
+
     @Override
     public DataNode getCmHandleDataNode(final String cmHandleId) {
         return this.getDataNode(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId));
     }
 
+    @Override
+    public Collection<DataNode> getCmHandleDataNodes(final Collection<String> cmHandleIds) {
+        final Collection<String> xpaths = cmHandleIds.stream().map(cmHandleId ->
+                        String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId))
+                .collect(Collectors.toList());
+        return this.getDataNodes(xpaths);
+    }
+
     @Override
     public Collection<String> getCmHandleIdsWithGivenModules(final Collection<String> moduleNamesForQuery) {
         return cpsAdminService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
@@ -191,4 +230,4 @@ public class InventoryPersistenceImpl implements InventoryPersistence {
     public void deleteDataNodes(final Collection<String> dataNodeXpaths) {
         cpsDataService.deleteDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpaths, NO_TIMESTAMP);
     }
-}
+}
\ No newline at end of file