Decouple configuration from application
[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  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.rest.controller;
22
23 import static org.onap.cps.rest.utils.MultipartFileUtil.extractYangResourcesMap;
24 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED;
25
26 import java.util.Collection;
27 import org.modelmapper.ModelMapper;
28 import org.onap.cps.api.CpsAdminService;
29 import org.onap.cps.api.CpsModuleService;
30 import org.onap.cps.rest.api.CpsAdminApi;
31 import org.onap.cps.spi.model.Anchor;
32 import org.onap.cps.spi.model.SchemaSet;
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<String> createSchemaSet(final MultipartFile multipartFile,
61         final String schemaSetName, final String dataspaceName) {
62         cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, extractYangResourcesMap(multipartFile));
63         return new ResponseEntity<>(schemaSetName, HttpStatus.CREATED);
64     }
65
66     @Override
67     public ResponseEntity<Object> getSchemaSet(final String dataspaceName, final String schemaSetName) {
68         final SchemaSet schemaSet = cpsModuleService.getSchemaSet(dataspaceName, schemaSetName);
69         return new ResponseEntity<>(schemaSet, HttpStatus.OK);
70     }
71
72     @Override
73     public ResponseEntity<Void> deleteSchemaSet(final String dataspaceName, final String schemaSetName) {
74         cpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED);
75         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
76     }
77
78     /**
79      * Create a new anchor.
80      *
81      * @param dataspaceName dataspace name
82      * @param schemaSetName schema set name
83      * @param anchorName    anchorName
84      * @return a ResponseEntity with the anchor name.
85      */
86     @Override
87     public ResponseEntity<String> createAnchor(final String dataspaceName, final String schemaSetName,
88         final String anchorName) {
89         cpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName);
90         return new ResponseEntity<>(anchorName, HttpStatus.CREATED);
91     }
92
93     @Override
94     public ResponseEntity<Object> deleteAnchor(final String dataspaceName, final String anchorName) {
95         return null;
96     }
97
98     @Override
99     public ResponseEntity<Object> deleteDataspace(final String dataspaceName) {
100         return null;
101     }
102
103     @Override
104     public ResponseEntity<Object> getAnchor(final String dataspaceName, final String anchorName) {
105         return null;
106     }
107
108     @Override
109     public ResponseEntity<Object> getAnchors(final String dataspaceName) {
110         final Collection<Anchor> anchorDetails = cpsAdminService.getAnchors(dataspaceName);
111         return new ResponseEntity<>(anchorDetails, HttpStatus.OK);
112     }
113 }