e57fb3c8c6d52b9908bebbe768a963eb98d23c42
[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<Void> deleteDataNode(final String dataspaceName, final String anchorName,
64                                                final String xpath, final String observedTimestamp) {
65         cpsDataService.deleteDataNode(dataspaceName, anchorName, xpath,
66             toOffsetDateTime(observedTimestamp));
67         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
68     }
69
70     @Override
71     public ResponseEntity<String> addListElements(final String parentNodeXpath,
72         final String dataspaceName, final String anchorName, final String jsonData, final String observedTimestamp) {
73         cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath, jsonData,
74             toOffsetDateTime(observedTimestamp));
75         return new ResponseEntity<>(HttpStatus.CREATED);
76     }
77
78     @Override
79     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
80         final String xpath, final Boolean includeDescendants) {
81         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
82             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
83         final var dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
84             fetchDescendantsOption);
85         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
86     }
87
88     @Override
89     public ResponseEntity<Object> updateNodeLeaves(final String dataspaceName,
90         final String anchorName, final String jsonData, final String parentNodeXpath, final String observedTimestamp) {
91         cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData,
92             toOffsetDateTime(observedTimestamp));
93         return new ResponseEntity<>(HttpStatus.OK);
94     }
95
96     @Override
97     public ResponseEntity<Object> replaceNode(final String dataspaceName, final String anchorName,
98         final String jsonData, final String parentNodeXpath, final String observedTimestamp) {
99         cpsDataService
100             .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData, toOffsetDateTime(observedTimestamp));
101         return new ResponseEntity<>(HttpStatus.OK);
102     }
103
104     @Override
105     public ResponseEntity<String> replaceListContent(final String parentNodeXpath,
106         final String dataspaceName, final String anchorName, final String jsonData,
107         final String observedTimestamp) {
108         cpsDataService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, jsonData,
109             toOffsetDateTime(observedTimestamp));
110         return new ResponseEntity<>(HttpStatus.OK);
111     }
112
113     @Override
114     public ResponseEntity<Void> deleteListOrListElement(final String dataspaceName, final String anchorName,
115         final String listElementXpath, final String observedTimestamp) {
116         cpsDataService
117             .deleteListOrListElement(dataspaceName, anchorName, listElementXpath, toOffsetDateTime(observedTimestamp));
118         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
119     }
120
121     private static boolean isRootXpath(final String xpath) {
122         return ROOT_XPATH.equals(xpath);
123     }
124
125     private static OffsetDateTime toOffsetDateTime(final String datetTimestamp) {
126         try {
127             return StringUtils.isEmpty(datetTimestamp)
128                 ? null : OffsetDateTime.parse(datetTimestamp, ISO_TIMESTAMP_FORMATTER);
129         } catch (final Exception exception) {
130             throw new ValidationException(
131                 String.format("observed-timestamp must be in '%s' format", ISO_TIMESTAMP_FORMAT));
132         }
133     }
134 }