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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.api.impl;
26 import io.micrometer.core.annotation.Timed;
27 import java.util.Collection;
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;
46 @Service("CpsModuleServiceImpl")
47 @RequiredArgsConstructor
48 public class CpsModuleServiceImpl implements CpsModuleService {
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;
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);
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);
78 public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
79 cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
80 final var yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
81 .get(dataspaceName, schemaSetName);
82 return SchemaSet.builder().name(schemaSetName).dataspaceName(dataspaceName)
83 .moduleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
87 public Collection<SchemaSet> getSchemaSets(final String dataspaceName) {
88 cpsValidator.validateNameCharacters(dataspaceName);
89 final Collection<SchemaSet> schemaSets =
90 cpsModulePersistenceService.getSchemaSetsByDataspaceName(dataspaceName);
91 schemaSets.forEach(schemaSet -> setModuleReferences(schemaSet, dataspaceName));
97 public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
98 final CascadeDeleteAllowed cascadeDeleteAllowed) {
99 cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
100 final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName, schemaSetName);
101 if (!anchors.isEmpty() && isCascadeDeleteProhibited(cascadeDeleteAllowed)) {
102 throw new SchemaSetInUseException(dataspaceName, schemaSetName);
104 for (final Anchor anchor : anchors) {
105 cpsAdminService.deleteAnchor(dataspaceName, anchor.getName());
107 cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName);
108 yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
109 cpsModulePersistenceService.deleteUnusedYangResourceModules();
114 public void deleteSchemaSetsWithCascade(final String dataspaceName, final Collection<String> schemaSetNames) {
115 cpsValidator.validateNameCharacters(dataspaceName);
116 cpsValidator.validateNameCharacters(schemaSetNames);
117 final Collection<String> anchorNames = cpsAdminService.getAnchors(dataspaceName, schemaSetNames)
118 .stream().map(Anchor::getName).collect(Collectors.toSet());
119 cpsAdminService.deleteAnchors(dataspaceName, anchorNames);
120 cpsModulePersistenceService.deleteUnusedYangResourceModules();
121 cpsModulePersistenceService.deleteSchemaSets(dataspaceName, schemaSetNames);
122 for (final String schemaSetName : schemaSetNames) {
123 yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
128 public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
129 cpsValidator.validateNameCharacters(dataspaceName);
130 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
134 public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
135 final String anchorName) {
136 cpsValidator.validateNameCharacters(dataspaceName, anchorName);
137 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
141 public Collection<ModuleDefinition> getModuleDefinitionsByAnchorName(final String dataspaceName,
142 final String anchorName) {
143 cpsValidator.validateNameCharacters(dataspaceName, anchorName);
144 return cpsModulePersistenceService.getYangResourceDefinitions(dataspaceName, anchorName);
148 public Collection<ModuleReference> identifyNewModuleReferences(
149 final Collection<ModuleReference> moduleReferencesToCheck) {
150 return cpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
153 private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
154 return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
157 private void setModuleReferences(final SchemaSet schemaSet, final String dataspaceName) {
158 final YangTextSchemaSourceSet yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
159 .get(dataspaceName, schemaSet.getName());
160 schemaSet.setModuleReferences(yangTextSchemaSourceSet.getModuleReferences());