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