2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * Modifications Copyright (C) 2023 TechMahindra Ltd.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.ncmp.api.impl;
25 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND;
26 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID;
27 import static org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServicePropertyHandler.PropertyType.DMI_PROPERTY;
28 import static org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServicePropertyHandler.PropertyType.PUBLIC_PROPERTY;
30 import com.google.common.collect.ImmutableMap;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashSet;
34 import java.util.LinkedHashMap;
35 import java.util.List;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39 import lombok.RequiredArgsConstructor;
40 import lombok.extern.slf4j.Slf4j;
41 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
42 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
43 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
44 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
45 import org.onap.cps.spi.exceptions.DataValidationException;
46 import org.onap.cps.spi.model.DataNode;
47 import org.onap.cps.spi.model.DataNodeBuilder;
48 import org.springframework.stereotype.Service;
52 @RequiredArgsConstructor
53 //Accepting the security hotspot as the string checked is generated from inside code and not user input.
54 @SuppressWarnings("squid:S5852")
55 public class NetworkCmProxyDataServicePropertyHandler {
57 private final InventoryPersistence inventoryPersistence;
60 * Iterates over incoming ncmpServiceCmHandles and update the dataNodes based on the updated attributes.
61 * The attributes which are not passed will remain as is.
63 * @param ncmpServiceCmHandles collection of ncmpServiceCmHandles
65 public List<CmHandleRegistrationResponse> updateCmHandleProperties(
66 final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles) {
67 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
68 for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
69 final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
71 final DataNode existingCmHandleDataNode = inventoryPersistence.getCmHandleDataNode(cmHandleId)
73 processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle);
74 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
75 } catch (final DataNodeNotFoundException e) {
76 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}", cmHandleId, e.getMessage());
77 cmHandleRegistrationResponses.add(
78 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
79 } catch (final DataValidationException e) {
80 log.error("Unable to update cm handle : {}, caused by : {}", cmHandleId, e.getMessage());
81 cmHandleRegistrationResponses.add(
82 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
83 } catch (final Exception exception) {
84 log.error("Unable to update cmHandle : {} , caused by : {}", cmHandleId, exception.getMessage());
85 cmHandleRegistrationResponses.add(
86 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
89 return cmHandleRegistrationResponses;
92 private void processUpdates(final DataNode existingCmHandleDataNode, final NcmpServiceCmHandle incomingCmHandle) {
93 if (!incomingCmHandle.getPublicProperties().isEmpty()) {
94 updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY, incomingCmHandle.getPublicProperties());
96 if (!incomingCmHandle.getDmiProperties().isEmpty()) {
97 updateProperties(existingCmHandleDataNode, DMI_PROPERTY, incomingCmHandle.getDmiProperties());
101 private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
102 final Map<String, String> incomingProperties) {
103 final Collection<DataNode> replacementPropertyDataNodes =
104 getReplacementDataNodes(existingCmHandleDataNode, propertyType, incomingProperties);
105 replacementPropertyDataNodes.addAll(
106 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, incomingProperties));
107 if (replacementPropertyDataNodes.isEmpty()) {
108 removeAllProperties(existingCmHandleDataNode, propertyType);
110 inventoryPersistence.replaceListContent(existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes);
114 private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
115 existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
116 final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
117 if (matcher.find()) {
118 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
119 inventoryPersistence.deleteDataNode(dataNode.getXpath());
124 private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
125 final PropertyType propertyType, final Map<String, String> incomingProperties) {
126 final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
127 for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
128 final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
129 if (matcher.find()) {
130 final String keyName = matcher.group(2);
131 if (!incomingProperties.containsKey(keyName)) {
132 unchangedPropertyDataNodes.add(existingPropertyDataNode);
136 return unchangedPropertyDataNodes;
139 private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
140 final PropertyType propertyType, final Map<String, String> incomingProperties) {
141 final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
142 incomingProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
143 final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
144 if (updatedAttributeValue != null) {
145 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
146 updatedAttributeKey, updatedAttributeValue);
147 replacementPropertyDataNodes.add(
148 buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
151 return replacementPropertyDataNodes;
154 private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
155 final String attributeKey) {
156 return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
159 private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
160 final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
161 updatedLeaves.put("name", attributeKey);
162 updatedLeaves.put("value", attributeValue);
163 log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
165 return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
169 DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
171 private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
173 final String xpathPrefix;
174 final Pattern propertyXpathPattern;
176 PropertyType(final String xpathPrefix) {
177 this.xpathPrefix = xpathPrefix;
178 this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);