2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
4 * Modifications Copyright (C) 2022 Bell Canada
5 * Modifications Copyright (C) 2024 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.impl.inventory;
25 import static org.onap.cps.api.parameters.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
26 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND;
27 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST;
28 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID;
29 import static org.onap.cps.ncmp.impl.inventory.CmHandleRegistrationServicePropertyHandler.PropertyType.ADDITIONAL_PROPERTY;
30 import static org.onap.cps.ncmp.impl.inventory.CmHandleRegistrationServicePropertyHandler.PropertyType.PUBLIC_PROPERTY;
31 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DATASPACE_NAME;
32 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR;
33 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
35 import com.google.common.collect.ImmutableMap;
36 import com.hazelcast.map.IMap;
37 import java.time.OffsetDateTime;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.LinkedHashMap;
43 import java.util.List;
45 import java.util.regex.Matcher;
46 import java.util.regex.Pattern;
47 import lombok.RequiredArgsConstructor;
48 import lombok.extern.slf4j.Slf4j;
49 import org.apache.commons.lang3.StringUtils;
50 import org.onap.cps.api.CpsDataService;
51 import org.onap.cps.api.exceptions.DataNodeNotFoundException;
52 import org.onap.cps.api.exceptions.DataValidationException;
53 import org.onap.cps.api.model.DataNode;
54 import org.onap.cps.impl.DataNodeBuilder;
55 import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse;
56 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle;
57 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
58 import org.onap.cps.ncmp.impl.inventory.sync.lcm.LcmEventsHelper;
59 import org.onap.cps.ncmp.impl.utils.YangDataConverter;
60 import org.onap.cps.utils.ContentType;
61 import org.onap.cps.utils.JsonObjectMapper;
62 import org.springframework.beans.factory.annotation.Qualifier;
63 import org.springframework.stereotype.Service;
67 @RequiredArgsConstructor
68 //Accepting the security hotspot as the string checked is generated from inside code and not user input.
69 @SuppressWarnings("squid:S5852")
70 public class CmHandleRegistrationServicePropertyHandler {
72 private final InventoryPersistence inventoryPersistence;
73 private final CpsDataService cpsDataService;
74 private final JsonObjectMapper jsonObjectMapper;
75 private final AlternateIdChecker alternateIdChecker;
76 @Qualifier("cmHandleIdPerAlternateId")
77 private final IMap<String, String> cmHandleIdPerAlternateId;
78 private final LcmEventsHelper lcmEventsHelper;
81 * Iterates over incoming updatedNcmpServiceCmHandles and update the dataNodes based on the updated attributes.
82 * The attributes which are not passed will remain as is.
84 * @param updatedNcmpServiceCmHandles collection of CmHandles
86 public List<CmHandleRegistrationResponse> updateCmHandleProperties(
87 final Collection<NcmpServiceCmHandle> updatedNcmpServiceCmHandles) {
88 final Collection<String> rejectedCmHandleIds = alternateIdChecker
89 .getIdsOfCmHandlesWithRejectedAlternateId(updatedNcmpServiceCmHandles, AlternateIdChecker.Operation.UPDATE);
90 final List<CmHandleRegistrationResponse> failureResponses =
91 CmHandleRegistrationResponse.createFailureResponses(rejectedCmHandleIds, CM_HANDLE_ALREADY_EXIST);
92 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failureResponses);
93 for (final NcmpServiceCmHandle updatedNcmpServiceCmHandle : updatedNcmpServiceCmHandles) {
94 final String cmHandleId = updatedNcmpServiceCmHandle.getCmHandleId();
95 if (!rejectedCmHandleIds.contains(cmHandleId)) {
97 final DataNode existingCmHandleDataNode = inventoryPersistence
98 .getCmHandleDataNodeByCmHandleId(cmHandleId, INCLUDE_ALL_DESCENDANTS).iterator().next();
99 processUpdates(existingCmHandleDataNode, updatedNcmpServiceCmHandle);
100 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
101 } catch (final DataNodeNotFoundException e) {
102 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}", cmHandleId,
104 cmHandleRegistrationResponses.add(
105 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
106 } catch (final DataValidationException e) {
107 log.error("Unable to update cm handle : {}, caused by : {}", cmHandleId, e.getMessage());
108 cmHandleRegistrationResponses.add(
109 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
110 } catch (final Exception exception) {
111 log.error("Unable to update cmHandle : {} , caused by : {}", cmHandleId, exception.getMessage());
112 cmHandleRegistrationResponses.add(
113 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
117 return cmHandleRegistrationResponses;
120 private void processUpdates(final DataNode existingCmHandleDataNode,
121 final NcmpServiceCmHandle updatedNcmpServiceCmHandle) {
122 updateAlternateId(updatedNcmpServiceCmHandle);
123 updateDataProducerIdentifier(existingCmHandleDataNode, updatedNcmpServiceCmHandle);
124 if (!updatedNcmpServiceCmHandle.getPublicProperties().isEmpty()) {
125 updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY,
126 updatedNcmpServiceCmHandle.getPublicProperties());
128 if (!updatedNcmpServiceCmHandle.getAdditionalProperties().isEmpty()) {
129 updateProperties(existingCmHandleDataNode, ADDITIONAL_PROPERTY,
130 updatedNcmpServiceCmHandle.getAdditionalProperties());
134 private void updateAlternateId(final NcmpServiceCmHandle ncmpServiceCmHandle) {
135 final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
136 final String newAlternateId = ncmpServiceCmHandle.getAlternateId();
137 if (StringUtils.isNotBlank(newAlternateId)) {
138 setAndUpdateCmHandleField(ncmpServiceCmHandle.getCmHandleId(), "alternate-id", newAlternateId);
139 cmHandleIdPerAlternateId.delete(cmHandleId);
140 cmHandleIdPerAlternateId.set(newAlternateId, cmHandleId);
144 private void updateDataProducerIdentifier(final DataNode cmHandleDataNode,
145 final NcmpServiceCmHandle ncmpServiceCmHandle) {
146 final String targetDataProducerIdentifier = ncmpServiceCmHandle.getDataProducerIdentifier();
147 final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
149 if (StringUtils.isBlank(targetDataProducerIdentifier)) {
150 log.warn("Ignoring update for cmHandle {}: target dataProducerIdentifier is null or blank.", cmHandleId);
154 final YangModelCmHandle existingYangModelCmHandle = YangDataConverter.toYangModelCmHandle(cmHandleDataNode);
155 final String existingDataProducerIdentifier = existingYangModelCmHandle.getDataProducerIdentifier();
157 if (existingDataProducerIdentifier.equals(targetDataProducerIdentifier)) {
158 log.debug("Ignoring update as dataProducerIdentifier for cmHandle {} is already set to {}.", cmHandleId,
159 targetDataProducerIdentifier);
163 setAndUpdateCmHandleField(cmHandleId, "data-producer-identifier", targetDataProducerIdentifier);
164 log.debug("dataProducerIdentifier for cmHandle {} updated from {} to {}", cmHandleId,
165 existingDataProducerIdentifier, targetDataProducerIdentifier);
166 sendLcmEventForDataProducerIdentifier(cmHandleId, existingYangModelCmHandle);
169 private void sendLcmEventForDataProducerIdentifier(final String cmHandleId,
170 final YangModelCmHandle existingYangModelCmHandle) {
171 final YangModelCmHandle updatedYangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
172 final NcmpServiceCmHandle existingNcmpServiceCmHandle =
173 YangDataConverter.toNcmpServiceCmHandle(existingYangModelCmHandle);
174 final NcmpServiceCmHandle updatedNcmpServiceCmHandle =
175 YangDataConverter.toNcmpServiceCmHandle(updatedYangModelCmHandle);
177 lcmEventsHelper.sendLcmEventAsynchronously(updatedNcmpServiceCmHandle,
178 existingNcmpServiceCmHandle);
181 private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
182 final Map<String, String> updatedProperties) {
183 final Collection<DataNode> replacementPropertyDataNodes =
184 getReplacementDataNodes(existingCmHandleDataNode, propertyType, updatedProperties);
185 replacementPropertyDataNodes.addAll(
186 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, updatedProperties));
187 if (replacementPropertyDataNodes.isEmpty()) {
188 removeAllProperties(existingCmHandleDataNode, propertyType);
190 inventoryPersistence.replaceListContent(existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes);
194 private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
195 existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
196 final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
197 if (matcher.find()) {
198 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
199 inventoryPersistence.deleteDataNode(dataNode.getXpath());
204 private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
205 final PropertyType propertyType,
206 final Map<String, String> updatedProperties) {
207 final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
208 for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
209 final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
210 if (matcher.find()) {
211 final String keyName = matcher.group(2);
212 if (!updatedProperties.containsKey(keyName)) {
213 unchangedPropertyDataNodes.add(existingPropertyDataNode);
217 return unchangedPropertyDataNodes;
220 private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
221 final PropertyType propertyType,
222 final Map<String, String> updatedProperties) {
223 final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
224 updatedProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
225 final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
226 if (updatedAttributeValue != null) {
227 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
228 updatedAttributeKey, updatedAttributeValue);
229 replacementPropertyDataNodes.add(
230 buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
233 return replacementPropertyDataNodes;
236 private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
237 final String attributeKey) {
238 return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
241 private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
242 final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
243 updatedLeaves.put("name", attributeKey);
244 updatedLeaves.put("value", attributeValue);
245 log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
247 return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
250 private void setAndUpdateCmHandleField(final String cmHandleIdToUpdate, final String fieldName,
251 final String newFieldValue) {
252 final Map<String, Map<String, String>> dmiRegistryData = new HashMap<>(1);
253 final Map<String, String> cmHandleData = new HashMap<>(2);
254 cmHandleData.put("id", cmHandleIdToUpdate);
255 cmHandleData.put(fieldName, newFieldValue);
256 dmiRegistryData.put("cm-handles", cmHandleData);
257 cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
258 jsonObjectMapper.asJsonString(dmiRegistryData), OffsetDateTime.now(), ContentType.JSON);
259 log.debug("Updating {} for cmHandle {} with value : {})", fieldName, cmHandleIdToUpdate, newFieldValue);
263 ADDITIONAL_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
265 private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
267 final String xpathPrefix;
268 final Pattern propertyXpathPattern;
270 PropertyType(final String xpathPrefix) {
271 this.xpathPrefix = xpathPrefix;
272 this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);