2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2023 Nordix Foundation
4 * Modifications Copyright (C) 2020-2021 Bell Canada.
5 * Modifications Copyright (C) 2021 Pantheon.tech
6 * Modifications Copyright (C) 2022 TechMahindra Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.rest.controller;
26 import static org.onap.cps.rest.utils.MultipartFileUtil.extractYangResourcesMap;
27 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED;
29 import io.micrometer.core.annotation.Timed;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.stream.Collectors;
33 import javax.validation.Valid;
34 import javax.validation.constraints.NotNull;
35 import lombok.RequiredArgsConstructor;
36 import org.onap.cps.api.CpsAdminService;
37 import org.onap.cps.api.CpsModuleService;
38 import org.onap.cps.rest.api.CpsAdminApi;
39 import org.onap.cps.rest.model.AnchorDetails;
40 import org.onap.cps.rest.model.DataspaceDetails;
41 import org.onap.cps.rest.model.SchemaSetDetails;
42 import org.onap.cps.spi.model.Anchor;
43 import org.onap.cps.spi.model.Dataspace;
44 import org.onap.cps.spi.model.SchemaSet;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import org.springframework.web.bind.annotation.RestController;
49 import org.springframework.web.multipart.MultipartFile;
52 @RequestMapping("${rest.api.cps-base-path}")
53 @RequiredArgsConstructor
54 public class AdminRestController implements CpsAdminApi {
56 private final CpsAdminService cpsAdminService;
57 private final CpsModuleService cpsModuleService;
58 private final CpsRestInputMapper cpsRestInputMapper;
63 * @param dataspaceName dataspace name
64 * @return a {@Link ResponseEntity} of created dataspace name & {@link HttpStatus} CREATED
67 public ResponseEntity<String> createDataspace(@NotNull @Valid final String dataspaceName) {
68 cpsAdminService.createDataspace(dataspaceName);
69 return new ResponseEntity<>(dataspaceName, HttpStatus.CREATED);
73 * Create a dataspace without returning any response body.
75 * @param dataspaceName dataspace name
76 * @return a {@Link ResponseEntity} of created dataspace name & {@link HttpStatus} CREATED
79 public ResponseEntity<Void> createDataspaceV2(@NotNull @Valid final String dataspaceName) {
80 cpsAdminService.createDataspace(dataspaceName);
81 return new ResponseEntity<>(HttpStatus.CREATED);
87 * @param dataspaceName name of dataspace to be deleted
88 * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
91 public ResponseEntity<Void> deleteDataspace(final String apiVersion, final String dataspaceName) {
92 cpsAdminService.deleteDataspace(dataspaceName);
93 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
97 * Create a {@link SchemaSet}.
99 * @param multipartFile multipart file
100 * @param schemaSetName schemaset name
101 * @param dataspaceName dataspace name
102 * @return a {@Link ResponseEntity} of created schemaset name & {@link HttpStatus} CREATED
105 public ResponseEntity<String> createSchemaSet(@NotNull @Valid final String schemaSetName,
106 final String dataspaceName, @Valid final MultipartFile multipartFile) {
107 cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
108 return new ResponseEntity<>(schemaSetName, HttpStatus.CREATED);
112 * Create a {@link SchemaSet}.
114 * @param multipartFile multipart file
115 * @param schemaSetName schemaset name
116 * @param dataspaceName dataspace name
117 * @return a {@Link ResponseEntity} of created schema set without any response body & {@link HttpStatus} CREATED
120 @Timed(value = "cps.rest.admin.controller.schemaset.create",
121 description = "Time taken to create schemaset from controller")
122 public ResponseEntity<Void> createSchemaSetV2(@NotNull @Valid final String schemaSetName,
123 final String dataspaceName, @Valid final MultipartFile multipartFile) {
124 cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
125 return new ResponseEntity<>(HttpStatus.CREATED);
129 * Get {@link SchemaSetDetails} based on dataspace name & {@link SchemaSet} name.
131 * @param apiVersion api version
132 * @param dataspaceName dataspace name
133 * @param schemaSetName schemaset name
134 * @return a {@Link ResponseEntity} of {@Link SchemaSetDetails} & {@link HttpStatus} OK
137 public ResponseEntity<SchemaSetDetails> getSchemaSet(final String apiVersion,
138 final String dataspaceName, final String schemaSetName) {
139 final var schemaSet = cpsModuleService.getSchemaSet(dataspaceName, schemaSetName);
140 final var schemaSetDetails = cpsRestInputMapper.toSchemaSetDetails(schemaSet);
141 return new ResponseEntity<>(schemaSetDetails, HttpStatus.OK);
145 * Get list of schema sets for a given dataspace name.
147 * @param apiVersion api version
148 * @param dataspaceName dataspace name
149 * @return a {@Link ResponseEntity} of schema sets & {@link HttpStatus} OK
152 public ResponseEntity<List<SchemaSetDetails>> getSchemaSets(final String apiVersion, final String dataspaceName) {
153 final Collection<SchemaSet> schemaSets = cpsModuleService.getSchemaSets(dataspaceName);
154 final List<SchemaSetDetails> schemaSetDetails = schemaSets.stream().map(cpsRestInputMapper::toSchemaSetDetails)
155 .collect(Collectors.toList());
156 return new ResponseEntity<>(schemaSetDetails, HttpStatus.OK);
160 * Delete a {@link SchemaSet} based on given dataspace name & schemaset name.
162 * @param apiVersion api version
163 * @param dataspaceName dataspace name
164 * @param schemaSetName schemaset name
165 * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
168 public ResponseEntity<Void> deleteSchemaSet(final String apiVersion,
169 final String dataspaceName, final String schemaSetName) {
170 cpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED);
171 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
175 * Create a new anchor.
177 * @param dataspaceName dataspace name
178 * @param schemaSetName schema set name
179 * @param anchorName anchorName
180 * @return a ResponseEntity with the anchor name & {@link HttpStatus} CREATED
183 public ResponseEntity<String> createAnchor(final String dataspaceName, @NotNull @Valid final String schemaSetName,
184 @NotNull @Valid final String anchorName) {
185 cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
186 return new ResponseEntity<>(anchorName, HttpStatus.CREATED);
192 * @param dataspaceName dataspace name
193 * @param schemaSetName schema set name
194 * @param anchorName anchorName
195 * @return a ResponseEntity without response body & {@link HttpStatus} CREATED
198 public ResponseEntity<Void> createAnchorV2(final String dataspaceName, @NotNull @Valid final String schemaSetName,
199 @NotNull @Valid final String anchorName) {
200 cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
201 return new ResponseEntity<>(HttpStatus.CREATED);
205 * Delete an {@link Anchor} based on given dataspace name & anchor name.
207 * @param apiVersion api version
208 * @param dataspaceName dataspace name
209 * @param anchorName anchor name
210 * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
213 public ResponseEntity<Void> deleteAnchor(final String apiVersion,
214 final String dataspaceName, final String anchorName) {
215 cpsAdminService.deleteAnchor(dataspaceName, anchorName);
216 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
220 * Get an {@link Anchor} based on given dataspace name & anchor name.
222 * @param apiVersion api version
223 * @param dataspaceName dataspace name
224 * @param anchorName anchor name
225 * @return a {@Link ResponseEntity} of an {@Link AnchorDetails} & {@link HttpStatus} OK
228 public ResponseEntity<AnchorDetails> getAnchor(final String apiVersion,
229 final String dataspaceName, final String anchorName) {
230 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
231 final var anchorDetails = cpsRestInputMapper.toAnchorDetails(anchor);
232 return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
236 * Get all {@link Anchor} based on given dataspace name.
238 * @param apiVersion api version
239 * @param dataspaceName dataspace name
240 * @return a {@Link ResponseEntity} of all {@Link AnchorDetails} & {@link HttpStatus} OK
243 public ResponseEntity<List<AnchorDetails>> getAnchors(final String apiVersion,
244 final String dataspaceName) {
245 final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName);
246 final List<AnchorDetails> anchorDetails = anchors.stream().map(cpsRestInputMapper::toAnchorDetails)
247 .collect(Collectors.toList());
248 return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
252 public ResponseEntity<List<DataspaceDetails>> getAllDataspaces(final String apiVersion) {
253 final Collection<Dataspace> dataspaces = cpsAdminService.getAllDataspaces();
254 final List<DataspaceDetails> dataspaceDetails = dataspaces.stream().map(cpsRestInputMapper::toDataspaceDetails)
255 .collect(Collectors.toList());
256 return new ResponseEntity<>(dataspaceDetails, HttpStatus.OK);
260 public ResponseEntity<DataspaceDetails> getDataspace(final String apiVersion, final String dataspaceName) {
261 final Dataspace dataspace = cpsAdminService.getDataspace(dataspaceName);
262 final DataspaceDetails dataspaceDetails = cpsRestInputMapper.toDataspaceDetails(dataspace);
263 return new ResponseEntity<>(dataspaceDetails, HttpStatus.OK);