Additional validation for names/identifiers
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / YangTextSchemaSourceSetCache.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Pantheon.tech
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.api.impl;
23
24 import com.google.errorprone.annotations.CanIgnoreReturnValue;
25 import java.util.Map;
26 import org.onap.cps.spi.CpsModulePersistenceService;
27 import org.onap.cps.utils.CpsValidator;
28 import org.onap.cps.yang.YangTextSchemaSourceSet;
29 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.cache.annotation.CacheConfig;
32 import org.springframework.cache.annotation.CacheEvict;
33 import org.springframework.cache.annotation.CachePut;
34 import org.springframework.cache.annotation.Cacheable;
35 import org.springframework.stereotype.Service;
36
37 /**
38  * Provides cached YangTextSchemaSourceSet.
39  */
40 @Service
41 @CacheConfig(cacheNames = {"yangSchema"})
42 public class YangTextSchemaSourceSetCache {
43
44     @Autowired
45     private CpsModulePersistenceService cpsModulePersistenceService;
46
47     /**
48      * Cache YangTextSchemaSourceSet.
49      *
50      * @param dataspaceName dataspace name
51      * @param schemaSetName schema set name
52      * @return YangTextSchemaSourceSet
53      */
54     @Cacheable(key = "#p0.concat('-').concat(#p1)")
55     public YangTextSchemaSourceSet get(final String dataspaceName, final String schemaSetName) {
56         CpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
57         final Map<String, String> yangResourceNameToContent =
58                 cpsModulePersistenceService.getYangSchemaResources(dataspaceName, schemaSetName);
59         return YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent);
60     }
61
62     /**
63      * Updates cache YangTextSchemaSourceSet.
64      *
65      * @param dataspaceName           dataspace name
66      * @param schemaSetName           schema set name
67      * @param yangTextSchemaSourceSet yangTextSchemaSourceSet
68      * @return YangTextSchemaSourceSet
69      */
70     @CachePut(key = "#p0.concat('-').concat(#p1)")
71     @CanIgnoreReturnValue
72     public YangTextSchemaSourceSet updateCache(final String dataspaceName, final String schemaSetName,
73             final YangTextSchemaSourceSet yangTextSchemaSourceSet) {
74         CpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
75         return yangTextSchemaSourceSet;
76     }
77
78
79     /**
80      * Remove the cached value for the given dataspace and schema-set.
81      *
82      * @param dataspaceName dataspace name
83      * @param schemaSetName schema set name
84      */
85     @CacheEvict(key = "#p0.concat('-').concat(#p1)")
86     public void removeFromCache(final String dataspaceName, final String schemaSetName) {
87         CpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
88         // Spring provides implementation for removing object from cache
89     }
90
91 }