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 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.api.impl;
25 import java.util.Collection;
27 import lombok.AllArgsConstructor;
28 import org.onap.cps.api.CpsAdminService;
29 import org.onap.cps.api.CpsModuleService;
30 import org.onap.cps.spi.CascadeDeleteAllowed;
31 import org.onap.cps.spi.CpsModulePersistenceService;
32 import org.onap.cps.spi.exceptions.SchemaSetInUseException;
33 import org.onap.cps.spi.model.Anchor;
34 import org.onap.cps.spi.model.ModuleReference;
35 import org.onap.cps.spi.model.SchemaSet;
36 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
37 import org.springframework.stereotype.Service;
38 import org.springframework.transaction.annotation.Transactional;
40 @Service("CpsModuleServiceImpl")
42 public class CpsModuleServiceImpl implements CpsModuleService {
44 private final CpsModulePersistenceService cpsModulePersistenceService;
45 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
46 private final CpsAdminService cpsAdminService;
49 public void createSchemaSet(final String dataspaceName, final String schemaSetName,
50 final Map<String, String> yangResourcesNameToContentMap) {
51 final var yangTextSchemaSourceSet
52 = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap);
53 cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
54 yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
58 public void createSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
59 final Map<String, String> newModuleNameToContentMap,
60 final Collection<ModuleReference> moduleReferences) {
61 cpsModulePersistenceService.storeSchemaSetFromModules(dataspaceName, schemaSetName,
62 newModuleNameToContentMap, moduleReferences);
67 public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
68 final var yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
69 .get(dataspaceName, schemaSetName);
70 return SchemaSet.builder().name(schemaSetName).dataspaceName(dataspaceName)
71 .moduleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
76 public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
77 final CascadeDeleteAllowed cascadeDeleteAllowed) {
78 final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName, schemaSetName);
79 if (!anchors.isEmpty() && isCascadeDeleteProhibited(cascadeDeleteAllowed)) {
80 throw new SchemaSetInUseException(dataspaceName, schemaSetName);
82 for (final Anchor anchor : anchors) {
83 cpsAdminService.deleteAnchor(dataspaceName, anchor.getName());
85 cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName);
86 yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
87 cpsModulePersistenceService.deleteUnusedYangResourceModules();
91 public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
92 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
96 public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
97 final String anchorName) {
98 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
101 private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
102 return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
106 public Collection<ModuleReference> identifyNewModuleReferences(
107 final Collection<ModuleReference> moduleReferencesToCheck) {
108 return cpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);