20b4a23a9bd228aa207cc40eb25bdf1251d25bc9
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsModuleServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2022 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.Map;
27 import lombok.AllArgsConstructor;
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.ModuleDefinition;
35 import org.onap.cps.spi.model.ModuleReference;
36 import org.onap.cps.spi.model.SchemaSet;
37 import org.onap.cps.utils.CpsValidator;
38 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
39 import org.springframework.stereotype.Service;
40 import org.springframework.transaction.annotation.Transactional;
41
42 @Service("CpsModuleServiceImpl")
43 @AllArgsConstructor
44 public class CpsModuleServiceImpl implements CpsModuleService {
45
46     private final CpsModulePersistenceService cpsModulePersistenceService;
47     private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
48     private final CpsAdminService cpsAdminService;
49
50     @Override
51     public void createSchemaSet(final String dataspaceName, final String schemaSetName,
52         final Map<String, String> yangResourcesNameToContentMap) {
53         CpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
54         final var yangTextSchemaSourceSet
55             = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap);
56         cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
57         yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
58     }
59
60     @Override
61     public void createSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
62         final Map<String, String> newModuleNameToContentMap,
63         final Collection<ModuleReference> allModuleReferences) {
64         CpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
65         cpsModulePersistenceService.storeSchemaSetFromModules(dataspaceName, schemaSetName,
66             newModuleNameToContentMap, allModuleReferences);
67
68     }
69
70     @Override
71     public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
72         CpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
73         final var yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
74             .get(dataspaceName, schemaSetName);
75         return SchemaSet.builder().name(schemaSetName).dataspaceName(dataspaceName)
76             .moduleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
77     }
78
79     @Override
80     @Transactional
81     public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
82         final CascadeDeleteAllowed cascadeDeleteAllowed) {
83         CpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
84         final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName, schemaSetName);
85         if (!anchors.isEmpty() && isCascadeDeleteProhibited(cascadeDeleteAllowed)) {
86             throw new SchemaSetInUseException(dataspaceName, schemaSetName);
87         }
88         for (final Anchor anchor : anchors) {
89             cpsAdminService.deleteAnchor(dataspaceName, anchor.getName());
90         }
91         cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName);
92         yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
93         cpsModulePersistenceService.deleteUnusedYangResourceModules();
94     }
95
96     @Override
97     public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
98         CpsValidator.validateNameCharacters(dataspaceName);
99         return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
100     }
101
102     @Override
103     public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
104         final String anchorName) {
105         CpsValidator.validateNameCharacters(dataspaceName, anchorName);
106         return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
107     }
108
109     @Override
110     public Collection<ModuleDefinition> getModuleDefinitionsByAnchorName(final String dataspaceName,
111                                                                          final String anchorName) {
112         CpsValidator.validateNameCharacters(dataspaceName, anchorName);
113         return cpsModulePersistenceService.getYangResourceDefinitions(dataspaceName, anchorName);
114     }
115
116     @Override
117     public Collection<ModuleReference> identifyNewModuleReferences(
118         final Collection<ModuleReference> moduleReferencesToCheck) {
119         return cpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
120     }
121
122     private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
123         return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
124     }
125
126 }