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