CPS Validator Changes
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServicePropertyHandler.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl;
23
24 import static org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServicePropertyHandler.PropertyType.DMI_PROPERTY;
25 import static org.onap.cps.ncmp.api.impl.NetworkCmProxyDataServicePropertyHandler.PropertyType.PUBLIC_PROPERTY;
26
27 import com.google.common.collect.ImmutableMap;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashSet;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 import lombok.RequiredArgsConstructor;
37 import lombok.extern.slf4j.Slf4j;
38 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
39 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
40 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
41 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
42 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
43 import org.onap.cps.spi.exceptions.DataValidationException;
44 import org.onap.cps.spi.model.DataNode;
45 import org.onap.cps.spi.model.DataNodeBuilder;
46 import org.springframework.stereotype.Service;
47
48 @Slf4j
49 @Service
50 @RequiredArgsConstructor
51 //Accepting the security hotspot as the string checked is generated from inside code and not user input.
52 @SuppressWarnings("squid:S5852")
53 public class NetworkCmProxyDataServicePropertyHandler {
54
55     private final InventoryPersistence inventoryPersistence;
56
57     /**
58      * Iterates over incoming ncmpServiceCmHandles and update the dataNodes based on the updated attributes.
59      * The attributes which are not passed will remain as is.
60      *
61      * @param ncmpServiceCmHandles collection of ncmpServiceCmHandles
62      */
63     public List<CmHandleRegistrationResponse> updateCmHandleProperties(
64         final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles) {
65         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
66         for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
67             final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
68             try {
69                 final DataNode existingCmHandleDataNode = inventoryPersistence.getCmHandleDataNode(cmHandleId);
70                 processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle);
71                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
72             } catch (final DataNodeNotFoundException e) {
73                 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
74                     cmHandleId, e.getMessage());
75                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
76                     .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
77             } catch (final DataValidationException e) {
78                 log.error("Unable to update cm handle : {}, caused by : {}",
79                     cmHandleId, e.getMessage());
80                 cmHandleRegistrationResponses.add(
81                     CmHandleRegistrationResponse.createFailureResponse(cmHandleId,
82                         RegistrationError.CM_HANDLE_INVALID_ID));
83             } catch (final Exception exception) {
84                 log.error("Unable to update cmHandle : {} , caused by : {}",
85                     cmHandleId, exception.getMessage());
86                 cmHandleRegistrationResponses.add(
87                     CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
88             }
89         }
90         return cmHandleRegistrationResponses;
91     }
92
93     private void processUpdates(final DataNode existingCmHandleDataNode, final NcmpServiceCmHandle incomingCmHandle) {
94         if (!incomingCmHandle.getPublicProperties().isEmpty()) {
95             updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY, incomingCmHandle.getPublicProperties());
96         }
97         if (!incomingCmHandle.getDmiProperties().isEmpty()) {
98             updateProperties(existingCmHandleDataNode, DMI_PROPERTY, incomingCmHandle.getDmiProperties());
99         }
100     }
101
102     private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
103             final Map<String, String> incomingProperties) {
104         final Collection<DataNode> replacementPropertyDataNodes =
105                 getReplacementDataNodes(existingCmHandleDataNode, propertyType, incomingProperties);
106         replacementPropertyDataNodes.addAll(
107                 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, incomingProperties));
108         if (replacementPropertyDataNodes.isEmpty()) {
109             removeAllProperties(existingCmHandleDataNode, propertyType);
110         } else {
111             inventoryPersistence.replaceListContent(existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes);
112         }
113     }
114
115     private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
116         existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
117             final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
118             if (matcher.find()) {
119                 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
120                 inventoryPersistence.deleteDataNode(dataNode.getXpath());
121             }
122         });
123     }
124
125     private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
126             final PropertyType propertyType, final Map<String, String> incomingProperties) {
127         final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
128         for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
129             final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
130             if (matcher.find()) {
131                 final String keyName = matcher.group(2);
132                 if (!incomingProperties.containsKey(keyName)) {
133                     unchangedPropertyDataNodes.add(existingPropertyDataNode);
134                 }
135             }
136         }
137         return unchangedPropertyDataNodes;
138     }
139
140     private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
141             final PropertyType propertyType, final Map<String, String> incomingProperties) {
142         final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
143         incomingProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
144             final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
145             if (updatedAttributeValue != null) {
146                 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
147                         updatedAttributeKey, updatedAttributeValue);
148                 replacementPropertyDataNodes.add(
149                         buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
150             }
151         });
152         return replacementPropertyDataNodes;
153     }
154
155     private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
156             final String attributeKey) {
157         return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
158     }
159
160     private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
161         final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
162         updatedLeaves.put("name", attributeKey);
163         updatedLeaves.put("value", attributeValue);
164         log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
165                 attributeValue);
166         return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
167     }
168
169     enum PropertyType {
170         DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
171
172         private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
173
174         final String xpathPrefix;
175         final Pattern propertyXpathPattern;
176
177         PropertyType(final String xpathPrefix) {
178             this.xpathPrefix = xpathPrefix;
179             this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);
180         }
181     }
182 }