Get Data under anchor using single root
[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  *  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
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.spi.model.DataNode;
28 import org.onap.cps.utils.DataMapUtils;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.web.bind.annotation.RequestMapping;
33 import org.springframework.web.bind.annotation.RestController;
34
35 @RestController
36 @RequestMapping("${rest.api.cps-base-path}")
37 public class DataRestController implements CpsDataApi {
38
39     private static final String ROOT_XPATH = "/";
40
41     @Autowired
42     private CpsDataService cpsDataService;
43
44     @Override
45     public ResponseEntity<String> createNode(final String jsonData, final String dataspaceName, final String anchorName,
46         final String parentNodeXpath) {
47         if (isRootXpath(parentNodeXpath)) {
48             cpsDataService.saveData(dataspaceName, anchorName, jsonData);
49         } else {
50             cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData);
51         }
52         return new ResponseEntity<>(HttpStatus.CREATED);
53     }
54
55     @Override
56     public ResponseEntity<Object> getNodesByDataspace(final String dataspaceName) {
57         return null;
58     }
59
60     @Override
61     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
62         final String xpath, final Boolean includeDescendants) {
63         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
64             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
65         final DataNode dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
66                 fetchDescendantsOption);
67         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
68     }
69
70     @Override
71     public ResponseEntity<Object> updateNodeLeaves(final String jsonData, final String dataspaceName,
72         final String anchorName, final String parentNodeXpath) {
73         cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData);
74         return new ResponseEntity<>(HttpStatus.OK);
75     }
76
77     @Override
78     public ResponseEntity<Object> replaceNode(final String jsonData, final String dataspaceName,
79         final String anchorName, final String parentNodeXpath) {
80         cpsDataService.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData);
81         return new ResponseEntity<>(HttpStatus.OK);
82     }
83
84     private static boolean isRootXpath(final String xpath) {
85         return ROOT_XPATH.equals(xpath);
86     }
87 }