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 jakarta.validation.Valid;
31 import jakarta.validation.constraints.NotNull;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.stream.Collectors;
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 dataspaceName dataspace name
100 * @param schemaSetName schemaset name
101 * @param multipartFile multipart file
102 * @return a {@Link ResponseEntity} of created schemaset name & {@link HttpStatus} CREATED
105 public ResponseEntity<String> createSchemaSet(final String dataspaceName,
106 @NotNull @Valid final String schemaSetName,
107 final MultipartFile multipartFile) {
108 cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
109 return new ResponseEntity<>(schemaSetName, HttpStatus.CREATED);
113 * Create a {@link SchemaSet}.
115 * @param dataspaceName dataspace name
116 * @param schemaSetName schemaset name
117 * @param multipartFile multipart file
118 * @return a {@Link ResponseEntity} of created schema set without any response body & {@link HttpStatus} CREATED
121 @Timed(value = "cps.rest.admin.controller.schemaset.create",
122 description = "Time taken to create schemaset from controller")
123 public ResponseEntity<Void> createSchemaSetV2(final String dataspaceName,
124 @NotNull @Valid final String schemaSetName,
125 final MultipartFile multipartFile) {
126 cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
127 return new ResponseEntity<>(HttpStatus.CREATED);
131 * Get {@link SchemaSetDetails} based on dataspace name & {@link SchemaSet} name.
133 * @param apiVersion api version
134 * @param dataspaceName dataspace name
135 * @param schemaSetName schemaset name
136 * @return a {@Link ResponseEntity} of {@Link SchemaSetDetails} & {@link HttpStatus} OK
139 public ResponseEntity<SchemaSetDetails> getSchemaSet(final String apiVersion,
140 final String dataspaceName, final String schemaSetName) {
141 final var schemaSet = cpsModuleService.getSchemaSet(dataspaceName, schemaSetName);
142 final var schemaSetDetails = cpsRestInputMapper.toSchemaSetDetails(schemaSet);
143 return new ResponseEntity<>(schemaSetDetails, HttpStatus.OK);
147 * Get list of schema sets for a given dataspace name.
149 * @param apiVersion api version
150 * @param dataspaceName dataspace name
151 * @return a {@Link ResponseEntity} of schema sets & {@link HttpStatus} OK
154 public ResponseEntity<List<SchemaSetDetails>> getSchemaSets(final String apiVersion, final String dataspaceName) {
155 final Collection<SchemaSet> schemaSets = cpsModuleService.getSchemaSets(dataspaceName);
156 final List<SchemaSetDetails> schemaSetDetails = schemaSets.stream().map(cpsRestInputMapper::toSchemaSetDetails)
157 .collect(Collectors.toList());
158 return new ResponseEntity<>(schemaSetDetails, HttpStatus.OK);
162 * Delete a {@link SchemaSet} based on given dataspace name & schemaset name.
164 * @param apiVersion api version
165 * @param dataspaceName dataspace name
166 * @param schemaSetName schemaset name
167 * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
170 public ResponseEntity<Void> deleteSchemaSet(final String apiVersion,
171 final String dataspaceName, final String schemaSetName) {
172 cpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED);
173 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
177 * Create a new anchor.
179 * @param dataspaceName dataspace name
180 * @param schemaSetName schema set name
181 * @param anchorName anchorName
182 * @return a ResponseEntity with the anchor name & {@link HttpStatus} CREATED
185 public ResponseEntity<String> createAnchor(final String dataspaceName, @NotNull @Valid final String schemaSetName,
186 @NotNull @Valid final String anchorName) {
187 cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
188 return new ResponseEntity<>(anchorName, HttpStatus.CREATED);
194 * @param dataspaceName dataspace name
195 * @param schemaSetName schema set name
196 * @param anchorName anchorName
197 * @return a ResponseEntity without response body & {@link HttpStatus} CREATED
200 public ResponseEntity<Void> createAnchorV2(final String dataspaceName, @NotNull @Valid final String schemaSetName,
201 @NotNull @Valid final String anchorName) {
202 cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
203 return new ResponseEntity<>(HttpStatus.CREATED);
207 * Delete an {@link Anchor} based on given dataspace name & anchor name.
209 * @param apiVersion api version
210 * @param dataspaceName dataspace name
211 * @param anchorName anchor name
212 * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
215 public ResponseEntity<Void> deleteAnchor(final String apiVersion,
216 final String dataspaceName, final String anchorName) {
217 cpsAdminService.deleteAnchor(dataspaceName, anchorName);
218 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
222 * Get an {@link Anchor} based on given dataspace name & anchor name.
224 * @param apiVersion api version
225 * @param dataspaceName dataspace name
226 * @param anchorName anchor name
227 * @return a {@Link ResponseEntity} of an {@Link AnchorDetails} & {@link HttpStatus} OK
230 public ResponseEntity<AnchorDetails> getAnchor(final String apiVersion,
231 final String dataspaceName, final String anchorName) {
232 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
233 final var anchorDetails = cpsRestInputMapper.toAnchorDetails(anchor);
234 return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
238 * Get all {@link Anchor} based on given dataspace name.
240 * @param apiVersion api version
241 * @param dataspaceName dataspace name
242 * @return a {@Link ResponseEntity} of all {@Link AnchorDetails} & {@link HttpStatus} OK
245 public ResponseEntity<List<AnchorDetails>> getAnchors(final String apiVersion,
246 final String dataspaceName) {
247 final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName);
248 final List<AnchorDetails> anchorDetails = anchors.stream().map(cpsRestInputMapper::toAnchorDetails)
249 .collect(Collectors.toList());
250 return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
254 public ResponseEntity<List<DataspaceDetails>> getAllDataspaces(final String apiVersion) {
255 final Collection<Dataspace> dataspaces = cpsAdminService.getAllDataspaces();
256 final List<DataspaceDetails> dataspaceDetails = dataspaces.stream().map(cpsRestInputMapper::toDataspaceDetails)
257 .collect(Collectors.toList());
258 return new ResponseEntity<>(dataspaceDetails, HttpStatus.OK);
262 public ResponseEntity<DataspaceDetails> getDataspace(final String apiVersion, final String dataspaceName) {
263 final Dataspace dataspace = cpsAdminService.getDataspace(dataspaceName);
264 final DataspaceDetails dataspaceDetails = cpsRestInputMapper.toDataspaceDetails(dataspace);
265 return new ResponseEntity<>(dataspaceDetails, HttpStatus.OK);