Merge "Improve code coverage"
[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 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;
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 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
103      */
104     @Override
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);
109     }
110
111     /**
112      * Create a {@link SchemaSet}.
113      *
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
118      */
119     @Override
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);
126     }
127
128     /**
129      * Get {@link SchemaSetDetails} based on dataspace name & {@link SchemaSet} name.
130      *
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
135      */
136     @Override
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);
142     }
143
144     /**
145      * Get list of schema sets for a given dataspace name.
146      *
147      * @param apiVersion api version
148      * @param dataspaceName dataspace name
149      * @return a {@Link ResponseEntity} of schema sets & {@link HttpStatus} OK
150      */
151     @Override
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);
157     }
158
159     /**
160      * Delete a {@link SchemaSet} based on given dataspace name & schemaset name.
161      *
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
166      */
167     @Override
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);
172     }
173
174     /**
175      * Create a new anchor.
176      *
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
181      */
182     @Override
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);
187     }
188
189     /**
190      * Create an anchor.
191      *
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
196      */
197     @Override
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);
202     }
203
204     /**
205      * Delete an {@link Anchor} based on given dataspace name & anchor name.
206      *
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
211      */
212     @Override
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);
217     }
218
219     /**
220      * Get an {@link Anchor} based on given dataspace name & anchor name.
221      *
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
226      */
227     @Override
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);
233     }
234
235     /**
236      *  Get all {@link Anchor} based on given dataspace name.
237      *
238      * @param apiVersion api version
239      * @param dataspaceName dataspace name
240      * @return a {@Link ResponseEntity} of all {@Link AnchorDetails} & {@link HttpStatus} OK
241      */
242     @Override
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);
249     }
250
251     @Override
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);
257     }
258
259     @Override
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);
264     }
265 }