2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 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;
26 import java.util.List;
28 import lombok.AllArgsConstructor;
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.ModuleReference;
36 import org.onap.cps.spi.model.SchemaSet;
37 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
38 import org.springframework.stereotype.Service;
39 import org.springframework.transaction.annotation.Transactional;
41 @Service("CpsModuleServiceImpl")
43 public class CpsModuleServiceImpl implements CpsModuleService {
45 private final CpsModulePersistenceService cpsModulePersistenceService;
46 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
47 private final CpsAdminService cpsAdminService;
50 public void createSchemaSet(final String dataspaceName, final String schemaSetName,
51 final Map<String, String> yangResourcesNameToContentMap) {
52 final var yangTextSchemaSourceSet
53 = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap);
54 cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
55 yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
59 public void createSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
60 final Map<String, String> newYangResourcesModuleNameToContentMap,
61 final List<ModuleReference> moduleReferences) {
62 cpsModulePersistenceService.storeSchemaSetFromModules(dataspaceName, schemaSetName,
63 newYangResourcesModuleNameToContentMap, moduleReferences);
68 public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
69 final var yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
70 .get(dataspaceName, schemaSetName);
71 return SchemaSet.builder().name(schemaSetName).dataspaceName(dataspaceName)
72 .extendedModuleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
77 public void deleteSchemaSet(final String dataspaceName, final String schemaSetName,
78 final CascadeDeleteAllowed cascadeDeleteAllowed) {
79 final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName, schemaSetName);
80 if (!anchors.isEmpty() && isCascadeDeleteProhibited(cascadeDeleteAllowed)) {
81 throw new SchemaSetInUseException(dataspaceName, schemaSetName);
83 for (final Anchor anchor : anchors) {
84 cpsAdminService.deleteAnchor(dataspaceName, anchor.getName());
86 cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName);
87 yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
88 cpsModulePersistenceService.deleteUnusedYangResourceModules();
92 public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
93 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
97 public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
98 final String anchorName) {
99 return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
102 private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
103 return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;