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 org.onap.cps.api.CpsAdminService;
25 import org.onap.cps.api.CpsDataService;
26 import org.onap.cps.api.CpsModuleService;
27 import org.onap.cps.spi.CpsDataPersistenceService;
28 import org.onap.cps.spi.FetchDescendantsOption;
29 import org.onap.cps.spi.model.Anchor;
30 import org.onap.cps.spi.model.DataNode;
31 import org.onap.cps.spi.model.DataNodeBuilder;
32 import org.onap.cps.utils.YangUtils;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
39 public class CpsDataServiceImpl implements CpsDataService {
41 private static final String ROOT_NODE_XPATH = "/";
44 private CpsDataPersistenceService cpsDataPersistenceService;
47 private CpsAdminService cpsAdminService;
50 private CpsModuleService cpsModuleService;
53 private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
56 public void saveData(final String dataspaceName, final String anchorName, final String jsonData) {
57 final DataNode dataNode = buildDataNodeFromJson(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
58 cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
62 public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
63 final String jsonData) {
64 final DataNode dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
65 cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
69 public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
70 final FetchDescendantsOption fetchDescendantsOption) {
71 return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
75 public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
76 final String jsonData) {
77 final DataNode dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
78 cpsDataPersistenceService
79 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
83 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
84 final String jsonData) {
85 final DataNode dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
86 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
89 private DataNode buildDataNodeFromJson(final String dataspaceName, final String anchorName,
90 final String parentNodeXpath, final String jsonData) {
92 final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
93 final SchemaContext schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
95 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
96 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
97 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
100 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
101 return new DataNodeBuilder()
102 .withParentNodeXpath(parentNodeXpath)
103 .withNormalizedNodeTree(normalizedNode)
107 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
108 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();