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.YangUtils;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.springframework.stereotype.Service;
47 public class CpsDataServiceImpl implements CpsDataService {
49 private static final String ROOT_NODE_XPATH = "/";
51 private final CpsDataPersistenceService cpsDataPersistenceService;
52 private final CpsAdminService cpsAdminService;
53 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
54 private final NotificationService notificationService;
57 public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
58 final OffsetDateTime observedTimestamp) {
59 final var dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
60 cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
61 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, ROOT_NODE_XPATH, Operation.CREATE);
65 public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
66 final String jsonData, final OffsetDateTime observedTimestamp) {
67 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
68 cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
69 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.CREATE);
73 public void saveListElements(final String dataspaceName, final String anchorName,
74 final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
75 final Collection<DataNode> listElementDataNodeCollection =
76 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
77 cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
78 listElementDataNodeCollection);
79 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
83 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
84 final FetchDescendantsOption fetchDescendantsOption) {
85 return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
89 public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
90 final String jsonData, final OffsetDateTime observedTimestamp) {
91 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
92 cpsDataPersistenceService
93 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
94 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
98 public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
99 final String parentNodeXpath,
100 final String dataNodeUpdatesAsJson,
101 final OffsetDateTime observedTimestamp) {
102 final Collection<DataNode> dataNodeUpdates =
103 buildDataNodes(dataspaceName, anchorName,
104 parentNodeXpath, dataNodeUpdatesAsJson);
105 for (final DataNode dataNodeUpdate : dataNodeUpdates) {
106 processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
108 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
112 public String startSession() {
113 final String sessionId = cpsDataPersistenceService.startSession();
118 public void closeSession(final String sessionId) {
119 cpsDataPersistenceService.closeSession(sessionId);
123 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
124 final String jsonData, final OffsetDateTime observedTimestamp) {
125 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
126 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
127 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
131 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
132 final String jsonData, final OffsetDateTime observedTimestamp) {
133 final Collection<DataNode> newListElements =
134 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
135 replaceListContent(dataspaceName, anchorName, parentNodeXpath, newListElements, observedTimestamp);
139 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
140 final Collection<DataNode> dataNodes, final OffsetDateTime observedTimestamp) {
141 cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, dataNodes);
142 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
146 public void deleteDataNode(final String dataspaceName, final String anchorName, final String dataNodeXpath,
147 final OffsetDateTime observedTimestamp) {
148 cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
149 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE);
153 public void deleteDataNodes(final String dataspaceName, final String anchorName,
154 final OffsetDateTime observedTimestamp) {
155 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
156 cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName);
157 processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, Operation.DELETE, observedTimestamp);
161 public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath,
162 final OffsetDateTime observedTimestamp) {
163 cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath);
164 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, Operation.DELETE);
167 private DataNode buildDataNode(final String dataspaceName, final String anchorName,
168 final String parentNodeXpath, final String jsonData) {
170 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
171 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
173 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
174 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
175 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
178 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
179 return new DataNodeBuilder()
180 .withParentNodeXpath(parentNodeXpath)
181 .withNormalizedNodeTree(normalizedNode)
185 private Collection<DataNode> buildDataNodes(final String dataspaceName,
186 final String anchorName,
187 final String parentNodeXpath,
188 final String jsonData) {
190 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
191 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
193 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
194 final Collection<DataNode> dataNodes = new DataNodeBuilder()
195 .withParentNodeXpath(parentNodeXpath)
196 .withNormalizedNodeTree(normalizedNode)
198 if (dataNodes.isEmpty()) {
199 throw new DataValidationException("Invalid data.", "No data nodes provided");
205 private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
206 final OffsetDateTime observedTimestamp, final String xpath,
207 final Operation operation) {
208 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
209 this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp);
212 private void processDataUpdatedEventAsync(final Anchor anchor, final String xpath, final Operation operation,
213 final OffsetDateTime observedTimestamp) {
215 notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, operation);
216 } catch (final Exception exception) {
217 //If async message can't be queued for notification service, the initial request should not failed.
218 log.error("Failed to send message to notification service", exception);
222 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
223 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
226 private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
227 final DataNode dataNodeUpdate) {
228 if (dataNodeUpdate == null) {
231 cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
232 dataNodeUpdate.getLeaves());
233 final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
234 for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
235 processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);