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;
30 import static org.onap.ncmp.cmhandle.lcm.event.Event.Operation.UPDATE;
32 import com.google.common.collect.ImmutableMap;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.HashSet;
36 import java.util.LinkedHashMap;
37 import java.util.List;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41 import lombok.RequiredArgsConstructor;
42 import lombok.extern.slf4j.Slf4j;
43 import org.onap.cps.api.CpsDataService;
44 import org.onap.cps.ncmp.api.impl.event.NcmpEventsService;
45 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
46 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
47 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
48 import org.onap.cps.spi.FetchDescendantsOption;
49 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
50 import org.onap.cps.spi.exceptions.DataValidationException;
51 import org.onap.cps.spi.model.DataNode;
52 import org.onap.cps.spi.model.DataNodeBuilder;
53 import org.onap.cps.utils.CpsValidator;
54 import org.springframework.stereotype.Service;
58 @RequiredArgsConstructor
59 //Accepting the security hotspot as the string checked is generated from inside code and not user input.
60 @SuppressWarnings("squid:S5852")
61 public class NetworkCmProxyDataServicePropertyHandler {
63 private static final String CM_HANDLE_XPATH_TEMPLATE = NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='%s']";
65 private final CpsDataService cpsDataService;
67 private final NcmpEventsService ncmpEventsService;
70 * Iterates over incoming ncmpServiceCmHandles and update the dataNodes based on the updated attributes.
71 * The attributes which are not passed will remain as is.
73 * @param ncmpServiceCmHandles collection of ncmpServiceCmHandles
75 public List<CmHandleRegistrationResponse> updateCmHandleProperties(
76 final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles) {
77 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
78 for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
79 final String cmHandle = ncmpServiceCmHandle.getCmHandleId();
81 CpsValidator.validateNameCharacters(cmHandle);
82 final String cmHandleXpath = String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandle);
83 final DataNode existingCmHandleDataNode =
84 cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cmHandleXpath,
85 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
86 processUpdates(existingCmHandleDataNode, ncmpServiceCmHandle);
87 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandle));
88 } catch (final DataNodeNotFoundException e) {
89 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
90 cmHandle, e.getMessage());
91 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
92 .createFailureResponse(cmHandle, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
93 } catch (final DataValidationException e) {
94 log.error("Unable to update cm handle : {}, caused by : {}",
95 cmHandle, e.getMessage());
96 cmHandleRegistrationResponses.add(
97 CmHandleRegistrationResponse.createFailureResponse(cmHandle,
98 RegistrationError.CM_HANDLE_INVALID_ID));
99 } catch (final Exception exception) {
100 log.error("Unable to update cmHandle : {} , caused by : {}",
101 cmHandle, exception.getMessage());
102 cmHandleRegistrationResponses.add(
103 CmHandleRegistrationResponse.createFailureResponse(cmHandle, exception));
106 return cmHandleRegistrationResponses;
109 private void processUpdates(final DataNode existingCmHandleDataNode, final NcmpServiceCmHandle incomingCmHandle) {
110 if (!incomingCmHandle.getPublicProperties().isEmpty()) {
111 updateProperties(existingCmHandleDataNode, PUBLIC_PROPERTY, incomingCmHandle.getPublicProperties());
112 publishLcmEventOnPublicPropertiesUpdate(incomingCmHandle.getCmHandleId());
114 if (!incomingCmHandle.getDmiProperties().isEmpty()) {
115 updateProperties(existingCmHandleDataNode, DMI_PROPERTY, incomingCmHandle.getDmiProperties());
119 private void updateProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType,
120 final Map<String, String> incomingProperties) {
121 final Collection<DataNode> replacementPropertyDataNodes =
122 getReplacementDataNodes(existingCmHandleDataNode, propertyType, incomingProperties);
123 replacementPropertyDataNodes.addAll(
124 getUnchangedPropertyDataNodes(existingCmHandleDataNode, propertyType, incomingProperties));
125 if (replacementPropertyDataNodes.isEmpty()) {
126 removeAllProperties(existingCmHandleDataNode, propertyType);
128 cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
129 existingCmHandleDataNode.getXpath(), replacementPropertyDataNodes, NO_TIMESTAMP);
133 private void removeAllProperties(final DataNode existingCmHandleDataNode, final PropertyType propertyType) {
134 existingCmHandleDataNode.getChildDataNodes().forEach(dataNode -> {
135 final Matcher matcher = propertyType.propertyXpathPattern.matcher(dataNode.getXpath());
136 if (matcher.find()) {
137 log.info("Deleting dataNode with xpath : [{}]", dataNode.getXpath());
138 cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNode.getXpath(),
144 private Collection<DataNode> getUnchangedPropertyDataNodes(final DataNode existingCmHandleDataNode,
145 final PropertyType propertyType, final Map<String, String> incomingProperties) {
146 final Collection<DataNode> unchangedPropertyDataNodes = new HashSet<>();
147 for (final DataNode existingPropertyDataNode : existingCmHandleDataNode.getChildDataNodes()) {
148 final Matcher matcher = propertyType.propertyXpathPattern.matcher(existingPropertyDataNode.getXpath());
149 if (matcher.find()) {
150 final String keyName = matcher.group(2);
151 if (!incomingProperties.containsKey(keyName)) {
152 unchangedPropertyDataNodes.add(existingPropertyDataNode);
156 return unchangedPropertyDataNodes;
159 private Collection<DataNode> getReplacementDataNodes(final DataNode existingCmHandleDataNode,
160 final PropertyType propertyType, final Map<String, String> incomingProperties) {
161 final Collection<DataNode> replacementPropertyDataNodes = new HashSet<>();
162 incomingProperties.forEach((updatedAttributeKey, updatedAttributeValue) -> {
163 final String propertyXpath = getAttributeXpath(existingCmHandleDataNode, propertyType, updatedAttributeKey);
164 if (updatedAttributeValue != null) {
165 log.info("Creating a new DataNode with xpath {} , key : {} and value : {}", propertyXpath,
166 updatedAttributeKey, updatedAttributeValue);
167 replacementPropertyDataNodes.add(
168 buildDataNode(propertyXpath, updatedAttributeKey, updatedAttributeValue));
171 return replacementPropertyDataNodes;
174 private String getAttributeXpath(final DataNode cmHandle, final PropertyType propertyType,
175 final String attributeKey) {
176 return cmHandle.getXpath() + "/" + propertyType.xpathPrefix + String.format("[@name='%s']", attributeKey);
179 private DataNode buildDataNode(final String xpath, final String attributeKey, final String attributeValue) {
180 final Map<String, String> updatedLeaves = new LinkedHashMap<>(1);
181 updatedLeaves.put("name", attributeKey);
182 updatedLeaves.put("value", attributeValue);
183 log.debug("Building a new node with xpath {} with leaves (name : {} , value : {})", xpath, attributeKey,
185 return new DataNodeBuilder().withXpath(xpath).withLeaves(ImmutableMap.copyOf(updatedLeaves)).build();
188 private void publishLcmEventOnPublicPropertiesUpdate(final String cmHandleId) {
189 log.debug("Publishing LCM Update event for cmHandleId : {}", cmHandleId);
190 ncmpEventsService.publishNcmpEvent(cmHandleId, UPDATE);
194 DMI_PROPERTY("additional-properties"), PUBLIC_PROPERTY("public-properties");
196 private static final String LIST_INDEX_PATTERN = "\\[@(\\w+)[^\\/]'([^']+)']";
198 final String xpathPrefix;
199 final Pattern propertyXpathPattern;
201 PropertyType(final String xpathPrefix) {
202 this.xpathPrefix = xpathPrefix;
203 this.propertyXpathPattern = Pattern.compile(xpathPrefix + LIST_INDEX_PATTERN);