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
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;
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;
39 * Provides cached YangTextSchemaSourceSet.
42 @CacheConfig(cacheNames = {"yangSchema"})
43 @RequiredArgsConstructor
44 public class YangTextSchemaSourceSetCache {
46 private final CpsModulePersistenceService cpsModulePersistenceService;
47 private final CpsValidator cpsValidator;
50 * Cache YangTextSchemaSourceSet.
52 * @param dataspaceName dataspace name
53 * @param schemaSetName schema set name
54 * @return YangTextSchemaSourceSet
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);
65 * Updates cache YangTextSchemaSourceSet.
67 * @param dataspaceName dataspace name
68 * @param schemaSetName schema set name
69 * @param yangTextSchemaSourceSet yangTextSchemaSourceSet
70 * @return YangTextSchemaSourceSet
72 @CachePut(key = "#p0.concat('-').concat(#p1)")
74 public YangTextSchemaSourceSet updateCache(final String dataspaceName, final String schemaSetName,
75 final YangTextSchemaSourceSet yangTextSchemaSourceSet) {
76 cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
77 return yangTextSchemaSourceSet;
82 * Remove the cached value for the given dataspace and schema-set.
84 * @param dataspaceName dataspace name
85 * @param schemaSetName schema set name
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