Merge "Introduce caffeine cache"
diff --git a/cps-rest/src/main/resources/application.yml b/cps-rest/src/main/resources/application.yml
index e8af0bc..80c6af6 100644
--- a/cps-rest/src/main/resources/application.yml
+++ b/cps-rest/src/main/resources/application.yml
@@ -22,6 +22,11 @@
         driverClassName: org.postgresql.Driver

         initialization-mode: always

 

+    cache:

+        type: caffeine

+        cache-names: yangSchema

+        caffeine:

+            spec: maximumSize=10000,expireAfterAccess=10m

 # Actuator

 management:

     endpoints:

diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
index cac41ca..b28beb4 100755
--- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
+++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java
@@ -127,8 +127,7 @@
     }
 
     @Override
-    public Map<String, String> getYangSchemaSetResources(final String dataspaceName,
-        final String anchorName) {
+    public Map<String, String> getYangSchemaSetResources(final String dataspaceName, final String anchorName) {
         final Anchor anchor = cpsAdminPersistenceService.getAnchor(dataspaceName, anchorName);
         return getYangSchemaResources(dataspaceName, anchor.getSchemaSetName());
     }
diff --git a/cps-service/pom.xml b/cps-service/pom.xml
index 642d764..fc4ca12 100644
--- a/cps-service/pom.xml
+++ b/cps-service/pom.xml
@@ -38,6 +38,14 @@
       <artifactId>lombok</artifactId>

     </dependency>

     <dependency>

+      <groupId>org.springframework.boot</groupId>

+      <artifactId>spring-boot-starter-cache</artifactId>

+    </dependency>

+    <dependency>

+      <groupId>com.github.ben-manes.caffeine</groupId>

+      <artifactId>caffeine</artifactId>

+    </dependency>

+    <dependency>

       <!-- For logging -->

       <groupId>org.slf4j</groupId>

       <artifactId>slf4j-api</artifactId>

@@ -64,6 +72,16 @@
       <scope>test</scope>

     </dependency>

     <dependency>

+      <groupId>org.spockframework</groupId>

+      <artifactId>spock-spring</artifactId>

+      <scope>test</scope>

+    </dependency>

+    <dependency>

+      <groupId>org.springframework.boot</groupId>

+      <artifactId>spring-boot-starter-test</artifactId>

+      <scope>test</scope>

+    </dependency>

+    <dependency>

       <groupId>cglib</groupId>

       <artifactId>cglib-nodep</artifactId>

       <scope>test</scope>

diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java
index 0cd1b20..427ddd6 100644
--- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java
+++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java
@@ -27,34 +27,32 @@
 import org.onap.cps.yang.YangTextSchemaSourceSet;
 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
 
-@Component("CpsModuleServiceImpl")
+@Service("CpsModuleServiceImpl")
 public class CpsModuleServiceImpl implements CpsModuleService {
 
     @Autowired
     private CpsModulePersistenceService cpsModulePersistenceService;
 
+    @Autowired
+    private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
+
     @Override
     public void createSchemaSet(final String dataspaceName, final String schemaSetName,
-                                final Map<String, String> yangResourcesNameToContentMap) {
-
-        YangTextSchemaSourceSetBuilder.validate(yangResourcesNameToContentMap);
-        cpsModulePersistenceService
-            .storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
+            final Map<String, String> yangResourcesNameToContentMap) {
+        final YangTextSchemaSourceSet yangTextSchemaSourceSet
+                = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap);
+        cpsModulePersistenceService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap);
+        yangTextSchemaSourceSetCache.updateCache(dataspaceName, schemaSetName, yangTextSchemaSourceSet);
     }
 
     @Override
     public SchemaSet getSchemaSet(final String dataspaceName, final String schemaSetName) {
-        final Map<String, String> yangResourceNameToContent =
-                cpsModulePersistenceService.getYangSchemaResources(dataspaceName, schemaSetName);
-        final YangTextSchemaSourceSet yangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder
-                                                                        .of(yangResourceNameToContent);
-        return SchemaSet.builder()
-                       .name(schemaSetName)
-                       .dataspaceName(dataspaceName)
-                       .moduleReferences(yangTextSchemaSourceSet.getModuleReferences())
-                .build();
+        final YangTextSchemaSourceSet yangTextSchemaSourceSet = yangTextSchemaSourceSetCache
+                                                                        .get(dataspaceName, schemaSetName);
+        return SchemaSet.builder().name(schemaSetName).dataspaceName(dataspaceName)
+                       .moduleReferences(yangTextSchemaSourceSet.getModuleReferences()).build();
     }
 
     @Override
diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/YangTextSchemaSourceSetCache.java b/cps-service/src/main/java/org/onap/cps/api/impl/YangTextSchemaSourceSetCache.java
new file mode 100644
index 0000000..af16727
--- /dev/null
+++ b/cps-service/src/main/java/org/onap/cps/api/impl/YangTextSchemaSourceSetCache.java
@@ -0,0 +1,70 @@
+package org.onap.cps.api.impl;
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Pantheon.tech
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.util.Map;
+import org.onap.cps.spi.CpsModulePersistenceService;
+import org.onap.cps.yang.YangTextSchemaSourceSet;
+import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CachePut;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Service;
+
+/**
+ * Provides cached YangTextSchemaSourceSet.
+ */
+@Service
+@CacheConfig(cacheNames = {"yangSchema"})
+public class YangTextSchemaSourceSetCache {
+
+    @Autowired
+    private CpsModulePersistenceService cpsModulePersistenceService;
+
+    /**
+     * Cache YangTextSchemaSourceSet.
+     *
+     * @param dataspaceName dataspace name
+     * @param schemaSetName schema set name
+     * @return YangTextSchemaSourceSet
+     */
+    @Cacheable(key = "#p0.concat('-').concat(#p1)")
+    public YangTextSchemaSourceSet get(final String dataspaceName, final String schemaSetName) {
+        final Map<String, String> yangResourceNameToContent =
+                cpsModulePersistenceService.getYangSchemaResources(dataspaceName, schemaSetName);
+        return YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent);
+    }
+
+    /**
+     * Updates cache YangTextSchemaSourceSet.
+     *
+     * @param dataspaceName dataspace name
+     * @param schemaSetName schema set name
+     * @param yangTextSchemaSourceSet yangTextSchemaSourceSet
+     * @return YangTextSchemaSourceSet
+     */
+    @CachePut(key = "#p0.concat('-').concat(#p1)")
+    @CanIgnoreReturnValue
+    public YangTextSchemaSourceSet updateCache(final String dataspaceName, final String schemaSetName,
+            final YangTextSchemaSourceSet yangTextSchemaSourceSet) {
+        return yangTextSchemaSourceSet;
+    }
+}
diff --git a/cps-service/src/main/java/org/onap/cps/config/CacheConfig.java b/cps-service/src/main/java/org/onap/cps/config/CacheConfig.java
new file mode 100644
index 0000000..4441e4f
--- /dev/null
+++ b/cps-service/src/main/java/org/onap/cps/config/CacheConfig.java
@@ -0,0 +1,29 @@
+package org.onap.cps.config;
+
+/*
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2020 Pantheon.tech
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@EnableCaching
+public class CacheConfig {
+
+}
\ No newline at end of file
diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy
index f380d10..5f2168a 100644
--- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy
@@ -21,23 +21,35 @@
 package org.onap.cps.api.impl
 
 import org.onap.cps.TestUtils
-import org.onap.cps.spi.CascadeDeleteAllowed
-import org.onap.cps.spi.CpsModulePersistenceService;
+import org.onap.cps.api.CpsAdminService
+import org.onap.cps.spi.CpsModulePersistenceService
 import org.onap.cps.spi.exceptions.ModelValidationException
 import org.onap.cps.spi.model.ModuleReference
+import org.spockframework.spring.SpringBean
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.test.context.SpringBootTest
+import org.springframework.cache.CacheManager
+import org.springframework.cache.caffeine.CaffeineCacheManager
+import org.springframework.context.annotation.ComponentScan
+import org.springframework.test.context.ContextConfiguration
 import spock.lang.Specification
 import spock.lang.Unroll
 
 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
 
+@SpringBootTest
+@ComponentScan("org.onap.cps")
+@ContextConfiguration(classes = CpsModuleServiceImplSpec.class)
 class CpsModuleServiceImplSpec extends Specification {
-    def mockModuleStoreService = Mock(CpsModulePersistenceService)
-    def objectUnderTest = new CpsModuleServiceImpl()
-
-    def setup() {
-        objectUnderTest.cpsModulePersistenceService = mockModuleStoreService
-    }
+    @SpringBean
+    CpsModulePersistenceService mockModuleStoreService = Mock()
+    @SpringBean
+    CpsAdminService mockCpsAdminService = Mock()
+    @Autowired
+    CpsModuleServiceImpl objectUnderTest = new CpsModuleServiceImpl()
+    @SpringBean
+    CacheManager cacheManager = new CaffeineCacheManager("yangSchema");
 
     def 'Create schema set'() {
         given: 'Valid yang resource as name-to-content map'
@@ -69,6 +81,17 @@
             result.getModuleReferences().contains(new ModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15'))
     }
 
+    def 'Schema set caching.'() {
+        given: 'an  schema set'
+            def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
+        when: 'get schema set method is invoked twice'
+            2.times {
+                objectUnderTest.getSchemaSet('someDataspace', 'someSchemaSet')
+            }
+        then: 'the persistency service called only once'
+            1 * mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap
+    }
+
     @Unroll
     def 'Delete set by name and dataspace with #cascadeDeleteOption.'(){
         when: 'schema set deletion is requested'
diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy
index 22dc39a..d6751bb 100755
--- a/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy
@@ -24,13 +24,14 @@
 import org.onap.cps.spi.CpsModulePersistenceService

 import spock.lang.Specification

 

-

 class E2ENetworkSliceSpec extends Specification {

     def mockModuleStoreService = Mock(CpsModulePersistenceService)

+    def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)

     def objectUnderTest = new CpsModuleServiceImpl()

 

     def setup() {

         objectUnderTest.cpsModulePersistenceService = mockModuleStoreService

+        objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache

     }

 

     def 'E2E model can be parsed by CPS.'() {