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=e1ff400c826ea097fd7bc3af5dc38c072254154e;hb=c7da68167b61674566e165955cbd9f518b32d02e;hp=a37b27199c89ef97a9ef450e9512924db160e910;hpb=fc7a5d30953cfedd7e2ea2f969adab03716de6df;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 a37b27199..e1ff400c8 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 @@ -39,6 +39,7 @@ import java.text.MessageFormat; import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Collection; +import java.util.EnumMap; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -48,6 +49,7 @@ import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsDataService; +import org.onap.cps.ncmp.api.NcmpResponseStatus; import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler; @@ -72,7 +74,6 @@ import org.onap.cps.ncmp.api.models.DataOperationRequest; import org.onap.cps.ncmp.api.models.DmiPluginRegistration; import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; -import org.onap.cps.ncmp.api.models.UpgradedCmHandles; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.onap.cps.spi.exceptions.CpsException; @@ -99,7 +100,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler; private final CpsDataService cpsDataService; private final IMap moduleSyncStartedOnCmHandles; - private final Map trustLevelPerDmiPlugin; + private final Map trustLevelPerCmHandle; @Override public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule( @@ -113,6 +114,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) { + populateTrustLevelPerCmHandleCache(dmiPluginRegistration); dmiPluginRegistrationResponse.setCreatedCmHandles( parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration)); } @@ -126,9 +128,6 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService dmiPluginRegistrationResponse.setUpgradedCmHandles( parseAndProcessUpgradedCmHandlesInRegistration(dmiPluginRegistration)); } - - setTrustLevelPerDmiPlugin(dmiPluginRegistration); - return dmiPluginRegistrationResponse; } @@ -218,26 +217,29 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService * based on the data sync enabled boolean for the cm handle id provided. * * @param cmHandleId cm handle id - * @param dataSyncEnabled data sync enabled flag + * @param dataSyncEnabledTargetValue data sync enabled flag */ @Override - public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) { + public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) { final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId); - if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) { - log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled); - } else if (compositeState.getCmHandleState() != CmHandleState.READY) { - throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: " - + compositeState.getCmHandleState()); - } else { + if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) { + log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue); + return; + } + if (CmHandleState.READY.equals(compositeState.getCmHandleState())) { final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores() .getOperationalDataStore().getDataStoreSyncState(); - if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) { + if (Boolean.FALSE.equals(dataSyncEnabledTargetValue) + && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) { + // TODO : This is hard-coded for onap dmi that need to be addressed cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, "/netconf-state", OffsetDateTime.now()); } - CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState); - inventoryPersistence.saveCmHandleState(cmHandleId, - compositeState); + CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState); + inventoryPersistence.saveCmHandleState(cmHandleId, compositeState); + } else { + throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: " + + compositeState.getCmHandleState()); } } @@ -363,37 +365,53 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService protected List parseAndProcessUpgradedCmHandlesInRegistration( final DmiPluginRegistration dmiPluginRegistration) { - final UpgradedCmHandles upgradedCmHandles = dmiPluginRegistration.getUpgradedCmHandles(); - final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag(); - final Map cmHandleStatePerCmHandle = - new HashMap<>(upgradedCmHandles.getCmHandles().size()); - final Collection notReadyCmHandles = new ArrayList<>(upgradedCmHandles.getCmHandles().size()); - final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle(); - final String formattedModuleSetTag = MessageFormat.format("new moduleSetTag: {0}", moduleSetTag); - - upgradedCmHandles.getCmHandles().forEach(cmHandleId -> { - if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) { - ncmpServiceCmHandle.setCmHandleId(cmHandleId); - ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder() - .withCmHandleState(CmHandleState.READY) - .withLockReason(MODULE_UPGRADE, formattedModuleSetTag).build()); - final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle( - dmiPluginRegistration.getDmiPlugin(), - dmiPluginRegistration.getDmiDataPlugin(), - dmiPluginRegistration.getDmiModelPlugin(), - ncmpServiceCmHandle, - moduleSetTag); - cmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED); - } else { - notReadyCmHandles.add(cmHandleId); + final List upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles(); + + final Map acceptedCmHandleStatePerCmHandle + = new HashMap<>(upgradedCmHandleIds.size()); + + final Map> failedCmHandlesPerResponseStatus + = new EnumMap<>(NcmpResponseStatus.class); + final List nonExistingCmHandleIds = new ArrayList<>(); + final List nonReadyCmHandleIds = new ArrayList<>(); + final List invalidCmHandleIds = new ArrayList<>(); + + upgradedCmHandleIds.forEach(cmHandleId -> { + try { + if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) { + final YangModelCmHandle yangModelCmHandleWithUpgradeDetails + = createYangModelCmHandle(dmiPluginRegistration, cmHandleId); + acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED); + } else { + nonReadyCmHandleIds.add(cmHandleId); + } + } catch (final DataNodeNotFoundException dataNodeNotFoundException) { + log.error("Unable to find data node for cm handle id : {} , caused by : {}", + cmHandleId, dataNodeNotFoundException.getMessage()); + nonExistingCmHandleIds.add(cmHandleId); + } catch (final DataValidationException dataValidationException) { + log.error("Unable to upgrade cm handle id: {}, caused by : {}", + cmHandleId, dataValidationException.getMessage()); + invalidCmHandleIds.add(cmHandleId); } }); + failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds); + failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds); + failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds); + return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus); + } - final List cmHandleRegistrationResponses - = upgradeCmHandles(cmHandleStatePerCmHandle); - cmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse.createFailureResponses(notReadyCmHandles, - CM_HANDLES_NOT_READY)); - return cmHandleRegistrationResponses; + private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration, + final String cmHandleId) { + final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle(); + ncmpServiceCmHandle.setCmHandleId(cmHandleId); + final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag(); + final String lockReasonWithModuleSetTag = MessageFormat.format("ModuleSetTag: {0}", moduleSetTag); + ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY) + .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build()); + return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(), + dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(), + ncmpServiceCmHandle, moduleSetTag); } private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) { @@ -455,13 +473,23 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds); } catch (final AlreadyDefinedException alreadyDefinedException) { return CmHandleRegistrationResponse.createFailureResponses( - alreadyDefinedException.getAlreadyDefinedObjectNames(), - CM_HANDLE_ALREADY_EXIST); + alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST); } catch (final Exception exception) { return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception); } } + private List mergeAllCmHandleResponses( + final Map acceptedCmHandleStatePerCmHandle, + final Map> failedCmHandlesPerResponseStatus) { + final List cmHandleUpgradeResponses + = upgradeCmHandles(acceptedCmHandleStatePerCmHandle); + failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) -> + cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse + .createFailureResponse(cmHandleId, ncmpResponseStatus)))); + return cmHandleUpgradeResponses; + } + private List upgradeCmHandles(final Map cmHandleStatePerCmHandle) { final List cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle); @@ -469,8 +497,9 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService try { lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle); return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds); - } catch (final Exception exception) { - return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception); + } catch (final Exception e) { + log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage()); + return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e); } } @@ -478,11 +507,17 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList(); } - private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) { - if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) { - trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE); - } else { - trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE); + private void populateTrustLevelPerCmHandleCache(final DmiPluginRegistration dmiPluginRegistration) { + for (final NcmpServiceCmHandle cmHandle: dmiPluginRegistration.getCreatedCmHandles()) { + if (cmHandle.getRegistrationTrustLevel() == null) { + if (trustLevelPerCmHandle.containsKey(cmHandle.getCmHandleId())) { + log.warn("CmHandle : {}, Already exists, Initial trustLevel ignored.", cmHandle.getCmHandleId()); + } else { + trustLevelPerCmHandle.put(cmHandle.getCmHandleId(), TrustLevel.COMPLETE); + } + } else { + trustLevelPerCmHandle.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel()); + } } }