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.DataNode;
37 import org.onap.cps.spi.model.DataNodeBuilder;
38 import org.onap.cps.utils.YangUtils;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.springframework.stereotype.Service;
46 public class CpsDataServiceImpl implements CpsDataService {
48 private static final String ROOT_NODE_XPATH = "/";
50 private final CpsDataPersistenceService cpsDataPersistenceService;
51 private final CpsAdminService cpsAdminService;
52 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
53 private final NotificationService notificationService;
56 public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
57 final OffsetDateTime observedTimestamp) {
58 final var dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
59 cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
60 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, ROOT_NODE_XPATH, Operation.CREATE);
64 public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
65 final String jsonData, final OffsetDateTime observedTimestamp) {
66 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
67 cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
68 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.CREATE);
72 public void saveListElements(final String dataspaceName, final String anchorName,
73 final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
74 final Collection<DataNode> listElementDataNodeCollection =
75 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
76 cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
77 listElementDataNodeCollection);
78 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
82 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
83 final FetchDescendantsOption fetchDescendantsOption) {
84 return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
88 public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
89 final String jsonData, final OffsetDateTime observedTimestamp) {
90 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
91 cpsDataPersistenceService
92 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
93 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
97 public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
98 final String parentNodeXpath,
99 final String dataNodeUpdatesAsJson,
100 final OffsetDateTime observedTimestamp) {
101 final Collection<DataNode> dataNodeUpdates =
102 buildDataNodes(dataspaceName, anchorName,
103 parentNodeXpath, dataNodeUpdatesAsJson);
104 for (final DataNode dataNodeUpdate : dataNodeUpdates) {
105 processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
107 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
111 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
112 final String jsonData, final OffsetDateTime observedTimestamp) {
113 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
114 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
115 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
119 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
120 final String jsonData, final OffsetDateTime observedTimestamp) {
121 final Collection<DataNode> newListElements =
122 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
123 cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, newListElements);
124 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
128 public void deleteDataNode(final String dataspaceName, final String anchorName, final String dataNodeXpath,
129 final OffsetDateTime observedTimestamp) {
130 cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
131 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE);
135 public void deleteDataNodes(final String dataspaceName, final String anchorName,
136 final OffsetDateTime observedTimestamp) {
137 cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName);
141 public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath,
142 final OffsetDateTime observedTimestamp) {
143 cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath);
144 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, Operation.DELETE);
147 private DataNode buildDataNode(final String dataspaceName, final String anchorName,
148 final String parentNodeXpath, final String jsonData) {
150 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
151 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
153 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
154 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
155 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
158 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
159 return new DataNodeBuilder()
160 .withParentNodeXpath(parentNodeXpath)
161 .withNormalizedNodeTree(normalizedNode)
165 private Collection<DataNode> buildDataNodes(final String dataspaceName,
166 final String anchorName,
167 final String parentNodeXpath,
168 final String jsonData) {
170 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
171 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
173 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
174 final Collection<DataNode> dataNodes = new DataNodeBuilder()
175 .withParentNodeXpath(parentNodeXpath)
176 .withNormalizedNodeTree(normalizedNode)
178 if (dataNodes.isEmpty()) {
179 throw new DataValidationException("Invalid data.", "No data nodes provided");
185 private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
186 final OffsetDateTime observedTimestamp, final String xpath,
187 final Operation operation) {
189 notificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, xpath, operation);
190 } catch (final Exception exception) {
191 log.error("Failed to send message to notification service", exception);
195 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
196 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
199 private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
200 final DataNode dataNodeUpdate) {
201 if (dataNodeUpdate == null) {
204 cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
205 dataNodeUpdate.getLeaves());
206 final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
207 for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
208 processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);