Merge "Change default use of Model Loader to TRUE"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / InventoryPersistence.java
index d47da6c..cbd30a8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Nordix Foundation
- *  Modifications Copyright (C) 2022 Bell Canada
+ *  Copyright (C) 2022-2023 Nordix Foundation
+ *  Modifications Copyright (C) 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.
 
 package org.onap.cps.ncmp.api.inventory;
 
-import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
-
-import java.time.OffsetDateTime;
 import java.util.Collection;
-import java.util.List;
-import lombok.RequiredArgsConstructor;
-import org.onap.cps.api.CpsDataService;
-import org.onap.cps.api.CpsModuleService;
-import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
+import java.util.Map;
 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
-import org.onap.cps.spi.CpsDataPersistenceService;
 import org.onap.cps.spi.FetchDescendantsOption;
 import org.onap.cps.spi.model.DataNode;
 import org.onap.cps.spi.model.ModuleDefinition;
-import org.onap.cps.utils.CpsValidator;
-import org.onap.cps.utils.JsonObjectMapper;
-import org.springframework.stereotype.Component;
+import org.onap.cps.spi.model.ModuleReference;
 
-@RequiredArgsConstructor
-@Component
-public class InventoryPersistence {
+public interface InventoryPersistence {
 
-    private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
+    /**
+     * Get the Cm Handle Composite State from the data node.
+     *
+     * @param cmHandleId cm handle id
+     * @return the cm handle composite state
+     */
+    CompositeState getCmHandleState(String cmHandleId);
 
-    private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
+    /**
+     * Save the cm handles state.
+     *
+     * @param cmHandleId     cm handle id
+     * @param compositeState composite state
+     */
+    void saveCmHandleState(String cmHandleId, CompositeState compositeState);
 
-    private String xpathCmHandle = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
+    /**
+     * Save all cm handles states in batch.
+     *
+     * @param cmHandleStatePerCmHandleId contains cm handle id and updated state
+     */
+    void saveCmHandleStateBatch(Map<String, CompositeState> cmHandleStatePerCmHandleId);
 
-    private static final String ANCESTOR_CM_HANDLES = "\"]/ancestor::cm-handles";
+    /**
+     * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
+     *
+     * @param cmHandleId the id of the cm handle
+     * @return yang model cm handle
+     */
+    YangModelCmHandle getYangModelCmHandle(String cmHandleId);
 
-    private final JsonObjectMapper jsonObjectMapper;
+    /**
+     * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
+     *
+     * @param cmHandleIds a list of the ids of the cm handles
+     * @return collection of yang model cm handles
+     */
+    Collection<YangModelCmHandle> getYangModelCmHandles(Collection<String> cmHandleIds);
 
-    private final CpsDataService cpsDataService;
+    /**
+     * Method to return module definitions by cmHandleId.
+     *
+     * @param cmHandleId cm handle ID
+     * @return a collection of module definitions (moduleName, revision and yang resource content)
+     */
+    Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(String cmHandleId);
 
-    private final CpsModuleService cpsModuleService;
+    /**
+     * Method to return module references by cmHandleId.
+     *
+     * @param cmHandleId cm handle ID
+     * @return a collection of module references (moduleName and revision)
+     */
+    Collection<ModuleReference> getYangResourcesModuleReferences(String cmHandleId);
 
-    private final CpsDataPersistenceService cpsDataPersistenceService;
+    /**
+     * Method to save cmHandle.
+     *
+     * @param yangModelCmHandle cmHandle represented as Yang Model
+     */
+    void saveCmHandle(YangModelCmHandle yangModelCmHandle);
 
     /**
-     * Get the Cm Handle Composite State from the data node.
+     * Method to save batch of cm handles.
      *
-     * @param cmHandleId cm handle id
-     * @return the cm handle composite state
+     * @param yangModelCmHandles cm handle represented as Yang Models
      */
-    public CompositeState getCmHandleState(final String cmHandleId) {
-        final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
-            String.format(xpathCmHandle, cmHandleId) + "/state",
-            FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
-        return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
-    }
+    void saveCmHandleBatch(Collection<YangModelCmHandle> yangModelCmHandles);
 
     /**
-     * Save the cm handles state.
+     * Method to delete a list or a list element.
      *
-     * @param cmHandleId    cm handle id
-     * @param compositeState composite state
+     * @param listElementXpath list element xPath
      */
-    public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
-        final String cmHandleJsonData = String.format("{\"state\":%s}",
-            jsonObjectMapper.asJsonString(compositeState));
-        cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
-            String.format(xpathCmHandle, cmHandleId),
-            cmHandleJsonData, OffsetDateTime.now());
-    }
+    void deleteListOrListElement(String listElementXpath);
 
     /**
-     * Method which returns cm handles by the cm handles state.
+     * Method to delete a schema set.
      *
-     * @param cmHandleState cm handle state
-     * @return a list of cm handles
+     * @param schemaSetName schema set name
      */
-    public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
-        return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
-            NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\""
-                + cmHandleState + ANCESTOR_CM_HANDLES,
-            FetchDescendantsOption.OMIT_DESCENDANTS);
-    }
+    void deleteSchemaSetWithCascade(String schemaSetName);
 
     /**
-     * Method to return data nodes representing the cm handles.
+     * Method to delete multiple schema sets.
      *
-     * @param cpsPath cps path for which the cmHandle is requested
-     * @param fetchDescendantsOption defines the scope of data to fetch: either single node or all the descendant nodes
-     * @return a list of data nodes representing the cm handles.
+     * @param schemaSetNames schema set names
      */
-    public List<DataNode> getCmHandleDataNodesByCpsPath(final String cpsPath,
-                                                        final FetchDescendantsOption fetchDescendantsOption) {
-        return cpsDataPersistenceService.queryDataNodes(
-            NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cpsPath, fetchDescendantsOption);
-    }
+    void deleteSchemaSetsWithCascade(Collection<String> schemaSetNames);
 
     /**
-     * Method which returns cm handles by the cm handle id and state.
-     * @param cmHandleId cm handle id
-     * @param cmHandleState cm handle state
-     * @return a list of cm handles
+     * Get data node via xpath.
+     *
+     * @param xpath xpath
+     * @return data node
      */
-    public List<DataNode> getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) {
-        return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
-            NCMP_DMI_REGISTRY_ANCHOR, "//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\""
-                + cmHandleState + ANCESTOR_CM_HANDLES,
-            FetchDescendantsOption.OMIT_DESCENDANTS);
-    }
+    Collection<DataNode> getDataNode(String xpath);
 
     /**
-     * Method which returns cm handles by the operational sync state of cm handle.
-     * @param dataStoreSyncState sync state
-     * @return a list of cm handles
+     * Get data node via xpath.
+     *
+     * @param xpath xpath
+     * @param fetchDescendantsOption fetch descendants option
+     * @return data node
      */
-    public List<DataNode> getCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
-        return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
-            NCMP_DMI_REGISTRY_ANCHOR, "//state/datastores"
-                + "/operational[@sync-state=\"" + dataStoreSyncState + ANCESTOR_CM_HANDLES,
-            FetchDescendantsOption.OMIT_DESCENDANTS);
-    }
+    Collection<DataNode> getDataNode(String xpath, FetchDescendantsOption fetchDescendantsOption);
 
     /**
-     * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
-     * @param cmHandleId the id of the cm handle
-     * @return yang model cm handle
+     * Get collection of data nodes via xpaths.
+     *
+     * @param xpaths collection of xpaths
+     * @return collection of data nodes
      */
-    public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
-        CpsValidator.validateNameCharacters(cmHandleId);
-        return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
-    }
+    Collection<DataNode> getDataNodes(Collection<String> xpaths);
 
     /**
-     * Method to return module definitions by cmHandleId.
+     * Get collection of data nodes via xpaths.
      *
-     * @param cmHandleId cm handle ID
-     * @return a collection of module definitions (moduleName, revision and yang resource content)
+     * @param xpaths collection of xpaths
+     * @param fetchDescendantsOption fetch descendants option
+     * @return collection of data nodes
      */
-    public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
-        return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
-    }
+    Collection<DataNode> getDataNodes(Collection<String> xpaths, FetchDescendantsOption fetchDescendantsOption);
 
-    private DataNode getCmHandleDataNode(final String cmHandle) {
-        return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
-            NCMP_DMI_REGISTRY_ANCHOR,
-            String.format(xpathCmHandle, cmHandle),
-            FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
-    }
+    /**
+     * Get data node of given cm handle.
+     *
+     * @param cmHandleId cmHandle ID
+     * @return data node
+     */
+    Collection<DataNode> getCmHandleDataNode(String cmHandleId);
 
-}
\ No newline at end of file
+    /**
+     * Get collection of data nodes of given cm handles.
+     *
+     * @param cmHandleIds collection of cmHandle IDs
+     * @return collection of data nodes
+     */
+    Collection<DataNode> getCmHandleDataNodes(Collection<String> cmHandleIds);
+
+    /**
+     * get CM handles that has given module names.
+     *
+     * @param moduleNamesForQuery module names
+     * @return Collection of CM handle Ids
+     */
+    Collection<String> getCmHandleIdsWithGivenModules(Collection<String> moduleNamesForQuery);
+
+    /**
+     * Replaces list content by removing all existing elements and inserting the given new elements as data nodes.
+     *
+     * @param parentNodeXpath parent node xpath
+     * @param dataNodes       datanodes representing the updated data
+     */
+    void replaceListContent(String parentNodeXpath, Collection<DataNode> dataNodes);
+
+    /**
+     * Deletes data node.
+     *
+     * @param dataNodeXpath data node xpath
+     */
+    void deleteDataNode(String dataNodeXpath);
+
+    /**
+     * Deletes multiple data nodes.
+     *
+     * @param dataNodeXpaths data node xpaths
+     */
+    void deleteDataNodes(Collection<String> dataNodeXpaths);
+}