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);
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();
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));
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);
105 for (final Anchor anchor : anchors) {
106 cpsAdminService.deleteAnchor(dataspaceName, anchor.getName());
108 cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName);
109 yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
110 cpsModulePersistenceService.deleteUnusedYangResourceModules();
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);
129 public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
130 cpsValidator.validateNameCharacters(dataspaceName);
131 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
135 public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
136 final String anchorName) {
137 cpsValidator.validateNameCharacters(dataspaceName, anchorName);
138 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
142 public Collection<ModuleDefinition> getModuleDefinitionsByAnchorName(final String dataspaceName,
143 final String anchorName) {
144 cpsValidator.validateNameCharacters(dataspaceName, anchorName);
145 return cpsModulePersistenceService.getYangResourceDefinitions(dataspaceName, anchorName);
149 public Collection<ModuleReference> identifyNewModuleReferences(
150 final Collection<ModuleReference> moduleReferencesToCheck) {
151 return cpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
154 private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
155 return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
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());