Merge "subscription-registry node in subscription loader"
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / QueryRestController.java
1 /*
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.rest.controller;
24
25 import io.micrometer.core.annotation.Timed;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.Map;
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;
42
43 @RestController
44 @RequestMapping("${rest.api.cps-base-path}")
45 @RequiredArgsConstructor
46 public class QueryRestController implements CpsQueryApi {
47
48     private final CpsQueryService cpsQueryService;
49     private final JsonObjectMapper jsonObjectMapper;
50     private final PrefixResolver prefixResolver;
51
52     @Override
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);
61     }
62
63     @Override
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);
72     }
73
74     private ResponseEntity<Object> executeNodesByDataspaceQueryAndCreateResponse(final String dataspaceName,
75              final String anchorName, final String cpsPath, final FetchDescendantsOption fetchDescendantsOption) {
76         final Collection<DataNode> dataNodes =
77             cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption);
78         final List<Map<String, Object>> dataNodesAsListOfMaps = new ArrayList<>(dataNodes.size());
79         String prefix = null;
80         for (final DataNode dataNode : dataNodes) {
81             if (prefix == null) {
82                 prefix = prefixResolver.getPrefix(dataspaceName, anchorName, dataNode.getXpath());
83             }
84             final Map<String, Object> dataMap = DataMapUtils.toDataMapWithIdentifier(dataNode, prefix);
85             dataNodesAsListOfMaps.add(dataMap);
86         }
87         return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataNodesAsListOfMaps), HttpStatus.OK);
88     }
89 }