Add more endpoints and swagger annotations
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / ApiRestController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.rest;
25
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiParam;
29 import io.swagger.annotations.ApiResponse;
30 import io.swagger.annotations.ApiResponses;
31 import io.swagger.annotations.Authorization;
32 import io.swagger.annotations.BasicAuthDefinition;
33 import io.swagger.annotations.Info;
34 import io.swagger.annotations.SecurityDefinition;
35 import io.swagger.annotations.SwaggerDefinition;
36 import io.swagger.annotations.Tag;
37 import javax.ws.rs.Consumes;
38 import javax.ws.rs.DELETE;
39 import javax.ws.rs.GET;
40 import javax.ws.rs.POST;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.QueryParam;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47 import org.onap.policy.api.main.rest.provider.HealthCheckProvider;
48 import org.onap.policy.api.main.rest.provider.PolicyProvider;
49 import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;
50 import org.onap.policy.api.main.rest.provider.StatisticsProvider;
51 import org.onap.policy.common.endpoints.report.HealthCheckReport;
52 import org.onap.policy.model.tosca.ToscaPolicy;
53 import org.onap.policy.model.tosca.ToscaPolicyList;
54 import org.onap.policy.model.tosca.ToscaPolicyType;
55 import org.onap.policy.model.tosca.ToscaPolicyTypeList;
56
57 /**
58  * Class to provide REST API services.
59  */
60 @Path("/policy/api/v1")
61 @Api(value = "Policy Design API")
62 @Produces(MediaType.APPLICATION_JSON)
63 @Consumes(MediaType.APPLICATION_JSON)
64 @SwaggerDefinition(info = @Info(
65         description = "Policy Design API is publicly exposed for clients to Create/Read/Update/Delete"
66                     + "policy types, policy type implementation and policies which can be recognized"
67                     + "and executable by incorporated policy engines XACML, Drools and APEX. It is a"
68                     + "standalone component running rest service that takes all policy design API calls"
69                     + "from clients and then assign them to different API working functions. Besides"
70                     + "that, API is also exposed for clients to retrieve healthcheck status of this API"
71                     + "rest service and the statistics report including the counters of API invocation.",
72         version = "v1.0", title = "Policy Design"),
73         consumes = { MediaType.APPLICATION_JSON }, produces = { MediaType.APPLICATION_JSON },
74         schemes = { SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS },
75         tags = { @Tag(name = "policy-api", description = "Policy API Service Operations") },
76         securityDefinition = @SecurityDefinition(basicAuthDefinitions = { @BasicAuthDefinition(key = "basicAuth") }))
77 public class ApiRestController {
78
79     /**
80      * Retrieves the healthcheck status of the API component.
81      *
82      * @return the Response object containing the results of the API operation
83      */
84     @GET
85     @Path("/healthcheck")
86     @ApiOperation(value = "Perform a system healthcheck",
87             notes = "Returns healthy status of the Policy API component",
88             response = HealthCheckReport.class,
89             authorizations = @Authorization(value = "basicAuth"),
90             tags = { "HealthCheck", })
91     @ApiResponses(value = {
92             @ApiResponse(code = 401, message = "Authentication Error"),
93             @ApiResponse(code = 403, message = "Authorization Error"),
94             @ApiResponse(code = 500, message = "Internal Server Error")
95         })
96     public Response getHealthCheck() {
97         return Response.status(Response.Status.OK).entity(new HealthCheckProvider().performHealthCheck()).build();
98     }
99
100     /**
101      * Retrieves the statistics report of the API component.
102      *
103      * @return the Response object containing the results of the API operation
104      */
105     @GET
106     @Path("/statistics")
107     @ApiOperation(value = "Retrieve current statistics",
108             notes = "Returns current statistics including the counters of API invocation",
109             response = StatisticsReport.class,
110             authorizations = @Authorization(value = "basicAuth"),
111             tags = { "Statistics", })
112     @ApiResponses(value = {
113             @ApiResponse(code = 401, message = "Authentication Error"),
114             @ApiResponse(code = 403, message = "Authorization Error"),
115             @ApiResponse(code = 500, message = "Internal Server Error")
116         })
117     public Response getStatistics() {
118         return Response.status(Response.Status.OK).entity(new StatisticsProvider().fetchCurrentStatistics()).build();
119     }
120
121     /**
122      * Retrieves all available policy types.
123      *
124      * @return the Response object containing the results of the API operation
125      */
126     @GET
127     @Path("/policytypes")
128     @ApiOperation(value = "Retrieve existing policy types",
129             notes = "Returns a list of existing policy types stored in Policy Framework",
130             response = ToscaPolicyTypeList.class,
131             authorizations = @Authorization(value = "basicAuth"),
132             tags = { "PolicyType", })
133     @ApiResponses(value = {
134             @ApiResponse(code = 401, message = "Authentication Error"),
135             @ApiResponse(code = 403, message = "Authorization Error"),
136             @ApiResponse(code = 500, message = "Internal Server Error")
137         })
138     public Response getAllPolicyTypes() {
139         return Response.status(Response.Status.OK)
140             .entity(new PolicyTypeProvider().fetchPolicyTypes(null, null)).build();
141     }
142
143     /**
144      * Retrieves all versions of a particular policy type.
145      *
146      * @param policyTypeId the ID of specified policy type
147      *
148      * @return the Response object containing the results of the API operation
149      */
150     @GET
151     @Path("/policytypes/{policyTypeId}")
152     @ApiOperation(value = "Retrieve all available versions of a policy type",
153             notes = "Returns a list of all available versions for the specified policy type",
154             response = ToscaPolicyTypeList.class,
155             authorizations = @Authorization(value = "basicAuth"),
156             tags = { "PolicyType", })
157     @ApiResponses(value = {
158             @ApiResponse(code = 401, message = "Authentication Error"),
159             @ApiResponse(code = 403, message = "Authorization Error"),
160             @ApiResponse(code = 404, message = "Resource Not Found"),
161             @ApiResponse(code = 500, message = "Internal Server Error")
162         })
163     public Response getAllVersionsOfPolicyType(
164             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId) {
165         return Response.status(Response.Status.OK)
166             .entity(new PolicyTypeProvider().fetchPolicyTypes(policyTypeId, null)).build();
167     }
168
169     /**
170      * Retrieves specified version of a particular policy type.
171      *
172      * @param policyTypeId the ID of specified policy type
173      * @param versionId the version of specified policy type
174      *
175      * @return the Response object containing the results of the API operation
176      */
177     @GET
178     @Path("/policytypes/{policyTypeId}/versions/{versionId}")
179     @ApiOperation(value = "Retrieve one particular version of a policy type",
180             notes = "Returns a particular version for the specified policy type",
181             response = ToscaPolicyTypeList.class,
182             authorizations = @Authorization(value = "basicAuth"),
183             tags = { "PolicyType", })
184     @ApiResponses(value = {
185             @ApiResponse(code = 401, message = "Authentication Error"),
186             @ApiResponse(code = 403, message = "Authorization Error"),
187             @ApiResponse(code = 404, message = "Resource Not Found"),
188             @ApiResponse(code = 500, message = "Internal Server Error")
189         })
190     public Response getSpecificVersionOfPolicyType(
191             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
192             @PathParam("versionId") @ApiParam("ID of version") String versionId) {
193         return Response.status(Response.Status.OK)
194             .entity(new PolicyTypeProvider().fetchPolicyTypes(policyTypeId, versionId)).build();
195     }
196
197     /**
198      * Creates a new policy type.
199      *
200      * @param body the body of policy type following TOSCA definition
201      *
202      * @return the Response object containing the results of the API operation
203      */
204     @POST
205     @Path("/policytypes")
206     @ApiOperation(value = "Create a new policy type",
207             notes = "Client should provide TOSCA body of the new policy type",
208             authorizations = @Authorization(value = "basicAuth"),
209             tags = { "PolicyType", })
210     @ApiResponses(value = {
211             @ApiResponse(code = 400, message = "Invalid Body"),
212             @ApiResponse(code = 401, message = "Authentication Error"),
213             @ApiResponse(code = 403, message = "Authorization Error"),
214             @ApiResponse(code = 500, message = "Internal Server Error")
215         })
216     public Response createPolicyType(ToscaPolicyType body) {
217         return Response.status(Response.Status.OK).entity(new PolicyTypeProvider().createPolicyType(body)).build();
218     }
219
220     /**
221      * Deletes all versions of a particular policy type.
222      *
223      * @param policyTypeId the ID of specified policy type
224      *
225      * @return the Response object containing the results of the API operation
226      */
227     @DELETE
228     @Path("/policytypes/{policyTypeId}")
229     @ApiOperation(value = "Delete all versions of a policy type",
230             notes = "Rule 1: pre-defined policy types cannot be deleted;"
231                   + "Rule 2: policy types that are in use (parameterized by a TOSCA policy) cannot be deleted."
232                   + "The parameterizing TOSCA policies must be deleted first;",
233             authorizations = @Authorization(value = "basicAuth"),
234             tags = { "PolicyType", })
235     @ApiResponses(value = {
236             @ApiResponse(code = 401, message = "Authentication Error"),
237             @ApiResponse(code = 403, message = "Authorization Error"),
238             @ApiResponse(code = 404, message = "Resource Not Found"),
239             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
240             @ApiResponse(code = 500, message = "Internal Server Error")
241         })
242     public Response deleteAllVersionsOfPolicyType(
243             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId) {
244         return Response.status(Response.Status.OK)
245             .entity(new PolicyTypeProvider().deletePolicyTypes(policyTypeId, null)).build();
246     }
247
248     /**
249      * Deletes specified version of a particular policy type.
250      *
251      * @param policyTypeId the ID of specified policy type
252      * @param versionId the version of specified policy type
253      *
254      * @return the Response object containing the results of the API operation
255      */
256     @DELETE
257     @Path("/policytypes/{policyTypeId}/versions/{versionId}")
258     @ApiOperation(value = "Delete one version of a policy type",
259             notes = "Rule 1: pre-defined policy types cannot be deleted;"
260                   + "Rule 2: policy types that are in use (parameterized by a TOSCA policy) cannot be deleted."
261                   + "The parameterizing TOSCA policies must be deleted first;",
262             authorizations = @Authorization(value = "basicAuth"),
263             tags = { "PolicyType", })
264     @ApiResponses(value = {
265             @ApiResponse(code = 401, message = "Authentication Error"),
266             @ApiResponse(code = 403, message = "Authorization Error"),
267             @ApiResponse(code = 404, message = "Resource Not Found"),
268             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
269             @ApiResponse(code = 500, message = "Internal Server Error")
270         })
271     public Response deleteSpecificVersionOfPolicyType(
272             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
273             @PathParam("versionId") @ApiParam("ID of version") String versionId) {
274         return Response.status(Response.Status.OK)
275             .entity(new PolicyTypeProvider().deletePolicyTypes(policyTypeId, versionId)).build();
276     }
277
278     /**
279      * Retrieves all versions of a particular policy.
280      *
281      * @param policyTypeId the ID of specified policy type
282      * @param policyTypeVersion the version of specified policy type
283      *
284      * @return the Response object containing the results of the API operation
285      */
286     @GET
287     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")
288     @ApiOperation(value = "Retrieve all versions of a policy created for a particular policy type version",
289             notes = "Returns a list of all versions of specified policy created for the specified policy type version",
290             response = ToscaPolicyList.class,
291             authorizations = @Authorization(value = "basicAuth"),
292             tags = { "Policy", })
293     @ApiResponses(value = {
294             @ApiResponse(code = 401, message = "Authentication Error"),
295             @ApiResponse(code = 403, message = "Authorization Error"),
296             @ApiResponse(code = 404, message = "Resource Not Found"),
297             @ApiResponse(code = 500, message = "Internal Server Error")
298         })
299     public Response getAllPolicies(
300             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
301             @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion) {
302         return Response.status(Response.Status.OK)
303             .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion, null, null)).build();
304     }
305
306     /**
307      * Retrieves all versions of a particular policy.
308      *
309      * @param policyTypeId the ID of specified policy type
310      * @param policyTypeVersion the version of specified policy type
311      * @param policyId the ID of specified policy
312      *
313      * @return the Response object containing the results of the API operation
314      */
315     @GET
316     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}")
317     @ApiOperation(value = "Retrieve all version details of a policy created for a particular policy type version",
318             notes = "Returns a list of all version details of the specified policy",
319             response = ToscaPolicyList.class,
320             authorizations = @Authorization(value = "basicAuth"),
321             tags = { "Policy", })
322     @ApiResponses(value = {
323             @ApiResponse(code = 401, message = "Authentication Error"),
324             @ApiResponse(code = 403, message = "Authorization Error"),
325             @ApiResponse(code = 404, message = "Resource Not Found"),
326             @ApiResponse(code = 500, message = "Internal Server Error")
327         })
328     public Response getAllVersionsOfPolicy(
329             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
330             @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
331             @PathParam("policyId") @ApiParam("ID of policy") String policyId) {
332         return Response.status(Response.Status.OK)
333             .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion, policyId, null)).build();
334     }
335
336     /**
337      * Retrieves the specified version of a particular policy.
338      *
339      * @param policyTypeId the ID of specified policy type
340      * @param policyTypeVersion the version of specified policy type
341      * @param policyId the ID of specified policy
342      * @param policyVersion the version of specified policy
343      *
344      * @return the Response object containing the results of the API operation
345      */
346     @GET
347     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")
348     @ApiOperation(value = "Retrieve one version of a policy created for a particular policy type version",
349             notes = "Returns a particular version of specified policy created for the specified policy type version",
350             response = ToscaPolicyList.class,
351             authorizations = @Authorization(value = "basicAuth"),
352             tags = { "Policy", })
353     @ApiResponses(value = {
354             @ApiResponse(code = 401, message = "Authentication Error"),
355             @ApiResponse(code = 403, message = "Authorization Error"),
356             @ApiResponse(code = 404, message = "Resource Not Found"),
357             @ApiResponse(code = 500, message = "Internal Server Error")
358         })
359     public Response getSpecificVersionOfPolicy(
360             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
361             @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
362             @PathParam("policyId") @ApiParam("ID of policy") String policyId,
363             @PathParam("policyVersion") @ApiParam("ID of policy version") String policyVersion) {
364         return Response.status(Response.Status.OK)
365             .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion,
366                                                        policyId, policyVersion)).build();
367     }
368
369     /**
370      * Retrieves either latest or deployed version of a particular policy depending on query parameter.
371      *
372      * @param policyTypeId the ID of specified policy type
373      * @param policyTypeVersion the version of specified policy type
374      * @param policyId the ID of specified policy
375      *
376      * @return the Response object containing the results of the API operation
377      */
378     @GET
379     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions")
380     @ApiOperation(value = "Retrieve either latest or deployed version of a particular policy depending on query param",
381             notes = "Returns either latest or deployed version of specified policy depending on query param",
382             response = ToscaPolicyList.class,
383             authorizations = @Authorization(value = "basicAuth"),
384             tags = { "Policy", })
385     @ApiResponses(value = {
386             @ApiResponse(code = 401, message = "Authentication Error"),
387             @ApiResponse(code = 403, message = "Authorization Error"),
388             @ApiResponse(code = 404, message = "Resource Not Found"),
389             @ApiResponse(code = 500, message = "Internal Server Error")
390         })
391     public Response getEitherLatestOrDeployedVersionOfPolicy(
392             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
393             @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
394             @PathParam("policyId") @ApiParam("ID of policy") String policyId,
395             @QueryParam("type") @ApiParam("Type of version that can only be 'latest' or 'deployed'") String type) {
396         return Response.status(Response.Status.OK)
397             .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion, policyId, type)).build();
398     }
399
400     /**
401      * Creates a new policy for a particular policy type and version.
402      *
403      * @param policyTypeId the ID of specified policy type
404      * @param policyTypeVersion the version of specified policy type
405      * @param body the body of policy following TOSCA definition
406      *
407      * @return the Response object containing the results of the API operation
408      */
409     @POST
410     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")
411     @ApiOperation(value = "Create a new policy for a policy type version",
412             notes = "Client should provide TOSCA body of the new policy",
413             authorizations = @Authorization(value = "basicAuth"),
414             tags = { "Policy", })
415     @ApiResponses(value = {
416             @ApiResponse(code = 400, message = "Invalid Body"),
417             @ApiResponse(code = 401, message = "Authentication Error"),
418             @ApiResponse(code = 403, message = "Authorization Error"),
419             @ApiResponse(code = 404, message = "Resource Not Found"),
420             @ApiResponse(code = 500, message = "Internal Server Error")
421         })
422     public Response createPolicy(
423             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
424             @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
425             ToscaPolicy body) {
426         return Response.status(Response.Status.OK)
427             .entity(new PolicyProvider().createPolicy(policyTypeId, policyTypeVersion, body)).build();
428     }
429
430     /**
431      * Deletes all versions of a particular policy.
432      *
433      * @param policyTypeId the ID of specified policy type
434      * @param policyTypeVersion the version of specified policy type
435      * @param policyId the ID of specified policy
436      *
437      * @return the Response object containing the results of the API operation
438      */
439     @DELETE
440     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}")
441     @ApiOperation(value = "Delete all versions of a policy",
442             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
443             authorizations = @Authorization(value = "basicAuth"),
444             tags = { "Policy", })
445     @ApiResponses(value = {
446             @ApiResponse(code = 401, message = "Authentication Error"),
447             @ApiResponse(code = 403, message = "Authorization Error"),
448             @ApiResponse(code = 404, message = "Resource Not Found"),
449             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
450             @ApiResponse(code = 500, message = "Internal Server Error")
451         })
452     public Response deleteAllVersionsOfPolicy(
453             @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
454             @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
455             @PathParam("policyId") @ApiParam("ID of policy") String policyId) {
456         return Response.status(Response.Status.OK)
457             .entity(new PolicyProvider().deletePolicies(policyTypeId, policyTypeVersion, policyId, null)).build();
458     }
459
460     /**
461      * Deletes the specified version of a particular policy.
462      *
463      * @param policyTypeId the ID of specified policy type
464      * @param policyTypeVersion the version of specified policy type
465      * @param policyId the ID of specified policy
466      * @param policyVersion the version of specified policy
467      *
468      * @return the Response object containing the results of the API operation
469      */
470     @DELETE
471     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")
472     @ApiOperation(value = "Delete a particular version of a policy",
473             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
474             authorizations = @Authorization(value = "basicAuth"),
475             tags = { "Policy", })
476     @ApiResponses(value = {
477             @ApiResponse(code = 401, message = "Authentication Error"),
478             @ApiResponse(code = 403, message = "Authorization Error"),
479             @ApiResponse(code = 404, message = "Resource Not Found"),
480             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
481             @ApiResponse(code = 500, message = "Internal Server Error")
482         })
483     public Response deleteSpecificVersionOfPolicy(
484             @PathParam("policyTypeId") @ApiParam("PolicyType ID") String policyTypeId,
485             @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
486             @PathParam("policyId") @ApiParam("ID of policy") String policyId,
487             @PathParam("policyVersion") @ApiParam("ID of policy version") String policyVersion) {
488         return Response.status(Response.Status.OK)
489             .entity(new PolicyProvider().deletePolicies(policyTypeId, policyTypeVersion,
490                                                         policyId, policyVersion)).build();
491     }
492 }