Merge "Increase minimum coverage"
[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         if (isRootXpath(xpath)) {
64             return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
65         }
66         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
67             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
68         final DataNode dataNode =
69             cpsDataService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
70         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
71     }
72
73     @Override
74     public ResponseEntity<Object> updateNodeLeaves(final String jsonData, final String dataspaceName,
75         final String anchorName, final String parentNodeXpath) {
76         cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData);
77         return new ResponseEntity<>(HttpStatus.OK);
78     }
79
80     @Override
81     public ResponseEntity<Object> replaceNode(final String jsonData, final String dataspaceName,
82         final String anchorName, final String parentNodeXpath) {
83         cpsDataService.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData);
84         return new ResponseEntity<>(HttpStatus.OK);
85     }
86
87     private static boolean isRootXpath(final String xpath) {
88         return ROOT_XPATH.equals(xpath);
89     }
90 }