Refactored Delete SchemaSet functionality
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsModuleServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 Nordix Foundation
4  *  Modifications Copyright (C) 2020-2021 Pantheon.tech
5  *  Modifications Copyright (C) 2022 Bell Canada
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.api.impl;
24
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Map;
28 import org.onap.cps.api.CpsAdminService;
29 import org.onap.cps.api.CpsModuleService;
30 import org.onap.cps.spi.CascadeDeleteAllowed;
31 import org.onap.cps.spi.CpsModulePersistenceService;
32 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
33 import org.onap.cps.spi.model.Anchor;
34 import org.onap.cps.spi.model.ModuleReference;
35 import org.onap.cps.spi.model.SchemaSet;
36 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
37 import org.springframework.stereotype.Service;
38 import org.springframework.transaction.annotation.Transactional;
39
40 @Service("CpsModuleServiceImpl")
41 public class CpsModuleServiceImpl implements CpsModuleService {
42
43     private CpsModulePersistenceService cpsModulePersistenceService;
44     private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
45     private CpsAdminService cpsAdminService;
46
47     /**
48      * Create an instance of CpsModuleServiceImpl.
49      *
50      * @param cpsModulePersistenceService  cpsModulePersistenceService
51      * @param yangTextSchemaSourceSetCache yangTextSchemaSourceSetCache
52      * @param cpsAdminService              cpsAdminService
53      */
54     public CpsModuleServiceImpl(final CpsModulePersistenceService cpsModulePersistenceService,
55         final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache, final CpsAdminService cpsAdminService) {
56         this.cpsModulePersistenceService = cpsModulePersistenceService;
57         this.yangTextSchemaSourceSetCache = yangTextSchemaSourceSetCache;
58         this.cpsAdminService = cpsAdminService;
59     }
60
61     @Override
62     public void createSchemaSet(final String dataspaceName, final String schemaSetName,
63         final Map<String, String> yangResourcesNameToContentMap) {
64         final var yangTextSchemaSourceSet
65             = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap);
66         cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
67         yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
68     }
69
70     @Override
71     public void createSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
72         final Map<String, String> newYangResourcesModuleNameToContentMap,
73         final List<ModuleReference> moduleReferences) {
74         cpsModulePersistenceService.storeSchemaSetFromModules(dataspaceName, schemaSetName,
75             newYangResourcesModuleNameToContentMap, moduleReferences);
76
77     }
78
79     @Override
80     public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
81         final var yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
82             .get(dataspaceName, schemaSetName);
83         return SchemaSet.builder().name(schemaSetName).dataspaceName(dataspaceName)
84             .extendedModuleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
85     }
86
87     @Override
88     @Transactional
89     public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
90         final CascadeDeleteAllowed cascadeDeleteAllowed) {
91         final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName, schemaSetName);
92         if (!anchors.isEmpty() && isCascadeDeleteProhibited(cascadeDeleteAllowed)) {
93             throw new SchemaSetInUseException(dataspaceName, schemaSetName);
94         }
95         for (final Anchor anchor : anchors) {
96             cpsAdminService.deleteAnchor(dataspaceName, anchor.getName());
97         }
98         cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName);
99         cpsModulePersistenceService.deleteUnusedYangResourceModules();
100     }
101
102     @Override
103     public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
104         return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
105     }
106
107     @Override
108     public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
109         final String anchorName) {
110         return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
111     }
112
113     private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
114         return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
115     }
116 }