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 com.google.gson.Gson;
26 import java.time.OffsetDateTime;
27 import java.time.format.DateTimeFormatter;
28 import javax.validation.ValidationException;
29 import lombok.RequiredArgsConstructor;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.cps.api.CpsDataService;
32 import org.onap.cps.rest.api.CpsDataApi;
33 import org.onap.cps.spi.FetchDescendantsOption;
34 import org.onap.cps.utils.DataMapUtils;
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 Gson gson;
53 public ResponseEntity<String> createNode(final String dataspaceName, final String anchorName,
54 final Object jsonData, final String parentNodeXpath, final String observedTimestamp) {
55 if (isRootXpath(parentNodeXpath)) {
56 cpsDataService.saveData(dataspaceName, anchorName, gson.toJson(jsonData),
57 toOffsetDateTime(observedTimestamp));
59 cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData),
60 toOffsetDateTime(observedTimestamp));
62 return new ResponseEntity<>(HttpStatus.CREATED);
66 public ResponseEntity<Void> deleteDataNode(final String dataspaceName, final String anchorName,
67 final String xpath, final String observedTimestamp) {
68 cpsDataService.deleteDataNode(dataspaceName, anchorName, xpath,
69 toOffsetDateTime(observedTimestamp));
70 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
74 public ResponseEntity<String> addListElements(final String parentNodeXpath,
75 final String dataspaceName, final String anchorName, final Object jsonData, final String observedTimestamp) {
76 cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData),
77 toOffsetDateTime(observedTimestamp));
78 return new ResponseEntity<>(HttpStatus.CREATED);
82 public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
83 final String xpath, final Boolean includeDescendants) {
84 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
85 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
86 final var dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
87 fetchDescendantsOption);
88 return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
92 public ResponseEntity<Object> updateNodeLeaves(final String dataspaceName,
93 final String anchorName, final Object jsonData, final String parentNodeXpath, final String observedTimestamp) {
94 cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData),
95 toOffsetDateTime(observedTimestamp));
96 return new ResponseEntity<>(HttpStatus.OK);
100 public ResponseEntity<Object> replaceNode(final String dataspaceName, final String anchorName,
101 final Object jsonData, final String parentNodeXpath, final String observedTimestamp) {
103 .replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData),
104 toOffsetDateTime(observedTimestamp));
105 return new ResponseEntity<>(HttpStatus.OK);
109 public ResponseEntity<Object> replaceListContent(final String parentNodeXpath,
110 final String dataspaceName, final String anchorName, final Object jsonData,
111 final String observedTimestamp) {
112 cpsDataService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, gson.toJson(jsonData),
113 toOffsetDateTime(observedTimestamp));
114 return new ResponseEntity<>(HttpStatus.OK);
118 public ResponseEntity<Void> deleteListOrListElement(final String dataspaceName, final String anchorName,
119 final String listElementXpath, final String observedTimestamp) {
121 .deleteListOrListElement(dataspaceName, anchorName, listElementXpath, toOffsetDateTime(observedTimestamp));
122 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
125 private static boolean isRootXpath(final String xpath) {
126 return ROOT_XPATH.equals(xpath);
129 private static OffsetDateTime toOffsetDateTime(final String datetTimestamp) {
131 return StringUtils.isEmpty(datetTimestamp)
132 ? null : OffsetDateTime.parse(datetTimestamp, ISO_TIMESTAMP_FORMATTER);
133 } catch (final Exception exception) {
134 throw new ValidationException(
135 String.format("observed-timestamp must be in '%s' format", ISO_TIMESTAMP_FORMAT));