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.List;
30 import lombok.RequiredArgsConstructor;
31 import org.onap.cps.api.CpsQueryService;
32 import org.onap.cps.rest.api.CpsQueryApi;
33 import org.onap.cps.spi.FetchDescendantsOption;
34 import org.onap.cps.spi.model.DataNode;
35 import org.onap.cps.utils.DataMapUtils;
36 import org.onap.cps.utils.JsonObjectMapper;
37 import org.onap.cps.utils.PrefixResolver;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RestController;
44 @RequestMapping("${rest.api.cps-base-path}")
45 @RequiredArgsConstructor
46 public class QueryRestController implements CpsQueryApi {
48 private final CpsQueryService cpsQueryService;
49 private final JsonObjectMapper jsonObjectMapper;
50 private final PrefixResolver prefixResolver;
53 @Timed(value = "cps.data.controller.datanode.query.v1",
54 description = "Time taken to query data nodes")
55 public ResponseEntity<Object> getNodesByDataspaceAndAnchorAndCpsPath(final String dataspaceName,
56 final String anchorName, final String cpsPath, final Boolean includeDescendants) {
57 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
58 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
59 return executeNodesByDataspaceQueryAndCreateResponse(dataspaceName, anchorName, cpsPath,
60 fetchDescendantsOption);
64 @Timed(value = "cps.data.controller.datanode.query.v2",
65 description = "Time taken to query data nodes")
66 public ResponseEntity<Object> getNodesByDataspaceAndAnchorAndCpsPathV2(final String dataspaceName,
67 final String anchorName, final String cpsPath, final String fetchDescendantsOptionAsString) {
68 final FetchDescendantsOption fetchDescendantsOption =
69 FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString);
70 return executeNodesByDataspaceQueryAndCreateResponse(dataspaceName, anchorName, cpsPath,
71 fetchDescendantsOption);
75 public ResponseEntity<Object> getNodesByDataspaceAndCpsPath(final String dataspaceName,
76 final String cpsPath, final String fetchDescendantsOptionAsString) {
77 final FetchDescendantsOption fetchDescendantsOption =
78 FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString);
79 final Collection<DataNode> dataNodes =
80 cpsQueryService.queryDataNodesAcrossAnchors(dataspaceName, cpsPath, fetchDescendantsOption);
81 final List<Map<String, Object>> dataMaps = new ArrayList<>(dataNodes.size());
83 for (final DataNode dataNode : dataNodes) {
85 prefix = prefixResolver.getPrefix(dataspaceName, dataNode.getAnchorName(), dataNode.getXpath());
87 final Map<String, Object> dataMap = DataMapUtils.toDataMapWithIdentifierAndAnchor(dataNode, prefix);
88 dataMaps.add(dataMap);
90 return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataMaps), HttpStatus.OK);
93 private ResponseEntity<Object> executeNodesByDataspaceQueryAndCreateResponse(final String dataspaceName,
94 final String anchorName, final String cpsPath, final FetchDescendantsOption fetchDescendantsOption) {
95 final Collection<DataNode> dataNodes =
96 cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption);
97 final List<Map<String, Object>> dataNodesAsListOfMaps = new ArrayList<>(dataNodes.size());
99 for (final DataNode dataNode : dataNodes) {
100 if (prefix == null) {
101 prefix = prefixResolver.getPrefix(dataspaceName, anchorName, dataNode.getXpath());
103 final Map<String, Object> dataMap = DataMapUtils.toDataMapWithIdentifier(dataNode, prefix);
104 dataNodesAsListOfMaps.add(dataMap);
106 return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataNodesAsListOfMaps), HttpStatus.OK);