Ensure prefix is correct module prefix

- Moved Prefix logic from RI to Service layer
- Prefix is no PREFIX propety, not the moduel name!
== RI (DB Layer) Changes
- Removed prefix logic incl hazelcast
- Added new basic ri-test for getDataNode and assert prefix is null
- Updated exsiting ri-test to us getDataNode
- Updated existing ri-test to only use " where really needed
== CPS Service Layer Changes
- Introduced PrefixResolver with clear and limited responsibility
- Use PrefixResolver where needed
- Cache prefix map per anchor, use cached entry when available
- Disabled SONAR on new Regex
== REST Layer
- Use PrefixResolver where needed

Issue-ID: CPS-1353
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Change-Id: Ie16f0e1ee1c280f3eb69c9e64fab69a780fb692a
diff --git a/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy
new file mode 100644
index 0000000..839444b
--- /dev/null
+++ b/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy
@@ -0,0 +1,52 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Nordix Foundation
+ *  ================================================================================
+ *  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=========================================================
+ */
+
+package org.onap.cps.cache
+import com.hazelcast.core.Hazelcast
+import com.hazelcast.map.IMap
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.test.context.SpringBootTest
+import org.springframework.test.context.ContextConfiguration
+import spock.lang.Specification
+
+@SpringBootTest
+@ContextConfiguration(classes = [AnchorDataCacheConfig])
+class AnchorDataCacheConfigSpec extends Specification {
+
+    @Autowired
+    private IMap<String, AnchorDataCacheEntry> anchorDataCache
+
+    def 'Embedded (hazelcast) cache for Anchor Data.'() {
+        expect: 'system is able to create an instance of the Anchor data cache'
+            assert null != anchorDataCache
+        and: 'there is at least 1 instance'
+            assert Hazelcast.allHazelcastInstances.size() > 0
+        and: 'anchorDataCache is present'
+            assert Hazelcast.allHazelcastInstances.name.contains('hazelCastInstanceCpsCore')
+    }
+
+    def 'Verify configs for Distributed Caches'(){
+        given: 'the Anchor Data Cache config'
+            def anchorDataCacheConfig =  Hazelcast.getHazelcastInstanceByName('hazelCastInstanceCpsCore').config.mapConfigs.get('anchorDataCacheMapConfig')
+        expect: 'system created instance with correct config'
+            assert anchorDataCacheConfig.backupCount == 3
+            assert anchorDataCacheConfig.asyncBackupCount == 3
+    }
+}
diff --git a/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheEntrySpec.groovy b/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheEntrySpec.groovy
new file mode 100644
index 0000000..f38b45d
--- /dev/null
+++ b/cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheEntrySpec.groovy
@@ -0,0 +1,40 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Nordix Foundation
+ *  ================================================================================
+ *  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=========================================================
+ */
+
+package org.onap.cps.cache
+
+
+import spock.lang.Specification
+
+class AnchorDataCacheEntrySpec extends Specification {
+
+    def objectUnderTest = new AnchorDataCacheEntry()
+
+    def 'Anchor Data Cache Properties Management.'() {
+        when: 'a property named "sample" is added to the cache'
+            objectUnderTest.setProperty('sample', 123)
+        then: 'the cache has that property'
+            assert objectUnderTest.hasProperty('sample')
+        and: 'the value is correct'
+            assert objectUnderTest.getProperty('sample') == 123
+        and: 'the cache does not have an an object called "something else"'
+            assert objectUnderTest.hasProperty('something else') == false
+    }
+}
diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy
index 682197d..6f9a148 100644
--- a/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy
@@ -1,6 +1,7 @@
 /*
- * ============LICENSE_START=======================================================
- * Copyright (c) 2021-2022 Bell Canada.
+ *  ============LICENSE_START=======================================================
+ *  Copyright (c) 2021-2022 Bell Canada.
+ *  Modifications Copyright (c) 2022 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -23,7 +24,7 @@
 import java.time.OffsetDateTime
 import java.time.format.DateTimeFormatter
 import org.onap.cps.utils.DateTimeUtility
-import org.onap.cps.api.CpsAdminService
+import org.onap.cps.utils.PrefixResolver
 import org.onap.cps.api.CpsDataService
 import org.onap.cps.event.model.Content
 import org.onap.cps.event.model.Data
@@ -37,7 +38,9 @@
 
     def mockCpsDataService = Mock(CpsDataService)
 
-    def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService)
+    def mockPrefixResolver = Mock(PrefixResolver)
+
+    def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockPrefixResolver)
 
     def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'
 
diff --git a/cps-service/src/test/groovy/org/onap/cps/spi/model/DataNodeBuilderSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/spi/model/DataNodeBuilderSpec.groovy
index 16d4efc..fcfb482 100644
--- a/cps-service/src/test/groovy/org/onap/cps/spi/model/DataNodeBuilderSpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/spi/model/DataNodeBuilderSpec.groovy
@@ -176,12 +176,11 @@
     def 'Use of adding the module name prefix attribute of data node.'() {
         when: 'data node is built with a prefix'
             def testDataNode = new DataNodeBuilder()
-                    .withModuleNamePrefix('sampleModuleNamePrefix')
                     .withXpath(xPath)
                     .withLeaves(sampleLeaves)
                     .build()
         then: 'the result when node request is a #scenario includes the correct prefix'
-            def result = new DataMapUtils().toDataMapWithIdentifier(testDataNode)
+            def result = new DataMapUtils().toDataMapWithIdentifier(testDataNode, 'sampleModuleNamePrefix')
             result.toString() == expectedResult
         where: 'the following parameters are used'
             scenario          | xPath                                       | sampleLeaves                   | expectedResult
diff --git a/cps-service/src/test/groovy/org/onap/cps/utils/DataMapUtilsSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/utils/DataMapUtilsSpec.groovy
index 7f2c638..84dddeb 100644
--- a/cps-service/src/test/groovy/org/onap/cps/utils/DataMapUtilsSpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/utils/DataMapUtilsSpec.groovy
@@ -65,11 +65,7 @@
 
     def 'Data node structure conversion to map with root node identifier.'() {
         when: 'data node structure is converted to a map with root node identifier'
-            def result = DataMapUtils.toDataMapWithIdentifier(dataNode)
-
-        then: 'root node identifier is not null'
-            result.parent != null
-
+            def result = DataMapUtils.toDataMapWithIdentifier(dataNode,dataNode.moduleNamePrefix)
         then: 'root node leaves are populated under its node identifier'
             def parentNode = result.parent
             parentNode.parentLeaf == 'parentLeafValue'
diff --git a/cps-service/src/test/groovy/org/onap/cps/utils/PrefixResolverSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/utils/PrefixResolverSpec.groovy
new file mode 100644
index 0000000..4c1b891
--- /dev/null
+++ b/cps-service/src/test/groovy/org/onap/cps/utils/PrefixResolverSpec.groovy
@@ -0,0 +1,117 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2021-2022 Nordix Foundation
+ *  Modifications Copyright (C) 2021 Pantheon.tech
+ *  Modifications Copyright (C) 2021-2022 Bell Canada.
+ *  ================================================================================
+ *  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=========================================================
+ */
+
+package org.onap.cps.utils
+
+import com.hazelcast.map.IMap
+import org.onap.cps.TestUtils
+import org.onap.cps.api.CpsAdminService
+import org.onap.cps.api.impl.YangTextSchemaSourceSetCache
+import org.onap.cps.cache.AnchorDataCacheEntry
+import org.onap.cps.spi.model.Anchor
+import org.onap.cps.yang.YangTextSchemaSourceSet
+import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
+import spock.lang.Specification
+
+class PrefixResolverSpec extends Specification {
+
+    def mockCpsAdminService = Mock(CpsAdminService)
+
+    def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
+
+    def mockAnchorDataCache = Mock(IMap<String, AnchorDataCacheEntry>)
+
+    def objectUnderTest = new PrefixResolver(mockCpsAdminService, mockYangTextSchemaSourceSetCache, mockAnchorDataCache)
+
+    def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
+
+    def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
+
+    def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
+
+    def setup() {
+        given: 'an anchor for the test-tree model'
+            def anchor = new Anchor(dataspaceName: 'testDataspace', name: 'testAnchor')
+        and: 'the system can get this anchor'
+            mockCpsAdminService.getAnchor('testDataspace', 'testAnchor') >> anchor
+        and: 'the schema source cache contains the schema context for the test-tree module'
+            mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext
+    }
+
+    def 'get xpath prefix using node schema context'() {
+        when: 'the prefix of the yang module is retrieved'
+            def result = objectUnderTest.getPrefix('testDataspace', 'testAnchor', xpath)
+        then: 'the expected prefix is returned'
+            result == expectedPrefix
+        and: 'the cache is updated for the given anchor with a map of prefixes per top level container (just one one this case)'
+            1 * mockAnchorDataCache.put('testAnchor',_ , _ ,_) >> { args -> {
+                def prefixPerContainerName = args[1].getProperty("prefixPerContainerName")
+                assert prefixPerContainerName.size() == 1
+                assert prefixPerContainerName.get('test-tree') == 'tree'
+                }
+            }
+        and: 'schema source cache is used (i.e. need to build schema context)'
+            1 * mockYangTextSchemaSourceSetCache.get(*_) >> mockYangTextSchemaSourceSet
+        where: 'the following scenarios are applied'
+            xpath                         || expectedPrefix
+            '/test-tree'                  || 'tree'
+            '/test-tree/with/descendants' || 'tree'
+            '/test-tree[@id=1]'           || 'tree'
+            '/test-tree[@id=1]/child'     || 'tree'
+            '/not-defined'                || ''
+            'invalid-xpath'               || ''
+    }
+
+    def 'get prefix with populated anchor data cache with #scenario cache entry'() {
+        given: 'anchor data cache is populated for the anchor with a prefix for top level container named #cachedTopLevelContainerName'
+            def anchorDataCacheEntry = new AnchorDataCacheEntry()
+            def prefixPerContainerName = [(cachedTopLevelContainerName): 'cachedPrefix']
+            anchorDataCacheEntry.setProperty('prefixPerContainerName',prefixPerContainerName)
+            mockAnchorDataCache.containsKey('testAnchor') >> true
+            mockAnchorDataCache.get('testAnchor') >> anchorDataCacheEntry
+        when: 'the prefix of the yang module is retrieved'
+            def result = objectUnderTest.getPrefix('testDataspace', 'testAnchor', '/test-tree')
+        then: 'the expected prefix is returned'
+            result == expectedPrefix
+        and: 'schema source cache is not used (i.e. no need to build schema context)'
+            0 * mockYangTextSchemaSourceSetCache.get(*_)
+        where: 'the following scenarios are applied'
+            scenario       | cachedTopLevelContainerName || expectedPrefix
+            'matching'     | 'test-tree'                 || 'cachedPrefix'
+            'non-matching' | 'other'                     || ''
+    }
+
+    def 'get prefix with other (non relevant) data in anchor data cache'() {
+        given: 'anchor data cache is populated with non relevant other property'
+            def anchorDataCacheEntry = new AnchorDataCacheEntry()
+            anchorDataCacheEntry.setProperty('something else', 'does not matter')
+            mockAnchorDataCache.containsKey('testAnchor') >> true
+            mockAnchorDataCache.get('testAnchor') >> anchorDataCacheEntry
+        when: 'the prefix of the yang module is retrieved'
+            def result = objectUnderTest.getPrefix('testDataspace', 'testAnchor', '/test-tree')
+        then: 'the expected prefix is returned'
+            result == 'tree'
+        and: 'schema source cache is used (i.e. need to build schema context)'
+            1 * mockYangTextSchemaSourceSetCache.get(*_) >> mockYangTextSchemaSourceSet
+    }
+
+}
diff --git a/cps-service/src/test/resources/test-tree.yang b/cps-service/src/test/resources/test-tree.yang
index 6310065..a9f7e22 100644
--- a/cps-service/src/test/resources/test-tree.yang
+++ b/cps-service/src/test/resources/test-tree.yang
@@ -33,4 +33,7 @@
 
         }
     }
+    leaf topLevelLeafAddedForBranchCoverageInPrefixResolverSpec {
+        type string;
+    }
 }