diff options
author | ToineSiebelink <toine.siebelink@est.tech> | 2022-10-27 17:29:04 +0100 |
---|---|---|
committer | ToineSiebelink <toine.siebelink@est.tech> | 2022-11-02 09:00:41 +0000 |
commit | 14e5bf958bb2b114e3e7d8876bd6f031900c79ec (patch) | |
tree | a72bcfee795ab6e27938e5e265327d6aace804e3 /cps-service/src/main/java/org | |
parent | a096a7faa35b345c765102201a5a09cc03ef541a (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/src/main/java/org')
7 files changed, 263 insertions, 12 deletions
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( |