Fix bugs in delete 02/120302/1
authorDan Timoney <dtimoney@att.com>
Wed, 7 Apr 2021 17:24:53 +0000 (13:24 -0400)
committerDan Timoney <dtimoney@att.com>
Wed, 7 Apr 2021 17:24:53 +0000 (13:24 -0400)
Fixed bugs in delete of items from existing services

Change-Id: Iaeeb8b6622276b5183f5f386763526cd15b0becb
Issue-ID: SDNC-1516
Signed-off-by: Dan Timoney <dtimoney@att.com>
ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/OperationsApiController.java
ms/generic-resource-api/src/main/java/org/onap/sdnc/apps/ms/gra/controllers/ServiceDataHelper.java

index cbf571d..6b0a463 100644 (file)
@@ -838,7 +838,7 @@ public class OperationsApiController implements OperationsApi {
         try {
             serviceDataHelper.saveService(configService, ctxSvcDataJson);
         } catch (JsonProcessingException e) {
-            log.error("exiting {} due to  error saving service data", svcOperation);
+            log.error("exiting {} due to  error saving service data", svcOperation, e);
             resp.setResponseCode("500");
             resp.setResponseMessage("internal error");
             resp.setAckFinalIndicator("Y");
index 1fbf02b..f04a3ac 100644 (file)
@@ -298,7 +298,7 @@ public class ServiceDataHelper {
         String svcInstanceId = configService.getSvcInstanceId();
         String svcDataAsString = configService.getSvcData();
 
-        if (svcDataAsString != null) {
+        if ((svcDataAsString != null) && !svcDataAsString.isEmpty()) {
             svcData = objectMapper.readValue(svcDataAsString, GenericResourceApiServicedataServiceData.class);
         } else {
             svcData = new GenericResourceApiServicedataServiceData();
@@ -371,18 +371,17 @@ public class ServiceDataHelper {
                 }
             }
 
-            // Remove any vf Modules for this vnf id not in this list
-            for (ConfigVfModules vfModuleToRemove : vfModuleMap.values()) {
-                if (transaction != null) {
-                    transaction.remove(vfModuleToRemove);
-                } else {
-                    configVfModulesRepository.delete(vfModuleToRemove);
-                }
-            }
-
             // Clear vf modules from vnfData before saving to avoid duplication
             vnfData.setVfModules(null);
         }
+        // Remove any vf Modules for this vnf id not in this list
+        for (ConfigVfModules vfModuleToRemove : vfModuleMap.values()) {
+            if (transaction != null) {
+                transaction.remove(vfModuleToRemove);
+            } else {
+                configVfModulesRepository.delete(vfModuleToRemove);
+            }
+        }
 
         // Save Vnf itself
         List<ConfigVnfs> configVnfs = configVnfsRepository.findBySvcInstanceIdAndVnfId(svcInstanceId, vnf.getVnfId());
@@ -425,7 +424,8 @@ public class ServiceDataHelper {
 
 
     public void saveService(ConfigServices configService, String svcDataAsString, ServiceDataTransaction transaction) throws JsonMappingException, JsonProcessingException {
-        if (svcDataAsString == null) {
+        if ((svcDataAsString == null) || (svcDataAsString.length() == 0)) {
+            configService.setSvcData(null);
             configServicesRepository.save(configService);
         } else {
             saveService(configService, objectMapper.readValue(svcDataAsString, GenericResourceApiServicedataServiceData.class), transaction);
@@ -438,36 +438,78 @@ public class ServiceDataHelper {
 
     public void saveService(ConfigServices configService, GenericResourceApiServicedataServiceData svcData, ServiceDataTransaction transaction) throws JsonProcessingException {
         
-        if (svcData != null) {
+        if (svcData == null) {
+            configService.setSvcData(null);
+        } else {
             String svcInstanceId = configService.getSvcInstanceId();
 
+            // Make a list of current networks for this service
+            HashMap<String, ConfigNetworks> networkMap = new HashMap<>();
+            List<ConfigNetworks> configNetworkList = configNetworksRepository.findBySvcInstanceId(svcInstanceId);
+            if ((configNetworkList != null) && !configNetworkList.isEmpty()) {
+                for (ConfigNetworks configNetworkItem : configNetworkList) {
+                    networkMap.put(configNetworkItem.getNetworkId(), configNetworkItem);
+                }
+            }
             // Save networks
             GenericResourceApiServicedataServicedataNetworks networks = svcData.getNetworks();
             if (networks != null) {
                 List<GenericResourceApiServicedataServicedataNetworksNetwork> networkList = networks.getNetwork();
                 if ((networkList != null) && !networkList.isEmpty()) {
                     for (GenericResourceApiServicedataServicedataNetworksNetwork networkItem : networkList) {
+                        if (networkMap.containsKey(networkItem.getNetworkId())) {
+                            networkMap.remove(networkItem.getNetworkId());
+                        }
                         saveNetwork(svcInstanceId, networkItem, transaction);
                     }
                 }
             }
+
+            // Remove networks removed from service object
+            for (ConfigNetworks networkToRemove : networkMap.values()) {
+                if (transaction != null) {
+                    transaction.remove(networkToRemove);
+                } else {
+                    configNetworksRepository.delete(networkToRemove);
+                }
+            }
+
             svcData.setNetworks(null);
+            
+            // Remove any networks no longer associated with this svcInstanceId
+            HashMap<String, ConfigVnfs> vnfMap = new HashMap<>();
+            List<ConfigVnfs> configVnfList = configVnfsRepository.findBySvcInstanceId(svcInstanceId);
+            if ((configVnfList != null) && !configVnfList.isEmpty()) {
+                for (ConfigVnfs configVnfItem : configVnfList) {
+                    vnfMap.put(configVnfItem.getVnfId(), configVnfItem);
+                }
+            }            
 
             // Save vnfs / vfModules
+
+            // Save current vnfs associated with this service
             GenericResourceApiServicedataServicedataVnfs vnfs = svcData.getVnfs();
             if (vnfs != null) {
                 List<GenericResourceApiServicedataServicedataVnfsVnf> vnfList = vnfs.getVnf();
                 if ((vnfList != null) && !vnfList.isEmpty()) {
                     for (GenericResourceApiServicedataServicedataVnfsVnf vnfItem : vnfList) {
+                        if (vnfMap.containsKey(vnfItem.getVnfId())) {
+                            vnfMap.remove(vnfItem.getVnfId());
+                        }
                         saveVnf(svcInstanceId, vnfItem, transaction);
                     }
                 }
             }
+            for (ConfigVnfs vnfToRemove : vnfMap.values()) {
+                if (transaction != null) {
+                    transaction.remove(vnfToRemove);
+                } else {
+                    configVnfsRepository.delete(vnfToRemove);
+                }
+            }
             svcData.setVnfs(null);
 
             configService.setSvcData(objectMapper.writeValueAsString(svcData));
-        } else {
-
         }
         if (transaction != null) {
             transaction.save(configService);