2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada.
5 * Modifications Copyright (C) 2022-2023 TechMahindra Ltd.
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 io.micrometer.core.annotation.Timed;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.List;
31 import lombok.RequiredArgsConstructor;
32 import org.onap.cps.api.CpsQueryService;
33 import org.onap.cps.rest.api.CpsQueryApi;
34 import org.onap.cps.spi.FetchDescendantsOption;
35 import org.onap.cps.spi.PaginationOption;
36 import org.onap.cps.spi.model.DataNode;
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.ResponseEntity;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RestController;
46 @RequestMapping("${rest.api.cps-base-path}")
47 @RequiredArgsConstructor
48 public class QueryRestController implements CpsQueryApi {
50 private final CpsQueryService cpsQueryService;
51 private final JsonObjectMapper jsonObjectMapper;
52 private final PrefixResolver prefixResolver;
55 @Timed(value = "cps.data.controller.datanode.query.v1",
56 description = "Time taken to query data nodes")
57 public ResponseEntity<Object> getNodesByDataspaceAndAnchorAndCpsPath(final String dataspaceName,
58 final String anchorName, final String cpsPath, final Boolean includeDescendants) {
59 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
60 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
61 return executeNodesByDataspaceQueryAndCreateResponse(dataspaceName, anchorName, cpsPath,
62 fetchDescendantsOption);
66 @Timed(value = "cps.data.controller.datanode.query.v2",
67 description = "Time taken to query data nodes")
68 public ResponseEntity<Object> getNodesByDataspaceAndAnchorAndCpsPathV2(final String dataspaceName,
69 final String anchorName, final String cpsPath, final String fetchDescendantsOptionAsString) {
70 final FetchDescendantsOption fetchDescendantsOption =
71 FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString);
72 return executeNodesByDataspaceQueryAndCreateResponse(dataspaceName, anchorName, cpsPath,
73 fetchDescendantsOption);
77 @Timed(value = "cps.data.controller.datanode.query.across.anchors",
78 description = "Time taken to query data nodes across anchors")
79 public ResponseEntity<Object> getNodesByDataspaceAndCpsPath(final String dataspaceName, final String cpsPath,
80 final String fetchDescendantsOptionAsString,
81 final Integer pageIndex, final Integer pageSize) {
82 final FetchDescendantsOption fetchDescendantsOption =
83 FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString);
84 final PaginationOption paginationOption = (pageIndex == null || pageSize == null)
85 ? PaginationOption.NO_PAGINATION : new PaginationOption(pageIndex, pageSize);
86 final Collection<DataNode> dataNodes = cpsQueryService.queryDataNodesAcrossAnchors(dataspaceName,
87 cpsPath, fetchDescendantsOption, paginationOption);
88 final List<Map<String, Object>> dataNodesAsListOfMaps = new ArrayList<>(dataNodes.size());
90 final Map<String, List<DataNode>> anchorDataNodeListMap = prepareDataNodesForAnchor(dataNodes);
91 for (final Map.Entry<String, List<DataNode>> anchorDataNodesMapEntry : anchorDataNodeListMap.entrySet()) {
93 prefix = prefixResolver.getPrefix(dataspaceName, anchorDataNodesMapEntry.getKey(),
94 anchorDataNodesMapEntry.getValue().get(0).getXpath());
96 final Map<String, Object> dataMap = DataMapUtils.toDataMapWithIdentifierAndAnchor(
97 anchorDataNodesMapEntry.getValue(), anchorDataNodesMapEntry.getKey(), prefix);
98 dataNodesAsListOfMaps.add(dataMap);
100 final Integer totalPages = getTotalPages(dataspaceName, cpsPath, paginationOption);
101 return ResponseEntity.ok().header("total-pages",
102 totalPages.toString()).body(jsonObjectMapper.asJsonString(dataNodesAsListOfMaps));
105 private Integer getTotalPages(final String dataspaceName, final String cpsPath,
106 final PaginationOption paginationOption) {
107 if (paginationOption == PaginationOption.NO_PAGINATION) {
110 final int totalAnchors = cpsQueryService.countAnchorsForDataspaceAndCpsPath(dataspaceName, cpsPath);
111 return totalAnchors <= paginationOption.getPageSize() ? 1
112 : (int) Math.ceil((double) totalAnchors / paginationOption.getPageSize());
115 private Map<String, List<DataNode>> prepareDataNodesForAnchor(final Collection<DataNode> dataNodes) {
116 final Map<String, List<DataNode>> dataNodesMapForAnchor = new HashMap<>();
117 for (final DataNode dataNode : dataNodes) {
118 List<DataNode> dataNodesInAnchor = dataNodesMapForAnchor.get(dataNode.getAnchorName());
119 if (dataNodesInAnchor == null) {
120 dataNodesInAnchor = new ArrayList<>();
121 dataNodesMapForAnchor.put(dataNode.getAnchorName(), dataNodesInAnchor);
123 dataNodesInAnchor.add(dataNode);
125 return dataNodesMapForAnchor;
128 private ResponseEntity<Object> executeNodesByDataspaceQueryAndCreateResponse(final String dataspaceName,
129 final String anchorName, final String cpsPath, final FetchDescendantsOption fetchDescendantsOption) {
130 final Collection<DataNode> dataNodes =
131 cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption);
132 final List<Map<String, Object>> dataNodesAsListOfMaps = new ArrayList<>(dataNodes.size());
133 String prefix = null;
134 for (final DataNode dataNode : dataNodes) {
135 if (prefix == null) {
136 prefix = prefixResolver.getPrefix(dataspaceName, anchorName, dataNode.getXpath());
138 final Map<String, Object> dataMap = DataMapUtils.toDataMapWithIdentifier(dataNode, prefix);
139 dataNodesAsListOfMaps.add(dataMap);
141 return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataNodesAsListOfMaps), HttpStatus.OK);