Refactored Delete SchemaSet functionality
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsModulePersistenceServiceImpl.java
index 1c7828f..3e39a05 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Nordix Foundation
- *  Modifications Copyright (C) 2020-2021 Bell Canada.
+ *  Modifications Copyright (C) 2020-2022 Bell Canada.
  *  Modifications Copyright (C) 2021 Pantheon.tech
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -44,17 +44,14 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.hibernate.exception.ConstraintViolationException;
-import org.onap.cps.spi.CascadeDeleteAllowed;
 import org.onap.cps.spi.CpsAdminPersistenceService;
 import org.onap.cps.spi.CpsModulePersistenceService;
-import org.onap.cps.spi.entities.AnchorEntity;
 import org.onap.cps.spi.entities.SchemaSetEntity;
 import org.onap.cps.spi.entities.YangResourceEntity;
 import org.onap.cps.spi.entities.YangResourceModuleReference;
 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException;
 import org.onap.cps.spi.exceptions.ModelValidationException;
-import org.onap.cps.spi.exceptions.SchemaSetInUseException;
 import org.onap.cps.spi.model.ModuleReference;
 import org.onap.cps.spi.repository.AnchorRepository;
 import org.onap.cps.spi.repository.DataspaceRepository;
@@ -116,11 +113,21 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
     }
 
     @Override
-    public List<ModuleReference> getAllYangResourcesModuleReferences() {
-        final List<YangResourceModuleReference> yangResourceModuleReferenceList =
-                yangResourceRepository.findAllModuleNameAndRevision();
+    public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
+        final Set<YangResourceModuleReference> yangResourceModuleReferenceList =
+            yangResourceRepository.findAllModuleReferences(dataspaceName);
         return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference)
-                .collect(Collectors.toList());
+            .collect(Collectors.toList());
+    }
+
+    @Override
+    public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName,
+        final String anchorName) {
+        final Set<YangResourceModuleReference> yangResourceModuleReferenceList =
+            yangResourceRepository
+                .findAllModuleReferences(dataspaceName, anchorName);
+        return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference)
+            .collect(Collectors.toList());
     }
 
     @Override
@@ -148,35 +155,30 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
     @Transactional
     public void storeSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
                                           final Map<String, String> newYangResourcesModuleNameToContentMap,
-                                          final List<ModuleReference> moduleReferenceList) {
+                                          final List<ModuleReference> moduleReferences) {
         storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesModuleNameToContentMap);
         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
         final var schemaSetEntity =
                 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
         final List<Long> listOfYangResourceIds = new ArrayList<>();
-        moduleReferenceList.forEach(moduleReference ->
+        moduleReferences.forEach(moduleReference ->
                 listOfYangResourceIds.add(yangResourceRepository.getIdByModuleNameAndRevision(
-                        moduleReference.getName(), moduleReference.getRevision())));
+                        moduleReference.getModuleName(), moduleReference.getRevision())));
         yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), listOfYangResourceIds);
     }
 
     @Override
     @Transactional
-    public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
-        final CascadeDeleteAllowed cascadeDeleteAllowed) {
+    public void deleteSchemaSet(final String dataspaceName, final String schemaSetName) {
         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
         final var schemaSetEntity =
             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
-
-        final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllBySchemaSet(schemaSetEntity);
-        if (!anchorEntities.isEmpty()) {
-            if (cascadeDeleteAllowed != CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) {
-                throw new SchemaSetInUseException(dataspaceName, schemaSetName);
-            }
-            fragmentRepository.deleteByAnchorIn(anchorEntities);
-            anchorRepository.deleteAll(anchorEntities);
-        }
         schemaSetRepository.delete(schemaSetEntity);
+    }
+
+    @Override
+    @Transactional
+    public void deleteUnusedYangResourceModules() {
         yangResourceRepository.deleteOrphans();
     }
 
@@ -267,6 +269,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
     /**
      * Convert the specified data integrity violation exception into a CPS duplicated Yang resource exception
      * if the cause of the error is a yang checksum database constraint violation.
+     *
      * @param originalException the original db exception.
      * @param yangResourceEntities the collection of Yang resources involved in the db failure.
      * @return an optional converted CPS duplicated Yang resource exception. The optional is empty if the original
@@ -297,6 +300,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
 
     /**
      * Get the name of the yang resource having the specified checksum.
+     *
      * @param checksum the checksum. Null is supported.
      * @param yangResourceEntities the list of yang resources to search among.
      * @return the name found or null if none.
@@ -313,6 +317,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
 
     /**
      * Get the checksum that caused the constraint violation exception.
+     *
      * @param exception the exception having the checksum in error.
      * @return the checksum in error or null if not found.
      */
@@ -325,10 +330,11 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
         return checksum;
     }
 
-    private static ModuleReference toModuleReference(final YangResourceModuleReference yangResourceModuleReference) {
+    private static ModuleReference toModuleReference(
+        final YangResourceModuleReference yangResourceModuleReference) {
         return ModuleReference.builder()
-                .name(yangResourceModuleReference.getModuleName())
-                .revision(yangResourceModuleReference.getRevision())
-                .build();
+            .moduleName(yangResourceModuleReference.getModuleName())
+            .revision(yangResourceModuleReference.getRevision())
+            .build();
     }
 }