64b441829d747231a493ae0afac2d4e0e085726f
[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 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;
33
34 @RestController
35 @RequestMapping("${rest.api.cps-base-path}")
36 public class DataRestController implements CpsDataApi {
37
38     private static final String ROOT_XPATH = "/";
39
40     @Autowired
41     private CpsDataService cpsDataService;
42
43     @Override
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);
48         } else {
49             cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData);
50         }
51         return new ResponseEntity<>(HttpStatus.CREATED);
52     }
53
54     @Override
55     public ResponseEntity<String> addListNodeElements(final String jsonData, final String parentNodeXpath,
56         final String dataspaceName, final String anchorName) {
57         cpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData);
58         return new ResponseEntity<>(HttpStatus.CREATED);
59     }
60
61     @Override
62     public ResponseEntity<Object> getNodesByDataspace(final String dataspaceName) {
63         return null;
64     }
65
66     @Override
67     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
68         final String xpath, final Boolean includeDescendants) {
69         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
70             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
71         final var dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
72             fetchDescendantsOption);
73         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
74     }
75
76     @Override
77     public ResponseEntity<Object> updateNodeLeaves(final String jsonData, final String dataspaceName,
78         final String anchorName, final String parentNodeXpath) {
79         cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData);
80         return new ResponseEntity<>(HttpStatus.OK);
81     }
82
83     @Override
84     public ResponseEntity<Object> replaceNode(final String jsonData, final String dataspaceName,
85         final String anchorName, final String parentNodeXpath) {
86         cpsDataService.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData);
87         return new ResponseEntity<>(HttpStatus.OK);
88     }
89
90     @Override
91     public ResponseEntity<String> replaceListNodeElements(final String jsonData, final String parentNodeXpath,
92         final String dataspaceName, final String anchorName) {
93         cpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData);
94         return new ResponseEntity<>(HttpStatus.OK);
95     }
96
97     private static boolean isRootXpath(final String xpath) {
98         return ROOT_XPATH.equals(xpath);
99     }
100 }