2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 Bell Canada.
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * Modifications Copyright (C) 2021-2022 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 lombok.RequiredArgsConstructor;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.cps.api.CpsDataService;
31 import org.onap.cps.rest.api.CpsDataApi;
32 import org.onap.cps.spi.FetchDescendantsOption;
33 import org.onap.cps.utils.DataMapUtils;
34 import org.onap.cps.utils.JsonObjectMapper;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RestController;
41 @RequestMapping("${rest.api.cps-base-path}")
42 @RequiredArgsConstructor
43 public class DataRestController implements CpsDataApi {
45 private static final String ROOT_XPATH = "/";
46 private static final String ISO_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
47 private static final DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(ISO_TIMESTAMP_FORMAT);
49 private final CpsDataService cpsDataService;
50 private final JsonObjectMapper jsonObjectMapper;
53 public ResponseEntity<String> createNode(final String dataspaceName, final String anchorName,
54 final Object jsonData, final String parentNodeXpath, final String observedTimestamp) {
55 final String jsonDataAsString = jsonObjectMapper.asJsonString(jsonData);
56 if (isRootXpath(parentNodeXpath)) {
57 cpsDataService.saveData(dataspaceName, anchorName, jsonDataAsString,
58 toOffsetDateTime(observedTimestamp));
60 cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath,
61 jsonDataAsString, toOffsetDateTime(observedTimestamp));
63 return new ResponseEntity<>(HttpStatus.CREATED);
67 public ResponseEntity<Void> deleteDataNode(final String dataspaceName, final String anchorName,
68 final String xpath, final String observedTimestamp) {
69 cpsDataService.deleteDataNode(dataspaceName, anchorName, xpath,
70 toOffsetDateTime(observedTimestamp));
71 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
75 public ResponseEntity<String> addListElements(final String parentNodeXpath,
76 final String dataspaceName, final String anchorName, final Object jsonData, final String observedTimestamp) {
77 cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath,
78 jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp));
79 return new ResponseEntity<>(HttpStatus.CREATED);
83 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
84 final String xpath, final Boolean includeDescendants) {
85 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
86 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
87 final var dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
88 fetchDescendantsOption);
89 return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
93 public ResponseEntity<Object> updateNodeLeaves(final String dataspaceName,
94 final String anchorName, final Object jsonData, final String parentNodeXpath, final String observedTimestamp) {
95 cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath,
96 jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp));
97 return new ResponseEntity<>(HttpStatus.OK);
101 public ResponseEntity<Object> replaceNode(final String dataspaceName, final String anchorName,
102 final Object jsonData, final String parentNodeXpath, final String observedTimestamp) {
104 .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath,
105 jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp));
106 return new ResponseEntity<>(HttpStatus.OK);
110 public ResponseEntity<Object> replaceListContent(final String parentNodeXpath,
111 final String dataspaceName, final String anchorName, final Object jsonData,
112 final String observedTimestamp) {
113 cpsDataService.replaceListContent(dataspaceName, anchorName, parentNodeXpath,
114 jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp));
115 return new ResponseEntity<>(HttpStatus.OK);
119 public ResponseEntity<Void> deleteListOrListElement(final String dataspaceName, final String anchorName,
120 final String listElementXpath, final String observedTimestamp) {
122 .deleteListOrListElement(dataspaceName, anchorName, listElementXpath, toOffsetDateTime(observedTimestamp));
123 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
126 private static boolean isRootXpath(final String xpath) {
127 return ROOT_XPATH.equals(xpath);
130 private static OffsetDateTime toOffsetDateTime(final String datetTimestamp) {
132 return StringUtils.isEmpty(datetTimestamp)
133 ? null : OffsetDateTime.parse(datetTimestamp, ISO_TIMESTAMP_FORMATTER);
134 } catch (final Exception exception) {
135 throw new ValidationException(
136 String.format("observed-timestamp must be in '%s' format", ISO_TIMESTAMP_FORMAT));