2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.utils;
23 import com.hazelcast.map.IMap;
24 import java.io.Serializable;
25 import java.util.HashMap;
27 import java.util.concurrent.TimeUnit;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import lombok.RequiredArgsConstructor;
31 import org.onap.cps.api.CpsAdminService;
32 import org.onap.cps.api.impl.YangTextSchemaSourceSetCache;
33 import org.onap.cps.cache.AnchorDataCacheEntry;
34 import org.onap.cps.spi.model.Anchor;
35 import org.onap.cps.yang.YangTextSchemaSourceSet;
36 import org.opendaylight.yangtools.yang.common.QNameModule;
37 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.springframework.stereotype.Component;
44 @RequiredArgsConstructor
45 public class PrefixResolver {
46 private static final long ANCHOR_DATA_CACHE_TTL_SECS = TimeUnit.HOURS.toSeconds(1);
48 private static final String CACHE_ENTRY_PROPERTY_NAME = "prefixPerContainerName";
50 private final CpsAdminService cpsAdminService;
52 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
54 private final IMap<String, AnchorDataCacheEntry> anchorDataCache;
56 private static final Pattern TOP_LEVEL_NODE_NAME_FINDER
57 = Pattern.compile("\\/([\\w-]*)(\\[@(?!.*\\[).*?])?(\\/.*)?"); //NOSONAR
60 * Get the module prefix for the given xpath for a dataspace and anchor name.
62 * @param dataspaceName the name of the dataspace
63 * @param anchorName the name of the anchor the xpath belongs to
64 * @param xpath the xpath to prefix a prefix for
65 * @return the prefix of the module the top level element of given xpath
67 public String getPrefix(final String dataspaceName, final String anchorName, final String xpath) {
68 final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
69 return getPrefix(anchor, xpath);
73 * Get the module prefix for the given xpath under the given anchor.
75 * @param anchor the anchor the xpath belong to
76 * @param xpath the xpath to prefix a prefix for
77 * @return the prefix of the module the top level element of given xpath
79 public String getPrefix(final Anchor anchor, final String xpath) {
80 final Map<String, String> prefixPerContainerName = getPrefixPerContainerName(anchor);
81 return getPrefixForTopContainer(prefixPerContainerName, xpath);
84 private Map<String, String> getPrefixPerContainerName(final Anchor anchor) {
85 if (anchorDataCache.containsKey(anchor.getName())) {
86 final AnchorDataCacheEntry anchorDataCacheEntry = anchorDataCache.get(anchor.getName());
87 if (anchorDataCacheEntry.hasProperty(CACHE_ENTRY_PROPERTY_NAME)) {
88 return (Map) anchorDataCacheEntry.getProperty(CACHE_ENTRY_PROPERTY_NAME);
91 return createAndCachePrefixPerContainerNameMap(anchor);
94 private String getPrefixForTopContainer(final Map<String, String> prefixPerContainerName,
96 final Matcher matcher = TOP_LEVEL_NODE_NAME_FINDER.matcher(xpath);
97 if (matcher.matches()) {
98 final String topLevelContainerName = matcher.group(1);
99 if (prefixPerContainerName.containsKey(topLevelContainerName)) {
100 return prefixPerContainerName.get(topLevelContainerName);
106 private Map<String, String> createAndCachePrefixPerContainerNameMap(final Anchor anchor) {
107 final YangTextSchemaSourceSet yangTextSchemaSourceSet =
108 yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(), anchor.getSchemaSetName());
109 final SchemaContext schemaContext = yangTextSchemaSourceSet.getSchemaContext();
110 final Map<QNameModule, String> prefixPerQNameModule = new HashMap<>(schemaContext.getModules().size());
111 for (final Module module : schemaContext.getModules()) {
112 prefixPerQNameModule.put(module.getQNameModule(), module.getPrefix());
114 final HashMap<String, String> prefixPerContainerName = new HashMap<>();
115 for (final DataSchemaNode dataSchemaNode : schemaContext.getChildNodes()) {
116 if (dataSchemaNode instanceof DataNodeContainer) {
117 final String containerName = dataSchemaNode.getQName().getLocalName();
118 final String prefix = prefixPerQNameModule.get(dataSchemaNode.getQName().getModule());
119 prefixPerContainerName.put(containerName, prefix);
122 cachePrefixPerContainerNameMap(anchor.getName(), prefixPerContainerName);
123 return prefixPerContainerName;
126 private void cachePrefixPerContainerNameMap(final String anchorName,
127 final Serializable prefixPerContainerName) {
128 final AnchorDataCacheEntry anchorDataCacheEntry = new AnchorDataCacheEntry();
129 anchorDataCacheEntry.setProperty(CACHE_ENTRY_PROPERTY_NAME, prefixPerContainerName);
130 anchorDataCache.put(anchorName, anchorDataCacheEntry, ANCHOR_DATA_CACHE_TTL_SECS, TimeUnit.SECONDS);