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 DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
63 final FetchDescendantsOption fetchDescendantsOption) {
64 return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
68 public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
69 final String jsonData) {
70 final DataNode dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
71 cpsDataPersistenceService
72 .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
76 public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
77 final String jsonData) {
78 final DataNode dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
79 cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
82 private DataNode buildDataNodeFromJson(final String dataspaceName, final String anchorName,
83 final String parentNodeXpath, final String jsonData) {
85 final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
86 final SchemaContext schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
88 if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
89 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
90 return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
93 final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
94 return new DataNodeBuilder()
95 .withParentNodeXpath(parentNodeXpath)
96 .withNormalizedNodeTree(normalizedNode)
100 private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
101 return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();