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 java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Map;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.cps.api.CpsQueryService;
31 import org.onap.cps.rest.api.CpsQueryApi;
32 import org.onap.cps.spi.FetchDescendantsOption;
33 import org.onap.cps.spi.model.DataNode;
34 import org.onap.cps.utils.DataMapUtils;
35 import org.onap.cps.utils.JsonObjectMapper;
36 import org.onap.cps.utils.PrefixResolver;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RestController;
41
42 @RestController
43 @RequestMapping("${rest.api.cps-base-path}")
44 @RequiredArgsConstructor
45 public class QueryRestController implements CpsQueryApi {
46
47     private final CpsQueryService cpsQueryService;
48     private final JsonObjectMapper jsonObjectMapper;
49     private final PrefixResolver prefixResolver;
50
51     @Override
52     public ResponseEntity<Object> getNodesByDataspaceAndAnchorAndCpsPath(final String dataspaceName,
53         final String anchorName, final String cpsPath, final Boolean includeDescendants) {
54         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
55             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
56         return executeNodesByDataspaceQueryAndCreateResponse(dataspaceName, anchorName, cpsPath,
57                 fetchDescendantsOption);
58     }
59
60     @Override
61     public ResponseEntity<Object> getNodesByDataspaceAndAnchorAndCpsPathV2(final String dataspaceName,
62         final String anchorName, final String cpsPath, final String fetchDescendantsOptionAsString) {
63         final FetchDescendantsOption fetchDescendantsOption =
64             FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString);
65         return executeNodesByDataspaceQueryAndCreateResponse(dataspaceName, anchorName, cpsPath,
66                 fetchDescendantsOption);
67     }
68
69     private ResponseEntity<Object> executeNodesByDataspaceQueryAndCreateResponse(final String dataspaceName,
70              final String anchorName, final String cpsPath, final FetchDescendantsOption fetchDescendantsOption) {
71         final Collection<DataNode> dataNodes =
72             cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption);
73         final List<Map<String, Object>> dataNodesAsListOfMaps = new ArrayList<>(dataNodes.size());
74         String prefix = null;
75         for (final DataNode dataNode : dataNodes) {
76             if (prefix == null) {
77                 prefix = prefixResolver.getPrefix(dataspaceName, anchorName, dataNode.getXpath());
78             }
79             final Map<String, Object> dataMap = DataMapUtils.toDataMapWithIdentifier(dataNode, prefix);
80             dataNodesAsListOfMaps.add(dataMap);
81         }
82         return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataNodesAsListOfMaps), HttpStatus.OK);
83     }
84 }