2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021 Bell Canada.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.temporal.controller.rest;
23 import java.time.OffsetDateTime;
24 import javax.validation.Valid;
25 import javax.validation.ValidationException;
26 import javax.validation.constraints.Min;
27 import javax.validation.constraints.NotNull;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.cps.temporal.controller.rest.model.AnchorHistory;
30 import org.onap.cps.temporal.controller.rest.model.SortMapper;
31 import org.onap.cps.temporal.controller.utils.DateTimeUtility;
32 import org.onap.cps.temporal.domain.NetworkData;
33 import org.onap.cps.temporal.domain.SearchCriteria;
34 import org.onap.cps.temporal.service.NetworkDataService;
35 import org.springframework.data.domain.Slice;
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.base-path}")
42 public class QueryController implements CpsTemporalQueryApi {
44 private NetworkDataService networkDataService;
45 private SortMapper sortMapper;
46 private QueryResponseFactory queryResponseFactory;
51 * @param networkDataService networkDataService
52 * @param sortMapper sortMapper
53 * @param queryResponseFactory anchorHistoryResponseFactory
55 public QueryController(final NetworkDataService networkDataService,
56 final SortMapper sortMapper,
57 final QueryResponseFactory queryResponseFactory) {
58 this.networkDataService = networkDataService;
59 this.sortMapper = sortMapper;
60 this.queryResponseFactory = queryResponseFactory;
64 public ResponseEntity<AnchorHistory> getAnchorDataByName(final String dataspaceName,
65 final String anchorName, final @Valid String observedTimestampAfter,
66 final @Valid String simplePayloadFilter,
67 final @Valid String pointInTime, final @Min(0) @Valid Integer pageNumber,
68 final @Min(0) @Valid Integer pageLimit, final @Valid String sortAsString) {
70 final var searchCriteriaBuilder =
71 getSearchCriteriaBuilder(observedTimestampAfter, simplePayloadFilter, pointInTime,
72 pageNumber, pageLimit, sortAsString)
73 .dataspaceName(dataspaceName).anchorName(anchorName);
74 final var searchCriteria = searchCriteriaBuilder.build();
75 final Slice<NetworkData> searchResult = networkDataService.searchNetworkData(searchCriteria);
76 final var anchorHistory = queryResponseFactory
77 .createAnchorDataByNameResponse(searchCriteria, searchResult);
78 return ResponseEntity.ok(anchorHistory);
82 public ResponseEntity<AnchorHistory> getAnchorsDataByFilter(final String dataspaceName,
83 final @NotNull @Valid String schemaSetName, final @Valid String observedTimestampAfter,
84 final @Valid String simplePayloadFilter,
85 final @Valid String pointInTime, final @Min(0) @Valid Integer pageNumber,
86 final @Min(0) @Valid Integer pageLimit, final @Valid String sortAsString) {
87 final var searchCriteriaBuilder =
88 getSearchCriteriaBuilder(observedTimestampAfter,
90 pointInTime, pageNumber,
91 pageLimit, sortAsString)
92 .dataspaceName(dataspaceName).schemaSetName(schemaSetName);
93 final var searchCriteria = searchCriteriaBuilder.build();
94 final Slice<NetworkData> searchResult = networkDataService.searchNetworkData(searchCriteria);
95 final var anchorHistory = queryResponseFactory
96 .createAnchorsDataByFilterResponse(searchCriteria, searchResult);
97 return ResponseEntity.ok(anchorHistory);
100 private SearchCriteria.Builder getSearchCriteriaBuilder(final String observedTimestampAfter,
101 final String simplePayloadFilter,
102 final String pointInTime, final Integer pageNumber,
103 final Integer pageLimit, final String sortAsString) {
105 final var searchCriteriaBuilder = SearchCriteria.builder()
106 .pagination(pageNumber, pageLimit)
107 .observedAfter(getOffsetDateTime(observedTimestampAfter, "observedTimestampAfter"))
108 .simplePayloadFilter(simplePayloadFilter)
109 .sort(sortMapper.toSort(sortAsString));
111 if (!StringUtils.isEmpty(pointInTime)) {
112 searchCriteriaBuilder.createdBefore(getOffsetDateTime(pointInTime, "pointInTime"));
115 return searchCriteriaBuilder;
119 private OffsetDateTime getOffsetDateTime(final String datetime, final String propertyName) {
121 return DateTimeUtility.toOffsetDateTime(datetime);
122 } catch (final Exception exception) {
123 throw new ValidationException(
124 String.format("%s must be in '%s' format", propertyName, DateTimeUtility.ISO_TIMESTAMP_PATTERN));