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