diff options
Diffstat (limited to 'cps-ncmp-service/src/main')
4 files changed, 64 insertions, 20 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NcmpResponseStatus.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NcmpResponseStatus.java index b9c834c559..e7293b2fd9 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NcmpResponseStatus.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NcmpResponseStatus.java @@ -36,7 +36,8 @@ public enum NcmpResponseStatus { SUBSCRIPTION_PENDING("106", "subscription pending for all cm handles"), UNKNOWN_ERROR("108", "Unknown error"), CM_HANDLE_ALREADY_EXIST("109", "cm-handle already exists"), - CM_HANDLE_INVALID_ID("110", "cm-handle has an invalid character(s) in id"); + CM_HANDLE_INVALID_ID("110", "cm-handle has an invalid character(s) in id"), + ALTERNATE_ID_ALREADY_ASSOCIATED("111", "cannot re-assign alternate id"); private final String code; private final String message; 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 05b83b98e4..ad1c5cdfb9 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 @@ -24,6 +24,7 @@ package org.onap.cps.ncmp.api.impl; +import static org.onap.cps.ncmp.api.NcmpResponseStatus.ALTERNATE_ID_ALREADY_ASSOCIATED; import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND; import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_READY; import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST; @@ -500,16 +501,35 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles, final Map<String, TrustLevel> initialTrustLevelPerCmHandleId) { - final Set<String> cmHandleIds = initialTrustLevelPerCmHandleId.keySet(); + final List<CmHandleRegistrationResponse> failureResponses = new ArrayList<>(); + final List<YangModelCmHandle> acceptedYangModelCmHandles = new ArrayList<>(yangModelCmHandles.size()); + final Set<String> acceptedCmHandleIds = new HashSet<>(yangModelCmHandles.size()); + for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandles) { + if (cmHandleIdMapper.isDuplicateId(yangModelCmHandle.getId(), yangModelCmHandle.getAlternateId())) { + initialTrustLevelPerCmHandleId.remove(yangModelCmHandle.getId()); + failureResponses.add(CmHandleRegistrationResponse.createFailureResponse( + yangModelCmHandle.getId(), ALTERNATE_ID_ALREADY_ASSOCIATED)); + } else { + acceptedCmHandleIds.add(yangModelCmHandle.getId()); + acceptedYangModelCmHandles.add(yangModelCmHandle); + } + } try { - lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandles); + lcmEventsCmHandleStateHandler.initiateStateAdvised(acceptedYangModelCmHandles); trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId); - return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds); + final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = CmHandleRegistrationResponse + .createSuccessResponses(acceptedCmHandleIds); + cmHandleRegistrationResponses.addAll(failureResponses); + return cmHandleRegistrationResponses; } catch (final AlreadyDefinedException alreadyDefinedException) { - return CmHandleRegistrationResponse.createFailureResponses( + final List<CmHandleRegistrationResponse> alreadyDefinedResponses = CmHandleRegistrationResponse + .createFailureResponses( alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST); + failureResponses.addAll(alreadyDefinedResponses); + return failureResponses; } catch (final Exception exception) { - return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception); + return CmHandleRegistrationResponse + .createFailureResponses(initialTrustLevelPerCmHandleId.keySet(), exception); } } @@ -556,5 +576,4 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } } } - } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServicePropertyHandler.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServicePropertyHandler.java index 13b3fcafb1..0520f456a5 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServicePropertyHandler.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServicePropertyHandler.java @@ -22,6 +22,7 @@ package org.onap.cps.ncmp.api.impl; +import static org.onap.cps.ncmp.api.NcmpResponseStatus.ALTERNATE_ID_ALREADY_ASSOCIATED; import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND; import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID; import static org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServicePropertyHandler.PropertyType.DMI_PROPERTY; @@ -81,11 +82,17 @@ public class NetworkCmProxyDataServicePropertyHandler { for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) { final String cmHandleId = ncmpServiceCmHandle.getCmHandleId(); try { - final DataNode existingCmHandleDataNode = inventoryPersistence.getCmHandleDataNode(cmHandleId) - .iterator().next(); - updateAlternateId(existingCmHandleDataNode, ncmpServiceCmHandle); - processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle); - cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)); + if (cmHandleIdMapper.isDuplicateId(ncmpServiceCmHandle.getCmHandleId(), + ncmpServiceCmHandle.getAlternateId())) { + cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(cmHandleId, + ALTERNATE_ID_ALREADY_ASSOCIATED)); + } else { + final DataNode existingCmHandleDataNode = inventoryPersistence.getCmHandleDataNode(cmHandleId) + .iterator().next(); + updateAlternateId(existingCmHandleDataNode, ncmpServiceCmHandle); + processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle); + cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)); + } } catch (final DataNodeNotFoundException e) { log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}", cmHandleId, e.getMessage()); cmHandleRegistrationResponses.add( diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmHandleIdMapper.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmHandleIdMapper.java index a88adbd110..40c620f508 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmHandleIdMapper.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmHandleIdMapper.java @@ -55,15 +55,10 @@ public class CmHandleIdMapper { private boolean addMappingWithValidation(final String cmHandleId, final String alternateId) { - if (alternateIdPerCmHandleId.containsKey(cmHandleId)) { - final String originalAlternateId = alternateIdPerCmHandleId.get(cmHandleId); - if (!originalAlternateId.equals(alternateId)) { - log.warn("Alternate id update ignored, cannot update cm handle {}, already has an alternate id of {}", - cmHandleId, originalAlternateId); - } + if (StringUtils.isBlank(alternateId)) { return false; } - if (StringUtils.isBlank(alternateId)) { + if (isDuplicateId(cmHandleId, alternateId)) { return false; } alternateIdPerCmHandleId.put(cmHandleId, alternateId); @@ -82,10 +77,32 @@ public class CmHandleIdMapper { } } + /** + * Check if alternate id is already used. + * + * @param cmHandleId the cmHandle id. + * @param alternateId the alternate id. + * @return boolean true if the alternate id is already used. + */ + public boolean isDuplicateId(final String cmHandleId, final String alternateId) { + if (StringUtils.isBlank(alternateId)) { + return false; + } + if (cmHandleIdPerAlternateId.get(alternateId) != null) { + log.warn("The given alternate id was added to the cache already: {}", alternateId); + return true; + } + if (alternateIdPerCmHandleId.get(cmHandleId) != null) { + log.warn("The given cmhandle id was added to the cache already: {}", cmHandleId); + return true; + } + return false; + } + private void initializeCache() { if (!cacheIsInitialized) { networkCmProxyCmHandleQueryService.getAllCmHandles().forEach(cmHandle -> - addMappingWithValidation(cmHandle.getCmHandleId(), cmHandle.getAlternateId()) + addMappingWithValidation(cmHandle.getCmHandleId(), cmHandle.getAlternateId()) ); log.info("Alternate ID cache initialized from DB with {} cm handle/alternate id pairs ", alternateIdPerCmHandleId.size()); |