2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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 void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
113 final String jsonData, final OffsetDateTime observedTimestamp) {
114 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
115 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
116 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
120 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
121 final String jsonData, final OffsetDateTime observedTimestamp) {
122 final Collection<DataNode> newListElements =
123 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
124 cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, newListElements);
125 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
129 public void deleteDataNode(final String dataspaceName, final String anchorName, final String dataNodeXpath,
130 final OffsetDateTime observedTimestamp) {
131 cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
132 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE);
136 public void deleteDataNodes(final String dataspaceName, final String anchorName,
137 final OffsetDateTime observedTimestamp) {
138 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
139 cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName);
140 processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, Operation.DELETE, observedTimestamp);
144 public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath,
145 final OffsetDateTime observedTimestamp) {
146 cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath);
147 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, Operation.DELETE);
150 private DataNode buildDataNode(final String dataspaceName, final String anchorName,
151 final String parentNodeXpath, final String jsonData) {
153 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
154 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
156 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
157 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
158 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
161 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
162 return new DataNodeBuilder()
163 .withParentNodeXpath(parentNodeXpath)
164 .withNormalizedNodeTree(normalizedNode)
168 private Collection<DataNode> buildDataNodes(final String dataspaceName,
169 final String anchorName,
170 final String parentNodeXpath,
171 final String jsonData) {
173 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
174 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
176 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
177 final Collection<DataNode> dataNodes = new DataNodeBuilder()
178 .withParentNodeXpath(parentNodeXpath)
179 .withNormalizedNodeTree(normalizedNode)
181 if (dataNodes.isEmpty()) {
182 throw new DataValidationException("Invalid data.", "No data nodes provided");
188 private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
189 final OffsetDateTime observedTimestamp, final String xpath,
190 final Operation operation) {
191 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
192 this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp);
195 private void processDataUpdatedEventAsync(final Anchor anchor, final String xpath, final Operation operation,
196 final OffsetDateTime observedTimestamp) {
198 notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, operation);
199 } catch (final Exception exception) {
200 //If async message can't be queued for notification service, the initial request should not failed.
201 log.error("Failed to send message to notification service", exception);
205 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
206 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
209 private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
210 final DataNode dataNodeUpdate) {
211 if (dataNodeUpdate == null) {
214 cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
215 dataNodeUpdate.getLeaves());
216 final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
217 for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
218 processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);