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