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 lombok.RequiredArgsConstructor;
30 import org.onap.cps.api.CpsAdminService;
31 import org.onap.cps.api.CpsModuleService;
32 import org.onap.cps.spi.CascadeDeleteAllowed;
33 import org.onap.cps.spi.CpsModulePersistenceService;
34 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
35 import org.onap.cps.spi.model.Anchor;
36 import org.onap.cps.spi.model.ModuleDefinition;
37 import org.onap.cps.spi.model.ModuleReference;
38 import org.onap.cps.spi.model.SchemaSet;
39 import org.onap.cps.spi.utils.CpsValidator;
40 import org.onap.cps.yang.TimedYangTextSchemaSourceSetBuilder;
41 import org.onap.cps.yang.YangTextSchemaSourceSet;
42 import org.springframework.stereotype.Service;
43 import org.springframework.transaction.annotation.Transactional;
45 @Service("CpsModuleServiceImpl")
46 @RequiredArgsConstructor
47 public class CpsModuleServiceImpl implements CpsModuleService {
49 private final CpsModulePersistenceService cpsModulePersistenceService;
50 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
51 private final CpsAdminService cpsAdminService;
52 private final CpsValidator cpsValidator;
53 private final TimedYangTextSchemaSourceSetBuilder timedYangTextSchemaSourceSetBuilder;
56 @Timed(value = "cps.module.service.schemaset.create",
57 description = "Time taken to create (and store) a schemaset")
58 public void createSchemaSet(final String dataspaceName, final String schemaSetName,
59 final Map<String, String> yangResourcesNameToContentMap) {
60 cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
61 final YangTextSchemaSourceSet yangTextSchemaSourceSet =
62 timedYangTextSchemaSourceSetBuilder.getYangTextSchemaSourceSet(yangResourcesNameToContentMap);
63 cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
64 yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
68 public void createSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
69 final Map<String, String> newModuleNameToContentMap,
70 final Collection<ModuleReference> allModuleReferences) {
71 cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
72 cpsModulePersistenceService.storeSchemaSetFromModules(dataspaceName, schemaSetName,
73 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();
113 public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
114 cpsValidator.validateNameCharacters(dataspaceName);
115 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
119 public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
120 final String anchorName) {
121 cpsValidator.validateNameCharacters(dataspaceName, anchorName);
122 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
126 public Collection<ModuleDefinition> getModuleDefinitionsByAnchorName(final String dataspaceName,
127 final String anchorName) {
128 cpsValidator.validateNameCharacters(dataspaceName, anchorName);
129 return cpsModulePersistenceService.getYangResourceDefinitions(dataspaceName, anchorName);
133 public Collection<ModuleReference> identifyNewModuleReferences(
134 final Collection<ModuleReference> moduleReferencesToCheck) {
135 return cpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
138 private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
139 return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
142 private void setModuleReferences(final SchemaSet schemaSet, final String dataspaceName) {
143 final YangTextSchemaSourceSet yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
144 .get(dataspaceName, schemaSet.getName());
145 schemaSet.setModuleReferences(yangTextSchemaSourceSet.getModuleReferences());