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 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.spi.model.DataNode;
30 import org.onap.cps.utils.DataMapUtils;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RestController;
38 @RequestMapping("${rest.api.cps-base-path}")
39 public class DataRestController implements CpsDataApi {
42 private CpsDataService cpsDataService;
45 public ResponseEntity<String> createNode(@Valid final String jsonData, @NotNull final String dataspaceName,
46 @NotNull @Valid final String anchorName) {
47 cpsDataService.saveData(dataspaceName, anchorName, jsonData);
48 return new ResponseEntity<>(HttpStatus.CREATED);
52 public ResponseEntity<Object> getNodesByDataspace(final String dataspaceName) {
57 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
58 final String xpath, final Boolean includeDescendants) {
59 if ("/".equals(xpath)) {
60 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
62 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
63 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
64 final DataNode dataNode =
65 cpsDataService.getDataNode(dataspaceName, anchorName, xpath, 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);