Merge "API versioning supported and added different versions for POST APIs"
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / AdminRestController.java
1 /*
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  *  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 java.util.Collection;
30 import java.util.List;
31 import java.util.stream.Collectors;
32 import javax.validation.Valid;
33 import javax.validation.constraints.NotNull;
34 import lombok.RequiredArgsConstructor;
35 import org.onap.cps.api.CpsAdminService;
36 import org.onap.cps.api.CpsModuleService;
37 import org.onap.cps.rest.api.CpsAdminApi;
38 import org.onap.cps.rest.model.AnchorDetails;
39 import org.onap.cps.rest.model.DataspaceDetails;
40 import org.onap.cps.rest.model.SchemaSetDetails;
41 import org.onap.cps.spi.model.Anchor;
42 import org.onap.cps.spi.model.Dataspace;
43 import org.onap.cps.spi.model.SchemaSet;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RestController;
48 import org.springframework.web.multipart.MultipartFile;
49
50 @RestController
51 @RequestMapping("${rest.api.cps-base-path}")
52 @RequiredArgsConstructor
53 public class AdminRestController implements CpsAdminApi {
54
55     private final CpsAdminService cpsAdminService;
56     private final CpsModuleService cpsModuleService;
57     private final CpsRestInputMapper cpsRestInputMapper;
58
59     /**
60      * Create a dataspace.
61      *
62      * @param dataspaceName dataspace name
63      * @return a {@Link ResponseEntity} of created dataspace name & {@link HttpStatus} CREATED
64      */
65     @Override
66     public ResponseEntity<String> createDataspace(@NotNull @Valid final String dataspaceName) {
67         cpsAdminService.createDataspace(dataspaceName);
68         return new ResponseEntity<>(dataspaceName, HttpStatus.CREATED);
69     }
70
71     /**
72      * Create a dataspace without returning any response body.
73      *
74      * @param dataspaceName dataspace name
75      * @return a {@Link ResponseEntity} of created dataspace name & {@link HttpStatus} CREATED
76      */
77     @Override
78     public ResponseEntity<Void> createDataspaceV2(@NotNull @Valid final String dataspaceName) {
79         cpsAdminService.createDataspace(dataspaceName);
80         return new ResponseEntity<>(HttpStatus.CREATED);
81     }
82
83     /**
84      * Delete a dataspace.
85      *
86      * @param dataspaceName name of dataspace to be deleted
87      * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
88      */
89     @Override
90     public ResponseEntity<Void> deleteDataspace(final String apiVersion, final String dataspaceName) {
91         cpsAdminService.deleteDataspace(dataspaceName);
92         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
93     }
94
95     /**
96      * Create a {@link SchemaSet}.
97      *
98      * @param multipartFile multipart file
99      * @param schemaSetName schemaset name
100      * @param dataspaceName dataspace name
101      * @return a {@Link ResponseEntity} of created schemaset name & {@link HttpStatus} CREATED
102      */
103     @Override
104     public ResponseEntity<String> createSchemaSet(@NotNull @Valid final String schemaSetName,
105         final String dataspaceName, @Valid final MultipartFile multipartFile) {
106         cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
107         return new ResponseEntity<>(schemaSetName, HttpStatus.CREATED);
108     }
109
110     /**
111      * Create a {@link SchemaSet}.
112      *
113      * @param multipartFile multipart file
114      * @param schemaSetName schemaset name
115      * @param dataspaceName dataspace name
116      * @return a {@Link ResponseEntity} of created schema set without any response body & {@link HttpStatus} CREATED
117      */
118     @Override
119     public ResponseEntity<Void> createSchemaSetV2(@NotNull @Valid final String schemaSetName,
120         final String dataspaceName, @Valid final MultipartFile multipartFile) {
121         cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
122         return new ResponseEntity<>(HttpStatus.CREATED);
123     }
124
125     /**
126      * Get {@link SchemaSetDetails} based on dataspace name & {@link SchemaSet} name.
127      *
128      * @param apiVersion api version
129      * @param dataspaceName dataspace name
130      * @param schemaSetName schemaset name
131      * @return a {@Link ResponseEntity} of {@Link SchemaSetDetails} & {@link HttpStatus} OK
132      */
133     @Override
134     public ResponseEntity<SchemaSetDetails> getSchemaSet(final String apiVersion,
135             final String dataspaceName, final String schemaSetName) {
136         final var schemaSet = cpsModuleService.getSchemaSet(dataspaceName, schemaSetName);
137         final var schemaSetDetails = cpsRestInputMapper.toSchemaSetDetails(schemaSet);
138         return new ResponseEntity<>(schemaSetDetails, HttpStatus.OK);
139     }
140
141     /**
142      * Get list of schema sets for a given dataspace name.
143      *
144      * @param apiVersion api version
145      * @param dataspaceName dataspace name
146      * @return a {@Link ResponseEntity} of schema sets & {@link HttpStatus} OK
147      */
148     @Override
149     public ResponseEntity<List<SchemaSetDetails>> getSchemaSets(final String apiVersion, final String dataspaceName) {
150         final Collection<SchemaSet> schemaSets = cpsModuleService.getSchemaSets(dataspaceName);
151         final List<SchemaSetDetails> schemaSetDetails = schemaSets.stream().map(cpsRestInputMapper::toSchemaSetDetails)
152                 .collect(Collectors.toList());
153         return new ResponseEntity<>(schemaSetDetails, HttpStatus.OK);
154     }
155
156     /**
157      * Delete a {@link SchemaSet} based on given dataspace name & schemaset name.
158      *
159      * @param apiVersion api version
160      * @param dataspaceName dataspace name
161      * @param schemaSetName schemaset name
162      * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
163      */
164     @Override
165     public ResponseEntity<Void> deleteSchemaSet(final String apiVersion,
166             final String dataspaceName, final String schemaSetName) {
167         cpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED);
168         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
169     }
170
171     /**
172      * Create a new anchor.
173      *
174      * @param dataspaceName dataspace name
175      * @param schemaSetName schema set name
176      * @param anchorName    anchorName
177      * @return a ResponseEntity with the anchor name & {@link HttpStatus} CREATED
178      */
179     @Override
180     public ResponseEntity<String> createAnchor(final String dataspaceName, @NotNull @Valid final String schemaSetName,
181         @NotNull @Valid final String anchorName) {
182         cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
183         return new ResponseEntity<>(anchorName, HttpStatus.CREATED);
184     }
185
186     /**
187      * Create an anchor.
188      *
189      * @param dataspaceName dataspace name
190      * @param schemaSetName schema set name
191      * @param anchorName    anchorName
192      * @return a ResponseEntity without response body & {@link HttpStatus} CREATED
193      */
194     @Override
195     public ResponseEntity<Void> createAnchorV2(final String dataspaceName, @NotNull @Valid final String schemaSetName,
196         @NotNull @Valid final String anchorName) {
197         cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
198         return new ResponseEntity<>(HttpStatus.CREATED);
199     }
200
201     /**
202      * Delete an {@link Anchor} based on given dataspace name & anchor name.
203      *
204      * @param apiVersion api version
205      * @param dataspaceName dataspace name
206      * @param anchorName anchor name
207      * @return a {@Link ResponseEntity} of {@link HttpStatus} NO_CONTENT
208      */
209     @Override
210     public ResponseEntity<Void> deleteAnchor(final String apiVersion,
211             final String dataspaceName, final String anchorName) {
212         cpsAdminService.deleteAnchor(dataspaceName, anchorName);
213         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
214     }
215
216     /**
217      * Get an {@link Anchor} based on given dataspace name & anchor name.
218      *
219      * @param apiVersion api version
220      * @param dataspaceName dataspace name
221      * @param anchorName anchor name
222      * @return a {@Link ResponseEntity} of an {@Link AnchorDetails} & {@link HttpStatus} OK
223      */
224     @Override
225     public ResponseEntity<AnchorDetails> getAnchor(final String apiVersion,
226             final String dataspaceName, final String anchorName) {
227         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
228         final var anchorDetails = cpsRestInputMapper.toAnchorDetails(anchor);
229         return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
230     }
231
232     /**
233      *  Get all {@link Anchor} based on given dataspace name.
234      *
235      * @param apiVersion api version
236      * @param dataspaceName dataspace name
237      * @return a {@Link ResponseEntity} of all {@Link AnchorDetails} & {@link HttpStatus} OK
238      */
239     @Override
240     public ResponseEntity<List<AnchorDetails>> getAnchors(final String apiVersion,
241             final String dataspaceName) {
242         final Collection<Anchor> anchors = cpsAdminService.getAnchors(dataspaceName);
243         final List<AnchorDetails> anchorDetails = anchors.stream().map(cpsRestInputMapper::toAnchorDetails)
244             .collect(Collectors.toList());
245         return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
246     }
247
248     @Override
249     public ResponseEntity<List<DataspaceDetails>> getAllDataspaces(final String apiVersion) {
250         final Collection<Dataspace> dataspaces = cpsAdminService.getAllDataspaces();
251         final List<DataspaceDetails> dataspaceDetails = dataspaces.stream().map(cpsRestInputMapper::toDataspaceDetails)
252                 .collect(Collectors.toList());
253         return new ResponseEntity<>(dataspaceDetails, HttpStatus.OK);
254     }
255
256     @Override
257     public ResponseEntity<DataspaceDetails> getDataspace(final String apiVersion, final String dataspaceName) {
258         final Dataspace dataspace = cpsAdminService.getDataspace(dataspaceName);
259         final DataspaceDetails dataspaceDetails = cpsRestInputMapper.toDataspaceDetails(dataspace);
260         return new ResponseEntity<>(dataspaceDetails, HttpStatus.OK);
261     }
262 }