Clean Up Code around List Nodes
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / DataRestController.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 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  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.rest.controller;
24
25 import java.time.OffsetDateTime;
26 import java.time.format.DateTimeFormatter;
27 import javax.validation.ValidationException;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.cps.api.CpsDataService;
30 import org.onap.cps.rest.api.CpsDataApi;
31 import org.onap.cps.spi.FetchDescendantsOption;
32 import org.onap.cps.utils.DataMapUtils;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RestController;
38
39 @RestController
40 @RequestMapping("${rest.api.cps-base-path}")
41 public class DataRestController implements CpsDataApi {
42
43     private static final String ROOT_XPATH = "/";
44     private static final String ISO_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
45     private static final DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(ISO_TIMESTAMP_FORMAT);
46
47     @Autowired
48     private CpsDataService cpsDataService;
49
50     @Override
51     public ResponseEntity<String> createNode(final String dataspaceName, final String anchorName,
52         final String jsonData, final String parentNodeXpath, final String observedTimestamp) {
53         if (isRootXpath(parentNodeXpath)) {
54             cpsDataService.saveData(dataspaceName, anchorName, jsonData, toOffsetDateTime(observedTimestamp));
55         } else {
56             cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData,
57                 toOffsetDateTime(observedTimestamp));
58         }
59         return new ResponseEntity<>(HttpStatus.CREATED);
60     }
61
62     @Override
63     public ResponseEntity<String> addListElements(final String parentNodeXpath,
64         final String dataspaceName, final String anchorName, final String jsonData, final String observedTimestamp) {
65         cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath, jsonData,
66             toOffsetDateTime(observedTimestamp));
67         return new ResponseEntity<>(HttpStatus.CREATED);
68     }
69
70     @Override
71     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
72         final String xpath, final Boolean includeDescendants) {
73         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
74             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
75         final var dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
76             fetchDescendantsOption);
77         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
78     }
79
80     @Override
81     public ResponseEntity<Object> updateNodeLeaves(final String dataspaceName,
82         final String anchorName, final String jsonData, final String parentNodeXpath, final String observedTimestamp) {
83         cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData,
84             toOffsetDateTime(observedTimestamp));
85         return new ResponseEntity<>(HttpStatus.OK);
86     }
87
88     @Override
89     public ResponseEntity<Object> replaceNode(final String dataspaceName, final String anchorName,
90         final String jsonData, final String parentNodeXpath, final String observedTimestamp) {
91         cpsDataService
92             .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData, toOffsetDateTime(observedTimestamp));
93         return new ResponseEntity<>(HttpStatus.OK);
94     }
95
96     @Override
97     public ResponseEntity<String> replaceListContent(final String parentNodeXpath,
98         final String dataspaceName, final String anchorName, final String jsonData,
99         final String observedTimestamp) {
100         cpsDataService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, jsonData,
101             toOffsetDateTime(observedTimestamp));
102         return new ResponseEntity<>(HttpStatus.OK);
103     }
104
105     @Override
106     public ResponseEntity<Void> deleteListOrListElement(final String dataspaceName, final String anchorName,
107         final String listElementXpath, final String observedTimestamp) {
108         cpsDataService
109             .deleteListOrListElement(dataspaceName, anchorName, listElementXpath, toOffsetDateTime(observedTimestamp));
110         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
111     }
112
113     private static boolean isRootXpath(final String xpath) {
114         return ROOT_XPATH.equals(xpath);
115     }
116
117     private static OffsetDateTime toOffsetDateTime(final String datetTimestamp) {
118         try {
119             return StringUtils.isEmpty(datetTimestamp)
120                 ? null : OffsetDateTime.parse(datetTimestamp, ISO_TIMESTAMP_FORMATTER);
121         } catch (final Exception exception) {
122             throw new ValidationException(
123                 String.format("observed-timestamp must be in '%s' format", ISO_TIMESTAMP_FORMAT));
124         }
125     }
126 }