Allow updating of cmHandles with an alternateId
[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.apache.commons.lang3.StringUtils;
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.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
71     /**
72      * Iterates over incoming ncmpServiceCmHandles and update the dataNodes based on the updated attributes.
73      * The attributes which are not passed will remain as is.
74      *
75      * @param ncmpServiceCmHandles collection of ncmpServiceCmHandles
76      */
77     public List<CmHandleRegistrationResponse> updateCmHandleProperties(
78         final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles) {
79         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
80         for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
81             final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
82             try {
83                 final DataNode existingCmHandleDataNode = inventoryPersistence.getCmHandleDataNode(cmHandleId)
84                         .iterator().next();
85                 updateAlternateId(existingCmHandleDataNode, ncmpServiceCmHandle);
86                 processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle);
87                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
88             } catch (final DataNodeNotFoundException e) {
89                 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}", cmHandleId, e.getMessage());
90                 cmHandleRegistrationResponses.add(
91                     CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
92             } catch (final DataValidationException e) {
93                 log.error("Unable to update cm handle : {}, caused by : {}", cmHandleId, e.getMessage());
94                 cmHandleRegistrationResponses.add(
95                     CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
96             } catch (final Exception exception) {
97                 log.error("Unable to update cmHandle : {} , caused by : {}", cmHandleId, exception.getMessage());
98                 cmHandleRegistrationResponses.add(
99                     CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
100             }
101         }
102         return cmHandleRegistrationResponses;
103     }
104
105     private void updateAlternateId(final DataNode existingCmHandleDataNode,
106                                    final NcmpServiceCmHandle ncmpServiceCmHandle) {
107         final String newAlternateId = ncmpServiceCmHandle.getAlternateId();
108         if (!StringUtils.isEmpty(newAlternateId)) {
109             final String existingAlternateId = (String) existingCmHandleDataNode.getLeaves().get("alternate-id");
110             if (StringUtils.isEmpty(existingAlternateId)) {
111                 final YangModelCmHandle yangModelCmHandle =
112                         YangDataConverter.convertCmHandleToYangModel(existingCmHandleDataNode,
113                                 ncmpServiceCmHandle.getCmHandleId());
114                 setAndUpdateAlternateId(yangModelCmHandle, newAlternateId);
115             } else {
116                 if (!newAlternateId.equals(existingAlternateId)) {
117                     log.warn("Unable to update alternateId for cmHandle {}. "
118                                     + "Value for alternateId has been set previously.",
119                             ncmpServiceCmHandle.getCmHandleId());
120                 }
121             }
122         }
123     }
124
125     private void processUpdates(final DataNode existingCmHandleDataNode, final NcmpServiceCmHandle incomingCmHandle) {
126         if (!incomingCmHandle.getPublicProperties().isEmpty()) {
127             updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY, incomingCmHandle.getPublicProperties());
128         }
129         if (!incomingCmHandle.getDmiProperties().isEmpty()) {
130             updateProperties(existingCmHandleDataNode, DMI_PROPERTY, incomingCmHandle.getDmiProperties());
131         }
132     }
133
134     private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
135             final Map<String, String> incomingProperties) {
136         final Collection<DataNode> replacementPropertyDataNodes =
137                 getReplacementDataNodes(existingCmHandleDataNode, propertyType, incomingProperties);
138         replacementPropertyDataNodes.addAll(
139                 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, incomingProperties));
140         if (replacementPropertyDataNodes.isEmpty()) {
141             removeAllProperties(existingCmHandleDataNode, propertyType);
142         } else {
143             inventoryPersistence.replaceListContent(existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes);
144         }
145     }
146
147     private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
148         existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
149             final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
150             if (matcher.find()) {
151                 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
152                 inventoryPersistence.deleteDataNode(dataNode.getXpath());
153             }
154         });
155     }
156
157     private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
158             final PropertyType propertyType, final Map<String, String> incomingProperties) {
159         final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
160         for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
161             final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
162             if (matcher.find()) {
163                 final String keyName = matcher.group(2);
164                 if (!incomingProperties.containsKey(keyName)) {
165                     unchangedPropertyDataNodes.add(existingPropertyDataNode);
166                 }
167             }
168         }
169         return unchangedPropertyDataNodes;
170     }
171
172     private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
173             final PropertyType propertyType, final Map<String, String> incomingProperties) {
174         final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
175         incomingProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
176             final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
177             if (updatedAttributeValue != null) {
178                 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
179                         updatedAttributeKey, updatedAttributeValue);
180                 replacementPropertyDataNodes.add(
181                         buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
182             }
183         });
184         return replacementPropertyDataNodes;
185     }
186
187     private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
188             final String attributeKey) {
189         return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
190     }
191
192     private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
193         final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
194         updatedLeaves.put("name", attributeKey);
195         updatedLeaves.put("value", attributeValue);
196         log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
197                 attributeValue);
198         return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
199     }
200
201     private void setAndUpdateAlternateId(final YangModelCmHandle upgradedCmHandle, final String alternateId) {
202         final Map<String, Map<String, String>> dmiRegistryProperties = new HashMap<>(1);
203         final Map<String, String> cmHandleProperties = new HashMap<>(2);
204         cmHandleProperties.put("id", upgradedCmHandle.getId());
205         cmHandleProperties.put("alternate-id", alternateId);
206         dmiRegistryProperties.put("cm-handles", cmHandleProperties);
207         cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
208                 jsonObjectMapper.asJsonString(dmiRegistryProperties), OffsetDateTime.now());
209         log.info("Updating alternateId for cmHandle {} with value : {})", upgradedCmHandle.getId(), alternateId);
210     }
211
212     enum PropertyType {
213         DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
214
215         private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
216
217         final String xpathPrefix;
218         final Pattern propertyXpathPattern;
219
220         PropertyType(final String xpathPrefix) {
221             this.xpathPrefix = xpathPrefix;
222             this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);
223         }
224     }
225 }