Merge "CM SUBSCRIPTION: add new subscription for non existing xpath"
[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-2023 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 io.micrometer.core.instrument.Metrics;
27 import java.util.Map;
28 import java.util.concurrent.atomic.AtomicInteger;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.cps.spi.CpsModulePersistenceService;
31 import org.onap.cps.spi.utils.CpsValidator;
32 import org.onap.cps.yang.YangTextSchemaSourceSet;
33 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
34 import org.springframework.cache.annotation.CacheConfig;
35 import org.springframework.cache.annotation.CacheEvict;
36 import org.springframework.cache.annotation.CachePut;
37 import org.springframework.cache.annotation.Cacheable;
38 import org.springframework.stereotype.Service;
39
40 /**
41  * Provides cached YangTextSchemaSourceSet.
42  */
43 @Service
44 @CacheConfig(cacheNames = {"yangSchema"})
45 @RequiredArgsConstructor
46 public class YangTextSchemaSourceSetCache {
47
48     private final CpsModulePersistenceService cpsModulePersistenceService;
49     private final CpsValidator cpsValidator;
50
51     private final AtomicInteger yangSchemaCacheCounter = Metrics.gauge("cps.yangschema.cache.gauge",
52                                                                         new AtomicInteger(0));
53
54     /**
55      * Cache YangTextSchemaSourceSet.
56      *
57      * @param dataspaceName dataspace name
58      * @param schemaSetName schema set name
59      * @return YangTextSchemaSourceSet
60      */
61     @Cacheable(key = "#p0.concat('-').concat(#p1)")
62     public YangTextSchemaSourceSet get(final String dataspaceName, final String schemaSetName) {
63         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
64         final Map<String, String> yangResourceNameToContent =
65                 cpsModulePersistenceService.getYangSchemaResources(dataspaceName, schemaSetName);
66         return YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent);
67     }
68
69     /**
70      * Updates cache YangTextSchemaSourceSet.
71      *
72      * @param dataspaceName           dataspace name
73      * @param schemaSetName           schema set name
74      * @param yangTextSchemaSourceSet yangTextSchemaSourceSet
75      * @return YangTextSchemaSourceSet
76      */
77     @CachePut(key = "#p0.concat('-').concat(#p1)")
78     @CanIgnoreReturnValue
79     public YangTextSchemaSourceSet updateCache(final String dataspaceName, final String schemaSetName,
80             final YangTextSchemaSourceSet yangTextSchemaSourceSet) {
81         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
82         yangSchemaCacheCounter.incrementAndGet();
83         return yangTextSchemaSourceSet;
84     }
85
86     /**
87      * Remove the cached value for the given dataspace and schema-set.
88      *
89      * @param dataspaceName dataspace name
90      * @param schemaSetName schema set name
91      */
92     @CacheEvict(key = "#p0.concat('-').concat(#p1)")
93     public void removeFromCache(final String dataspaceName, final String schemaSetName) {
94         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
95         yangSchemaCacheCounter.decrementAndGet();
96         // Spring provides implementation for removing object from cache
97     }
98
99 }