Introduce Instrumentation
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsModuleServiceImpl.java
1 /*
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.api.impl;
25
26 import io.micrometer.core.annotation.Timed;
27 import java.util.Collection;
28 import java.util.Map;
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;
44
45 @Service("CpsModuleServiceImpl")
46 @RequiredArgsConstructor
47 public class CpsModuleServiceImpl implements CpsModuleService {
48
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;
54
55     @Override
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);
65     }
66
67     @Override
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);
74
75     }
76
77     @Override
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();
84     }
85
86     @Override
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));
92         return schemaSets;
93     }
94
95     @Override
96     @Transactional
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);
103         }
104         for (final Anchor anchor : anchors) {
105             cpsAdminService.deleteAnchor(dataspaceName, anchor.getName());
106         }
107         cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName);
108         yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName);
109         cpsModulePersistenceService.deleteUnusedYangResourceModules();
110     }
111
112     @Override
113     public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
114         cpsValidator.validateNameCharacters(dataspaceName);
115         return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName);
116     }
117
118     @Override
119     public Collection<ModuleReference> getYangResourcesModuleReferences(final String dataspaceName,
120         final String anchorName) {
121         cpsValidator.validateNameCharacters(dataspaceName, anchorName);
122         return cpsModulePersistenceService.getYangResourceModuleReferences(dataspaceName, anchorName);
123     }
124
125     @Override
126     public Collection<ModuleDefinition> getModuleDefinitionsByAnchorName(final String dataspaceName,
127                                                                          final String anchorName) {
128         cpsValidator.validateNameCharacters(dataspaceName, anchorName);
129         return cpsModulePersistenceService.getYangResourceDefinitions(dataspaceName, anchorName);
130     }
131
132     @Override
133     public Collection<ModuleReference> identifyNewModuleReferences(
134         final Collection<ModuleReference> moduleReferencesToCheck) {
135         return cpsModulePersistenceService.identifyNewModuleReferences(moduleReferencesToCheck);
136     }
137
138     private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) {
139         return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed;
140     }
141
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());
146     }
147 }