Merge "Handling Yang module upgrade error scenarios"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServicePropertyHandler.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 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
30 import com.google.common.collect.ImmutableMap;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashSet;
34 import java.util.LinkedHashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39 import lombok.RequiredArgsConstructor;
40 import lombok.extern.slf4j.Slf4j;
41 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
42 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
43 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
44 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
45 import org.onap.cps.spi.exceptions.DataValidationException;
46 import org.onap.cps.spi.model.DataNode;
47 import org.onap.cps.spi.model.DataNodeBuilder;
48 import org.springframework.stereotype.Service;
49
50 @Slf4j
51 @Service
52 @RequiredArgsConstructor
53 //Accepting the security hotspot as the string checked is generated from inside code and not user input.
54 @SuppressWarnings("squid:S5852")
55 public class NetworkCmProxyDataServicePropertyHandler {
56
57     private final InventoryPersistence inventoryPersistence;
58
59     /**
60      * Iterates over incoming ncmpServiceCmHandles and update the dataNodes based on the updated attributes.
61      * The attributes which are not passed will remain as is.
62      *
63      * @param ncmpServiceCmHandles collection of ncmpServiceCmHandles
64      */
65     public List<CmHandleRegistrationResponse> updateCmHandleProperties(
66         final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles) {
67         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
68         for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
69             final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
70             try {
71                 final DataNode existingCmHandleDataNode = inventoryPersistence.getCmHandleDataNode(cmHandleId)
72                         .iterator().next();
73                 processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle);
74                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
75             } catch (final DataNodeNotFoundException e) {
76                 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}", cmHandleId, e.getMessage());
77                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
78                     .createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
79             } catch (final DataValidationException e) {
80                 log.error("Unable to update cm handle : {}, caused by : {}", cmHandleId, e.getMessage());
81                 cmHandleRegistrationResponses.add(
82                     CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
83             } catch (final Exception exception) {
84                 log.error("Unable to update cmHandle : {} , caused by : {}", cmHandleId, exception.getMessage());
85                 cmHandleRegistrationResponses.add(
86                     CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
87             }
88         }
89         return cmHandleRegistrationResponses;
90     }
91
92     private void processUpdates(final DataNode existingCmHandleDataNode, final NcmpServiceCmHandle incomingCmHandle) {
93         if (!incomingCmHandle.getPublicProperties().isEmpty()) {
94             updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY, incomingCmHandle.getPublicProperties());
95         }
96         if (!incomingCmHandle.getDmiProperties().isEmpty()) {
97             updateProperties(existingCmHandleDataNode, DMI_PROPERTY, incomingCmHandle.getDmiProperties());
98         }
99     }
100
101     private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
102             final Map<String, String> incomingProperties) {
103         final Collection<DataNode> replacementPropertyDataNodes =
104                 getReplacementDataNodes(existingCmHandleDataNode, propertyType, incomingProperties);
105         replacementPropertyDataNodes.addAll(
106                 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, incomingProperties));
107         if (replacementPropertyDataNodes.isEmpty()) {
108             removeAllProperties(existingCmHandleDataNode, propertyType);
109         } else {
110             inventoryPersistence.replaceListContent(existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes);
111         }
112     }
113
114     private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
115         existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
116             final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
117             if (matcher.find()) {
118                 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
119                 inventoryPersistence.deleteDataNode(dataNode.getXpath());
120             }
121         });
122     }
123
124     private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
125             final PropertyType propertyType, final Map<String, String> incomingProperties) {
126         final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
127         for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
128             final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
129             if (matcher.find()) {
130                 final String keyName = matcher.group(2);
131                 if (!incomingProperties.containsKey(keyName)) {
132                     unchangedPropertyDataNodes.add(existingPropertyDataNode);
133                 }
134             }
135         }
136         return unchangedPropertyDataNodes;
137     }
138
139     private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
140             final PropertyType propertyType, final Map<String, String> incomingProperties) {
141         final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
142         incomingProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
143             final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
144             if (updatedAttributeValue != null) {
145                 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
146                         updatedAttributeKey, updatedAttributeValue);
147                 replacementPropertyDataNodes.add(
148                         buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
149             }
150         });
151         return replacementPropertyDataNodes;
152     }
153
154     private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
155             final String attributeKey) {
156         return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
157     }
158
159     private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
160         final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
161         updatedLeaves.put("name", attributeKey);
162         updatedLeaves.put("value", attributeValue);
163         log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
164                 attributeValue);
165         return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
166     }
167
168     enum PropertyType {
169         DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
170
171         private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
172
173         final String xpathPrefix;
174         final Pattern propertyXpathPattern;
175
176         PropertyType(final String xpathPrefix) {
177             this.xpathPrefix = xpathPrefix;
178             this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);
179         }
180     }
181 }