Data fragment update by xpath #3 - rest and service layers
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / DataRestController.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Bell Canada. All rights reserved.
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.rest.controller;
22
23 import javax.validation.Valid;
24 import javax.validation.constraints.NotNull;
25 import org.onap.cps.api.CpsDataService;
26 import org.onap.cps.rest.api.CpsDataApi;
27 import org.onap.cps.spi.FetchDescendantsOption;
28 import org.onap.cps.spi.model.DataNode;
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;
35
36 @RestController
37 @RequestMapping("${rest.api.cps-base-path}")
38 public class DataRestController implements CpsDataApi {
39
40     @Autowired
41     private CpsDataService cpsDataService;
42
43     @Override
44     public ResponseEntity<String> createNode(@Valid final String jsonData, @NotNull final String dataspaceName,
45         @NotNull @Valid final String anchorName) {
46         cpsDataService.saveData(dataspaceName, anchorName, jsonData);
47         return new ResponseEntity<>(HttpStatus.CREATED);
48     }
49
50     @Override
51     public ResponseEntity<Object> getNodesByDataspace(final String dataspaceName) {
52         return null;
53     }
54
55     @Override
56     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
57         final String xpath, final Boolean includeDescendants) {
58         if ("/".equals(xpath)) {
59             // TODO: extracting data by anchor only (root data node and below)
60             return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
61         }
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);
67     }
68
69     @Override
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);
74     }
75
76     @Override
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);
81     }
82 }