Merge "Error reporting when registering cm handle with alternate id 2 - update scenario"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServicePropertyHandler.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2024 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
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.api.impl;
24
25 import static org.onap.cps.ncmp.api.NcmpResponseStatus.ALTERNATE_ID_ALREADY_ASSOCIATED;
26 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND;
27 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID;
28 import static org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServicePropertyHandler.PropertyType.DMI_PROPERTY;
29 import static org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServicePropertyHandler.PropertyType.PUBLIC_PROPERTY;
30 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME;
31 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR;
32 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
33
34 import com.google.common.collect.ImmutableMap;
35 import java.time.OffsetDateTime;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.LinkedHashMap;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.regex.Matcher;
44 import java.util.regex.Pattern;
45 import lombok.RequiredArgsConstructor;
46 import lombok.extern.slf4j.Slf4j;
47 import org.onap.cps.api.CpsDataService;
48 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
49 import org.onap.cps.ncmp.api.impl.utils.AlternateIdChecker;
50 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
51 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
52 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
53 import org.onap.cps.spi.exceptions.DataValidationException;
54 import org.onap.cps.spi.model.DataNode;
55 import org.onap.cps.spi.model.DataNodeBuilder;
56 import org.onap.cps.utils.JsonObjectMapper;
57 import org.springframework.stereotype.Service;
58
59 @Slf4j
60 @Service
61 @RequiredArgsConstructor
62 //Accepting the security hotspot as the string checked is generated from inside code and not user input.
63 @SuppressWarnings("squid:S5852")
64 public class NetworkCmProxyDataServicePropertyHandler {
65
66     private final InventoryPersistence inventoryPersistence;
67     private final CpsDataService cpsDataService;
68     private final JsonObjectMapper jsonObjectMapper;
69     private final AlternateIdChecker alternateIdChecker;
70
71     /**
72      * Iterates over incoming updatedNcmpServiceCmHandles and update the dataNodes based on the updated attributes.
73      * The attributes which are not passed will remain as is.
74      *
75      * @param updatedNcmpServiceCmHandles collection of CmHandles
76      */
77     public List<CmHandleRegistrationResponse> updateCmHandleProperties(
78             final Collection<NcmpServiceCmHandle> updatedNcmpServiceCmHandles) {
79         final Collection<String> rejectedCmHandleIds = alternateIdChecker
80             .getIdsOfCmHandlesWithRejectedAlternateId(updatedNcmpServiceCmHandles, AlternateIdChecker.Operation.UPDATE);
81         final List<CmHandleRegistrationResponse> failureResponses =
82             CmHandleRegistrationResponse.createFailureResponses(rejectedCmHandleIds, ALTERNATE_ID_ALREADY_ASSOCIATED);
83         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failureResponses);
84         for (final NcmpServiceCmHandle updatedNcmpServiceCmHandle : updatedNcmpServiceCmHandles) {
85             final String cmHandleId = updatedNcmpServiceCmHandle.getCmHandleId();
86             if (!rejectedCmHandleIds.contains(cmHandleId)) {
87                 try {
88                     final DataNode existingCmHandleDataNode = inventoryPersistence
89                             .getCmHandleDataNodeByCmHandleId(cmHandleId).iterator().next();
90                     processUpdates(existingCmHandleDataNode, updatedNcmpServiceCmHandle);
91                     cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
92                 } catch (final DataNodeNotFoundException e) {
93                     log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}", cmHandleId,
94                             e.getMessage());
95                     cmHandleRegistrationResponses.add(
96                             CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
97                 } catch (final DataValidationException e) {
98                     log.error("Unable to update cm handle : {}, caused by : {}", cmHandleId, e.getMessage());
99                     cmHandleRegistrationResponses.add(
100                             CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
101                 } catch (final Exception exception) {
102                     log.error("Unable to update cmHandle : {} , caused by : {}", cmHandleId, exception.getMessage());
103                     cmHandleRegistrationResponses.add(
104                             CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
105                 }
106             }
107         }
108         return cmHandleRegistrationResponses;
109     }
110
111     private void processUpdates(final DataNode existingCmHandleDataNode,
112                                 final NcmpServiceCmHandle updatedNcmpServiceCmHandle) {
113         updateAlternateId(updatedNcmpServiceCmHandle);
114         if (!updatedNcmpServiceCmHandle.getPublicProperties().isEmpty()) {
115             updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY,
116                 updatedNcmpServiceCmHandle.getPublicProperties());
117         }
118         if (!updatedNcmpServiceCmHandle.getDmiProperties().isEmpty()) {
119             updateProperties(existingCmHandleDataNode, DMI_PROPERTY, updatedNcmpServiceCmHandle.getDmiProperties());
120         }
121     }
122
123     private void updateAlternateId(final NcmpServiceCmHandle updatedNcmpServiceCmHandle) {
124         final String updatedAlternateId = updatedNcmpServiceCmHandle.getAlternateId();
125         final String cmHandleId = updatedNcmpServiceCmHandle.getCmHandleId();
126         final Map<String, String> cmHandleProperties = new HashMap<>(2);
127         cmHandleProperties.put("id", cmHandleId);
128         cmHandleProperties.put("alternate-id", updatedAlternateId);
129         final Map<String, Map<String, String>> dmiRegistryProperties = new HashMap<>(1);
130         dmiRegistryProperties.put("cm-handles", cmHandleProperties);
131         cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
132                 jsonObjectMapper.asJsonString(dmiRegistryProperties), OffsetDateTime.now());
133         log.debug("Updating alternateId for cmHandle {} with value : {})", cmHandleId, updatedAlternateId);
134     }
135
136     private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
137                                   final Map<String, String> updatedProperties) {
138         final Collection<DataNode> replacementPropertyDataNodes =
139                 getReplacementDataNodes(existingCmHandleDataNode, propertyType, updatedProperties);
140         replacementPropertyDataNodes.addAll(
141                 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, updatedProperties));
142         if (replacementPropertyDataNodes.isEmpty()) {
143             removeAllProperties(existingCmHandleDataNode, propertyType);
144         } else {
145             inventoryPersistence.replaceListContent(existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes);
146         }
147     }
148
149     private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
150         existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
151             final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
152             if (matcher.find()) {
153                 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
154                 inventoryPersistence.deleteDataNode(dataNode.getXpath());
155             }
156         });
157     }
158
159     private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
160                                                                final PropertyType propertyType,
161                                                                final Map<String, String> updatedProperties) {
162         final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
163         for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
164             final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
165             if (matcher.find()) {
166                 final String keyName = matcher.group(2);
167                 if (!updatedProperties.containsKey(keyName)) {
168                     unchangedPropertyDataNodes.add(existingPropertyDataNode);
169                 }
170             }
171         }
172         return unchangedPropertyDataNodes;
173     }
174
175     private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
176                                                          final PropertyType propertyType,
177                                                          final Map<String, String> updatedProperties) {
178         final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
179         updatedProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
180             final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
181             if (updatedAttributeValue != null) {
182                 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
183                         updatedAttributeKey, updatedAttributeValue);
184                 replacementPropertyDataNodes.add(
185                         buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
186             }
187         });
188         return replacementPropertyDataNodes;
189     }
190
191     private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
192                                      final String attributeKey) {
193         return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
194     }
195
196     private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
197         final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
198         updatedLeaves.put("name", attributeKey);
199         updatedLeaves.put("value", attributeValue);
200         log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
201                 attributeValue);
202         return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
203     }
204
205     enum PropertyType {
206         DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
207
208         private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
209
210         final String xpathPrefix;
211         final Pattern propertyXpathPattern;
212
213         PropertyType(final String xpathPrefix) {
214             this.xpathPrefix = xpathPrefix;
215             this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);
216         }
217     }
218 }