7a6cb14d1759f42d64402e695ac0e79cd921f08a
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2025 Nordix Foundation
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.ncmp.impl.inventory;
24
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.DMI_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;
34
35 import com.google.common.collect.ImmutableMap;
36 import java.time.OffsetDateTime;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.LinkedHashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.regex.Matcher;
45 import java.util.regex.Pattern;
46 import lombok.RequiredArgsConstructor;
47 import lombok.extern.slf4j.Slf4j;
48 import org.apache.commons.lang3.StringUtils;
49 import org.onap.cps.api.CpsDataService;
50 import org.onap.cps.api.exceptions.DataNodeNotFoundException;
51 import org.onap.cps.api.exceptions.DataValidationException;
52 import org.onap.cps.api.model.DataNode;
53 import org.onap.cps.impl.DataNodeBuilder;
54 import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse;
55 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle;
56 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
57 import org.onap.cps.ncmp.impl.utils.YangDataConverter;
58 import org.onap.cps.utils.ContentType;
59 import org.onap.cps.utils.JsonObjectMapper;
60 import org.springframework.stereotype.Service;
61
62 @Slf4j
63 @Service
64 @RequiredArgsConstructor
65 //Accepting the security hotspot as the string checked is generated from inside code and not user input.
66 @SuppressWarnings("squid:S5852")
67 public class CmHandleRegistrationServicePropertyHandler {
68
69     private final InventoryPersistence inventoryPersistence;
70     private final CpsDataService cpsDataService;
71     private final JsonObjectMapper jsonObjectMapper;
72     private final AlternateIdChecker alternateIdChecker;
73
74     /**
75      * Iterates over incoming updatedNcmpServiceCmHandles and update the dataNodes based on the updated attributes.
76      * The attributes which are not passed will remain as is.
77      *
78      * @param updatedNcmpServiceCmHandles collection of CmHandles
79      */
80     public List<CmHandleRegistrationResponse> updateCmHandleProperties(
81             final Collection<NcmpServiceCmHandle> updatedNcmpServiceCmHandles) {
82         final Collection<String> rejectedCmHandleIds = alternateIdChecker
83             .getIdsOfCmHandlesWithRejectedAlternateId(updatedNcmpServiceCmHandles, AlternateIdChecker.Operation.UPDATE);
84         final List<CmHandleRegistrationResponse> failureResponses =
85             CmHandleRegistrationResponse.createFailureResponses(rejectedCmHandleIds, CM_HANDLE_ALREADY_EXIST);
86         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failureResponses);
87         for (final NcmpServiceCmHandle updatedNcmpServiceCmHandle : updatedNcmpServiceCmHandles) {
88             final String cmHandleId = updatedNcmpServiceCmHandle.getCmHandleId();
89             if (!rejectedCmHandleIds.contains(cmHandleId)) {
90                 try {
91                     final DataNode existingCmHandleDataNode = inventoryPersistence
92                             .getCmHandleDataNodeByCmHandleId(cmHandleId, INCLUDE_ALL_DESCENDANTS).iterator().next();
93                     processUpdates(existingCmHandleDataNode, updatedNcmpServiceCmHandle);
94                     cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
95                 } catch (final DataNodeNotFoundException e) {
96                     log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}", cmHandleId,
97                             e.getMessage());
98                     cmHandleRegistrationResponses.add(
99                             CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
100                 } catch (final DataValidationException e) {
101                     log.error("Unable to update cm handle : {}, caused by : {}", cmHandleId, e.getMessage());
102                     cmHandleRegistrationResponses.add(
103                             CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
104                 } catch (final Exception exception) {
105                     log.error("Unable to update cmHandle : {} , caused by : {}", cmHandleId, exception.getMessage());
106                     cmHandleRegistrationResponses.add(
107                             CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
108                 }
109             }
110         }
111         return cmHandleRegistrationResponses;
112     }
113
114     private void processUpdates(final DataNode existingCmHandleDataNode,
115                                 final NcmpServiceCmHandle updatedNcmpServiceCmHandle) {
116         updateAlternateId(updatedNcmpServiceCmHandle);
117         updateDataProducerIdentifier(existingCmHandleDataNode, updatedNcmpServiceCmHandle);
118         if (!updatedNcmpServiceCmHandle.getPublicProperties().isEmpty()) {
119             updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY,
120                 updatedNcmpServiceCmHandle.getPublicProperties());
121         }
122         if (!updatedNcmpServiceCmHandle.getDmiProperties().isEmpty()) {
123             updateProperties(existingCmHandleDataNode, DMI_PROPERTY, updatedNcmpServiceCmHandle.getDmiProperties());
124         }
125     }
126
127     private void updateAlternateId(final NcmpServiceCmHandle ncmpServiceCmHandle) {
128         final String newAlternateId = ncmpServiceCmHandle.getAlternateId();
129         if (StringUtils.isNotBlank(newAlternateId)) {
130             setAndUpdateCmHandleField(ncmpServiceCmHandle.getCmHandleId(), "alternate-id", newAlternateId);
131         }
132     }
133
134     private void updateDataProducerIdentifier(final DataNode cmHandleDataNode,
135                                               final NcmpServiceCmHandle ncmpServiceCmHandle) {
136         final String newDataProducerIdentifier = ncmpServiceCmHandle.getDataProducerIdentifier();
137         if (StringUtils.isNotBlank(newDataProducerIdentifier)) {
138             final YangModelCmHandle yangModelCmHandle = YangDataConverter.toYangModelCmHandle(cmHandleDataNode);
139             final String existingDataProducerIdentifier = yangModelCmHandle.getDataProducerIdentifier();
140             if (StringUtils.isNotBlank(existingDataProducerIdentifier)) {
141                 if (!existingDataProducerIdentifier.equals(newDataProducerIdentifier)) {
142                     log.warn("Unable to update dataProducerIdentifier for cmHandle {}. "
143                             + "Value for dataProducerIdentifier has been set previously.",
144                         ncmpServiceCmHandle.getCmHandleId());
145                 } else {
146                     log.debug("dataProducerIdentifier for cmHandle {} is already set to {}.",
147                         ncmpServiceCmHandle.getCmHandleId(), newDataProducerIdentifier);
148                 }
149             } else {
150                 setAndUpdateCmHandleField(
151                     yangModelCmHandle.getId(), "data-producer-identifier", newDataProducerIdentifier);
152             }
153         }
154     }
155
156     private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
157                                   final Map<String, String> updatedProperties) {
158         final Collection<DataNode> replacementPropertyDataNodes =
159                 getReplacementDataNodes(existingCmHandleDataNode, propertyType, updatedProperties);
160         replacementPropertyDataNodes.addAll(
161                 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, updatedProperties));
162         if (replacementPropertyDataNodes.isEmpty()) {
163             removeAllProperties(existingCmHandleDataNode, propertyType);
164         } else {
165             inventoryPersistence.replaceListContent(existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes);
166         }
167     }
168
169     private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
170         existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
171             final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
172             if (matcher.find()) {
173                 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
174                 inventoryPersistence.deleteDataNode(dataNode.getXpath());
175             }
176         });
177     }
178
179     private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
180                                                                final PropertyType propertyType,
181                                                                final Map<String, String> updatedProperties) {
182         final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
183         for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
184             final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
185             if (matcher.find()) {
186                 final String keyName = matcher.group(2);
187                 if (!updatedProperties.containsKey(keyName)) {
188                     unchangedPropertyDataNodes.add(existingPropertyDataNode);
189                 }
190             }
191         }
192         return unchangedPropertyDataNodes;
193     }
194
195     private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
196                                                          final PropertyType propertyType,
197                                                          final Map<String, String> updatedProperties) {
198         final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
199         updatedProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
200             final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
201             if (updatedAttributeValue != null) {
202                 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
203                         updatedAttributeKey, updatedAttributeValue);
204                 replacementPropertyDataNodes.add(
205                         buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
206             }
207         });
208         return replacementPropertyDataNodes;
209     }
210
211     private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
212                                      final String attributeKey) {
213         return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
214     }
215
216     private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
217         final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
218         updatedLeaves.put("name", attributeKey);
219         updatedLeaves.put("value", attributeValue);
220         log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
221                 attributeValue);
222         return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
223     }
224
225     private void setAndUpdateCmHandleField(final String cmHandleIdToUpdate, final String fieldName,
226                                            final String newFieldValue) {
227         final Map<String, Map<String, String>> dmiRegistryData = new HashMap<>(1);
228         final Map<String, String> cmHandleData = new HashMap<>(2);
229         cmHandleData.put("id", cmHandleIdToUpdate);
230         cmHandleData.put(fieldName, newFieldValue);
231         dmiRegistryData.put("cm-handles", cmHandleData);
232         cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
233                 jsonObjectMapper.asJsonString(dmiRegistryData), OffsetDateTime.now(), ContentType.JSON);
234         log.debug("Updating {} for cmHandle {} with value : {})", fieldName, cmHandleIdToUpdate, newFieldValue);
235     }
236
237     enum PropertyType {
238         DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
239
240         private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
241
242         final String xpathPrefix;
243         final Pattern propertyXpathPattern;
244
245         PropertyType(final String xpathPrefix) {
246             this.xpathPrefix = xpathPrefix;
247             this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);
248         }
249     }
250 }