993dad58c3b6048e8d1f1fc938d587d2bb1ae70e
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / AdminRestController.java
1 /*
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.rest.controller;
25
26 import static org.onap.cps.rest.utils.MultipartFileUtil.extractYangResourcesMap;
27 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED;
28
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;
50
51 @RestController
52 @RequestMapping("${rest.api.cps-base-path}")
53 @RequiredArgsConstructor
54 public class AdminRestController implements CpsAdminApi {
55
56     private final CpsAdminService cpsAdminService;
57     private final CpsModuleService cpsModuleService;
58     private final CpsRestInputMapper cpsRestInputMapper;
59
60     /**
61      * Create a dataspace.
62      *
63      * @param dataspaceName dataspace name
64      * @return a {@Link ResponseEntity} of created dataspace name & {@link HttpStatus} CREATED
65      */
66     @Override
67     public ResponseEntity<String> createDataspace(@NotNull @Valid final String dataspaceName) {
68         cpsAdminService.createDataspace(dataspaceName);
69         return new ResponseEntity<>(dataspaceName, HttpStatus.CREATED);
70     }
71
72     /**
73      * Create a dataspace without returning any response body.
74      *
75      * @param dataspaceName dataspace name
76      * @return a {@Link ResponseEntity} of created dataspace name & {@link HttpStatus} CREATED
77      */
78     @Override
79     public ResponseEntity<Void> createDataspaceV2(@NotNull @Valid final String dataspaceName) {
80         cpsAdminService.createDataspace(dataspaceName);
81         return new ResponseEntity<>(HttpStatus.CREATED);
82     }
83
84     /**
85      * Delete a dataspace.
86      *
87      * @param dataspaceName name of dataspace to be deleted
88      * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
89      */
90     @Override
91     public ResponseEntity<Void> deleteDataspace(final String apiVersion, final String dataspaceName) {
92         cpsAdminService.deleteDataspace(dataspaceName);
93         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
94     }
95
96     /**
97      * Create a {@link SchemaSet}.
98      *
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
103      */
104     @Override
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);
110     }
111
112     /**
113      * Create a {@link SchemaSet}.
114      *
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
119      */
120     @Override
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);
128     }
129
130     /**
131      * Get {@link SchemaSetDetails} based on dataspace name & {@link SchemaSet} name.
132      *
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
137      */
138     @Override
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);
144     }
145
146     /**
147      * Get list of schema sets for a given dataspace name.
148      *
149      * @param apiVersion api version
150      * @param dataspaceName dataspace name
151      * @return a {@Link ResponseEntity} of schema sets & {@link HttpStatus} OK
152      */
153     @Override
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);
159     }
160
161     /**
162      * Delete a {@link SchemaSet} based on given dataspace name & schemaset name.
163      *
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
168      */
169     @Override
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);
174     }
175
176     /**
177      * Create a new anchor.
178      *
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
183      */
184     @Override
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);
189     }
190
191     /**
192      * Create an anchor.
193      *
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
198      */
199     @Override
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);
204     }
205
206     /**
207      * Delete an {@link Anchor} based on given dataspace name & anchor name.
208      *
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
213      */
214     @Override
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);
219     }
220
221     /**
222      * Get an {@link Anchor} based on given dataspace name & anchor name.
223      *
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
228      */
229     @Override
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);
235     }
236
237     /**
238      *  Get all {@link Anchor} based on given dataspace name.
239      *
240      * @param apiVersion api version
241      * @param dataspaceName dataspace name
242      * @return a {@Link ResponseEntity} of all {@Link AnchorDetails} & {@link HttpStatus} OK
243      */
244     @Override
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);
251     }
252
253     @Override
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);
259     }
260
261     @Override
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);
266     }
267 }