7a8662c34ec9f7b9fdecaedd80490aa4bb99e073
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.policy.clamp.acm.element.main.rest;
22
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiResponse;
25 import io.swagger.annotations.ApiResponses;
26 import io.swagger.annotations.Authorization;
27 import io.swagger.annotations.Extension;
28 import io.swagger.annotations.ExtensionProperty;
29 import io.swagger.annotations.ResponseHeader;
30 import java.util.UUID;
31 import lombok.RequiredArgsConstructor;
32 import org.onap.policy.clamp.acm.element.service.ConfigService;
33 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.MediaType;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.DeleteMapping;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PostMapping;
40 import org.springframework.web.bind.annotation.RequestBody;
41 import org.springframework.web.bind.annotation.RestController;
42
43 @RestController
44 @RequiredArgsConstructor
45 public class AcElementController extends AbstractRestController {
46
47     private final ConfigService configService;
48
49
50     /**
51      * REST endpoint to get the existing element config.
52      *
53      * @return the element config params
54      */
55     // @formatter:off
56     @GetMapping(path = "/config", produces = MediaType.APPLICATION_JSON_VALUE)
57     @ApiOperation(
58             value = "Return the element config",
59             response = ElementConfig.class,
60             tags = {
61                 "Clamp Automation Composition AC Element Impl API"
62             },
63             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
64             responseHeaders = {
65                 @ResponseHeader(
66                     name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
67                     response = String.class),
68                 @ResponseHeader(
69                     name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
70                     response = String.class),
71                 @ResponseHeader(
72                     name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
73                     response = String.class),
74                 @ResponseHeader(
75                     name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
76                     response = UUID.class)},
77             extensions = {
78                 @Extension(
79                     name = EXTENSION_NAME,
80                     properties = {
81                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
82                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
83                     }
84                     )
85             })
86     @ApiResponses(
87         value = {
88             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
89             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
90             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
91         }
92     )
93     // @formatter:on
94     public ResponseEntity<ElementConfig> getElementConfig() {
95         return new ResponseEntity<>(configService.getElementConfig(), HttpStatus.OK);
96     }
97
98     /**
99      * REST endpoint to activate the element.
100      *
101      * @param params element parameters for this ac element
102      */
103     // @formatter:off
104     @PostMapping(path = "/activate", consumes = MediaType.APPLICATION_JSON_VALUE,
105          produces = MediaType.APPLICATION_JSON_VALUE)
106     @ApiOperation(
107         value = "Activates the element config",
108         response = ElementConfig.class,
109         tags = {
110             "Clamp Automation Composition AC Element Impl API"
111         },
112         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
113         responseHeaders = {
114             @ResponseHeader(
115                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
116                 response = String.class),
117             @ResponseHeader(
118                 name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
119                 response = String.class),
120             @ResponseHeader(
121                 name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
122                 response = String.class),
123             @ResponseHeader(
124                 name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
125                 response = UUID.class)
126             },
127         extensions = {
128             @Extension (
129                 name = EXTENSION_NAME,
130                 properties = {
131                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
132                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
133                 }
134                 )
135         }
136     )
137     @ApiResponses(
138         value = {
139             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
140             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
141             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
142         }
143     )
144     // formatter:on
145     public ResponseEntity<Object> activateElement(@RequestBody ElementConfig params) {
146         configService.activateElement(params);
147         return new ResponseEntity<>(HttpStatus.CREATED);
148     }
149
150     /**
151      * REST endpoint to delete the element config.
152      *
153      * @return Status of operation
154      */
155     // @formatter:off
156     @DeleteMapping(path = "/deactivate")
157     @ApiOperation(
158         value = "Delete the element config",
159         response = ElementConfig.class,
160         tags = {
161             "Clamp Automation Composition AC Element Impl API"
162         },
163         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
164         responseHeaders = {
165             @ResponseHeader(
166                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
167                 response = String.class),
168             @ResponseHeader(
169                 name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
170                 response = String.class),
171             @ResponseHeader(
172                 name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
173                 response = String.class),
174             @ResponseHeader(
175                 name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
176                 response = UUID.class)},
177         extensions = {
178             @Extension(
179                 name = EXTENSION_NAME,
180                 properties = {
181                     @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
182                     @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
183                 }
184                 )
185         }
186     )
187     @ApiResponses(
188         value = {
189             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
190             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
191             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
192         }
193     )
194     // @formatter:on
195     public ResponseEntity<Object> deleteConfig()  {
196         configService.deleteConfig();
197         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
198
199     }
200 }