5c79472a4c1600e1ab1faed26094727525f7c009
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / DataRestController.java
1 /*
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
10  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.rest.controller;
23
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;
35
36 @RestController
37 @RequestMapping("${rest.api.cps-base-path}")
38 public class DataRestController implements CpsDataApi {
39
40     private static final String ROOT_XPATH = "/";
41
42     @Autowired
43     private CpsDataService cpsDataService;
44
45     @Override
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);
50         } else {
51             cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData);
52         }
53         return new ResponseEntity<>(HttpStatus.CREATED);
54     }
55
56     @Override
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);
61     }
62
63     @Override
64     public ResponseEntity<Object> getNodesByDataspace(final String dataspaceName) {
65         return null;
66     }
67
68     @Override
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);
76     }
77
78     @Override
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);
83     }
84
85     @Override
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);
90     }
91
92     @Override
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);
97     }
98
99     @Override
100     public ResponseEntity<Void> deleteListNodeElements(final String dataspaceName, final String anchorName,
101                                                          final String listNodeXpath) {
102         cpsDataService.deleteListNodeData(dataspaceName, anchorName, listNodeXpath);
103         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
104     }
105
106     private static boolean isRootXpath(final String xpath) {
107         return ROOT_XPATH.equals(xpath);
108     }
109 }