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 if (dataNodesCollection.isEmpty()) {
75 throw new DataValidationException("Invalid list data.", "List node is empty.");
77 cpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodesCollection);
81 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
82 final FetchDescendantsOption fetchDescendantsOption) {
83 return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
87 public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
88 final String jsonData) {
89 final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
90 cpsDataPersistenceService
91 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
95 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
96 final String jsonData) {
97 final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
98 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
101 private DataNode buildDataNodeFromJson(final String dataspaceName, final String anchorName,
102 final String parentNodeXpath, final String jsonData) {
104 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
105 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
107 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
108 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
109 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
112 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
113 return new DataNodeBuilder()
114 .withParentNodeXpath(parentNodeXpath)
115 .withNormalizedNodeTree(normalizedNode)
119 private Collection<DataNode> buildDataNodeCollectionFromJson(final String dataspaceName, final String anchorName,
120 final String parentNodeXpath, final String jsonData) {
122 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
123 final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
125 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
126 return new DataNodeBuilder()
127 .withParentNodeXpath(parentNodeXpath)
128 .withNormalizedNodeTree(normalizedNode)
132 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
133 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();