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 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
72 for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
73 final String cmHandle = ncmpServiceCmHandle.getCmHandleID();
75 final String cmHandleXpath = String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandle);
76 final DataNode existingCmHandleDataNode =
77 cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cmHandleXpath,
78 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
79 processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle);
80 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandle));
81 } catch (final DataNodeNotFoundException e) {
82 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
83 cmHandle, e.getMessage());
84 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
85 .createFailureResponse(cmHandle, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
86 } catch (final Exception exception) {
87 log.error("Unable to update dataNode for cmHandleId : {} , caused by : {}",
88 cmHandle, exception.getMessage());
89 cmHandleRegistrationResponses.add(
90 CmHandleRegistrationResponse.createFailureResponse(cmHandle, exception));
93 return cmHandleRegistrationResponses;
96 private void processUpdates(final DataNode existingCmHandleDataNode, final NcmpServiceCmHandle incomingCmHandle) {
97 if (!incomingCmHandle.getPublicProperties().isEmpty()) {
98 updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY, incomingCmHandle.getPublicProperties());
100 if (!incomingCmHandle.getDmiProperties().isEmpty()) {
101 updateProperties(existingCmHandleDataNode, DMI_PROPERTY, incomingCmHandle.getDmiProperties());
105 private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
106 final Map<String, String> incomingProperties) {
107 final Collection<DataNode> replacementPropertyDataNodes =
108 getReplacementDataNodes(existingCmHandleDataNode, propertyType, incomingProperties);
109 replacementPropertyDataNodes.addAll(
110 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, incomingProperties));
111 if (replacementPropertyDataNodes.isEmpty()) {
112 removeAllProperties(existingCmHandleDataNode, propertyType);
114 cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
115 existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes, NO_TIMESTAMP);
119 private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
120 existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
121 final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
122 if (matcher.find()) {
123 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
124 cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNode.getXpath(),
130 private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
131 final PropertyType propertyType, final Map<String, String> incomingProperties) {
132 final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
133 for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
134 final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
135 if (matcher.find()) {
136 final String keyName = matcher.group(2);
137 if (!incomingProperties.containsKey(keyName)) {
138 unchangedPropertyDataNodes.add(existingPropertyDataNode);
142 return unchangedPropertyDataNodes;
145 private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
146 final PropertyType propertyType, final Map<String, String> incomingProperties) {
147 final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
148 incomingProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
149 final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
150 if (updatedAttributeValue != null) {
151 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
152 updatedAttributeKey, updatedAttributeValue);
153 replacementPropertyDataNodes.add(
154 buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
157 return replacementPropertyDataNodes;
160 private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
161 final String attributeKey) {
162 return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
165 private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
166 final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
167 updatedLeaves.put("name", attributeKey);
168 updatedLeaves.put("value", attributeValue);
169 log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
171 return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
175 DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
177 private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
179 final String xpathPrefix;
180 final Pattern propertyXpathPattern;
182 PropertyType(final String xpathPrefix) {
183 this.xpathPrefix = xpathPrefix;
184 this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);