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