Align JSON DataNode for Get and Post/Put API in CPS
[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  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.rest.controller;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Map;
28 import javax.validation.Valid;
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.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RestController;
40
41 @RestController
42 @RequestMapping("${rest.api.cps-base-path}")
43 @RequiredArgsConstructor
44 public class QueryRestController implements CpsQueryApi {
45
46     private final CpsQueryService cpsQueryService;
47     private final JsonObjectMapper jsonObjectMapper;
48
49     @Override
50     public ResponseEntity<Object> getNodesByDataspaceAndAnchorAndCpsPath(final String dataspaceName,
51         final String anchorName, @Valid final String cpsPath, @Valid final Boolean includeDescendants) {
52         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
53             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
54         final Collection<DataNode> dataNodes =
55             cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption);
56         final List<Map<String, Object>> dataNodeList = new ArrayList<>();
57         dataNodes.stream()
58             .forEach(dataNode -> dataNodeList.add(DataMapUtils.toDataMapWithIdentifier(dataNode)));
59         return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataNodeList), HttpStatus.OK);
60     }
61 }