Delete schema set - persistence layer

Issue-ID: CPS-121
Change-Id: I6fc8343969971b76d7f78ad202dd8ec1058c03fb
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
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 0a870ba..cac41ca 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.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 @@
     private DataspaceRepository dataspaceRepository;
 
     @Autowired
+    private AnchorRepository anchorRepository;
+
+    @Autowired
+    private FragmentRepository fragmentRepository;
+
+    @Autowired
     private CpsAdminPersistenceService cpsAdminPersistenceService;
 
     @Override
@@ -121,4 +132,24 @@
         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 cc9c2e0..0305555 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 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 @@
     }
 
     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 28585f8..4d44943 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 8e57f63..25d7689 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 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 @@
 
     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 8455e57..050ee26 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.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 @@
     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 @@
     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 @@
     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 @@
     }
 
     @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 @@
         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 @@
     }
 
     @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 @@
     }
 
     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 @@
         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 e3fc69b..0ec1ec3 100644
--- a/cps-ri/src/test/resources/data/schemaset.sql
+++ b/cps-ri/src/test/resources/data/schemaset.sql
@@ -2,13 +2,26 @@
     (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);
diff --git a/cps-service/src/main/java/org/onap/cps/spi/CascadeDeleteAllowed.java b/cps-service/src/main/java/org/onap/cps/spi/CascadeDeleteAllowed.java
new file mode 100644
index 0000000..e685434
--- /dev/null
+++ b/cps-service/src/main/java/org/onap/cps/spi/CascadeDeleteAllowed.java
@@ -0,0 +1,28 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Pantheon.tech
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi;
+
+/**
+ * Cascade delete option.
+ */
+public enum CascadeDeleteAllowed {
+    CASCADE_DELETE_ALLOWED,
+    CASCADE_DELETE_PROHIBITED
+}
diff --git a/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java
index dc4e26b..f5837e8 100755
--- a/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java
+++ b/cps-service/src/main/java/org/onap/cps/spi/CpsModulePersistenceService.java
@@ -22,6 +22,7 @@
 
 import java.util.Map;
 import org.checkerframework.checker.nullness.qual.NonNull;
+import org.onap.cps.spi.exceptions.DataInUseException;
 
 /**
  * Service to manage modules.
@@ -39,6 +40,18 @@
         @NonNull Map<String, String> yangResourcesNameToContentMap);
 
     /**
+     * Deletes Schema Set.
+     *
+     * @param dataspaceName        dataspace name
+     * @param schemaSetName        schema set name
+     * @param cascadeDeleteAllowed indicates the allowance to remove associated anchors and data if exist
+     * @throws DataInUseException if cascadeDeleteAllowed is set to CASCADE_DELETE_PROHIBITED and there
+     *                           is associated anchor record exists in database
+     */
+    void deleteSchemaSet(@NonNull String dataspaceName, @NonNull String schemaSetName,
+        @NonNull CascadeDeleteAllowed cascadeDeleteAllowed);
+
+    /**
      * Returns YANG resources per specific dataspace / schemaSetName.
      *
      * @param dataspaceName   dataspace name
diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/DataInUseException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/DataInUseException.java
new file mode 100644
index 0000000..fb02391
--- /dev/null
+++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/DataInUseException.java
@@ -0,0 +1,38 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Pantheon.tech
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.exceptions;
+
+/**
+ * Runtime exception. Thrown when data record rejected to be deleted because it's being referenced by other data.
+ */
+public class DataInUseException extends CpsException {
+
+    private static final long serialVersionUID = 5011830482789788314L;
+
+    /**
+     * Constructor.
+     *
+     * @param message error message
+     * @param details error details
+     */
+    public DataInUseException(final String message, final String details) {
+        super(message, details);
+    }
+}
diff --git a/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetInUseException.java b/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetInUseException.java
new file mode 100644
index 0000000..28bbb57
--- /dev/null
+++ b/cps-service/src/main/java/org/onap/cps/spi/exceptions/SchemaSetInUseException.java
@@ -0,0 +1,42 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Pantheon.tech
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.exceptions;
+
+/**
+ * Runtime exception. Thrown when schema set record rejected to be deleted because it has anchor records associated.
+ */
+@SuppressWarnings("squid:S110")  // Team agreed to accept 6 levels of inheritance for CPS Exceptions
+public class SchemaSetInUseException extends DataInUseException {
+
+    private static final long serialVersionUID = -3729328573253023683L;
+
+    /**
+     * Constructor.
+     *
+     * @param dataspaceName dataspace name
+     * @param schemaSetName schema set name
+     */
+    public SchemaSetInUseException(final String dataspaceName, final String schemaSetName) {
+        super("Schema Set is being used.",
+            String.format("Schema Set with name %s in dataspace %s is having Anchor records associated.",
+                schemaSetName, dataspaceName)
+        );
+    }
+}
diff --git a/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy
index e00a640..914a395 100755
--- a/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/spi/exceptions/CpsExceptionsSpec.groovy
@@ -52,7 +52,7 @@
                     == "Dataspace with name ${dataspaceName} does not exist."
     }
 
-    def'Creating a data validation exception.'() {
+    def 'Creating a data validation exception.'() {
         given: 'a data validation exception is created'
             def exception = new DataValidationException(providedMessage, providedDetails, rootCause)
         expect: 'the exception has the provided message'
@@ -63,7 +63,7 @@
             exception.cause == rootCause
     }
 
-    def'Creating a model validation exception.'() {
+    def 'Creating a model validation exception.'() {
         given: 'a data validation exception is created'
             def exception = new ModelValidationException(providedMessage, providedDetails)
         expect: 'the exception has the provided message'
@@ -86,7 +86,7 @@
     def 'Creating a exception for an object not found in a dataspace.'() {
         def descriptionOfObject = 'some object'
         expect: 'the exception details contains the correct message with dataspace name and description of the object'
-            (new NotFoundInDataspaceException(dataspaceName,descriptionOfObject)).details
+            (new NotFoundInDataspaceException(dataspaceName, descriptionOfObject)).details
                     == "${descriptionOfObject} does not exist in dataspace ${dataspaceName}."
     }
 
@@ -101,13 +101,20 @@
 
     def 'Creating a exception that a schema set cannot be found.'() {
         expect: 'the exception details contains the correct message with dataspace and schema set names'
-            (new SchemaSetNotFoundException(dataspaceName,schemaSetName)).details
+            (new SchemaSetNotFoundException(dataspaceName, schemaSetName)).details
                     == "Schema Set with name ${schemaSetName} was not found for dataspace ${dataspaceName}."
     }
-    
+
     def 'Creating a exception that an anchor cannot be found.'() {
         expect: 'the exception details contains the correct message with dataspace and anchor name'
             (new AnchorNotFoundException(anchorName, dataspaceName)).details
                     == "Anchor with name ${anchorName} does not exist in dataspace ${dataspaceName}."
     }
+
+    def 'Creating an exception that the schema set being used and cannot be deleted.'() {
+        expect: 'the exception details contains the correct message with dataspace and schema set names'
+            (new SchemaSetInUseException(dataspaceName, schemaSetName)).details
+                    == ("Schema Set with name ${schemaSetName} in dataspace ${dataspaceName} is having "
+                    + "Anchor records associated.")
+    }
 }
\ No newline at end of file