Delete anchor part 2: cps rest
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / AdminRestController.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  *  Modifications Copyright (C) 2021 Pantheon.tech
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.rest.controller;
23
24 import static org.onap.cps.rest.utils.MultipartFileUtil.extractYangResourcesMap;
25 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED;
26
27 import java.util.Collection;
28 import org.modelmapper.ModelMapper;
29 import org.onap.cps.api.CpsAdminService;
30 import org.onap.cps.api.CpsModuleService;
31 import org.onap.cps.rest.api.CpsAdminApi;
32 import org.onap.cps.spi.model.Anchor;
33 import org.onap.cps.spi.model.SchemaSet;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RestController;
39 import org.springframework.web.multipart.MultipartFile;
40
41 @RestController
42 @RequestMapping("${rest.api.cps-base-path}")
43 public class AdminRestController implements CpsAdminApi {
44
45     @Autowired
46     private CpsAdminService cpsAdminService;
47
48     @Autowired
49     private CpsModuleService cpsModuleService;
50
51     @Autowired
52     private ModelMapper modelMapper;
53
54     @Override
55     public ResponseEntity<String> createDataspace(final String dataspaceName) {
56         cpsAdminService.createDataspace(dataspaceName);
57         return new ResponseEntity<>(dataspaceName, HttpStatus.CREATED);
58     }
59
60     @Override
61     public ResponseEntity<Object> deleteDataspace(final String dataspaceName) {
62         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
63     }
64
65     @Override
66     public ResponseEntity<String> createSchemaSet(final MultipartFile multipartFile,
67         final String schemaSetName, final String dataspaceName) {
68         cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
69         return new ResponseEntity<>(schemaSetName, HttpStatus.CREATED);
70     }
71
72     @Override
73     public ResponseEntity<Object> getSchemaSet(final String dataspaceName, final String schemaSetName) {
74         final SchemaSet schemaSet = cpsModuleService.getSchemaSet(dataspaceName, schemaSetName);
75         return new ResponseEntity<>(schemaSet, HttpStatus.OK);
76     }
77
78     @Override
79     public ResponseEntity<Void> deleteSchemaSet(final String dataspaceName, final String schemaSetName) {
80         cpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED);
81         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
82     }
83
84     /**
85      * Create a new anchor.
86      *
87      * @param dataspaceName dataspace name
88      * @param schemaSetName schema set name
89      * @param anchorName    anchorName
90      * @return a ResponseEntity with the anchor name.
91      */
92     @Override
93     public ResponseEntity<String> createAnchor(final String dataspaceName, final String schemaSetName,
94         final String anchorName) {
95         cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
96         return new ResponseEntity<>(anchorName, HttpStatus.CREATED);
97     }
98
99     @Override
100     public ResponseEntity<Void> deleteAnchor(final String dataspaceName, final String anchorName) {
101         cpsAdminService.deleteAnchor(dataspaceName, anchorName);
102         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
103     }
104
105     @Override
106     public ResponseEntity<Object> getAnchor(final String dataspaceName, final String anchorName) {
107         final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
108         return new ResponseEntity<>(anchor, HttpStatus.OK);
109     }
110
111     @Override
112     public ResponseEntity<Object> getAnchors(final String dataspaceName) {
113         final Collection<Anchor> anchorDetails = cpsAdminService.getAnchors(dataspaceName);
114         return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
115     }
116 }