X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-ncmp-service%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Fncmp%2Fapi%2Fimpl%2FNetworkCmProxyDataServiceImpl.java;h=446e45b2b88f57484a2ceb42d1798c6bbdb97a87;hb=d042fcdae75be100f840b6313e078eab278d87ef;hp=0ea6f396458860ddc7391040cfb85efb87d252b7;hpb=ecd7f5e418c17d8f0f3f12af4fd0a543084e8254;p=cps.git diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index 0ea6f3964..446e45b2b 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 highstreet technologies GmbH - * Modifications Copyright (C) 2021 Nordix Foundation + * Modifications Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech * Modifications Copyright (C) 2021 Bell Canada * ================================================================================ @@ -23,24 +23,27 @@ package org.onap.cps.ncmp.api.impl; +import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME; +import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR; +import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_PARENT; +import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME; +import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP; import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum; import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.time.OffsetDateTime; -import java.util.ArrayList; 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; import org.onap.cps.api.CpsDataService; import org.onap.cps.api.CpsModuleService; -import org.onap.cps.api.CpsQueryService; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; -import org.onap.cps.ncmp.api.impl.exception.NcmpException; +import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException; import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations; import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations; import org.onap.cps.ncmp.api.impl.operations.DmiOperations; @@ -48,122 +51,48 @@ import org.onap.cps.ncmp.api.models.CmHandle; import org.onap.cps.ncmp.api.models.DmiPluginRegistration; import org.onap.cps.ncmp.api.models.PersistenceCmHandle; import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList; -import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.exceptions.DataNodeNotFoundException; import org.onap.cps.spi.exceptions.DataValidationException; -import org.onap.cps.spi.model.DataNode; import org.onap.cps.spi.model.ModuleReference; +import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; -import org.springframework.util.StringUtils; @Slf4j @Service +@RequiredArgsConstructor public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService { - private static final String NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME = "NFP-Operational"; + private final CpsDataService cpsDataService; - private static final String NCMP_DATASPACE_NAME = "NCMP-Admin"; + private final JsonObjectMapper jsonObjectMapper; - private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry"; + private final DmiDataOperations dmiDataOperations; - private static final OffsetDateTime NO_TIMESTAMP = null; + private final DmiModelOperations dmiModelOperations; - private CpsDataService cpsDataService; + private final CpsModuleService cpsModuleService; - private ObjectMapper objectMapper; + private final CpsAdminService cpsAdminService; - private CpsQueryService cpsQueryService; - - private DmiDataOperations dmiDataOperations; - - private DmiModelOperations dmiModelOperations; - - private CpsModuleService cpsModuleService; - - private CpsAdminService cpsAdminService; - - /** - * Constructor Injection for Dependencies. - * @param dmiDataOperations DMI operation - * @param cpsDataService Data Service Interface - * @param cpsQueryService Query Service Interface - * @param objectMapper Object Mapper - */ - public NetworkCmProxyDataServiceImpl(final DmiDataOperations dmiDataOperations, - final DmiModelOperations dmiModelOperations, - final CpsModuleService cpsModuleService, - final CpsDataService cpsDataService, - final CpsQueryService cpsQueryService, - final CpsAdminService cpsAdminService, - final ObjectMapper objectMapper) { - this.dmiDataOperations = dmiDataOperations; - this.dmiModelOperations = dmiModelOperations; - this.cpsModuleService = cpsModuleService; - this.cpsDataService = cpsDataService; - this.cpsQueryService = cpsQueryService; - this.cpsAdminService = cpsAdminService; - this.objectMapper = objectMapper; - } - - @Override - public DataNode getDataNode(final String cmHandle, final String xpath, - final FetchDescendantsOption fetchDescendantsOption) { - return cpsDataService - .getDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandle, xpath, fetchDescendantsOption); - } - - @Override - public Collection queryDataNodes(final String cmHandle, final String cpsPath, - final FetchDescendantsOption fetchDescendantsOption) { - return cpsQueryService - .queryDataNodes(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandle, cpsPath, fetchDescendantsOption); - } - - @Override - public void createDataNode(final String cmHandle, final String parentNodeXpath, final String jsonData) { - if (!StringUtils.hasText(parentNodeXpath) || "/".equals(parentNodeXpath)) { - cpsDataService.saveData(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandle, jsonData, NO_TIMESTAMP); - } else { - cpsDataService - .saveData(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP); - } - } - - @Override - public void addListNodeElements(final String cmHandle, final String parentNodeXpath, final String jsonData) { - cpsDataService.saveListElements(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandle, parentNodeXpath, jsonData, - NO_TIMESTAMP); - } - - @Override - public void updateNodeLeaves(final String cmHandle, final String parentNodeXpath, final String jsonData) { - cpsDataService - .updateNodeLeaves(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandle, parentNodeXpath, jsonData, - NO_TIMESTAMP); - } - - @Override - public void replaceNodeTree(final String cmHandle, final String parentNodeXpath, final String jsonData) { - cpsDataService.replaceNodeTree(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandle, parentNodeXpath, jsonData, - NO_TIMESTAMP); - } + private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler; @Override public void updateDmiRegistrationAndSyncModule(final DmiPluginRegistration dmiPluginRegistration) { dmiPluginRegistration.validateDmiPluginRegistration(); try { - if (dmiPluginRegistration.getCreatedCmHandles() != null) { + if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) { parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration); } - if (dmiPluginRegistration.getUpdatedCmHandles() != null) { + if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) { parseAndUpdateCmHandlesInDmiRegistration(dmiPluginRegistration); } - if (dmiPluginRegistration.getRemovedCmHandles() != null) { - parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration); - } - } catch (final JsonProcessingException e) { - handleJsonProcessingException(dmiPluginRegistration, e); + parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration); + } catch (final JsonProcessingException | DataNodeNotFoundException e) { + final String errorMessage = String.format( + "Error occurred while processing the CM-handle registration request, caused by : [%s]", + e.getMessage()); + throw new DataValidationException(errorMessage, e.getMessage(), e); } } @@ -240,19 +169,14 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService if (responseEntity.getStatusCode().is2xxSuccessful()) { return responseEntity.getBody(); } else { - throw new NcmpException(exceptionMessage, + throw new ServerNcmpException(exceptionMessage, "DMI status code: " + responseEntity.getStatusCodeValue() + ", DMI response body: " + responseEntity.getBody()); } } - private void parseAndUpdateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) - throws JsonProcessingException { - final PersistenceCmHandlesList updatedPersistenceCmHandlesList = - getUpdatedPersistenceCmHandlesList(dmiPluginRegistration, dmiPluginRegistration.getUpdatedCmHandles()); - final String cmHandlesAsJson = objectMapper.writeValueAsString(updatedPersistenceCmHandlesList); - cpsDataService.updateNodeLeavesAndExistingDescendantLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - "/dmi-registry", cmHandlesAsJson, NO_TIMESTAMP); + private void parseAndUpdateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) { + networkCmProxyDataServicePropertyHandler.updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()); } private PersistenceCmHandlesList getUpdatedPersistenceCmHandlesList( @@ -265,19 +189,10 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService updatedCmHandles); } - private static void handleJsonProcessingException(final DmiPluginRegistration dmiPluginRegistration, - final JsonProcessingException e) { - final String message = "Parsing error occurred while processing DMI Plugin Registration" - + dmiPluginRegistration; - log.error(message); - throw new DataValidationException(message, e.getMessage(), e); - } - - private void registerAndSyncNewCmHandles(final PersistenceCmHandlesList persistenceCmHandlesList) - throws JsonProcessingException { - final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList); - cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, "/dmi-registry", - cmHandleJsonData, NO_TIMESTAMP); + private void registerAndSyncNewCmHandles(final PersistenceCmHandlesList persistenceCmHandlesList) { + final String cmHandleJsonData = jsonObjectMapper.asJsonString(persistenceCmHandlesList); + cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT, + cmHandleJsonData, NO_TIMESTAMP); for (final PersistenceCmHandle persistenceCmHandle : persistenceCmHandlesList.getPersistenceCmHandles()) { syncModulesAndCreateAnchor(persistenceCmHandle); @@ -311,45 +226,31 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } private void syncAndCreateSchemaSet(final PersistenceCmHandle persistenceCmHandle) { - final List moduleReferencesFromCmHandle = + final Collection moduleReferencesFromCmHandle = dmiModelOperations.getModuleReferences(persistenceCmHandle); - final List existingModuleReferences = new ArrayList<>(); - final List unknownModuleReferences = new ArrayList<>(); - prepareModuleSubsets(moduleReferencesFromCmHandle, existingModuleReferences, unknownModuleReferences); - final Map newYangResourcesModuleNameToContentMap; - if (unknownModuleReferences.isEmpty()) { - newYangResourcesModuleNameToContentMap = new HashMap<>(); + final Collection identifiedNewModuleReferencesFromCmHandle = cpsModuleService + .identifyNewModuleReferences(moduleReferencesFromCmHandle); + + final Collection existingModuleReferencesFromCmHandle = + moduleReferencesFromCmHandle.stream().filter(moduleReferenceFromCmHandle -> + !identifiedNewModuleReferencesFromCmHandle.contains(moduleReferenceFromCmHandle) + ).collect(Collectors.toList()); + + final Map newModuleNameToContentMap; + if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) { + newModuleNameToContentMap = new HashMap<>(); } else { - newYangResourcesModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(persistenceCmHandle, - unknownModuleReferences); + newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(persistenceCmHandle, + identifiedNewModuleReferencesFromCmHandle); } cpsModuleService .createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, persistenceCmHandle.getId(), - newYangResourcesModuleNameToContentMap, existingModuleReferences); - } - - private void prepareModuleSubsets(final List moduleReferencesFromCmHandle, - final List existingModuleReferences, - final List unknownModuleReferences) { - - final Collection knownModuleReferencesInCps = - cpsModuleService.getYangResourceModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME); - - for (final ModuleReference moduleReferenceFromDmiForCmHandle : moduleReferencesFromCmHandle) { - if (knownModuleReferencesInCps.contains(moduleReferenceFromDmiForCmHandle)) { - existingModuleReferences.add(moduleReferenceFromDmiForCmHandle); - } else { - unknownModuleReferences.add(moduleReferenceFromDmiForCmHandle); - } - } + newModuleNameToContentMap, existingModuleReferencesFromCmHandle); } private void createAnchor(final PersistenceCmHandle persistenceCmHandle) { cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, persistenceCmHandle.getId(), persistenceCmHandle.getId()); } - - - }