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 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 replaceListContent(dataspaceName, anchorName, parentNodeXpath, newListElements, observedTimestamp);
128 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
129 final Collection<DataNode> dataNodes, final OffsetDateTime observedTimestamp) {
130 cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, dataNodes);
131 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
135 public void deleteDataNode(final String dataspaceName, final String anchorName, final String dataNodeXpath,
136 final OffsetDateTime observedTimestamp) {
137 cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
138 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE);
142 public void deleteDataNodes(final String dataspaceName, final String anchorName,
143 final OffsetDateTime observedTimestamp) {
144 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
145 cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName);
146 processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, Operation.DELETE, observedTimestamp);
150 public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath,
151 final OffsetDateTime observedTimestamp) {
152 cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath);
153 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, Operation.DELETE);
156 private DataNode buildDataNode(final String dataspaceName, final String anchorName,
157 final String parentNodeXpath, final String jsonData) {
159 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
160 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
162 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
163 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
164 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
167 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
168 return new DataNodeBuilder()
169 .withParentNodeXpath(parentNodeXpath)
170 .withNormalizedNodeTree(normalizedNode)
174 private Collection<DataNode> buildDataNodes(final String dataspaceName,
175 final String anchorName,
176 final String parentNodeXpath,
177 final String jsonData) {
179 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
180 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
182 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
183 final Collection<DataNode> dataNodes = new DataNodeBuilder()
184 .withParentNodeXpath(parentNodeXpath)
185 .withNormalizedNodeTree(normalizedNode)
187 if (dataNodes.isEmpty()) {
188 throw new DataValidationException("Invalid data.", "No data nodes provided");
194 private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
195 final OffsetDateTime observedTimestamp, final String xpath,
196 final Operation operation) {
197 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
198 this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp);
201 private void processDataUpdatedEventAsync(final Anchor anchor, final String xpath, final Operation operation,
202 final OffsetDateTime observedTimestamp) {
204 notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, operation);
205 } catch (final Exception exception) {
206 //If async message can't be queued for notification service, the initial request should not failed.
207 log.error("Failed to send message to notification service", exception);
211 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
212 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
215 private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
216 final DataNode dataNodeUpdate) {
217 if (dataNodeUpdate == null) {
220 cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
221 dataNodeUpdate.getLeaves());
222 final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
223 for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
224 processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);