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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.rest.controller;
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;
40 @RequestMapping("${rest.api.cps-base-path}")
41 public class DataRestController implements CpsDataApi {
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);
48 private CpsDataService cpsDataService;
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));
56 cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData,
57 toOffsetDateTime(observedTimestamp));
59 return new ResponseEntity<>(HttpStatus.CREATED);
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);
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);
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);
89 public ResponseEntity<Object> replaceNode(final String dataspaceName, final String anchorName,
90 final String jsonData, final String parentNodeXpath, final String observedTimestamp) {
92 .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData, toOffsetDateTime(observedTimestamp));
93 return new ResponseEntity<>(HttpStatus.OK);
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);
106 public ResponseEntity<Void> deleteListOrListElement(final String dataspaceName, final String anchorName,
107 final String listElementXpath, final String observedTimestamp) {
109 .deleteListOrListElement(dataspaceName, anchorName, listElementXpath, toOffsetDateTime(observedTimestamp));
110 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
113 private static boolean isRootXpath(final String xpath) {
114 return ROOT_XPATH.equals(xpath);
117 private static OffsetDateTime toOffsetDateTime(final String datetTimestamp) {
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));