aboutsummaryrefslogtreecommitdiffstats
path: root/cps-ri/src
diff options
context:
space:
mode:
authorRuslan Kashapov <ruslan.kashapov@pantheon.tech>2021-01-11 15:34:10 +0200
committerRuslan Kashapov <ruslan.kashapov@pantheon.tech>2021-01-18 17:44:56 +0200
commit6f25d3a2ae84aff3f22b50d10592c5321a7c98ce (patch)
tree7edb777884552ed4e8f28e8f0d66a5cbd22ea0ea /cps-ri/src
parentaee547cfbeb8780ed21d2ee902110a888aee3b56 (diff)
Delete schema set - persistence layer
Issue-ID: CPS-121 Change-Id: I6fc8343969971b76d7f78ad202dd8ec1058c03fb Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-ri/src')
-rwxr-xr-xcps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java31
-rwxr-xr-xcps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java3
-rwxr-xr-xcps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java10
-rw-r--r--cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java6
-rwxr-xr-xcps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java101
-rw-r--r--cps-ri/src/test/resources/data/schemaset.sql19
6 files changed, 158 insertions, 12 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
index 0a870bac14..cac41ca9d5 100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
@@ -28,14 +28,19 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
+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.DataspaceEntity;
import org.onap.cps.spi.entities.SchemaSetEntity;
import org.onap.cps.spi.entities.YangResourceEntity;
import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
+import org.onap.cps.spi.exceptions.SchemaSetInUseException;
import org.onap.cps.spi.model.Anchor;
+import org.onap.cps.spi.repository.AnchorRepository;
import org.onap.cps.spi.repository.DataspaceRepository;
+import org.onap.cps.spi.repository.FragmentRepository;
import org.onap.cps.spi.repository.SchemaSetRepository;
import org.onap.cps.spi.repository.YangResourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
@@ -56,6 +61,12 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
private DataspaceRepository dataspaceRepository;
@Autowired
+ private AnchorRepository anchorRepository;
+
+ @Autowired
+ private FragmentRepository fragmentRepository;
+
+ @Autowired
private CpsAdminPersistenceService cpsAdminPersistenceService;
@Override
@@ -121,4 +132,24 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
final Anchor anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName);
return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName());
}
+
+ @Override
+ @Transactional
+ public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
+ final CascadeDeleteAllowed cascadeDeleteAllowed) {
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
+ final SchemaSetEntity 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);
+ yangResourceRepository.deleteOrphans();
+ }
}
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java
index cc9c2e09c7..0305555456 100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java
@@ -24,6 +24,7 @@ import java.util.Optional;
import javax.validation.constraints.NotNull;
import org.onap.cps.spi.entities.AnchorEntity;
import org.onap.cps.spi.entities.DataspaceEntity;
+import org.onap.cps.spi.entities.SchemaSetEntity;
import org.onap.cps.spi.exceptions.AnchorNotFoundException;
import org.springframework.data.jpa.repository.JpaRepository;
@@ -38,4 +39,6 @@ public interface AnchorRepository extends JpaRepository<AnchorEntity, Integer> {
}
Collection<AnchorEntity> findAllByDataspace(@NotNull DataspaceEntity dataspaceEntity);
+
+ Collection<AnchorEntity> findAllBySchemaSet(@NotNull SchemaSetEntity schemaSetEntity);
} \ No newline at end of file
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
index 28585f8761..4d44943963 100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java
@@ -21,11 +21,21 @@
package org.onap.cps.spi.repository;
+import java.util.Collection;
+import javax.validation.constraints.NotNull;
+import org.onap.cps.spi.entities.AnchorEntity;
import org.onap.cps.spi.entities.FragmentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface FragmentRepository extends JpaRepository<FragmentEntity, Long> {
+ @Modifying
+ @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")
+ void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);
+
} \ No newline at end of file
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java
index 8e57f633f1..25d76890be 100644
--- a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java
@@ -24,6 +24,8 @@ import java.util.Set;
import javax.validation.constraints.NotNull;
import org.onap.cps.spi.entities.YangResourceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
@@ -31,4 +33,8 @@ public interface YangResourceRepository extends JpaRepository<YangResourceEntity
List<YangResourceEntity> findAllByChecksumIn(@NotNull Set<String> checksum);
+ @Modifying
+ @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS "
+ + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true)
+ void deleteOrphans();
}
diff --git a/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java b/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
index 8455e57517..050ee265a5 100755
--- a/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
+++ b/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
@@ -20,7 +20,11 @@
package org.onap.cps.spi.impl;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
+import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
@@ -36,8 +40,13 @@ import org.onap.cps.spi.entities.SchemaSetEntity;
import org.onap.cps.spi.entities.YangResourceEntity;
import org.onap.cps.spi.exceptions.DataspaceNotFoundException;
import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
+import org.onap.cps.spi.exceptions.SchemaSetInUseException;
+import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
+import org.onap.cps.spi.repository.AnchorRepository;
import org.onap.cps.spi.repository.DataspaceRepository;
+import org.onap.cps.spi.repository.FragmentRepository;
import org.onap.cps.spi.repository.SchemaSetRepository;
+import org.onap.cps.spi.repository.YangResourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
@@ -57,6 +66,8 @@ public class CpsModulePersistenceServiceTest {
private static final String DATASPACE_NAME_INVALID = "DATASPACE-X";
private static final String SCHEMA_SET_NAME = "SCHEMA-SET-001";
private static final String SCHEMA_SET_NAME_NEW = "SCHEMA-SET-NEW";
+ private static final String SCHEMA_SET_NAME_NO_ANCHORS = "SCHEMA-SET-100";
+ private static final String SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA = "SCHEMA-SET-101";
private static final String EXISTING_RESOURCE_NAME = "module1@2020-02-02.yang";
private static final String EXISTING_RESOURCE_CONTENT = "CONTENT-001";
@@ -68,6 +79,13 @@ public class CpsModulePersistenceServiceTest {
private static final String NEW_RESOURCE_CHECKSUM = "c94d40a1350eb1c0b1c1949eac84fc59";
private static final Long NEW_RESOURCE_ABSTRACT_ID = 0L;
+ private static final Long SHARED_RESOURCE_ID1 = 3003L;
+ private static final Long SHARED_RESOURCE_ID2 = 3004L;
+ private static final Long ORPHAN_RESOURCE_ID = 3100L;
+ private static final Integer REMOVED_ANCHOR_ID1 = 6001;
+ private static final Integer REMOVED_ANCHOR_ID2 = 6002;
+ private static final Long REMOVED_FRAGMENT_ID = 7001L;
+
@ClassRule
public static DatabaseTestContainer testContainer = DatabaseTestContainer.getInstance();
@@ -78,11 +96,19 @@ public class CpsModulePersistenceServiceTest {
private CpsAdminPersistenceService cpsAdminPersistenceService;
@Autowired
- private DataspaceRepository dataspaceRepository;
+ DataspaceRepository dataspaceRepository;
@Autowired
private SchemaSetRepository schemaSetRepository;
+ @Autowired
+ private YangResourceRepository yangResourceRepository;
+
+ @Autowired
+ private AnchorRepository anchorRepository;
+
+ @Autowired
+ private FragmentRepository fragmentRepository;
@Test(expected = DataspaceNotFoundException.class)
@Sql(CLEAR_DATA)
@@ -92,14 +118,14 @@ public class CpsModulePersistenceServiceTest {
}
@Test(expected = SchemaSetAlreadyDefinedException.class)
- @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+ @Sql({CLEAR_DATA, SET_DATA})
public void testStoreDuplicateSchemaSet() {
cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME,
toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT));
}
@Test
- @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+ @Sql({CLEAR_DATA, SET_DATA})
public void testStoreSchemaSetWithNewYangResource() {
final Map<String, String> yangResourcesNameToContentMap = toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT);
cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
@@ -107,11 +133,11 @@ public class CpsModulePersistenceServiceTest {
assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM);
assertEquals(yangResourcesNameToContentMap,
- cpsModulePersistenceService.getYangSchemaResources(DATASPACE_NAME, SCHEMA_SET_NAME_NEW));
+ cpsModulePersistenceService.getYangSchemaResources(DATASPACE_NAME, SCHEMA_SET_NAME_NEW));
}
@Test
- @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+ @Sql({CLEAR_DATA, SET_DATA})
public void testGetYangResourcesWithAnchorName() {
final Map<String, String> yangResourcesNameToContentMap =
toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT);
@@ -124,7 +150,7 @@ public class CpsModulePersistenceServiceTest {
}
@Test
- @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+ @Sql({CLEAR_DATA, SET_DATA})
public void testStoreSchemaSetWithExistingYangResourceReuse() {
cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
toMap(NEW_RESOURCE_NAME, EXISTING_RESOURCE_CONTENT));
@@ -133,9 +159,9 @@ public class CpsModulePersistenceServiceTest {
}
private void assertSchemaSetPersisted(final String expectedDataspaceName, final String expectedSchemaSetName,
- final Long expectedYangResourceId, final String expectedYangResourceName,
- final String expectedYangResourceContent,
- final String expectedYangResourceChecksum) {
+ final Long expectedYangResourceId, final String expectedYangResourceName,
+ final String expectedYangResourceContent,
+ final String expectedYangResourceChecksum) {
// assert the schema set is persisted
final SchemaSetEntity schemaSetEntity = getSchemaSetFromDatabase(expectedDataspaceName, expectedSchemaSetName);
@@ -159,6 +185,63 @@ public class CpsModulePersistenceServiceTest {
assertEquals(expectedYangResourceChecksum, yangResourceEntity.getChecksum());
}
+ @Test
+ @Sql({CLEAR_DATA, SET_DATA})
+ public void testStrictDeleteSchemaSetNoAnchors() {
+ cpsModulePersistenceService.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS,
+ CASCADE_DELETE_PROHIBITED);
+
+ // validate schema set removed
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME);
+ assertFalse(schemaSetRepository
+ .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent());
+
+ // validate shared resource remain, but orphan one is removed
+ assertTrue(yangResourceRepository.findById(SHARED_RESOURCE_ID1).isPresent());
+ assertFalse(yangResourceRepository.findById(ORPHAN_RESOURCE_ID).isPresent());
+ }
+
+
+ @Test
+ @Sql({CLEAR_DATA, SET_DATA})
+ public void testFullDeleteSchemaSetWithAnchorsAndData() {
+ cpsModulePersistenceService
+ .deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA, CASCADE_DELETE_ALLOWED);
+
+ // validate schema set removed
+ final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME);
+ assertFalse(schemaSetRepository
+ .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA).isPresent());
+
+ // validate shared resources remain
+ assertTrue(yangResourceRepository.findById(SHARED_RESOURCE_ID1).isPresent());
+ assertTrue(yangResourceRepository.findById(SHARED_RESOURCE_ID2).isPresent());
+
+ // validate associated anchors and data are removed
+ assertFalse(anchorRepository.findById(REMOVED_ANCHOR_ID1).isPresent());
+ assertFalse(anchorRepository.findById(REMOVED_ANCHOR_ID2).isPresent());
+ assertFalse(fragmentRepository.findById(REMOVED_FRAGMENT_ID).isPresent());
+ }
+
+ @Test(expected = DataspaceNotFoundException.class)
+ @Sql(CLEAR_DATA)
+ public void testDeleteSchemaSetWithinInvalidDataspace() {
+ cpsModulePersistenceService.deleteSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME, CASCADE_DELETE_ALLOWED);
+ }
+
+ @Test(expected = SchemaSetNotFoundException.class)
+ @Sql({CLEAR_DATA, SET_DATA})
+ public void testDeleteNonExistingSchemaSet() {
+ cpsModulePersistenceService.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, CASCADE_DELETE_ALLOWED);
+ }
+
+ @Test(expected = SchemaSetInUseException.class)
+ @Sql({CLEAR_DATA, SET_DATA})
+ public void testStrictDeleteSchemaSetInUse() {
+ cpsModulePersistenceService
+ .deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA, CASCADE_DELETE_PROHIBITED);
+ }
+
private static Map<String, String> toMap(final String key, final String value) {
return ImmutableMap.<String, String>builder().put(key, value).build();
}
diff --git a/cps-ri/src/test/resources/data/schemaset.sql b/cps-ri/src/test/resources/data/schemaset.sql
index e3fc69b086..0ec1ec3a0d 100644
--- a/cps-ri/src/test/resources/data/schemaset.sql
+++ b/cps-ri/src/test/resources/data/schemaset.sql
@@ -2,13 +2,26 @@ INSERT INTO DATASPACE (ID, NAME) VALUES
(1001, 'DATASPACE-001'), (1002, 'DATASPACE-002');
INSERT INTO SCHEMA_SET (ID, NAME, DATASPACE_ID) VALUES
- (2001, 'SCHEMA-SET-001', 1001), (2002, 'SCHEMA-SET-002', 1001);
+ (2001, 'SCHEMA-SET-001', 1001), (2002, 'SCHEMA-SET-002', 1001),
+ (2100, 'SCHEMA-SET-100', 1001), -- for removal, not referenced by anchors
+ (2101, 'SCHEMA-SET-101', 1001); -- for removal, having anchor and data associated
INSERT INTO YANG_RESOURCE (ID, NAME, CONTENT, CHECKSUM) VALUES
(3001, 'module1@2020-02-02.yang', 'CONTENT-001', '877e65a9f36d54e7702c3f073f6bc42b'),
(3002, 'module2@2020-02-02.yang', 'CONTENT-002', '88892586b1f23fe8c1595759784a18f8'),
(3003, 'module3@2020-02-02.yang', 'CONTENT-003', 'fc5740499a09a48e0c95d6fc45d4bde8'),
- (3004, 'module4@2020-02-02.yang', 'CONTENT-004', '3801280fe532f5cbf535695cf6122026');
+ (3004, 'module4@2020-02-02.yang', 'CONTENT-004', '3801280fe532f5cbf535695cf6122026'),
+ (3100, 'orphan@2020-02-02.yang', 'ORPHAN', 'checksum'); -- for auto-removal as orphan
INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES
- (2001, 3001), (2001, 3002), (2002, 3003), (2002, 3004);
+ (2001, 3001), (2001, 3002),
+ (2002, 3003), (2002, 3004),
+ (2100, 3003), (2100, 3100), -- orphan removal case
+ (2101, 3003), (2101, 3004);
+
+INSERT INTO ANCHOR (ID, NAME, DATASPACE_ID, SCHEMA_SET_ID) VALUES -- anchors for removal
+ (6001, 'ANCHOR1', 1001, 2101),
+ (6002, 'ANCHOR2', 1001, 2101);
+
+INSERT INTO FRAGMENT (ID, XPATH, ANCHOR_ID, DATASPACE_ID) VALUES
+ (7001, '/XPATH', 6001, 1001);