2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2020-2022 Bell Canada.
5 * Modifications Copyright (C) 2021 Pantheon.tech
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.api.impl;
25 import java.time.OffsetDateTime;
26 import java.util.Collection;
27 import lombok.AllArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.api.CpsAdminService;
30 import org.onap.cps.api.CpsDataService;
31 import org.onap.cps.notification.NotificationService;
32 import org.onap.cps.notification.Operation;
33 import org.onap.cps.spi.CpsDataPersistenceService;
34 import org.onap.cps.spi.FetchDescendantsOption;
35 import org.onap.cps.spi.exceptions.DataValidationException;
36 import org.onap.cps.spi.model.Anchor;
37 import org.onap.cps.spi.model.DataNode;
38 import org.onap.cps.spi.model.DataNodeBuilder;
39 import org.onap.cps.utils.CpsValidator;
40 import org.onap.cps.utils.YangUtils;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.springframework.stereotype.Service;
48 public class CpsDataServiceImpl implements CpsDataService {
50 private static final String ROOT_NODE_XPATH = "/";
52 private final CpsDataPersistenceService cpsDataPersistenceService;
53 private final CpsAdminService cpsAdminService;
54 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
55 private final NotificationService notificationService;
58 public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
59 final OffsetDateTime observedTimestamp) {
60 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
61 final DataNode dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
62 cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
63 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, ROOT_NODE_XPATH, Operation.CREATE);
67 public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
68 final String jsonData, final OffsetDateTime observedTimestamp) {
69 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
70 final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
71 cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
72 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.CREATE);
76 public void saveListElements(final String dataspaceName, final String anchorName,
77 final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
78 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
79 final Collection<DataNode> listElementDataNodeCollection =
80 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
81 cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
82 listElementDataNodeCollection);
83 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
87 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
88 final FetchDescendantsOption fetchDescendantsOption) {
89 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
90 return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
94 public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
95 final String jsonData, final OffsetDateTime observedTimestamp) {
96 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
97 final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
98 cpsDataPersistenceService
99 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
100 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
104 public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
105 final String parentNodeXpath,
106 final String dataNodeUpdatesAsJson,
107 final OffsetDateTime observedTimestamp) {
108 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
109 final Collection<DataNode> dataNodeUpdates =
110 buildDataNodes(dataspaceName, anchorName,
111 parentNodeXpath, dataNodeUpdatesAsJson);
112 for (final DataNode dataNodeUpdate : dataNodeUpdates) {
113 processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
115 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
119 public String startSession() {
120 return cpsDataPersistenceService.startSession();
124 public void closeSession(final String sessionId) {
125 cpsDataPersistenceService.closeSession(sessionId);
129 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
130 final String jsonData, final OffsetDateTime observedTimestamp) {
131 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
132 final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
133 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
134 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
138 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
139 final String jsonData, final OffsetDateTime observedTimestamp) {
140 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
141 final Collection<DataNode> newListElements =
142 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
143 replaceListContent(dataspaceName, anchorName, parentNodeXpath, newListElements, observedTimestamp);
147 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
148 final Collection<DataNode> dataNodes, final OffsetDateTime observedTimestamp) {
149 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
150 cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, dataNodes);
151 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
155 public void deleteDataNode(final String dataspaceName, final String anchorName, final String dataNodeXpath,
156 final OffsetDateTime observedTimestamp) {
157 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
158 cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
159 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE);
163 public void deleteDataNodes(final String dataspaceName, final String anchorName,
164 final OffsetDateTime observedTimestamp) {
165 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
166 final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
167 cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName);
168 processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, Operation.DELETE, observedTimestamp);
172 public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath,
173 final OffsetDateTime observedTimestamp) {
174 CpsValidator.validateNameCharacters(dataspaceName, anchorName);
175 cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath);
176 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, Operation.DELETE);
179 private DataNode buildDataNode(final String dataspaceName, final String anchorName,
180 final String parentNodeXpath, final String jsonData) {
182 final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
183 final SchemaContext schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
185 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
186 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
187 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
190 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
191 return new DataNodeBuilder()
192 .withParentNodeXpath(parentNodeXpath)
193 .withNormalizedNodeTree(normalizedNode)
197 private Collection<DataNode> buildDataNodes(final String dataspaceName,
198 final String anchorName,
199 final String parentNodeXpath,
200 final String jsonData) {
202 final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
203 final SchemaContext schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
205 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
206 final Collection<DataNode> dataNodes = new DataNodeBuilder()
207 .withParentNodeXpath(parentNodeXpath)
208 .withNormalizedNodeTree(normalizedNode)
210 if (dataNodes.isEmpty()) {
211 throw new DataValidationException("Invalid data.", "No data nodes provided");
217 private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
218 final OffsetDateTime observedTimestamp, final String xpath,
219 final Operation operation) {
220 final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
221 this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp);
224 private void processDataUpdatedEventAsync(final Anchor anchor, final String xpath, final Operation operation,
225 final OffsetDateTime observedTimestamp) {
227 notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, operation);
228 } catch (final Exception exception) {
229 //If async message can't be queued for notification service, the initial request should not failed.
230 log.error("Failed to send message to notification service", exception);
234 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
235 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
238 private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
239 final DataNode dataNodeUpdate) {
240 if (dataNodeUpdate == null) {
243 cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
244 dataNodeUpdate.getLeaves());
245 final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
246 for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
247 processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);