Merge "API versioning supported and added different versions for POST APIs"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / InventoryPersistence.java
index c24063c..6d006d9 100644 (file)
@@ -1,7 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 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.
 
 package org.onap.cps.ncmp.api.inventory;
 
-import java.time.OffsetDateTime;
-import java.util.List;
-import lombok.RequiredArgsConstructor;
-import org.onap.cps.api.CpsDataService;
-import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
+import java.util.Collection;
+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.utils.CpsValidator;
-import org.onap.cps.utils.JsonObjectMapper;
-import org.springframework.stereotype.Component;
+import org.onap.cps.spi.model.ModuleDefinition;
+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 static final String XPATH_TO_CM_HANDLE = "/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 final JsonObjectMapper jsonObjectMapper;
+    /**
+     * 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 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 CpsDataPersistenceService cpsDataPersistenceService;
+    /**
+     * 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 static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
+    /**
+     * 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(XPATH_TO_CM_HANDLE, cmHandleId) + "/state",
-            FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
-        return 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(XPATH_TO_CM_HANDLE, 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.
+     * Get data node via xpath.
      *
-     * @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 xpath xpath
+     * @return data node
      */
-    public List<DataNode> getCmHandleDataNodesByCpsPath(final String cpsPath,
-                                                        final FetchDescendantsOption fetchDescendantsOption) {
-        return cpsDataPersistenceService.queryDataNodes(
-            NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cpsPath, fetchDescendantsOption);
-    }
+    DataNode getDataNode(String xpath);
 
     /**
-     * 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
+     * @param fetchDescendantsOption fetch descendants option
+     * @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);
-    }
+    DataNode getDataNode(String xpath, FetchDescendantsOption fetchDescendantsOption);
 
     /**
-     * Method which returns cm handles by the operational sync state of cm handle.
-     * @param syncState sync state
-     * @return a list of cm handles
+     * Get data node of given cm handle.
+     *
+     * @param cmHandleId cmHandle ID
+     * @return data node
      */
-    public List<DataNode> getCmHandlesByOperationalSyncState(final SyncState syncState) {
-        return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
-            NCMP_DMI_REGISTRY_ANCHOR, "//state/datastores"
-                + "/operational[@sync-state=\"" + syncState + "\"]/ancestor::cm-handles",
-            FetchDescendantsOption.OMIT_DESCENDANTS);
-    }
+    DataNode getCmHandleDataNode(String cmHandleId);
 
     /**
-     * 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 CM handles that has given module names.
+     *
+     * @param moduleNamesForQuery module names
+     * @return Collection of CM handle Ids
      */
-    public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
-        CpsValidator.validateNameCharacters(cmHandleId);
-        return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
-    }
+    Collection<String> getCmHandleIdsWithGivenModules(Collection<String> moduleNamesForQuery);
 
-    private DataNode getCmHandleDataNode(final String cmHandle) {
-        return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
-            NCMP_DMI_REGISTRY_ANCHOR,
-            String.format(XPATH_TO_CM_HANDLE, cmHandle),
-            FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
-    }
+    /**
+     * 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);
 
-}
\ No newline at end of file
+    /**
+     * Deletes data node for given anchor and dataspace.
+     *
+     * @param dataNodeXpath data node xpath
+     */
+    void deleteDataNode(String dataNodeXpath);
+}