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