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;
import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
import org.onap.cps.api.CpsDataService;
import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
+import org.onap.cps.ncmp.api.impl.utils.CmHandleIdMapper;
import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
private final InventoryPersistence inventoryPersistence;
private final CpsDataService cpsDataService;
private final JsonObjectMapper jsonObjectMapper;
+ private final CmHandleIdMapper cmHandleIdMapper;
/**
* Iterates over incoming ncmpServiceCmHandles and update the dataNodes based on the updated attributes.
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(
private void updateAlternateId(final DataNode existingCmHandleDataNode,
final NcmpServiceCmHandle ncmpServiceCmHandle) {
final String newAlternateId = ncmpServiceCmHandle.getAlternateId();
- if (!StringUtils.isEmpty(newAlternateId)) {
- final String existingAlternateId = (String) existingCmHandleDataNode.getLeaves().get("alternate-id");
- if (StringUtils.isEmpty(existingAlternateId)) {
+ if (cmHandleIdMapper.addMapping(ncmpServiceCmHandle.getCmHandleId(), newAlternateId)) {
+ try {
final YangModelCmHandle yangModelCmHandle =
YangDataConverter.convertCmHandleToYangModel(existingCmHandleDataNode,
ncmpServiceCmHandle.getCmHandleId());
setAndUpdateAlternateId(yangModelCmHandle, newAlternateId);
- } else {
- if (!newAlternateId.equals(existingAlternateId)) {
- log.warn("Unable to update alternateId for cmHandle {}. "
- + "Value for alternateId has been set previously.",
- ncmpServiceCmHandle.getCmHandleId());
- }
+ } catch (final Exception e) {
+ cmHandleIdMapper.removeMapping(ncmpServiceCmHandle.getCmHandleId());
+ throw e;
}
}
}