29919f528acf4029002b5dbb6edeffeddf8c6cda
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.runtime.main.rest;
22
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiParam;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27 import io.swagger.annotations.Authorization;
28 import io.swagger.annotations.Extension;
29 import io.swagger.annotations.ExtensionProperty;
30 import io.swagger.annotations.ResponseHeader;
31 import java.util.UUID;
32 import lombok.RequiredArgsConstructor;
33 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
35 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand;
36 import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse;
37 import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider;
38 import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController;
39 import org.onap.policy.models.base.PfModelException;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.DeleteMapping;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.PutMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RequestHeader;
48 import org.springframework.web.bind.annotation.RequestParam;
49 import org.springframework.web.bind.annotation.RestController;
50
51 /**
52  * Class to provide REST end points for creating, deleting, query and commanding a control loop definition.
53  */
54 @RestController
55 @RequiredArgsConstructor
56 public class InstantiationController extends AbstractRestController {
57
58     private static final String TAGS = "Clamp Control Loop Instantiation API";
59
60     // The CL provider for instantiation requests
61     private final ControlLoopInstantiationProvider provider;
62
63     /**
64      * Creates a control loop.
65      *
66      * @param requestId request ID used in ONAP logging
67      * @param controlLoops the control loops
68      * @return a response
69      * @throws PfModelException on errors creating a control loop
70      */
71     // @formatter:off
72     @PostMapping(value = "/instantiation",
73             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
74             consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
75     @ApiOperation(
76             value = "Commissions control loop definitions",
77             notes = "Commissions control loop definitions, returning the control loop IDs",
78             response = InstantiationResponse.class,
79             tags = {TAGS},
80             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
81             responseHeaders = {
82                 @ResponseHeader(
83                     name = VERSION_MINOR_NAME,
84                     description = VERSION_MINOR_DESCRIPTION,
85                     response = String.class),
86                 @ResponseHeader(
87                     name = VERSION_PATCH_NAME,
88                     description = VERSION_PATCH_DESCRIPTION,
89                     response = String.class),
90                 @ResponseHeader(
91                     name = VERSION_LATEST_NAME,
92                     description = VERSION_LATEST_DESCRIPTION,
93                     response = String.class),
94                 @ResponseHeader(
95                     name = REQUEST_ID_NAME,
96                     description = REQUEST_ID_HDR_DESCRIPTION,
97                     response = UUID.class)
98                 },
99             extensions = {
100                 @Extension
101                     (
102                         name = EXTENSION_NAME,
103                         properties = {
104                             @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
105                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
106                         }
107                     )
108             }
109         )
110     @ApiResponses(
111             value = {
112                 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
113                 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
114                 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
115             }
116         )
117     // @formatter:on
118     public ResponseEntity<InstantiationResponse> create(
119             @RequestHeader(
120                     name = REQUEST_ID_NAME,
121                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
122             @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
123             throws PfModelException {
124
125         return ResponseEntity.ok().body(provider.createControlLoops(controlLoops));
126     }
127
128     /**
129      * Queries details of all control loops.
130      *
131      * @param requestId request ID used in ONAP logging
132      * @param name the name of the control loop to get, null for all control loops
133      * @param version the version of the control loop to get, null for all control loops
134      * @return the control loops
135      * @throws PfModelException on errors getting commissioning of control loop
136      */
137     // @formatter:off
138     @GetMapping(value = "/instantiation",
139             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
140     @ApiOperation(value = "Query details of the requested control loops",
141             notes = "Queries details of the requested control loops, returning all control loop details",
142             response = ControlLoops.class,
143             tags = {TAGS},
144             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
145             responseHeaders = {
146                 @ResponseHeader(
147                     name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
148                     response = String.class),
149                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
150                     response = String.class),
151                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
152                     response = String.class),
153                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
154                     response = UUID.class)},
155             extensions = {
156                 @Extension
157                      (
158                          name = EXTENSION_NAME,
159                          properties = {
160                              @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
161                              @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
162                          }
163                     )
164                 }
165         )
166     @ApiResponses(
167             value = {
168                 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
169                 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
170                 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
171             }
172         )
173     // @formatter:on
174     public ResponseEntity<ControlLoops> query(
175             @RequestHeader(
176                     name = REQUEST_ID_NAME,
177                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
178             @ApiParam(value = "Control Loop definition name", required = false) @RequestParam(
179                     value = "name",
180                     required = false) String name,
181             @ApiParam(value = "Control Loop definition version", required = false) @RequestParam(
182                     value = "version",
183                     required = false) String version)
184             throws PfModelException {
185
186         return ResponseEntity.ok().body(provider.getControlLoops(name, version));
187     }
188
189     /**
190      * Updates a control loop.
191      *
192      * @param requestId request ID used in ONAP logging
193      * @param controlLoops the control loops
194      * @return a response
195      * @throws PfModelException on errors updating of control loops
196      */
197     // @formatter:off
198     @PutMapping(value = "/instantiation",
199             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
200             consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
201     @ApiOperation(
202             value = "Updates control loop definitions",
203             notes = "Updates control loop definitions, returning the updated control loop definition IDs",
204             response = InstantiationResponse.class,
205             tags = {TAGS},
206             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
207             responseHeaders = {
208                 @ResponseHeader(
209                     name = VERSION_MINOR_NAME,
210                     description = VERSION_MINOR_DESCRIPTION,
211                     response = String.class),
212                 @ResponseHeader(
213                     name = VERSION_PATCH_NAME,
214                     description = VERSION_PATCH_DESCRIPTION,
215                     response = String.class),
216                 @ResponseHeader(
217                     name = VERSION_LATEST_NAME,
218                     description = VERSION_LATEST_DESCRIPTION,
219                     response = String.class),
220                 @ResponseHeader(
221                     name = REQUEST_ID_NAME,
222                     description = REQUEST_ID_HDR_DESCRIPTION,
223                     response = UUID.class)
224             },
225             extensions = {
226                 @Extension
227                     (
228                         name = EXTENSION_NAME,
229                         properties = {
230                             @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
231                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
232                         }
233                     )
234             }
235         )
236     @ApiResponses(
237             value = {
238                 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
239                 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
240                 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
241             }
242         )
243     // @formatter:on
244     public ResponseEntity<InstantiationResponse> update(
245             @RequestHeader(
246                     name = REQUEST_ID_NAME,
247                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
248             @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ControlLoops controlLoops)
249             throws PfModelException {
250
251         return ResponseEntity.ok().body(provider.updateControlLoops(controlLoops));
252     }
253
254     /**
255      * Deletes a control loop definition.
256      *
257      * @param requestId request ID used in ONAP logging
258      * @param name the name of the control loop to delete
259      * @param version the version of the control loop to delete
260      * @return a response
261      * @throws PfModelException on errors deleting of control loop
262      */
263     // @formatter:off
264     @DeleteMapping(value = "/instantiation",
265             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
266     @ApiOperation(value = "Delete a control loop",
267             notes = "Deletes a control loop, returning optional error details",
268             response = InstantiationResponse.class,
269             tags = {TAGS},
270             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
271             responseHeaders = {
272                 @ResponseHeader(
273                     name = VERSION_MINOR_NAME,
274                     description = VERSION_MINOR_DESCRIPTION,
275                     response = String.class),
276                 @ResponseHeader(
277                     name = VERSION_PATCH_NAME,
278                     description = VERSION_PATCH_DESCRIPTION,
279                     response = String.class),
280                 @ResponseHeader(
281                     name = VERSION_LATEST_NAME,
282                     description = VERSION_LATEST_DESCRIPTION,
283                     response = String.class),
284                 @ResponseHeader(
285                     name = REQUEST_ID_NAME,
286                     description = REQUEST_ID_HDR_DESCRIPTION,
287                     response = UUID.class)},
288             extensions = {
289                 @Extension
290                     (
291                         name = EXTENSION_NAME,
292                         properties = {
293                             @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
294                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
295                         }
296                     )
297                 }
298         )
299     @ApiResponses(
300         value = {
301             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
302             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
303             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
304         }
305     )
306     // @formatter:on
307
308     public ResponseEntity<InstantiationResponse> delete(
309             @RequestHeader(
310                     name = REQUEST_ID_NAME,
311                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
312             @ApiParam(value = "Control Loop definition name", required = true) @RequestParam("name") String name,
313             @ApiParam(value = "Control Loop definition version") @RequestParam(
314                     value = "version",
315                     required = false) String version)
316             throws PfModelException {
317
318         return ResponseEntity.ok().body(provider.deleteControlLoop(name, version));
319     }
320
321     /**
322      * Issues control loop commands to control loops.
323      *
324      * @param requestId request ID used in ONAP logging
325      * @param command the command to issue to control loops
326      * @return the control loop definitions
327      * @throws PfModelException on errors issuing a command
328      * @throws ControlLoopException on errors issuing a command
329      */
330     // @formatter:off
331     @PutMapping(value = "/instantiation/command",
332             produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML},
333             consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML})
334     @ApiOperation(value = "Issue a command to the requested control loops",
335             notes = "Issues a command to a control loop, ordering a state change on the control loop",
336             response = InstantiationResponse.class,
337             tags = {TAGS},
338             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
339             responseHeaders = {
340                 @ResponseHeader(
341                     name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
342                     response = String.class),
343                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
344                     response = String.class),
345                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
346                     response = String.class),
347                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
348                     response = UUID.class)},
349             extensions = {
350                 @Extension
351                     (
352                         name = EXTENSION_NAME,
353                         properties = {
354                             @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
355                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
356                         }
357                     )
358                 }
359         )
360     @ApiResponses(
361             value = {
362                 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
363                 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
364                 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
365             }
366         )
367     // @formatter:on
368     public ResponseEntity<InstantiationResponse> issueControlLoopCommand(
369             @RequestHeader(
370                     name = REQUEST_ID_NAME,
371                     required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
372             @ApiParam(
373                     value = "Entity Body of control loop command",
374                     required = true) @RequestBody InstantiationCommand command)
375             throws ControlLoopException, PfModelException {
376
377         return ResponseEntity.accepted().body(provider.issueControlLoopCommand(command));
378     }
379 }