2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
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
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.api.impl;
24 import java.util.Collection;
25 import org.onap.cps.api.CpsAdminService;
26 import org.onap.cps.api.CpsDataService;
27 import org.onap.cps.api.CpsModuleService;
28 import org.onap.cps.spi.CpsDataPersistenceService;
29 import org.onap.cps.spi.FetchDescendantsOption;
30 import org.onap.cps.spi.exceptions.DataValidationException;
31 import org.onap.cps.spi.model.DataNode;
32 import org.onap.cps.spi.model.DataNodeBuilder;
33 import org.onap.cps.utils.YangUtils;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Service;
40 public class CpsDataServiceImpl implements CpsDataService {
42 private static final String ROOT_NODE_XPATH = "/";
45 private CpsDataPersistenceService cpsDataPersistenceService;
48 private CpsAdminService cpsAdminService;
51 private CpsModuleService cpsModuleService;
54 private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
57 public void saveData(final String dataspaceName, final String anchorName, final String jsonData) {
58 final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
59 cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
63 public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
64 final String jsonData) {
65 final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
66 cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
70 public void saveListNodeData(final String dataspaceName, final String anchorName,
71 final String parentNodeXpath, final String jsonData) {
72 final Collection<DataNode> dataNodesCollection =
73 buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
74 cpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodesCollection);
78 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
79 final FetchDescendantsOption fetchDescendantsOption) {
80 return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
84 public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
85 final String jsonData) {
86 final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
87 cpsDataPersistenceService
88 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
92 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
93 final String jsonData) {
94 final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
95 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
99 public void replaceListNodeData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
100 final String jsonData) {
101 final Collection<DataNode> dataNodes =
102 buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
103 cpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodes);
106 private DataNode buildDataNodeFromJson(final String dataspaceName, final String anchorName,
107 final String parentNodeXpath, final String jsonData) {
109 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
110 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
112 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
113 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
114 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
117 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
118 return new DataNodeBuilder()
119 .withParentNodeXpath(parentNodeXpath)
120 .withNormalizedNodeTree(normalizedNode)
124 private Collection<DataNode> buildDataNodeCollectionFromJson(final String dataspaceName, final String anchorName,
125 final String parentNodeXpath, final String jsonData) {
127 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
128 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
130 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
131 final Collection<DataNode> dataNodes = new DataNodeBuilder()
132 .withParentNodeXpath(parentNodeXpath)
133 .withNormalizedNodeTree(normalizedNode)
135 if (dataNodes.isEmpty()) {
136 throw new DataValidationException("Invalid list data.", "List node is empty.");
142 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
143 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();