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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.api.impl;
25 import com.google.errorprone.annotations.CanIgnoreReturnValue;
26 import io.micrometer.core.instrument.Metrics;
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;
41 * Provides cached YangTextSchemaSourceSet.
44 @CacheConfig(cacheNames = {"yangSchema"})
45 @RequiredArgsConstructor
46 public class YangTextSchemaSourceSetCache {
48 private final CpsModulePersistenceService cpsModulePersistenceService;
49 private final CpsValidator cpsValidator;
51 private final AtomicInteger yangSchemaCacheCounter = Metrics.gauge("cps.yangschema.cache.gauge",
52 new AtomicInteger(0));
55 * Cache YangTextSchemaSourceSet.
57 * @param dataspaceName dataspace name
58 * @param schemaSetName schema set name
59 * @return YangTextSchemaSourceSet
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);
70 * Updates cache YangTextSchemaSourceSet.
72 * @param dataspaceName dataspace name
73 * @param schemaSetName schema set name
74 * @param yangTextSchemaSourceSet yangTextSchemaSourceSet
75 * @return YangTextSchemaSourceSet
77 @CachePut(key = "#p0.concat('-').concat(#p1)")
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;
87 * Remove the cached value for the given dataspace and schema-set.
89 * @param dataspaceName dataspace name
90 * @param schemaSetName schema set name
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