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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl;
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 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME;
27 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR;
28 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_PARENT;
29 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;
31 import com.google.common.collect.ImmutableMap;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.HashSet;
35 import java.util.LinkedHashMap;
36 import java.util.List;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40 import lombok.RequiredArgsConstructor;
41 import lombok.extern.slf4j.Slf4j;
42 import org.onap.cps.api.CpsDataService;
43 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
44 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
45 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
46 import org.onap.cps.spi.FetchDescendantsOption;
47 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
48 import org.onap.cps.spi.model.DataNode;
49 import org.onap.cps.spi.model.DataNodeBuilder;
50 import org.springframework.stereotype.Service;
54 @RequiredArgsConstructor
55 //Accepting the security hotspot as the string checked is generated from inside code and not user input.
56 @SuppressWarnings("squid:S5852")
57 public class NetworkCmProxyDataServicePropertyHandler {
59 private static final String CM_HANDLE_XPATH_TEMPLATE = NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='%s']";
61 private final CpsDataService cpsDataService;
64 * Iterates over incoming ncmpServiceCmHandles and update the dataNodes based on the updated attributes.
65 * The attributes which are not passed will remain as is.
67 * @param ncmpServiceCmHandles collection of ncmpServiceCmHandles
69 public List<CmHandleRegistrationResponse> updateCmHandleProperties(
70 final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles)
71 throws DataNodeNotFoundException {
72 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
73 for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
74 final String cmHandle = ncmpServiceCmHandle.getCmHandleID();
76 final String cmHandleXpath = String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandle);
77 final DataNode existingCmHandleDataNode =
78 cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cmHandleXpath,
79 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
80 processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle);
81 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandle));
82 } catch (final DataNodeNotFoundException e) {
83 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
84 cmHandle, e.getMessage());
85 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
86 .createFailureResponse(cmHandle, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
87 } catch (final Exception exception) {
88 log.error("Unable to update dataNode for cmHandleId : {} , caused by : {}",
89 cmHandle, exception.getMessage());
90 cmHandleRegistrationResponses.add(
91 CmHandleRegistrationResponse.createFailureResponse(cmHandle, exception));
94 return cmHandleRegistrationResponses;
97 private void processUpdates(final DataNode existingCmHandleDataNode, final NcmpServiceCmHandle incomingCmHandle) {
98 if (!incomingCmHandle.getPublicProperties().isEmpty()) {
99 updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY, incomingCmHandle.getPublicProperties());
101 if (!incomingCmHandle.getDmiProperties().isEmpty()) {
102 updateProperties(existingCmHandleDataNode, DMI_PROPERTY, incomingCmHandle.getDmiProperties());
106 private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
107 final Map<String, String> incomingProperties) {
108 final Collection<DataNode> replacementPropertyDataNodes =
109 getReplacementDataNodes(existingCmHandleDataNode, propertyType, incomingProperties);
110 replacementPropertyDataNodes.addAll(
111 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, incomingProperties));
112 if (replacementPropertyDataNodes.isEmpty()) {
113 removeAllProperties(existingCmHandleDataNode, propertyType);
115 cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
116 existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes, NO_TIMESTAMP);
120 private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
121 existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
122 final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
123 if (matcher.find()) {
124 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
125 cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNode.getXpath(),
131 private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
132 final PropertyType propertyType, final Map<String, String> incomingProperties) {
133 final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
134 for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
135 final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
136 if (matcher.find()) {
137 final String keyName = matcher.group(2);
138 if (!incomingProperties.containsKey(keyName)) {
139 unchangedPropertyDataNodes.add(existingPropertyDataNode);
143 return unchangedPropertyDataNodes;
146 private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
147 final PropertyType propertyType, final Map<String, String> incomingProperties) {
148 final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
149 incomingProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
150 final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
151 if (updatedAttributeValue != null) {
152 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
153 updatedAttributeKey, updatedAttributeValue);
154 replacementPropertyDataNodes.add(
155 buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
158 return replacementPropertyDataNodes;
161 private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
162 final String attributeKey) {
163 return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
166 private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
167 final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
168 updatedLeaves.put("name", attributeKey);
169 updatedLeaves.put("value", attributeValue);
170 log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
172 return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
176 DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
178 private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
180 final String xpathPrefix;
181 final Pattern propertyXpathPattern;
183 PropertyType(final String xpathPrefix) {
184 this.xpathPrefix = xpathPrefix;
185 this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);