Merge "Introduce Hazelcast for alternateId-cmHandle relation pt. 2 - error collection"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServicePropertyHandler.java
index 1520932..0520f45 100644 (file)
@@ -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;
@@ -43,9 +44,9 @@ import java.util.regex.Matcher;
 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;
@@ -67,6 +68,7 @@ public class NetworkCmProxyDataServicePropertyHandler {
     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.
@@ -80,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(
@@ -105,19 +113,15 @@ public class NetworkCmProxyDataServicePropertyHandler {
     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;
             }
         }
     }