2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * Modifications (C) 2021 Nordix Foundation
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.rest.controller;
24 import org.onap.cps.api.CpsDataService;
25 import org.onap.cps.rest.api.CpsDataApi;
26 import org.onap.cps.spi.FetchDescendantsOption;
27 import org.onap.cps.utils.DataMapUtils;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.web.bind.annotation.RequestMapping;
32 import org.springframework.web.bind.annotation.RestController;
35 @RequestMapping("${rest.api.cps-base-path}")
36 public class DataRestController implements CpsDataApi {
38 private static final String ROOT_XPATH = "/";
41 private CpsDataService cpsDataService;
44 public ResponseEntity<String> createNode(final String jsonData, final String dataspaceName, final String anchorName,
45 final String parentNodeXpath) {
46 if (isRootXpath(parentNodeXpath)) {
47 cpsDataService.saveData(dataspaceName, anchorName, jsonData);
49 cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData);
51 return new ResponseEntity<>(HttpStatus.CREATED);
55 public ResponseEntity<Object> getNodesByDataspace(final String dataspaceName) {
60 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
61 final String xpath, final Boolean includeDescendants) {
62 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
63 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
64 final var dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
65 fetchDescendantsOption);
66 return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
70 public ResponseEntity<Object> updateNodeLeaves(final String jsonData, final String dataspaceName,
71 final String anchorName, final String parentNodeXpath) {
72 cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData);
73 return new ResponseEntity<>(HttpStatus.OK);
77 public ResponseEntity<Object> replaceNode(final String jsonData, final String dataspaceName,
78 final String anchorName, final String parentNodeXpath) {
79 cpsDataService.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData);
80 return new ResponseEntity<>(HttpStatus.OK);
83 private static boolean isRootXpath(final String xpath) {
84 return ROOT_XPATH.equals(xpath);