Fix Code smells
[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.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RestController;
38 import org.springframework.web.multipart.MultipartFile;
39
40 @RestController
41 @RequestMapping("${rest.api.cps-base-path}")
42 public class AdminRestController implements CpsAdminApi {
43
44     @Autowired
45     private CpsAdminService cpsAdminService;
46
47     @Autowired
48     private CpsModuleService cpsModuleService;
49
50     @Autowired
51     private ModelMapper modelMapper;
52
53     @Override
54     public ResponseEntity<String> createDataspace(final String dataspaceName) {
55         cpsAdminService.createDataspace(dataspaceName);
56         return new ResponseEntity<>(dataspaceName, HttpStatus.CREATED);
57     }
58
59     @Override
60     public ResponseEntity<Object> deleteDataspace(final String dataspaceName) {
61         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
62     }
63
64     @Override
65     public ResponseEntity<String> createSchemaSet(final MultipartFile multipartFile,
66         final String schemaSetName, final String dataspaceName) {
67         cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
68         return new ResponseEntity<>(schemaSetName, HttpStatus.CREATED);
69     }
70
71     @Override
72     public ResponseEntity<Object> getSchemaSet(final String dataspaceName, final String schemaSetName) {
73         final var schemaSet = cpsModuleService.getSchemaSet(dataspaceName, schemaSetName);
74         return new ResponseEntity<>(schemaSet, HttpStatus.OK);
75     }
76
77     @Override
78     public ResponseEntity<Void> deleteSchemaSet(final String dataspaceName, final String schemaSetName) {
79         cpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED);
80         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
81     }
82
83     /**
84      * Create a new anchor.
85      *
86      * @param dataspaceName dataspace name
87      * @param schemaSetName schema set name
88      * @param anchorName    anchorName
89      * @return a ResponseEntity with the anchor name.
90      */
91     @Override
92     public ResponseEntity<String> createAnchor(final String dataspaceName, final String schemaSetName,
93         final String anchorName) {
94         cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
95         return new ResponseEntity<>(anchorName, HttpStatus.CREATED);
96     }
97
98     @Override
99     public ResponseEntity<Void> deleteAnchor(final String dataspaceName, final String anchorName) {
100         cpsAdminService.deleteAnchor(dataspaceName, anchorName);
101         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
102     }
103
104     @Override
105     public ResponseEntity<Object> getAnchor(final String dataspaceName, final String anchorName) {
106         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
107         return new ResponseEntity<>(anchor, HttpStatus.OK);
108     }
109
110     @Override
111     public ResponseEntity<Object> getAnchors(final String dataspaceName) {
112         final Collection<Anchor> anchorDetails = cpsAdminService.getAnchors(dataspaceName);
113         return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
114     }
115 }