API versioning supported and added different versions for POST APIs
[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 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 apiVersion,
53         final String dataspaceName, 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         final Collection<DataNode> dataNodes =
57             cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption);
58         final List<Map<String, Object>> dataMaps = new ArrayList<>(dataNodes.size());
59         String prefix = null;
60         for (final DataNode dataNode : dataNodes) {
61             if (prefix == null) {
62                 prefix = prefixResolver.getPrefix(dataspaceName, anchorName, dataNode.getXpath());
63             }
64             final Map<String, Object> dataMap = DataMapUtils.toDataMapWithIdentifier(dataNode, prefix);
65             dataMaps.add(dataMap);
66         }
67
68         return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataMaps), HttpStatus.OK);
69     }
70 }