2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2020-2021 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.api.CpsModuleService;
31 import org.onap.cps.notification.NotificationService;
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 CpsModuleService cpsModuleService;
59 private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
62 private NotificationService notificationService;
65 public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
66 final OffsetDateTime observedTimestamp) {
67 final var dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
68 cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
69 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
73 public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
74 final String jsonData, final OffsetDateTime observedTimestamp) {
75 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
76 cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
77 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
81 public void saveListElements(final String dataspaceName, final String anchorName,
82 final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
83 final Collection<DataNode> listElementDataNodeCollection =
84 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
85 cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
86 listElementDataNodeCollection);
87 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
91 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
92 final FetchDescendantsOption fetchDescendantsOption) {
93 return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
97 public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
98 final String jsonData, final OffsetDateTime observedTimestamp) {
99 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
100 cpsDataPersistenceService
101 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
102 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
106 public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
107 final String parentNodeXpath,
108 final String dataNodeUpdatesAsJson,
109 final OffsetDateTime observedTimestamp) {
110 final Collection<DataNode> dataNodeUpdates =
111 buildDataNodes(dataspaceName, anchorName,
112 parentNodeXpath, dataNodeUpdatesAsJson);
113 for (final DataNode dataNodeUpdate : dataNodeUpdates) {
114 processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
116 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
120 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
121 final String jsonData, final OffsetDateTime observedTimestamp) {
122 final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
123 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
124 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
128 public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
129 final String jsonData, final OffsetDateTime observedTimestamp) {
130 final Collection<DataNode> newListElements =
131 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
132 cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, newListElements);
133 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
137 public void deleteDataNode(final String dataspaceName, final String anchorName, final String dataNodeXpath,
138 final OffsetDateTime observedTimestamp) {
139 cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
140 processDataUpdatedEventAsync(dataspaceName, anchorName, 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);
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) {
191 notificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp);
192 } catch (final Exception exception) {
193 log.error("Failed to send message to notification service", exception);
197 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
198 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
201 private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
202 final DataNode dataNodeUpdate) {
203 if (dataNodeUpdate == null) {
206 cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
207 dataNodeUpdate.getLeaves());
208 final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
209 for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
210 processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);