2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Bell Canada.
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * Modifications Copyright (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 javax.validation.Valid;
25 import javax.validation.constraints.NotNull;
26 import org.onap.cps.api.CpsDataService;
27 import org.onap.cps.rest.api.CpsDataApi;
28 import org.onap.cps.spi.FetchDescendantsOption;
29 import org.onap.cps.utils.DataMapUtils;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RestController;
37 @RequestMapping("${rest.api.cps-base-path}")
38 public class DataRestController implements CpsDataApi {
40 private static final String ROOT_XPATH = "/";
43 private CpsDataService cpsDataService;
46 public ResponseEntity<String> createNode(final String dataspaceName, final String anchorName,
47 final String jsonData, final String parentNodeXpath) {
48 if (isRootXpath(parentNodeXpath)) {
49 cpsDataService.saveData(dataspaceName, anchorName, jsonData);
51 cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData);
53 return new ResponseEntity<>(HttpStatus.CREATED);
57 public ResponseEntity<String> addListNodeElements(final String parentNodeXpath,
58 final String dataspaceName, final String anchorName, final String jsonData) {
59 cpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData);
60 return new ResponseEntity<>(HttpStatus.CREATED);
64 public ResponseEntity<Object> getNodesByDataspace(final String dataspaceName) {
69 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
70 final String xpath, final Boolean includeDescendants) {
71 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
72 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
73 final var dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
74 fetchDescendantsOption);
75 return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
79 public ResponseEntity<Object> updateNodeLeaves(final String dataspaceName,
80 final String anchorName, final String jsonData, final String parentNodeXpath) {
81 cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData);
82 return new ResponseEntity<>(HttpStatus.OK);
86 public ResponseEntity<Object> replaceNode(final String dataspaceName,
87 final String anchorName, @Valid final String jsonData, @Valid final String parentNodeXpath) {
88 cpsDataService.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData);
89 return new ResponseEntity<>(HttpStatus.OK);
93 public ResponseEntity<String> replaceListNodeElements(@NotNull @Valid final String parentNodeXpath,
94 final String dataspaceName, final String anchorName, @Valid final String jsonData) {
95 cpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData);
96 return new ResponseEntity<>(HttpStatus.OK);
99 private static boolean isRootXpath(final String xpath) {
100 return ROOT_XPATH.equals(xpath);