02e56aa6e25b8f22778a3f8fcfb25fa14d8adb79
[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.acm.participant.simulator.simulation.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.List;
32 import java.util.UUID;
33 import org.onap.policy.clamp.acm.participant.simulator.main.rest.AbstractRestController;
34 import org.onap.policy.clamp.acm.participant.simulator.simulation.SimulationProvider;
35 import org.onap.policy.clamp.models.acm.concepts.Participant;
36 import org.onap.policy.clamp.models.acm.messages.rest.TypedSimpleResponse;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.PutMapping;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestHeader;
43 import org.springframework.web.bind.annotation.RestController;
44
45 /**
46  * Class to provide REST end points for participant simulator to query/update details of all participants.
47  */
48 @RestController
49 public class SimulationParticipantController extends AbstractRestController {
50
51     /**
52      * Constructor.
53      *
54      * @param simulationProvider the Simulation Provider
55      */
56     public SimulationParticipantController(SimulationProvider simulationProvider) {
57         super(simulationProvider);
58     }
59
60     /**
61      * Queries details of all participants within the simulator.
62      *
63      * @param requestId request ID used in ONAP logging
64      * @param name the name of the participant to get, null to get all
65      * @param version the version of the participant to get, null to get all
66      * @return the participants
67      */
68     // @formatter:off
69     @GetMapping("/participants/{name}/{version}")
70     @ApiOperation(value = "Query details of the requested simulated participants",
71             notes = "Queries details of the requested simulated participants, "
72                     + "returning all participant details",
73             response = List.class,
74             tags = {
75                 "Clamp Automation Composition Participant Simulator API"
76             },
77             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
78             responseHeaders = {
79                 @ResponseHeader(
80                     name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
81                     response = String.class),
82                 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
83                     response = String.class),
84                 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
85                     response = String.class),
86                 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
87                             response = UUID.class)},
88             extensions = {
89                 @Extension
90                     (
91                         name = EXTENSION_NAME,
92                         properties = {
93                             @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
94                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
95                         }
96                     )
97             }
98     )
99     @ApiResponses(
100             value = {
101                 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
102                 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
103                 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
104             }
105     )
106     // @formatter:on
107     public ResponseEntity<List<Participant>> participants(
108         @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
109         @ApiParam(value = "Participant name", required = true) @PathVariable("name") String name,
110         @ApiParam(value = "Participant version", required = true) @PathVariable("version") String version) {
111
112         return ResponseEntity
113             .ok()
114             .headers(super.getCommonHeaders(requestId))
115             .body(getSimulationProvider().getParticipants(name, version));
116     }
117
118     /**
119      * Updates a participant in the simulator.
120      *
121      * @param requestId request ID used in ONAP logging
122      * @param body the body of a participant
123      * @return a response
124      */
125     // @formatter:off
126     @PutMapping("/participants")
127     @ApiOperation(
128             value = "Updates simulated participants",
129             notes = "Updates simulated participants, returning the updated automation composition definition IDs",
130             response = TypedSimpleResponse.class,
131             tags = {
132                 "Clamp Automation Composition Participant Simulator API"
133                 },
134             authorizations = @Authorization(value = AUTHORIZATION_TYPE),
135             responseHeaders = {
136                 @ResponseHeader(
137                         name = VERSION_MINOR_NAME,
138                         description = VERSION_MINOR_DESCRIPTION,
139                         response = String.class),
140                 @ResponseHeader(
141                         name = VERSION_PATCH_NAME,
142                         description = VERSION_PATCH_DESCRIPTION,
143                         response = String.class),
144                 @ResponseHeader(
145                         name = VERSION_LATEST_NAME,
146                         description = VERSION_LATEST_DESCRIPTION,
147                         response = String.class),
148                 @ResponseHeader(
149                         name = REQUEST_ID_NAME,
150                         description = REQUEST_ID_HDR_DESCRIPTION,
151                         response = UUID.class)
152             },
153             extensions = {
154                 @Extension
155                     (
156                         name = EXTENSION_NAME,
157                         properties = {
158                             @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
159                             @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
160                         }
161                     )
162             }
163         )
164     @ApiResponses(
165             value = {
166                 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
167                 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
168                 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
169             }
170         )
171     // @formatter:on
172     public ResponseEntity<TypedSimpleResponse<Participant>> update(
173         @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
174         @ApiParam(value = "Body of a participant", required = true) @RequestBody Participant body) {
175
176         return ResponseEntity
177             .ok()
178             .headers(super.getCommonHeaders(requestId))
179             .body(getSimulationProvider().updateParticipant(body));
180     }
181 }