0f620b0dd8921bb1a76f57edbc961a894fe115dc
[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  *  Modifications Copyright (C) 2022 Nordix Foundation
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.api.impl;
24
25 import com.google.errorprone.annotations.CanIgnoreReturnValue;
26 import java.util.Map;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.cps.spi.CpsModulePersistenceService;
29 import org.onap.cps.spi.utils.CpsValidator;
30 import org.onap.cps.yang.YangTextSchemaSourceSet;
31 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
32 import org.springframework.cache.annotation.CacheConfig;
33 import org.springframework.cache.annotation.CacheEvict;
34 import org.springframework.cache.annotation.CachePut;
35 import org.springframework.cache.annotation.Cacheable;
36 import org.springframework.stereotype.Service;
37
38 /**
39  * Provides cached YangTextSchemaSourceSet.
40  */
41 @Service
42 @CacheConfig(cacheNames = {"yangSchema"})
43 @RequiredArgsConstructor
44 public class YangTextSchemaSourceSetCache {
45
46     private final CpsModulePersistenceService cpsModulePersistenceService;
47     private final CpsValidator cpsValidator;
48
49     /**
50      * Cache YangTextSchemaSourceSet.
51      *
52      * @param dataspaceName dataspace name
53      * @param schemaSetName schema set name
54      * @return YangTextSchemaSourceSet
55      */
56     @Cacheable(key = "#p0.concat('-').concat(#p1)")
57     public YangTextSchemaSourceSet get(final String dataspaceName, final String schemaSetName) {
58         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
59         final Map<String, String> yangResourceNameToContent =
60                 cpsModulePersistenceService.getYangSchemaResources(dataspaceName, schemaSetName);
61         return YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent);
62     }
63
64     /**
65      * Updates cache YangTextSchemaSourceSet.
66      *
67      * @param dataspaceName           dataspace name
68      * @param schemaSetName           schema set name
69      * @param yangTextSchemaSourceSet yangTextSchemaSourceSet
70      * @return YangTextSchemaSourceSet
71      */
72     @CachePut(key = "#p0.concat('-').concat(#p1)")
73     @CanIgnoreReturnValue
74     public YangTextSchemaSourceSet updateCache(final String dataspaceName, final String schemaSetName,
75             final YangTextSchemaSourceSet yangTextSchemaSourceSet) {
76         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
77         return yangTextSchemaSourceSet;
78     }
79
80
81     /**
82      * Remove the cached value for the given dataspace and schema-set.
83      *
84      * @param dataspaceName dataspace name
85      * @param schemaSetName schema set name
86      */
87     @CacheEvict(key = "#p0.concat('-').concat(#p1)")
88     public void removeFromCache(final String dataspaceName, final String schemaSetName) {
89         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
90         // Spring provides implementation for removing object from cache
91     }
92
93 }