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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.rest.controller;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.cps.api.CpsQueryService;
30 import org.onap.cps.rest.api.CpsQueryApi;
31 import org.onap.cps.spi.FetchDescendantsOption;
32 import org.onap.cps.spi.model.DataNode;
33 import org.onap.cps.utils.DataMapUtils;
34 import org.onap.cps.utils.JsonObjectMapper;
35 import org.onap.cps.utils.PrefixResolver;
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;
42 @RequestMapping("${rest.api.cps-base-path}")
43 @RequiredArgsConstructor
44 public class QueryRestController implements CpsQueryApi {
46 private final CpsQueryService cpsQueryService;
47 private final JsonObjectMapper jsonObjectMapper;
48 private final PrefixResolver prefixResolver;
51 public ResponseEntity<Object> getNodesByDataspaceAndAnchorAndCpsPath(final String dataspaceName,
52 final String anchorName, final String cpsPath, final Boolean includeDescendants) {
53 final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
54 ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
55 final Collection<DataNode> dataNodes =
56 cpsQueryService.queryDataNodes(dataspaceName, anchorName, cpsPath, fetchDescendantsOption);
57 final List<Map<String, Object>> dataMaps = new ArrayList<>(dataNodes.size());
59 for (final DataNode dataNode : dataNodes) {
61 prefix = prefixResolver.getPrefix(dataspaceName, anchorName, dataNode.getXpath());
63 final Map<String, Object> dataMap = DataMapUtils.toDataMapWithIdentifier(dataNode, prefix);
64 dataMaps.add(dataMap);
67 return new ResponseEntity<>(jsonObjectMapper.asJsonString(dataMaps), HttpStatus.OK);