Expose REST endpoint to update YANG schema set using moduleSetTag
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsModuleServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2020-2021 Pantheon.tech
5  *  Modifications Copyright (C) 2022 Bell Canada
6  *  Modifications Copyright (C) 2022 TechMahindra Ltd
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.api.impl;
25
26 import io.micrometer.core.annotation.Timed;
27 import java.util.Collection;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30 import lombok.RequiredArgsConstructor;
31 import org.onap.cps.api.CpsAdminService;
32 import org.onap.cps.api.CpsModuleService;
33 import org.onap.cps.spi.CascadeDeleteAllowed;
34 import org.onap.cps.spi.CpsModulePersistenceService;
35 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
36 import org.onap.cps.spi.model.Anchor;
37 import org.onap.cps.spi.model.ModuleDefinition;
38 import org.onap.cps.spi.model.ModuleReference;
39 import org.onap.cps.spi.model.SchemaSet;
40 import org.onap.cps.spi.utils.CpsValidator;
41 import org.onap.cps.yang.TimedYangTextSchemaSourceSetBuilder;
42 import org.onap.cps.yang.YangTextSchemaSourceSet;
43 import org.springframework.stereotype.Service;
44 import org.springframework.transaction.annotation.Transactional;
45
46 @Service("CpsModuleServiceImpl")
47 @RequiredArgsConstructor
48 public class CpsModuleServiceImpl implements CpsModuleService {
49
50     private final CpsModulePersistenceService cpsModulePersistenceService;
51     private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
52     private final CpsAdminService cpsAdminService;
53     private final CpsValidator cpsValidator;
54     private final TimedYangTextSchemaSourceSetBuilder timedYangTextSchemaSourceSetBuilder;
55
56     @Override
57     @Timed(value = "cps.module.service.schemaset.create",
58         description = "Time taken to create (and store) a schemaset")
59     public void createSchemaSet(final String dataspaceName, final String schemaSetName,
60         final Map<String, String> yangResourcesNameToContentMap) {
61         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
62         cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
63         final YangTextSchemaSourceSet yangTextSchemaSourceSet =
64             timedYangTextSchemaSourceSetBuilder.getYangTextSchemaSourceSet(yangResourcesNameToContentMap);
65         yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
66     }
67
68     @Override
69     public void createOrUpgradeSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
70         final Map<String, String> newModuleNameToContentMap,
71         final Collection<ModuleReference> allModuleReferences) {
72         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
73         cpsModulePersistenceService.storeSchemaSetFromModules(dataspaceName, schemaSetName,
74             newModuleNameToContentMap, allModuleReferences);
75
76     }
77
78     @Override
79     public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
80         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
81         final var yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
82             .get(dataspaceName, schemaSetName);
83         return SchemaSet.builder().name(schemaSetName).dataspaceName(dataspaceName)
84             .moduleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
85     }
86
87     @Override
88     public Collection<SchemaSet> getSchemaSets(final String dataspaceName) {
89         cpsValidator.validateNameCharacters(dataspaceName);
90         final Collection<SchemaSet> schemaSets =
91             cpsModulePersistenceService.getSchemaSetsByDataspaceName(dataspaceName);
92         schemaSets.forEach(schemaSet -> setModuleReferences(schemaSet, dataspaceName));
93         return schemaSets;
94     }
95
96     @Override
97     @Transactional
98     public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
99                                 final CascadeDeleteAllowed cascadeDeleteAllowed) {
100         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
101         final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName, schemaSetName);
102         if (!anchors.isEmpty() && isCascadeDeleteProhibited(cascadeDeleteAllowed)) {
103             throw new SchemaSetInUseException(dataspaceName, schemaSetName);
104         }
105         for (final Anchor anchor : anchors) {
106             cpsAdminService.deleteAnchor(dataspaceName, anchor.getName());
107         }
108         cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName);
109         yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
110         cpsModulePersistenceService.deleteUnusedYangResourceModules();
111     }
112
113     @Override
114     @Transactional
115     public void deleteSchemaSetsWithCascade(final String dataspaceName, final Collection<String> schemaSetNames) {
116         cpsValidator.validateNameCharacters(dataspaceName);
117         cpsValidator.validateNameCharacters(schemaSetNames);
118         final Collection<String> anchorNames = cpsAdminService.getAnchors(dataspaceName, schemaSetNames)
119             .stream().map(Anchor::getName).collect(Collectors.toSet());
120         cpsAdminService.deleteAnchors(dataspaceName, anchorNames);
121         cpsModulePersistenceService.deleteUnusedYangResourceModules();
122         cpsModulePersistenceService.deleteSchemaSets(dataspaceName, schemaSetNames);
123         for (final String schemaSetName : schemaSetNames) {
124             yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
125         }
126     }
127
128     @Override
129     public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
130         cpsValidator.validateNameCharacters(dataspaceName);
131         return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
132     }
133
134     @Override
135     public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
136         final String anchorName) {
137         cpsValidator.validateNameCharacters(dataspaceName, anchorName);
138         return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
139     }
140
141     @Override
142     public Collection<ModuleDefinition> getModuleDefinitionsByAnchorName(final String dataspaceName,
143                                                                          final String anchorName) {
144         cpsValidator.validateNameCharacters(dataspaceName, anchorName);
145         return cpsModulePersistenceService.getYangResourceDefinitions(dataspaceName, anchorName);
146     }
147
148     @Override
149     public Collection<ModuleReference> identifyNewModuleReferences(
150         final Collection<ModuleReference> moduleReferencesToCheck) {
151         return cpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
152     }
153
154     private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
155         return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
156     }
157
158     private void setModuleReferences(final SchemaSet schemaSet, final String dataspaceName) {
159         final YangTextSchemaSourceSet yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
160             .get(dataspaceName, schemaSet.getName());
161         schemaSet.setModuleReferences(yangTextSchemaSourceSet.getModuleReferences());
162     }
163 }