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.ncmp.api.impl;
23 import static org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServicePropertyHandler.PropertyType.DMI_PROPERTY;
24 import static org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServicePropertyHandler.PropertyType.PUBLIC_PROPERTY;
25 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME;
26 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR;
27 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_PARENT;
28 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;
30 import com.google.common.collect.ImmutableMap;
31 import java.util.Collection;
32 import java.util.HashSet;
33 import java.util.LinkedHashMap;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37 import lombok.RequiredArgsConstructor;
38 import lombok.extern.slf4j.Slf4j;
39 import org.onap.cps.api.CpsDataService;
40 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
41 import org.onap.cps.spi.FetchDescendantsOption;
42 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
43 import org.onap.cps.spi.model.DataNode;
44 import org.onap.cps.spi.model.DataNodeBuilder;
45 import org.springframework.stereotype.Service;
49 @RequiredArgsConstructor
50 //Accepting the security hotspot as the string checked is generated from inside code and not user input.
51 @SuppressWarnings("squid:S5852")
52 public class NetworkCmProxyDataServicePropertyHandler {
54 private static final String CM_HANDLE_XPATH_TEMPLATE = NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='%s']";
56 private final CpsDataService cpsDataService;
59 * Iterates over incoming ncmpServiceCmHandles and update the dataNodes based on the updated attributes.
60 * The attributes which are not passed will remain as is.
62 * @param ncmpServiceCmHandles collection of ncmpServiceCmHandles
64 public void updateCmHandleProperties(final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles)
65 throws DataNodeNotFoundException {
66 for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
68 final String cmHandleXpath = String.format(CM_HANDLE_XPATH_TEMPLATE,
69 ncmpServiceCmHandle.getCmHandleID());
70 final DataNode existingCmHandleDataNode =
71 cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cmHandleXpath,
72 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
73 processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle);
74 } catch (final DataNodeNotFoundException e) {
75 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
76 ncmpServiceCmHandle.getCmHandleID(),
83 private void processUpdates(final DataNode existingCmHandleDataNode, final NcmpServiceCmHandle incomingCmHandle) {
84 if (!incomingCmHandle.getPublicProperties().isEmpty()) {
85 updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY, incomingCmHandle.getPublicProperties());
87 if (!incomingCmHandle.getDmiProperties().isEmpty()) {
88 updateProperties(existingCmHandleDataNode, DMI_PROPERTY, incomingCmHandle.getDmiProperties());
92 private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
93 final Map<String, String> incomingProperties) {
94 final Collection<DataNode> replacementPropertyDataNodes =
95 getReplacementDataNodes(existingCmHandleDataNode, propertyType, incomingProperties);
96 replacementPropertyDataNodes.addAll(
97 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, incomingProperties));
98 if (replacementPropertyDataNodes.isEmpty()) {
99 removeAllProperties(existingCmHandleDataNode, propertyType);
101 cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
102 existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes, NO_TIMESTAMP);
106 private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
107 existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
108 final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
109 if (matcher.find()) {
110 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
111 cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNode.getXpath(),
117 private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
118 final PropertyType propertyType, final Map<String, String> incomingProperties) {
119 final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
120 for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
121 final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
122 if (matcher.find()) {
123 final String keyName = matcher.group(2);
124 if (!incomingProperties.containsKey(keyName)) {
125 unchangedPropertyDataNodes.add(existingPropertyDataNode);
129 return unchangedPropertyDataNodes;
132 private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
133 final PropertyType propertyType, final Map<String, String> incomingProperties) {
134 final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
135 incomingProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
136 final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
137 if (updatedAttributeValue != null) {
138 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
139 updatedAttributeKey, updatedAttributeValue);
140 replacementPropertyDataNodes.add(
141 buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
144 return replacementPropertyDataNodes;
147 private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
148 final String attributeKey) {
149 return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
152 private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
153 final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
154 updatedLeaves.put("name", attributeKey);
155 updatedLeaves.put("value", attributeValue);
156 log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
158 return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
162 DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
164 private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
166 final String xpathPrefix;
167 final Pattern propertyXpathPattern;
169 PropertyType(final String xpathPrefix) {
170 this.xpathPrefix = xpathPrefix;
171 this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);