summaryrefslogtreecommitdiffstats
path: root/cps-service
diff options
context:
space:
mode:
authorToineSiebelink <toine.siebelink@est.tech>2022-10-27 17:29:04 +0100
committerToineSiebelink <toine.siebelink@est.tech>2022-11-02 09:00:41 +0000
commit14e5bf958bb2b114e3e7d8876bd6f031900c79ec (patch)
treea72bcfee795ab6e27938e5e265327d6aace804e3 /cps-service
parenta096a7faa35b345c765102201a5a09cc03ef541a (diff)
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
Diffstat (limited to 'cps-service')
-rw-r--r--cps-service/pom.xml5
-rw-r--r--cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java70
-rw-r--r--cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheEntry.java44
-rw-r--r--cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java14
-rw-r--r--cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java3
-rw-r--r--cps-service/src/main/java/org/onap/cps/utils/DataMapUtils.java7
-rw-r--r--cps-service/src/main/java/org/onap/cps/utils/PrefixResolver.java133
-rw-r--r--cps-service/src/main/java/org/onap/cps/utils/YangUtils.java4
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheConfigSpec.groovy52
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/cache/AnchorDataCacheEntrySpec.groovy40
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy11
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/spi/model/DataNodeBuilderSpec.groovy3
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/utils/DataMapUtilsSpec.groovy6
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/utils/PrefixResolverSpec.groovy117
-rw-r--r--cps-service/src/test/resources/test-tree.yang3
15 files changed, 489 insertions, 23 deletions
diff --git a/cps-service/pom.xml b/cps-service/pom.xml
index f47a963154..4a98cc27d8 100644
--- a/cps-service/pom.xml
+++ b/cps-service/pom.xml
@@ -110,6 +110,11 @@
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
</dependency>
+ <dependency>
+ <!-- Hazelcast provide Distributed Caches -->
+ <groupId>com.hazelcast</groupId>
+ <artifactId>hazelcast-spring</artifactId>
+ </dependency>
<!-- T E S T D E P E N D E N C I E S -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
diff --git a/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java b/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java
new file mode 100644
index 0000000000..54d6ff3953
--- /dev/null
+++ b/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheConfig.java
@@ -0,0 +1,70 @@
+/*
+ * ============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.config.Config;
+import com.hazelcast.config.MapConfig;
+import com.hazelcast.config.NamedConfig;
+import com.hazelcast.core.Hazelcast;
+import com.hazelcast.core.HazelcastInstance;
+import com.hazelcast.map.IMap;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * Core infrastructure of the hazelcast distributed cache for anchor data config use cases.
+ */
+@Configuration
+public class AnchorDataCacheConfig {
+
+ private static final MapConfig anchorDataCacheMapConfig = createMapConfig("anchorDataCacheMapConfig");
+
+ /**
+ * Distributed instance of anchor data cache that contains module prefix by anchor name as properties.
+ *
+ * @return configured map of anchor data cache
+ */
+ @Bean
+ public IMap<String, AnchorDataCacheEntry> anchorDataCache() {
+ return createHazelcastInstance("hazelCastInstanceCpsCore", anchorDataCacheMapConfig)
+ .getMap("anchorDataCache");
+ }
+
+ private HazelcastInstance createHazelcastInstance(final String hazelcastInstanceName,
+ final NamedConfig namedConfig) {
+ return Hazelcast.newHazelcastInstance(initializeConfig(hazelcastInstanceName, namedConfig));
+ }
+
+ private Config initializeConfig(final String instanceName, final NamedConfig namedConfig) {
+ final Config config = new Config(instanceName);
+ config.addMapConfig((MapConfig) namedConfig);
+ config.setClusterName("cps-service-caches");
+ return config;
+ }
+
+ private static MapConfig createMapConfig(final String configName) {
+ final MapConfig mapConfig = new MapConfig(configName);
+ mapConfig.setBackupCount(3);
+ mapConfig.setAsyncBackupCount(3);
+ return mapConfig;
+ }
+
+}
diff --git a/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheEntry.java b/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheEntry.java
new file mode 100644
index 0000000000..41adbdd5dc
--- /dev/null
+++ b/cps-service/src/main/java/org/onap/cps/cache/AnchorDataCacheEntry.java
@@ -0,0 +1,44 @@
+/*
+ * ============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 java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+
+public class AnchorDataCacheEntry implements Serializable {
+
+ private static final long serialVersionUID = 2111243947810370698L;
+
+ private Map<String, Serializable> properties = new HashMap<>();
+
+ public Object getProperty(final String propertyName) {
+ return properties.get(propertyName);
+ }
+
+ public void setProperty(final String propertyName, final Serializable value) {
+ properties.put(propertyName, value);
+ }
+
+ public boolean hasProperty(final String propertyName) {
+ return properties.containsKey(propertyName);
+ }
+}
diff --git a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java
index 1013c13b76..f0cdaee8d6 100644
--- a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java
+++ b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java
@@ -1,6 +1,7 @@
/*
* ============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.
@@ -34,6 +35,7 @@ import org.onap.cps.spi.FetchDescendantsOption;
import org.onap.cps.spi.model.Anchor;
import org.onap.cps.spi.model.DataNode;
import org.onap.cps.utils.DataMapUtils;
+import org.onap.cps.utils.PrefixResolver;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@@ -60,6 +62,9 @@ public class CpsDataUpdatedEventFactory {
@Lazy
private final CpsDataService cpsDataService;
+ @Lazy
+ private final PrefixResolver prefixResolver;
+
/**
* Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used.
*
@@ -87,9 +92,9 @@ public class CpsDataUpdatedEventFactory {
return cpsDataUpdatedEvent;
}
- private Data createData(final DataNode dataNode) {
- final var data = new Data();
- DataMapUtils.toDataMapWithIdentifier(dataNode).forEach(data::setAdditionalProperty);
+ private Data createData(final DataNode dataNode, final String prefix) {
+ final Data data = new Data();
+ DataMapUtils.toDataMapWithIdentifier(dataNode, prefix).forEach(data::setAdditionalProperty);
return data;
}
@@ -103,7 +108,8 @@ public class CpsDataUpdatedEventFactory {
content.withObservedTimestamp(
DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp));
if (dataNode != null) {
- content.withData(createData(dataNode));
+ final String prefix = prefixResolver.getPrefix(anchor, dataNode.getXpath());
+ content.withData(createData(dataNode, prefix));
}
return content;
}
diff --git a/cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java b/cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java
index 19d5adef02..8170db3dad 100644
--- a/cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java
+++ b/cps-service/src/main/java/org/onap/cps/spi/model/DataNode.java
@@ -38,14 +38,13 @@ public class DataNode implements Serializable {
private static final long serialVersionUID = 1482619410918597467L;
- DataNode() { }
+ DataNode() {}
private String dataspace;
private String schemaSetName;
private String anchorName;
private ModuleReference moduleReference;
private String xpath;
- @Setter(AccessLevel.PUBLIC)
private String moduleNamePrefix;
private Map<String, Object> leaves = Collections.emptyMap();
private Collection<String> xpathsChildren;
diff --git a/cps-service/src/main/java/org/onap/cps/utils/DataMapUtils.java b/cps-service/src/main/java/org/onap/cps/utils/DataMapUtils.java
index 4413c6b08b..14641e05e6 100644
--- a/cps-service/src/main/java/org/onap/cps/utils/DataMapUtils.java
+++ b/cps-service/src/main/java/org/onap/cps/utils/DataMapUtils.java
@@ -43,10 +43,9 @@ public class DataMapUtils {
* @param dataNode data node object
* @return a map representing same data with the root node identifier
*/
- public static Map<String, Object> toDataMapWithIdentifier(final DataNode dataNode) {
- return ImmutableMap.<String, Object>builder()
- .put(getNodeIdentifierWithPrefix(dataNode.getXpath(), dataNode.getModuleNamePrefix()), toDataMap(dataNode))
- .build();
+ public static Map<String, Object> toDataMapWithIdentifier(final DataNode dataNode, final String prefix) {
+ final String nodeIdentifierWithPrefix = getNodeIdentifierWithPrefix(dataNode.getXpath(), prefix);
+ return ImmutableMap.<String, Object>builder().put(nodeIdentifierWithPrefix, toDataMap(dataNode)).build();
}
/**
diff --git a/cps-service/src/main/java/org/onap/cps/utils/PrefixResolver.java b/cps-service/src/main/java/org/onap/cps/utils/PrefixResolver.java
new file mode 100644
index 0000000000..58b239c34c
--- /dev/null
+++ b/cps-service/src/main/java/org/onap/cps/utils/PrefixResolver.java
@@ -0,0 +1,133 @@
+/*
+ * ============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.utils;
+
+import com.hazelcast.map.IMap;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import lombok.RequiredArgsConstructor;
+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.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+public class PrefixResolver {
+ private static final long ANCHOR_DATA_CACHE_TTL_SECS = TimeUnit.HOURS.toSeconds(1);
+
+ private static final String CACHE_ENTRY_PROPERTY_NAME = "prefixPerContainerName";
+
+ private final CpsAdminService cpsAdminService;
+
+ private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
+
+ private final IMap<String, AnchorDataCacheEntry> anchorDataCache;
+
+ private static final Pattern TOP_LEVEL_NODE_NAME_FINDER
+ = Pattern.compile("\\/([\\w-]*)(\\[@(?!.*\\[).*?])?(\\/.*)?"); //NOSONAR
+
+ /**
+ * Get the module prefix for the given xpath for a dataspace and anchor name.
+ *
+ * @param dataspaceName the name of the dataspace
+ * @param anchorName the name of the anchor the xpath belongs to
+ * @param xpath the xpath to prefix a prefix for
+ * @return the prefix of the module the top level element of given xpath
+ */
+ public String getPrefix(final String dataspaceName, final String anchorName, final String xpath) {
+ final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
+ return getPrefix(anchor, xpath);
+ }
+
+ /**
+ * Get the module prefix for the given xpath under the given anchor.
+ *
+ * @param anchor the anchor the xpath belong to
+ * @param xpath the xpath to prefix a prefix for
+ * @return the prefix of the module the top level element of given xpath
+ */
+ public String getPrefix(final Anchor anchor, final String xpath) {
+ final Map<String, String> prefixPerContainerName = getPrefixPerContainerName(anchor);
+ return getPrefixForTopContainer(prefixPerContainerName, xpath);
+ }
+
+ private Map<String, String> getPrefixPerContainerName(final Anchor anchor) {
+ if (anchorDataCache.containsKey(anchor.getName())) {
+ final AnchorDataCacheEntry anchorDataCacheEntry = anchorDataCache.get(anchor.getName());
+ if (anchorDataCacheEntry.hasProperty(CACHE_ENTRY_PROPERTY_NAME)) {
+ return (Map) anchorDataCacheEntry.getProperty(CACHE_ENTRY_PROPERTY_NAME);
+ }
+ }
+ return createAndCachePrefixPerContainerNameMap(anchor);
+ }
+
+ private String getPrefixForTopContainer(final Map<String, String> prefixPerContainerName,
+ final String xpath) {
+ final Matcher matcher = TOP_LEVEL_NODE_NAME_FINDER.matcher(xpath);
+ if (matcher.matches()) {
+ final String topLevelContainerName = matcher.group(1);
+ if (prefixPerContainerName.containsKey(topLevelContainerName)) {
+ return prefixPerContainerName.get(topLevelContainerName);
+ }
+ }
+ return "";
+ }
+
+ private Map<String, String> createAndCachePrefixPerContainerNameMap(final Anchor anchor) {
+ final YangTextSchemaSourceSet yangTextSchemaSourceSet =
+ yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(), anchor.getSchemaSetName());
+ final SchemaContext schemaContext = yangTextSchemaSourceSet.getSchemaContext();
+ final Map<QNameModule, String> prefixPerQNameModule = new HashMap<>(schemaContext.getModules().size());
+ for (final Module module : schemaContext.getModules()) {
+ prefixPerQNameModule.put(module.getQNameModule(), module.getPrefix());
+ }
+ final HashMap<String, String> prefixPerContainerName = new HashMap<>();
+ for (final DataSchemaNode dataSchemaNode : schemaContext.getChildNodes()) {
+ if (dataSchemaNode instanceof DataNodeContainer) {
+ final String containerName = dataSchemaNode.getQName().getLocalName();
+ final String prefix = prefixPerQNameModule.get(dataSchemaNode.getQName().getModule());
+ prefixPerContainerName.put(containerName, prefix);
+ }
+ }
+ cachePrefixPerContainerNameMap(anchor.getName(), prefixPerContainerName);
+ return prefixPerContainerName;
+ }
+
+ private void cachePrefixPerContainerNameMap(final String anchorName,
+ final Serializable prefixPerContainerName) {
+ final AnchorDataCacheEntry anchorDataCacheEntry = new AnchorDataCacheEntry();
+ anchorDataCacheEntry.setProperty(CACHE_ENTRY_PROPERTY_NAME, prefixPerContainerName);
+ anchorDataCache.put(anchorName, anchorDataCacheEntry, ANCHOR_DATA_CACHE_TTL_SECS, TimeUnit.SECONDS);
+ }
+
+}
diff --git a/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java b/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java
index 5e1b486aa0..8fcdc4ebdb 100644
--- a/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java
+++ b/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java
@@ -1,6 +1,6 @@
/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2020-2021 Nordix Foundation
+ * Copyright (C) 2020-2022 Nordix Foundation
* Modifications Copyright (C) 2021 Bell Canada.
* Modifications Copyright (C) 2021 Pantheon.tech
* ================================================================================
@@ -53,7 +53,6 @@ public class YangUtils {
private static final String XPATH_DELIMITER_REGEX = "\\/";
private static final String XPATH_NODE_KEY_ATTRIBUTES_REGEX = "\\[.*?\\]";
- //Might cause an issue with [] inside [] in key-values
/**
* Parses jsonData into NormalizedNode according to given schema context.
@@ -125,6 +124,7 @@ public class YangUtils {
return xpathBuilder.toString();
}
+
private static String getKeyAttributesStatement(
final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map(
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 0000000000..839444b680
--- /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 0000000000..f38b45d17c
--- /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 682197d517..6f9a148eb2 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 @@ package org.onap.cps.notification
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 @@ class CpsDataUpdateEventFactorySpec extends Specification {
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 16d4efc273..fcfb4826d9 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 @@ class DataNodeBuilderSpec extends Specification {
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 7f2c638ff5..84dddeb60e 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 @@ class DataMapUtilsSpec extends Specification {
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 0000000000..4c1b891168
--- /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 63100657ef..a9f7e224b7 100644
--- a/cps-service/src/test/resources/test-tree.yang
+++ b/cps-service/src/test/resources/test-tree.yang
@@ -33,4 +33,7 @@ module test-tree {
}
}
+ leaf topLevelLeafAddedForBranchCoverageInPrefixResolverSpec {
+ type string;
+ }
}