/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2022 Nordix Foundation
+ * 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 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 {
-
- private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
-
- private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
-
- private final JsonObjectMapper jsonObjectMapper;
-
- private final CpsDataService cpsDataService;
-
- private final CpsDataPersistenceService cpsDataPersistenceService;
-
- private static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
+public interface InventoryPersistence {
/**
* Get the Cm Handle Composite State from the data node.
* @param cmHandleId cm handle id
* @return the cm handle composite state
*/
- public CompositeState getCmHandleState(final String cmHandleId) {
- final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
- "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state",
- FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
- return compositeStateBuilder.fromDataNode(stateAsDataNode).build();
- }
+ CompositeState getCmHandleState(String cmHandleId);
/**
* Save the cm handles state.
*
- * @param cmHandleId cm handle id
+ * @param cmHandleId cm handle id
* @param compositeState composite state
*/
- 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,
- "/dmi-registry/cm-handles[@id='" + cmHandleId + "']",
- cmHandleJsonData, OffsetDateTime.now());
- }
+ void saveCmHandleState(String cmHandleId, CompositeState compositeState);
/**
- * Method which returns cm handles by the cm handles state.
+ * Save all cm handles states in batch.
*
- * @param cmHandleState cm handle state
- * @return a list of cm handles
+ * @param cmHandleStatePerCmHandleId contains cm handle id and updated state
*/
- 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 saveCmHandleStateBatch(Map<String, CompositeState> cmHandleStatePerCmHandleId);
/**
* 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
*/
- public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
- CpsValidator.validateNameCharacters(cmHandleId);
- return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
- }
+ YangModelCmHandle getYangModelCmHandle(String cmHandleId);
+
+ /**
+ * 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);
+
+ /**
+ * 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);
+
+ /**
+ * 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);
+
+ /**
+ * Method to save cmHandle.
+ *
+ * @param yangModelCmHandle cmHandle represented as Yang Model
+ */
+ void saveCmHandle(YangModelCmHandle yangModelCmHandle);
+
+ /**
+ * Method to save batch of cm handles.
+ *
+ * @param yangModelCmHandles cm handle represented as Yang Models
+ */
+ void saveCmHandleBatch(Collection<YangModelCmHandle> yangModelCmHandles);
+
+ /**
+ * Method to delete a list or a list element.
+ *
+ * @param listElementXpath list element xPath
+ */
+ void deleteListOrListElement(String listElementXpath);
+
+ /**
+ * Method to delete a schema set.
+ *
+ * @param schemaSetName schema set name
+ */
+ void deleteSchemaSetWithCascade(String schemaSetName);
+
+ /**
+ * Method to delete multiple schema sets.
+ *
+ * @param schemaSetNames schema set names
+ */
+ void deleteSchemaSetsWithCascade(Collection<String> schemaSetNames);
+
+ /**
+ * Get data node via xpath.
+ *
+ * @param xpath xpath
+ * @return data node
+ */
+ Collection<DataNode> getDataNode(String xpath);
+
+ /**
+ * Get data node via xpath.
+ *
+ * @param xpath xpath
+ * @param fetchDescendantsOption fetch descendants option
+ * @return data node
+ */
+ Collection<DataNode> getDataNode(String xpath, FetchDescendantsOption fetchDescendantsOption);
+
+ /**
+ * Get collection of data nodes via xpaths.
+ *
+ * @param xpaths collection of xpaths
+ * @return collection of data nodes
+ */
+ Collection<DataNode> getDataNodes(Collection<String> xpaths);
+
+ /**
+ * Get collection of data nodes via xpaths.
+ *
+ * @param xpaths collection of xpaths
+ * @param fetchDescendantsOption fetch descendants option
+ * @return collection of data nodes
+ */
+ Collection<DataNode> getDataNodes(Collection<String> xpaths, FetchDescendantsOption fetchDescendantsOption);
- private DataNode getCmHandleDataNode(final String cmHandle) {
- final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
- return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
- NCMP_DMI_REGISTRY_ANCHOR,
- xpathForDmiRegistryToFetchCmHandle,
- FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
- }
+ /**
+ * Get data node of given cm handle.
+ *
+ * @param cmHandleId cmHandle ID
+ * @return data node
+ */
+ Collection<DataNode> getCmHandleDataNode(String cmHandleId);
+ /**
+ * 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);
}