afe864edc3669073b5c90c25cc78939b6278afd8
[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.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.headers.Header;
25 import io.swagger.v3.oas.annotations.media.Content;
26 import io.swagger.v3.oas.annotations.media.Schema;
27 import io.swagger.v3.oas.annotations.responses.ApiResponse;
28 import io.swagger.v3.oas.annotations.responses.ApiResponses;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.clamp.acm.element.service.ConfigService;
31 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.web.bind.annotation.DeleteMapping;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.PostMapping;
38 import org.springframework.web.bind.annotation.RequestBody;
39 import org.springframework.web.bind.annotation.RestController;
40
41 @RestController
42 @RequiredArgsConstructor
43 public class AcElementController extends AbstractRestController {
44
45     private final ConfigService configService;
46
47     /**
48      * REST endpoint to get the existing element config.
49      *
50      * @return the element config params
51      */
52     // @formatter:off
53     @GetMapping(path = "/config", produces = MediaType.APPLICATION_JSON_VALUE)
54     @Operation(summary = "Return the element config",
55         tags = { "Clamp Automation Composition AC Element Impl API" })
56     @ApiResponses(
57         value = {
58             @ApiResponse(responseCode = OK_CODE, description = SERVER_OK_MESSAGE,
59                     content = @Content(schema = @Schema(implementation = ElementConfig.class)),
60                     headers = {
61                         @Header(name = API_VERSION_NAME),
62                         @Header(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION),
63                         @Header(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION),
64                         @Header(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION),
65                         @Header(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION),
66                         @Header(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION)
67                     }),
68             @ApiResponse(responseCode = AUTHENTICATION_ERROR_CODE, description = AUTHENTICATION_ERROR_MESSAGE)
69         }
70     )
71     // @formatter:on
72     public ResponseEntity<ElementConfig> getElementConfig() {
73         return new ResponseEntity<>(configService.getElementConfig(), HttpStatus.OK);
74     }
75
76     /**
77      * REST endpoint to activate the element.
78      *
79      * @param params element parameters for this ac element
80      */
81     // @formatter:off
82     @PostMapping(path = "/activate", consumes = MediaType.APPLICATION_JSON_VALUE,
83         produces = MediaType.APPLICATION_JSON_VALUE)
84     @Operation(summary = "Activates the element config",
85         tags = { "Clamp Automation Composition AC Element Impl API" }
86     )
87     @ApiResponses(
88         value = {
89             @ApiResponse(responseCode = CREATED_CODE, description = SERVER_OK_MESSAGE,
90                     headers = {
91                         @Header(name = API_VERSION_NAME),
92                         @Header(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION),
93                         @Header(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION),
94                         @Header(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION),
95                         @Header(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION),
96                         @Header(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION)
97                     }),
98             @ApiResponse(responseCode = AUTHENTICATION_ERROR_CODE, description = AUTHENTICATION_ERROR_MESSAGE),
99             @ApiResponse(responseCode = BAD_REQUEST_ERROR_CODE, description = BAD_REQUEST_ERROR_MESSAGE)
100         }
101     )
102     // formatter:on
103     public ResponseEntity<Object> activateElement(@RequestBody ElementConfig params) {
104         configService.activateElement(params);
105         return new ResponseEntity<>(HttpStatus.CREATED);
106     }
107
108     /**
109      * REST endpoint to delete the element config.
110      *
111      * @return Status of operation
112      */
113     // @formatter:off
114     @DeleteMapping(path = "/deactivate")
115     @Operation(summary = "Delete the element config",
116         tags = { "Clamp Automation Composition AC Element Impl API" }
117     )
118     @ApiResponses(
119         value = {
120             @ApiResponse(responseCode = NO_CONTENT_CODE, description = SERVER_OK_MESSAGE,
121                     headers = {
122                         @Header(name = API_VERSION_NAME),
123                         @Header(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION),
124                         @Header(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION),
125                         @Header(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION),
126                         @Header(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION),
127                         @Header(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION)
128                     }),
129             @ApiResponse(responseCode = AUTHENTICATION_ERROR_CODE, description = AUTHENTICATION_ERROR_MESSAGE)
130         }
131     )
132     // @formatter:on
133     public ResponseEntity<Void> deleteConfig() {
134         configService.deleteConfig();
135         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
136     }
137 }