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 * 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 java.util.Collection;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.cps.api.CpsAdminService;
30 import org.onap.cps.api.CpsModuleService;
31 import org.onap.cps.spi.CascadeDeleteAllowed;
32 import org.onap.cps.spi.CpsModulePersistenceService;
33 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
34 import org.onap.cps.spi.model.Anchor;
35 import org.onap.cps.spi.model.ModuleDefinition;
36 import org.onap.cps.spi.model.ModuleReference;
37 import org.onap.cps.spi.model.SchemaSet;
38 import org.onap.cps.spi.utils.CpsValidator;
39 import org.onap.cps.yang.YangTextSchemaSourceSet;
40 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
41 import org.springframework.stereotype.Service;
42 import org.springframework.transaction.annotation.Transactional;
44 @Service("CpsModuleServiceImpl")
45 @RequiredArgsConstructor
46 public class CpsModuleServiceImpl implements CpsModuleService {
48 private final CpsModulePersistenceService cpsModulePersistenceService;
49 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
50 private final CpsAdminService cpsAdminService;
51 private final CpsValidator cpsValidator;
54 public void createSchemaSet(final String dataspaceName, final String schemaSetName,
55 final Map<String, String> yangResourcesNameToContentMap) {
56 cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
57 final var yangTextSchemaSourceSet
58 = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap);
59 cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
60 yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
64 public void createSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
65 final Map<String, String> newModuleNameToContentMap,
66 final Collection<ModuleReference> allModuleReferences) {
67 cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
68 cpsModulePersistenceService.storeSchemaSetFromModules(dataspaceName, schemaSetName,
69 newModuleNameToContentMap, allModuleReferences);
74 public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
75 cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
76 final var yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
77 .get(dataspaceName, schemaSetName);
78 return SchemaSet.builder().name(schemaSetName).dataspaceName(dataspaceName)
79 .moduleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
83 public Collection<SchemaSet> getSchemaSets(final String dataspaceName) {
84 cpsValidator.validateNameCharacters(dataspaceName);
85 final Collection<SchemaSet> schemaSets =
86 cpsModulePersistenceService.getSchemaSetsByDataspaceName(dataspaceName);
87 schemaSets.forEach(schemaSet -> setModuleReferences(schemaSet, dataspaceName));
93 public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
94 final CascadeDeleteAllowed cascadeDeleteAllowed) {
95 cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
96 final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName, schemaSetName);
97 if (!anchors.isEmpty() && isCascadeDeleteProhibited(cascadeDeleteAllowed)) {
98 throw new SchemaSetInUseException(dataspaceName, schemaSetName);
100 for (final Anchor anchor : anchors) {
101 cpsAdminService.deleteAnchor(dataspaceName, anchor.getName());
103 cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName);
104 yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
105 cpsModulePersistenceService.deleteUnusedYangResourceModules();
109 public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
110 cpsValidator.validateNameCharacters(dataspaceName);
111 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
115 public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
116 final String anchorName) {
117 cpsValidator.validateNameCharacters(dataspaceName, anchorName);
118 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
122 public Collection<ModuleDefinition> getModuleDefinitionsByAnchorName(final String dataspaceName,
123 final String anchorName) {
124 cpsValidator.validateNameCharacters(dataspaceName, anchorName);
125 return cpsModulePersistenceService.getYangResourceDefinitions(dataspaceName, anchorName);
129 public Collection<ModuleReference> identifyNewModuleReferences(
130 final Collection<ModuleReference> moduleReferencesToCheck) {
131 return cpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
134 private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
135 return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
138 private void setModuleReferences(final SchemaSet schemaSet, final String dataspaceName) {
139 final YangTextSchemaSourceSet yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
140 .get(dataspaceName, schemaSet.getName());
141 schemaSet.setModuleReferences(yangTextSchemaSourceSet.getModuleReferences());