2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2022 Nordix Foundation
4 * Modifications Copyright (C) 2020-2021 Bell Canada.
5 * Modifications Copyright (C) 2021 Pantheon.tech
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.rest.controller;
25 import static org.onap.cps.rest.utils.MultipartFileUtil.extractYangResourcesMap;
26 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED;
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.stream.Collectors;
31 import javax.validation.Valid;
32 import javax.validation.constraints.NotNull;
33 import lombok.RequiredArgsConstructor;
34 import org.onap.cps.api.CpsAdminService;
35 import org.onap.cps.api.CpsModuleService;
36 import org.onap.cps.rest.api.CpsAdminApi;
37 import org.onap.cps.rest.model.AnchorDetails;
38 import org.onap.cps.rest.model.SchemaSetDetails;
39 import org.onap.cps.spi.model.Anchor;
40 import org.onap.cps.spi.model.SchemaSet;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RestController;
45 import org.springframework.web.multipart.MultipartFile;
48 @RequestMapping("${rest.api.cps-base-path}")
49 @RequiredArgsConstructor
50 public class AdminRestController implements CpsAdminApi {
52 private final CpsAdminService cpsAdminService;
53 private final CpsModuleService cpsModuleService;
54 private final CpsRestInputMapper cpsRestInputMapper;
59 * @param dataspaceName dataspace name
60 * @return a {@Link ResponseEntity} of created dataspace name & {@link HttpStatus} CREATED
63 public ResponseEntity<String> createDataspace(@NotNull @Valid final String dataspaceName) {
64 cpsAdminService.createDataspace(dataspaceName);
65 return new ResponseEntity<>(dataspaceName, HttpStatus.CREATED);
71 * @param dataspaceName name of dataspace to be deleted
72 * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
75 public ResponseEntity<Void> deleteDataspace(final String dataspaceName) {
76 cpsAdminService.deleteDataspace(dataspaceName);
77 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
81 * Create a {@link SchemaSet}.
83 * @param multipartFile multipart file
84 * @param schemaSetName schemaset name
85 * @param dataspaceName dataspace name
86 * @return a {@Link ResponseEntity} of created schemaset name & {@link HttpStatus} CREATED
89 public ResponseEntity<String> createSchemaSet(@NotNull @Valid final String schemaSetName,
90 final String dataspaceName, @Valid final MultipartFile multipartFile) {
91 cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
92 return new ResponseEntity<>(schemaSetName, HttpStatus.CREATED);
96 * Get {@link SchemaSetDetails} based on dataspace name & {@link SchemaSet} name.
98 * @param dataspaceName dataspace name
99 * @param schemaSetName schemaset name
100 * @return a {@Link ResponseEntity} of {@Link SchemaSetDetails} & {@link HttpStatus} OK
103 public ResponseEntity<SchemaSetDetails> getSchemaSet(final String dataspaceName, final String schemaSetName) {
104 final var schemaSet = cpsModuleService.getSchemaSet(dataspaceName, schemaSetName);
105 final var schemaSetDetails = cpsRestInputMapper.toSchemaSetDetails(schemaSet);
106 return new ResponseEntity<>(schemaSetDetails, HttpStatus.OK);
110 * Delete a {@link SchemaSet} based on given dataspace name & schemaset name.
112 * @param dataspaceName dataspace name
113 * @param schemaSetName schemaset name
114 * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
117 public ResponseEntity<Void> deleteSchemaSet(final String dataspaceName, final String schemaSetName) {
118 cpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED);
119 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
123 * Create a new anchor.
125 * @param dataspaceName dataspace name
126 * @param schemaSetName schema set name
127 * @param anchorName anchorName
128 * @return a ResponseEntity with the anchor name & {@link HttpStatus} CREATED
131 public ResponseEntity<String> createAnchor(final String dataspaceName, @NotNull @Valid final String schemaSetName,
132 @NotNull @Valid final String anchorName) {
133 cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
134 return new ResponseEntity<>(anchorName, HttpStatus.CREATED);
138 * Delete an {@link Anchor} based on given dataspace name & anchor name.
140 * @param dataspaceName dataspace name
141 * @param anchorName anchor name
142 * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
145 public ResponseEntity<Void> deleteAnchor(final String dataspaceName, final String anchorName) {
146 cpsAdminService.deleteAnchor(dataspaceName, anchorName);
147 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
151 * Get an {@link Anchor} based on given dataspace name & anchor name.
153 * @param dataspaceName dataspace name
154 * @param anchorName anchor name
155 * @return a {@Link ResponseEntity} of an {@Link AnchorDetails} & {@link HttpStatus} OK
158 public ResponseEntity<AnchorDetails> getAnchor(final String dataspaceName, final String anchorName) {
159 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
160 final var anchorDetails = cpsRestInputMapper.toAnchorDetails(anchor);
161 return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
165 * Get all {@link Anchor} based on given dataspace name.
167 * @param dataspaceName dataspace name
168 * @return a {@Link ResponseEntity} of all {@Link AnchorDetails} & {@link HttpStatus} OK
171 public ResponseEntity<List<AnchorDetails>> getAnchors(final String dataspaceName) {
172 final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName);
173 final List<AnchorDetails> anchorDetails = anchors.stream().map(cpsRestInputMapper::toAnchorDetails)
174 .collect(Collectors.toList());
175 return new ResponseEntity<>(anchorDetails, HttpStatus.OK);