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 = buildDataNodeFromJson(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 = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
76 cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
77 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
81 public void saveListNodeData(final String dataspaceName, final String anchorName,
82 final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
83 final Collection<DataNode> dataNodesCollection =
84 buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
85 cpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodesCollection);
86 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
90 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
91 final FetchDescendantsOption fetchDescendantsOption) {
92 return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
96 public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
97 final String jsonData, final OffsetDateTime observedTimestamp) {
98 final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
99 cpsDataPersistenceService
100 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
101 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
105 public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
106 final String parentNodeXpath,
107 final String dataNodeUpdatesAsJson,
108 final OffsetDateTime observedTimestamp) {
109 final Collection<DataNode> dataNodeUpdates =
110 buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, dataNodeUpdatesAsJson);
111 for (final DataNode dataNodeUpdate : dataNodeUpdates) {
112 processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
114 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
118 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
119 final String jsonData, final OffsetDateTime observedTimestamp) {
120 final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
121 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
122 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
126 public void replaceListNodeData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
127 final String jsonData, final OffsetDateTime observedTimestamp) {
128 final Collection<DataNode> dataNodes =
129 buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
130 cpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodes);
131 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
135 public void deleteListNodeData(final String dataspaceName, final String anchorName, final String listNodeXpath,
136 final OffsetDateTime observedTimestamp) {
137 cpsDataPersistenceService.deleteListDataNodes(dataspaceName, anchorName, listNodeXpath);
138 processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
142 private DataNode buildDataNodeFromJson(final String dataspaceName, final String anchorName,
143 final String parentNodeXpath, final String jsonData) {
145 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
146 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
148 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
149 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
150 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
153 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
154 return new DataNodeBuilder()
155 .withParentNodeXpath(parentNodeXpath)
156 .withNormalizedNodeTree(normalizedNode)
160 private Collection<DataNode> buildDataNodeCollectionFromJson(final String dataspaceName, final String anchorName,
161 final String parentNodeXpath, final String jsonData) {
163 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
164 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
166 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
167 final Collection<DataNode> dataNodes = new DataNodeBuilder()
168 .withParentNodeXpath(parentNodeXpath)
169 .withNormalizedNodeTree(normalizedNode)
171 if (dataNodes.isEmpty()) {
172 throw new DataValidationException("Invalid list data.", "List node is empty.");
178 private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
179 final OffsetDateTime observedTimestamp) {
181 notificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp);
182 } catch (final Exception exception) {
183 log.error("Failed to send message to notification service", exception);
187 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
188 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
191 private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
192 final DataNode dataNodeUpdate) {
193 if (dataNodeUpdate == null) {
196 cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
197 dataNodeUpdate.getLeaves());
198 final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
199 for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
200 processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);