/*
* ============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 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.Anchor;
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;
-
-@RequiredArgsConstructor
-@Component
-public class InventoryPersistence {
+import org.onap.cps.spi.model.ModuleReference;
- private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
+public interface InventoryPersistence {
- private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
+ /**
+ * 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 XPATH_TO_CM_HANDLE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
+ /**
+ * Save the cm handles state.
+ *
+ * @param cmHandleId cm handle id
+ * @param compositeState composite state
+ */
+ void saveCmHandleState(String cmHandleId, CompositeState compositeState);
- private final JsonObjectMapper jsonObjectMapper;
+ /**
+ * Save all cm handles states in batch.
+ *
+ * @param cmHandleStatePerCmHandleId contains cm handle id and updated state
+ */
+ void saveCmHandleStateBatch(Map<String, CompositeState> cmHandleStatePerCmHandleId);
- private final CpsDataService cpsDataService;
+ /**
+ * 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 CpsModuleService cpsModuleService;
+ /**
+ * 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 dataStoreSyncState 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 DataStoreSyncState dataStoreSyncState) {
- return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
- NCMP_DMI_REGISTRY_ANCHOR, "//state/datastores"
- + "/operational[@sync-state=\"" + dataStoreSyncState + "\"]/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
+ * Query anchors via module names.
+ *
+ * @param moduleNamesForQuery module names
+ * @return Collection of anchors
*/
- public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
- CpsValidator.validateNameCharacters(cmHandleId);
- return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
- }
+ Collection<Anchor> queryAnchors(Collection<String> moduleNamesForQuery);
/**
- * Method to return module definitions by cmHandleId.
+ * Method to get all anchors.
*
- * @param cmHandleId cm handle ID
- * @return a collection of module definitions (moduleName, revision and yang resource content)
+ * @return Collection of anchors
*/
- public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
- return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
- }
+ Collection<Anchor> getAnchors();
- 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);
+}