2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 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 lombok.RequiredArgsConstructor;
29 import org.onap.cps.api.CpsAnchorService;
30 import org.onap.cps.api.impl.YangTextSchemaSourceSetCache;
31 import org.onap.cps.cache.AnchorDataCacheEntry;
32 import org.onap.cps.cpspath.parser.CpsPathPrefixType;
33 import org.onap.cps.cpspath.parser.CpsPathQuery;
34 import org.onap.cps.cpspath.parser.CpsPathUtil;
35 import org.onap.cps.spi.model.Anchor;
36 import org.onap.cps.yang.YangTextSchemaSourceSet;
37 import org.opendaylight.yangtools.yang.common.QNameModule;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.springframework.stereotype.Component;
45 @RequiredArgsConstructor
46 public class PrefixResolver {
47 private static final long ANCHOR_DATA_CACHE_TTL_SECS = TimeUnit.HOURS.toSeconds(1);
49 private static final String CACHE_ENTRY_PROPERTY_NAME = "prefixPerContainerName";
51 private final CpsAnchorService cpsAnchorService;
53 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
55 private final IMap<String, AnchorDataCacheEntry> anchorDataCache;
58 * Get the module prefix for the given xpath for a dataspace and anchor name.
60 * @param dataspaceName the name of the dataspace
61 * @param anchorName the name of the anchor the xpath belongs to
62 * @param xpath the xpath to prefix a prefix for
63 * @return the prefix of the module the top level element of given xpath
65 public String getPrefix(final String dataspaceName, final String anchorName, final String xpath) {
66 final Anchor anchor = cpsAnchorService.getAnchor(dataspaceName, anchorName);
67 return getPrefix(anchor, xpath);
71 * Get the module prefix for the given xpath under the given anchor.
73 * @param anchor the anchor the xpath belong to
74 * @param xpath the xpath to prefix a prefix for
75 * @return the prefix of the module the top level element of given xpath
77 public String getPrefix(final Anchor anchor, final String xpath) {
78 final Map<String, String> prefixPerContainerName = getPrefixPerContainerName(anchor);
79 return getPrefixForTopContainer(prefixPerContainerName, xpath);
82 private Map<String, String> getPrefixPerContainerName(final Anchor anchor) {
83 if (anchorDataCache.containsKey(anchor.getName())) {
84 final AnchorDataCacheEntry anchorDataCacheEntry = anchorDataCache.get(anchor.getName());
85 if (anchorDataCacheEntry.hasProperty(CACHE_ENTRY_PROPERTY_NAME)) {
86 return (Map) anchorDataCacheEntry.getProperty(CACHE_ENTRY_PROPERTY_NAME);
89 return createAndCachePrefixPerContainerNameMap(anchor);
92 private String getPrefixForTopContainer(final Map<String, String> prefixPerContainerName,
94 final CpsPathQuery cpsPathQuery = CpsPathUtil.getCpsPathQuery(xpath);
95 if (cpsPathQuery.getCpsPathPrefixType() == CpsPathPrefixType.ABSOLUTE) {
96 final String topLevelContainerName = cpsPathQuery.getContainerNames().get(0);
97 if (prefixPerContainerName.containsKey(topLevelContainerName)) {
98 return prefixPerContainerName.get(topLevelContainerName);
104 private Map<String, String> createAndCachePrefixPerContainerNameMap(final Anchor anchor) {
105 final YangTextSchemaSourceSet yangTextSchemaSourceSet =
106 yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(), anchor.getSchemaSetName());
107 final SchemaContext schemaContext = yangTextSchemaSourceSet.getSchemaContext();
108 final Map<QNameModule, String> prefixPerQNameModule = new HashMap<>(schemaContext.getModules().size());
109 for (final Module module : schemaContext.getModules()) {
110 prefixPerQNameModule.put(module.getQNameModule(), module.getPrefix());
112 final HashMap<String, String> prefixPerContainerName = new HashMap<>();
113 for (final DataSchemaNode dataSchemaNode : schemaContext.getChildNodes()) {
114 if (dataSchemaNode instanceof DataNodeContainer) {
115 final String containerName = dataSchemaNode.getQName().getLocalName();
116 final String prefix = prefixPerQNameModule.get(dataSchemaNode.getQName().getModule());
117 prefixPerContainerName.put(containerName, prefix);
120 cachePrefixPerContainerNameMap(anchor.getName(), prefixPerContainerName);
121 return prefixPerContainerName;
124 private void cachePrefixPerContainerNameMap(final String anchorName,
125 final Serializable prefixPerContainerName) {
126 final AnchorDataCacheEntry anchorDataCacheEntry = new AnchorDataCacheEntry();
127 anchorDataCacheEntry.setProperty(CACHE_ENTRY_PROPERTY_NAME, prefixPerContainerName);
128 anchorDataCache.put(anchorName, anchorDataCacheEntry, ANCHOR_DATA_CACHE_TTL_SECS, TimeUnit.SECONDS);