03b52a3088df7aaeeedf897dc8d8be2e267f16e8
[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.yang.YangTextSchemaSourceSet;
28 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.cache.annotation.CacheConfig;
31 import org.springframework.cache.annotation.CacheEvict;
32 import org.springframework.cache.annotation.CachePut;
33 import org.springframework.cache.annotation.Cacheable;
34 import org.springframework.stereotype.Service;
35
36 /**
37  * Provides cached YangTextSchemaSourceSet.
38  */
39 @Service
40 @CacheConfig(cacheNames = {"yangSchema"})
41 public class YangTextSchemaSourceSetCache {
42
43     @Autowired
44     private CpsModulePersistenceService cpsModulePersistenceService;
45
46     /**
47      * Cache YangTextSchemaSourceSet.
48      *
49      * @param dataspaceName dataspace name
50      * @param schemaSetName schema set name
51      * @return YangTextSchemaSourceSet
52      */
53     @Cacheable(key = "#p0.concat('-').concat(#p1)")
54     public YangTextSchemaSourceSet get(final String dataspaceName, final String schemaSetName) {
55         final Map<String, String> yangResourceNameToContent =
56                 cpsModulePersistenceService.getYangSchemaResources(dataspaceName, schemaSetName);
57         return YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent);
58     }
59
60     /**
61      * Updates cache YangTextSchemaSourceSet.
62      *
63      * @param dataspaceName           dataspace name
64      * @param schemaSetName           schema set name
65      * @param yangTextSchemaSourceSet yangTextSchemaSourceSet
66      * @return YangTextSchemaSourceSet
67      */
68     @CachePut(key = "#p0.concat('-').concat(#p1)")
69     @CanIgnoreReturnValue
70     public YangTextSchemaSourceSet updateCache(final String dataspaceName, final String schemaSetName,
71             final YangTextSchemaSourceSet yangTextSchemaSourceSet) {
72         return yangTextSchemaSourceSet;
73     }
74
75
76     /**
77      * Remove the cached value for the given dataspace and schema-set.
78      *
79      * @param dataspaceName dataspace name
80      * @param schemaSetName schema set name
81      */
82     @CacheEvict(key = "#p0.concat('-').concat(#p1)")
83     public void removeFromCache(final String dataspaceName, final String schemaSetName) {
84         // Spring provides implementation for removing object from cache
85     }
86
87 }