Merge "[NCMP] Consume & Forward to client topic"
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / DataRestController.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2022 Bell Canada.
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021-2022 Nordix Foundation
6  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
7  *  Modifications Copyright (C) 2022 Deutsche Telekom AG
8  *  ================================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *
13  *        http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  *
21  *  SPDX-License-Identifier: Apache-2.0
22  *  ============LICENSE_END=========================================================
23  */
24
25 package org.onap.cps.rest.controller;
26
27 import java.time.OffsetDateTime;
28 import java.time.format.DateTimeFormatter;
29 import javax.validation.ValidationException;
30 import lombok.RequiredArgsConstructor;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.cps.api.CpsDataService;
33 import org.onap.cps.rest.api.CpsDataApi;
34 import org.onap.cps.spi.FetchDescendantsOption;
35 import org.onap.cps.spi.model.DataNode;
36 import org.onap.cps.utils.ContentType;
37 import org.onap.cps.utils.DataMapUtils;
38 import org.onap.cps.utils.JsonObjectMapper;
39 import org.onap.cps.utils.PrefixResolver;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.RequestHeader;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RestController;
46
47 @RestController
48 @RequestMapping("${rest.api.cps-base-path}")
49 @RequiredArgsConstructor
50 public class DataRestController implements CpsDataApi {
51
52     private static final String ROOT_XPATH = "/";
53     private static final String ISO_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
54     private static final DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern(ISO_TIMESTAMP_FORMAT);
55
56     private final CpsDataService cpsDataService;
57     private final JsonObjectMapper jsonObjectMapper;
58     private final PrefixResolver prefixResolver;
59
60     @Override
61     public ResponseEntity<String> createNode(@RequestHeader(value = "Content-Type") final String contentTypeHeader,
62                                              final String apiVersion,
63                                              final String dataspaceName, final String anchorName,
64                                              final String nodeData, final String parentNodeXpath,
65                                              final String observedTimestamp) {
66         final ContentType contentType = contentTypeHeader.contains(MediaType.APPLICATION_XML_VALUE) ? ContentType.XML
67                 : ContentType.JSON;
68         if (isRootXpath(parentNodeXpath)) {
69             cpsDataService.saveData(dataspaceName, anchorName, nodeData,
70                     toOffsetDateTime(observedTimestamp), contentType);
71         } else {
72             cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath,
73                     nodeData, toOffsetDateTime(observedTimestamp), contentType);
74         }
75         return new ResponseEntity<>(HttpStatus.CREATED);
76     }
77
78     @Override
79     public ResponseEntity<Void> deleteDataNode(final String apiVersion,
80         final String dataspaceName, final String anchorName,
81         final String xpath, final String observedTimestamp) {
82         cpsDataService.deleteDataNode(dataspaceName, anchorName, xpath,
83             toOffsetDateTime(observedTimestamp));
84         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
85     }
86
87     @Override
88     public ResponseEntity<String> addListElements(final String parentNodeXpath, final String apiVersion,
89         final String dataspaceName, final String anchorName, final Object jsonData, final String observedTimestamp) {
90         cpsDataService.saveListElements(dataspaceName, anchorName, parentNodeXpath,
91                 jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp));
92         return new ResponseEntity<>(HttpStatus.CREATED);
93     }
94
95     @Override
96     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String apiVersion,
97         final String dataspaceName, final String anchorName, final String xpath, final Boolean includeDescendants) {
98         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
99             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
100         final DataNode dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
101             fetchDescendantsOption);
102         final String prefix = prefixResolver.getPrefix(dataspaceName, anchorName, xpath);
103         return new ResponseEntity<>(DataMapUtils.toDataMapWithIdentifier(dataNode, prefix), HttpStatus.OK);
104     }
105
106     @Override
107     public ResponseEntity<Object> updateNodeLeaves(final String apiVersion, final String dataspaceName,
108         final String anchorName, final Object jsonData, final String parentNodeXpath, final String observedTimestamp) {
109         cpsDataService.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath,
110                 jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp));
111         return new ResponseEntity<>(HttpStatus.OK);
112     }
113
114     @Override
115     public ResponseEntity<Object> replaceNode(final String apiVersion,
116         final String dataspaceName, final String anchorName,
117         final Object jsonData, final String parentNodeXpath, final String observedTimestamp) {
118         cpsDataService
119                 .updateDataNodeAndDescendants(dataspaceName, anchorName, parentNodeXpath,
120                         jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp));
121         return new ResponseEntity<>(HttpStatus.OK);
122     }
123
124     @Override
125     public ResponseEntity<Object> replaceListContent(final String parentNodeXpath,
126         final String apiVersion, final String dataspaceName, final String anchorName, final Object jsonData,
127         final String observedTimestamp) {
128         cpsDataService.replaceListContent(dataspaceName, anchorName, parentNodeXpath,
129                 jsonObjectMapper.asJsonString(jsonData), toOffsetDateTime(observedTimestamp));
130         return new ResponseEntity<>(HttpStatus.OK);
131     }
132
133     @Override
134     public ResponseEntity<Void> deleteListOrListElement(final String dataspaceName, final String anchorName,
135         final String listElementXpath, final String observedTimestamp) {
136         cpsDataService
137             .deleteListOrListElement(dataspaceName, anchorName, listElementXpath, toOffsetDateTime(observedTimestamp));
138         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
139     }
140
141     private static boolean isRootXpath(final String xpath) {
142         return ROOT_XPATH.equals(xpath);
143     }
144
145     private static OffsetDateTime toOffsetDateTime(final String datetTimestamp) {
146         try {
147             return StringUtils.isEmpty(datetTimestamp)
148                 ? null : OffsetDateTime.parse(datetTimestamp, ISO_TIMESTAMP_FORMATTER);
149         } catch (final Exception exception) {
150             throw new ValidationException(
151                 String.format("observed-timestamp must be in '%s' format", ISO_TIMESTAMP_FORMAT));
152         }
153     }
154 }