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.extern.slf4j.Slf4j;
28 import org.onap.cps.api.CpsAdminService;
29 import org.onap.cps.api.CpsDataService;
30 import org.onap.cps.notification.NotificationService;
31 import org.onap.cps.notification.Operation;
32 import org.onap.cps.spi.CpsDataPersistenceService;
33 import org.onap.cps.spi.FetchDescendantsOption;
34 import org.onap.cps.spi.exceptions.DataValidationException;
35 import org.onap.cps.spi.model.DataNode;
36 import org.onap.cps.spi.model.DataNodeBuilder;
37 import org.onap.cps.utils.YangUtils;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Service;
45 public class CpsDataServiceImpl implements CpsDataService {
47 private static final String ROOT_NODE_XPATH = "/";
50 private CpsDataPersistenceService cpsDataPersistenceService;
53 private CpsAdminService cpsAdminService;
56 private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
59 private NotificationService notificationService;
62 public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
63 final OffsetDateTime observedTimestamp) {
64 final var dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
65 cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
66 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, ROOT_NODE_XPATH, Operation.CREATE);
70 public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
71 final String jsonData, final OffsetDateTime observedTimestamp) {
72 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
73 cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
74 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.CREATE);
78 public void saveListElements(final String dataspaceName, final String anchorName,
79 final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
80 final Collection<DataNode> listElementDataNodeCollection =
81 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
82 cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
83 listElementDataNodeCollection);
84 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
88 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
89 final FetchDescendantsOption fetchDescendantsOption) {
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 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
97 cpsDataPersistenceService
98 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
99 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
103 public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
104 final String parentNodeXpath,
105 final String dataNodeUpdatesAsJson,
106 final OffsetDateTime observedTimestamp) {
107 final Collection<DataNode> dataNodeUpdates =
108 buildDataNodes(dataspaceName, anchorName,
109 parentNodeXpath, dataNodeUpdatesAsJson);
110 for (final DataNode dataNodeUpdate : dataNodeUpdates) {
111 processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
113 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
117 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
118 final String jsonData, final OffsetDateTime observedTimestamp) {
119 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
120 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
121 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
125 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
126 final String jsonData, final OffsetDateTime observedTimestamp) {
127 final Collection<DataNode> newListElements =
128 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
129 cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, newListElements);
130 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
134 public void deleteDataNode(final String dataspaceName, final String anchorName, final String dataNodeXpath,
135 final OffsetDateTime observedTimestamp) {
136 cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
137 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE);
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);