Add code to follow ONAP API CVS guidelines
[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.Extension;
34 import io.swagger.annotations.ExtensionProperty;
35 import io.swagger.annotations.Info;
36 import io.swagger.annotations.ResponseHeader;
37 import io.swagger.annotations.SecurityDefinition;
38 import io.swagger.annotations.SwaggerDefinition;
39 import java.util.UUID;
40 import javax.ws.rs.Consumes;
41 import javax.ws.rs.DELETE;
42 import javax.ws.rs.GET;
43 import javax.ws.rs.HeaderParam;
44 import javax.ws.rs.POST;
45 import javax.ws.rs.Path;
46 import javax.ws.rs.PathParam;
47 import javax.ws.rs.Produces;
48 import javax.ws.rs.QueryParam;
49 import javax.ws.rs.core.MediaType;
50 import javax.ws.rs.core.Response;
51 import javax.ws.rs.core.Response.ResponseBuilder;
52 import org.onap.policy.api.main.rest.provider.HealthCheckProvider;
53 import org.onap.policy.api.main.rest.provider.PolicyProvider;
54 import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;
55 import org.onap.policy.api.main.rest.provider.StatisticsProvider;
56 import org.onap.policy.common.endpoints.report.HealthCheckReport;
57 import org.onap.policy.models.tosca.ToscaPolicy;
58 import org.onap.policy.models.tosca.ToscaPolicyList;
59 import org.onap.policy.models.tosca.ToscaPolicyType;
60 import org.onap.policy.models.tosca.ToscaPolicyTypeList;
61
62 /**
63  * Class to provide REST API services.
64  */
65 @Path("/policy/api/v1")
66 @Api(value = "Policy Design API")
67 @Produces(MediaType.APPLICATION_JSON)
68 @Consumes(MediaType.APPLICATION_JSON)
69 @SwaggerDefinition(info = @Info(
70         description = "Policy Design API is publicly exposed for clients to Create/Read/Update/Delete"
71                     + " policy types, policy type implementation and policies which can be recognized"
72                     + " and executable by incorporated policy engines. It is an"
73                     + " independent component running rest service that takes all policy design API calls"
74                     + " from clients and then assign them to different API working functions. Besides"
75                     + " that, API is also exposed for clients to retrieve healthcheck status of this API"
76                     + " rest service and the statistics report including the counters of API invocation.",
77         version = "1.0.0",
78         title = "Policy Design",
79         extensions = {
80                 @Extension(properties = {
81                         @ExtensionProperty(name = "planned-retirement-date", value = "tbd"),
82                         @ExtensionProperty(name = "component", value = "Policy Framework")
83                 })
84         }),
85         schemes = { SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS },
86         securityDefinition = @SecurityDefinition(basicAuthDefinitions = { @BasicAuthDefinition(key = "basicAuth") }))
87 public class ApiRestController {
88
89     /**
90      * Retrieves the healthcheck status of the API component.
91      *
92      * @return the Response object containing the results of the API operation
93      */
94     @GET
95     @Path("/healthcheck")
96     @ApiOperation(value = "Perform a system healthcheck",
97             notes = "Returns healthy status of the Policy API component",
98             response = HealthCheckReport.class,
99             responseHeaders = {
100                     @ResponseHeader(name = "X-MinorVersion",
101                                     description = "Used to request or communicate a MINOR version back from the client"
102                                                 + " to the server, and from the server back to the client",
103                                     response = String.class),
104                     @ResponseHeader(name = "X-PatchVersion",
105                                     description = "Used only to communicate a PATCH version in a response for"
106                                                 + " troubleshooting purposes only, and will not be provided by"
107                                                 + " the client on request",
108                                     response = String.class),
109                     @ResponseHeader(name = "X-LatestVersion",
110                                     description = "Used only to communicate an API's latest version",
111                                     response = String.class),
112                     @ResponseHeader(name = "X-ONAP-RequestID",
113                                     description = "Used to track REST transactions for logging purpose",
114                                     response = UUID.class)
115             },
116             authorizations = @Authorization(value = "basicAuth"),
117             tags = { "HealthCheck", },
118             extensions = {
119                     @Extension(name = "interface info", properties = {
120                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
121                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
122                     })
123             })
124     @ApiResponses(value = {
125             @ApiResponse(code = 401, message = "Authentication Error"),
126             @ApiResponse(code = 403, message = "Authorization Error"),
127             @ApiResponse(code = 500, message = "Internal Server Error")
128         })
129     public Response getHealthCheck(
130             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
131         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
132             .entity(new HealthCheckProvider().performHealthCheck()).build();
133     }
134
135     /**
136      * Retrieves the statistics report of the API component.
137      *
138      * @return the Response object containing the results of the API operation
139      */
140     @GET
141     @Path("/statistics")
142     @ApiOperation(value = "Retrieve current statistics",
143             notes = "Returns current statistics including the counters of API invocation",
144             response = StatisticsReport.class,
145             responseHeaders = {
146                     @ResponseHeader(name = "X-MinorVersion",
147                                     description = "Used to request or communicate a MINOR version back from the client"
148                                                 + " to the server, and from the server back to the client",
149                                     response = String.class),
150                     @ResponseHeader(name = "X-PatchVersion",
151                                     description = "Used only to communicate a PATCH version in a response for"
152                                                 + " troubleshooting purposes only, and will not be provided by"
153                                                 + " the client on request",
154                                     response = String.class),
155                     @ResponseHeader(name = "X-LatestVersion",
156                                     description = "Used only to communicate an API's latest version",
157                                     response = String.class),
158                     @ResponseHeader(name = "X-ONAP-RequestID",
159                                     description = "Used to track REST transactions for logging purpose",
160                                     response = UUID.class)
161             },
162             authorizations = @Authorization(value = "basicAuth"),
163             tags = { "Statistics", },
164             extensions = {
165                     @Extension(name = "interface info", properties = {
166                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
167                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
168                     })
169             })
170     @ApiResponses(value = {
171             @ApiResponse(code = 401, message = "Authentication Error"),
172             @ApiResponse(code = 403, message = "Authorization Error"),
173             @ApiResponse(code = 500, message = "Internal Server Error")
174         })
175     public Response getStatistics(
176             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
177         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
178             .entity(new StatisticsProvider().fetchCurrentStatistics()).build();
179     }
180
181     /**
182      * Retrieves all available policy types.
183      *
184      * @return the Response object containing the results of the API operation
185      */
186     @GET
187     @Path("/policytypes")
188     @ApiOperation(value = "Retrieve existing policy types",
189             notes = "Returns a list of existing policy types stored in Policy Framework",
190             response = ToscaPolicyTypeList.class,
191             responseHeaders = {
192                     @ResponseHeader(name = "X-MinorVersion",
193                                     description = "Used to request or communicate a MINOR version back from the client"
194                                                 + " to the server, and from the server back to the client",
195                                     response = String.class),
196                     @ResponseHeader(name = "X-PatchVersion",
197                                     description = "Used only to communicate a PATCH version in a response for"
198                                                 + " troubleshooting purposes only, and will not be provided by"
199                                                 + " the client on request",
200                                     response = String.class),
201                     @ResponseHeader(name = "X-LatestVersion",
202                                     description = "Used only to communicate an API's latest version",
203                                     response = String.class),
204                     @ResponseHeader(name = "X-ONAP-RequestID",
205                                     description = "Used to track REST transactions for logging purpose",
206                                     response = UUID.class)
207             },
208             authorizations = @Authorization(value = "basicAuth"),
209             tags = { "PolicyType", },
210             extensions = {
211                     @Extension(name = "interface info", properties = {
212                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
213                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
214                     })
215             })
216     @ApiResponses(value = {
217             @ApiResponse(code = 401, message = "Authentication Error"),
218             @ApiResponse(code = 403, message = "Authorization Error"),
219             @ApiResponse(code = 500, message = "Internal Server Error")
220         })
221     public Response getAllPolicyTypes(
222             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
223         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
224             .entity(new PolicyTypeProvider().fetchPolicyTypes(null, null)).build();
225     }
226
227     /**
228      * Retrieves all versions of a particular policy type.
229      *
230      * @param policyTypeId the ID of specified policy type
231      *
232      * @return the Response object containing the results of the API operation
233      */
234     @GET
235     @Path("/policytypes/{policyTypeId}")
236     @ApiOperation(value = "Retrieve all available versions of a policy type",
237             notes = "Returns a list of all available versions for the specified policy type",
238             response = ToscaPolicyTypeList.class,
239             responseHeaders = {
240                     @ResponseHeader(name = "X-MinorVersion",
241                                     description = "Used to request or communicate a MINOR version back from the client"
242                                                 + " to the server, and from the server back to the client",
243                                     response = String.class),
244                     @ResponseHeader(name = "X-PatchVersion",
245                                     description = "Used only to communicate a PATCH version in a response for"
246                                                 + " troubleshooting purposes only, and will not be provided by"
247                                                 + " the client on request",
248                                     response = String.class),
249                     @ResponseHeader(name = "X-LatestVersion",
250                                     description = "Used only to communicate an API's latest version",
251                                     response = String.class),
252                     @ResponseHeader(name = "X-ONAP-RequestID",
253                                     description = "Used to track REST transactions for logging purpose",
254                                     response = UUID.class)
255             },
256             authorizations = @Authorization(value = "basicAuth"),
257             tags = { "PolicyType", },
258             extensions = {
259                     @Extension(name = "interface info", properties = {
260                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
261                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
262                     })
263             })
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 = 500, message = "Internal Server Error")
269         })
270     public Response getAllVersionsOfPolicyType(
271             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
272             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
273         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
274             .entity(new PolicyTypeProvider().fetchPolicyTypes(policyTypeId, null)).build();
275     }
276
277     /**
278      * Retrieves specified version of a particular policy type.
279      *
280      * @param policyTypeId the ID of specified policy type
281      * @param versionId the version of specified policy type
282      *
283      * @return the Response object containing the results of the API operation
284      */
285     @GET
286     @Path("/policytypes/{policyTypeId}/versions/{versionId}")
287     @ApiOperation(value = "Retrieve one particular version of a policy type",
288             notes = "Returns a particular version for the specified policy type",
289             response = ToscaPolicyTypeList.class,
290             responseHeaders = {
291                     @ResponseHeader(name = "X-MinorVersion",
292                                     description = "Used to request or communicate a MINOR version back from the client"
293                                                 + " to the server, and from the server back to the client",
294                                     response = String.class),
295                     @ResponseHeader(name = "X-PatchVersion",
296                                     description = "Used only to communicate a PATCH version in a response for"
297                                                 + " troubleshooting purposes only, and will not be provided by"
298                                                 + " the client on request",
299                                     response = String.class),
300                     @ResponseHeader(name = "X-LatestVersion",
301                                     description = "Used only to communicate an API's latest version",
302                                     response = String.class),
303                     @ResponseHeader(name = "X-ONAP-RequestID",
304                                     description = "Used to track REST transactions for logging purpose",
305                                     response = UUID.class)
306             },
307             authorizations = @Authorization(value = "basicAuth"),
308             tags = { "PolicyType", },
309             extensions = {
310                     @Extension(name = "interface info", properties = {
311                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
312                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
313                     })
314             })
315     @ApiResponses(value = {
316             @ApiResponse(code = 401, message = "Authentication Error"),
317             @ApiResponse(code = 403, message = "Authorization Error"),
318             @ApiResponse(code = 404, message = "Resource Not Found"),
319             @ApiResponse(code = 500, message = "Internal Server Error")
320         })
321     public Response getSpecificVersionOfPolicyType(
322             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
323             @PathParam("versionId") @ApiParam(value = "Version of policy type", required = true) String versionId,
324             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
325         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
326             .entity(new PolicyTypeProvider().fetchPolicyTypes(policyTypeId, versionId)).build();
327     }
328
329     /**
330      * Creates a new policy type.
331      *
332      * @param body the body of policy type following TOSCA definition
333      *
334      * @return the Response object containing the results of the API operation
335      */
336     @POST
337     @Path("/policytypes")
338     @ApiOperation(value = "Create a new policy type",
339             notes = "Client should provide TOSCA body of the new policy type",
340             authorizations = @Authorization(value = "basicAuth"),
341             tags = { "PolicyType", },
342             extensions = {
343                     @Extension(name = "interface info", properties = {
344                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
345                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
346                     })
347             })
348     @ApiResponses(value = {
349             @ApiResponse(code = 201, message = "Resource successfully created",
350             responseHeaders = {
351                     @ResponseHeader(name = "X-MinorVersion",
352                                     description = "Used to request or communicate a MINOR version back from the client"
353                                                 + " to the server, and from the server back to the client",
354                                     response = String.class),
355                     @ResponseHeader(name = "X-PatchVersion",
356                                     description = "Used only to communicate a PATCH version in a response for"
357                                                 + " troubleshooting purposes only, and will not be provided by"
358                                                 + " the client on request",
359                                     response = String.class),
360                     @ResponseHeader(name = "X-LatestVersion",
361                                     description = "Used only to communicate an API's latest version",
362                                     response = String.class),
363                     @ResponseHeader(name = "X-ONAP-RequestID",
364                                     description = "Used to track REST transactions for logging purpose",
365                                     response = UUID.class)
366             }),
367             @ApiResponse(code = 400, message = "Invalid Body"),
368             @ApiResponse(code = 401, message = "Authentication Error"),
369             @ApiResponse(code = 403, message = "Authorization Error"),
370             @ApiResponse(code = 500, message = "Internal Server Error")
371         })
372     public Response createPolicyType(
373             @ApiParam(value = "Entity body of policy type", required = true) ToscaPolicyType body,
374             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
375         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
376             .entity(new PolicyTypeProvider().createPolicyType(body)).build();
377     }
378
379     /**
380      * Deletes all versions of a particular policy type.
381      *
382      * @param policyTypeId the ID of specified policy type
383      *
384      * @return the Response object containing the results of the API operation
385      */
386     @DELETE
387     @Path("/policytypes/{policyTypeId}")
388     @ApiOperation(value = "Delete all versions of a policy type",
389             notes = "Rule 1: pre-defined policy types cannot be deleted;"
390                   + "Rule 2: policy types that are in use (parameterized by a TOSCA policy) cannot be deleted."
391                   + "The parameterizing TOSCA policies must be deleted first;",
392             authorizations = @Authorization(value = "basicAuth"),
393             tags = { "PolicyType", },
394             extensions = {
395                     @Extension(name = "interface info", properties = {
396                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
397                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
398                     })
399             })
400     @ApiResponses(value = {
401             @ApiResponse(code = 204, message = "Resources successfully deleted, no content returned",
402             responseHeaders = {
403                     @ResponseHeader(name = "X-MinorVersion",
404                                     description = "Used to request or communicate a MINOR version back from the client"
405                                                 + " to the server, and from the server back to the client",
406                                     response = String.class),
407                     @ResponseHeader(name = "X-PatchVersion",
408                                     description = "Used only to communicate a PATCH version in a response for"
409                                                 + " troubleshooting purposes only, and will not be provided by"
410                                                 + " the client on request",
411                                     response = String.class),
412                     @ResponseHeader(name = "X-LatestVersion",
413                                     description = "Used only to communicate an API's latest version",
414                                     response = String.class),
415                     @ResponseHeader(name = "X-ONAP-RequestID",
416                                     description = "Used to track REST transactions for logging purpose",
417                                     response = UUID.class)
418             }),
419             @ApiResponse(code = 401, message = "Authentication Error"),
420             @ApiResponse(code = 403, message = "Authorization Error"),
421             @ApiResponse(code = 404, message = "Resource Not Found"),
422             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
423             @ApiResponse(code = 500, message = "Internal Server Error")
424         })
425     public Response deleteAllVersionsOfPolicyType(
426             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
427             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
428         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
429             .entity(new PolicyTypeProvider().deletePolicyTypes(policyTypeId, null)).build();
430     }
431
432     /**
433      * Deletes specified version of a particular policy type.
434      *
435      * @param policyTypeId the ID of specified policy type
436      * @param versionId the version of specified policy type
437      *
438      * @return the Response object containing the results of the API operation
439      */
440     @DELETE
441     @Path("/policytypes/{policyTypeId}/versions/{versionId}")
442     @ApiOperation(value = "Delete one version of a policy type",
443             notes = "Rule 1: pre-defined policy types cannot be deleted;"
444                   + "Rule 2: policy types that are in use (parameterized by a TOSCA policy) cannot be deleted."
445                   + "The parameterizing TOSCA policies must be deleted first;",
446             authorizations = @Authorization(value = "basicAuth"),
447             tags = { "PolicyType", },
448             extensions = {
449                     @Extension(name = "interface info", properties = {
450                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
451                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
452                     })
453             })
454     @ApiResponses(value = {
455             @ApiResponse(code = 204, message = "Resource successfully deleted, no content returned",
456             responseHeaders = {
457                     @ResponseHeader(name = "X-MinorVersion",
458                                     description = "Used to request or communicate a MINOR version back from the client"
459                                                 + " to the server, and from the server back to the client",
460                                     response = String.class),
461                     @ResponseHeader(name = "X-PatchVersion",
462                                     description = "Used only to communicate a PATCH version in a response for"
463                                                 + " troubleshooting purposes only, and will not be provided by"
464                                                 + " the client on request",
465                                     response = String.class),
466                     @ResponseHeader(name = "X-LatestVersion",
467                                     description = "Used only to communicate an API's latest version",
468                                     response = String.class),
469                     @ResponseHeader(name = "X-ONAP-RequestID",
470                                     description = "Used to track REST transactions for logging purpose",
471                                     response = UUID.class)
472             }),
473             @ApiResponse(code = 401, message = "Authentication Error"),
474             @ApiResponse(code = 403, message = "Authorization Error"),
475             @ApiResponse(code = 404, message = "Resource Not Found"),
476             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
477             @ApiResponse(code = 500, message = "Internal Server Error")
478         })
479     public Response deleteSpecificVersionOfPolicyType(
480             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
481             @PathParam("versionId") @ApiParam(value = "Version of policy type", required = true) String versionId,
482             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
483         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
484             .entity(new PolicyTypeProvider().deletePolicyTypes(policyTypeId, versionId)).build();
485     }
486
487     /**
488      * Retrieves all versions of a particular policy.
489      *
490      * @param policyTypeId the ID of specified policy type
491      * @param policyTypeVersion the version of specified policy type
492      *
493      * @return the Response object containing the results of the API operation
494      */
495     @GET
496     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")
497     @ApiOperation(value = "Retrieve all versions of a policy created for a particular policy type version",
498             notes = "Returns a list of all versions of specified policy created for the specified policy type version",
499             response = ToscaPolicyList.class,
500             responseHeaders = {
501                     @ResponseHeader(name = "X-MinorVersion",
502                                     description = "Used to request or communicate a MINOR version back from the client"
503                                                 + " to the server, and from the server back to the client",
504                                     response = String.class),
505                     @ResponseHeader(name = "X-PatchVersion",
506                                     description = "Used only to communicate a PATCH version in a response for"
507                                                 + " troubleshooting purposes only, and will not be provided by"
508                                                 + " the client on request",
509                                     response = String.class),
510                     @ResponseHeader(name = "X-LatestVersion",
511                                     description = "Used only to communicate an API's latest version",
512                                     response = String.class),
513                     @ResponseHeader(name = "X-ONAP-RequestID",
514                                     description = "Used to track REST transactions for logging purpose",
515                                     response = UUID.class)
516             },
517             authorizations = @Authorization(value = "basicAuth"),
518             tags = { "Policy", },
519             extensions = {
520                     @Extension(name = "interface info", properties = {
521                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
522                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
523                     })
524             })
525     @ApiResponses(value = {
526             @ApiResponse(code = 401, message = "Authentication Error"),
527             @ApiResponse(code = 403, message = "Authorization Error"),
528             @ApiResponse(code = 404, message = "Resource Not Found"),
529             @ApiResponse(code = 500, message = "Internal Server Error")
530         })
531     public Response getAllPolicies(
532             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
533             @PathParam("policyTypeVersion")
534                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,
535             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
536         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
537             .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion, null, null)).build();
538     }
539
540     /**
541      * Retrieves all versions of a particular policy.
542      *
543      * @param policyTypeId the ID of specified policy type
544      * @param policyTypeVersion the version of specified policy type
545      * @param policyId the ID of specified policy
546      *
547      * @return the Response object containing the results of the API operation
548      */
549     @GET
550     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}")
551     @ApiOperation(value = "Retrieve all version details of a policy created for a particular policy type version",
552             notes = "Returns a list of all version details of the specified policy",
553             response = ToscaPolicyList.class,
554             responseHeaders = {
555                     @ResponseHeader(name = "X-MinorVersion",
556                                     description = "Used to request or communicate a MINOR version back from the client"
557                                                 + " to the server, and from the server back to the client",
558                                     response = String.class),
559                     @ResponseHeader(name = "X-PatchVersion",
560                                     description = "Used only to communicate a PATCH version in a response for"
561                                                 + " troubleshooting purposes only, and will not be provided by"
562                                                 + " the client on request",
563                                     response = String.class),
564                     @ResponseHeader(name = "X-LatestVersion",
565                                     description = "Used only to communicate an API's latest version",
566                                     response = String.class),
567                     @ResponseHeader(name = "X-ONAP-RequestID",
568                                     description = "Used to track REST transactions for logging purpose",
569                                     response = UUID.class)
570             },
571             authorizations = @Authorization(value = "basicAuth"),
572             tags = { "Policy", },
573             extensions = {
574                     @Extension(name = "interface info", properties = {
575                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
576                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
577                     })
578             })
579     @ApiResponses(value = {
580             @ApiResponse(code = 401, message = "Authentication Error"),
581             @ApiResponse(code = 403, message = "Authorization Error"),
582             @ApiResponse(code = 404, message = "Resource Not Found"),
583             @ApiResponse(code = 500, message = "Internal Server Error")
584         })
585     public Response getAllVersionsOfPolicy(
586             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
587             @PathParam("policyTypeVersion")
588                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,
589             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
590             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
591         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
592             .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion, policyId, null)).build();
593     }
594
595     /**
596      * Retrieves the specified version of a particular policy.
597      *
598      * @param policyTypeId the ID of specified policy type
599      * @param policyTypeVersion the version of specified policy type
600      * @param policyId the ID of specified policy
601      * @param policyVersion the version of specified policy
602      *
603      * @return the Response object containing the results of the API operation
604      */
605     @GET
606     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")
607     @ApiOperation(value = "Retrieve one version of a policy created for a particular policy type version",
608             notes = "Returns a particular version of specified policy created for the specified policy type version",
609             response = ToscaPolicyList.class,
610             responseHeaders = {
611                     @ResponseHeader(name = "X-MinorVersion",
612                                     description = "Used to request or communicate a MINOR version back from the client"
613                                                 + " to the server, and from the server back to the client",
614                                     response = String.class),
615                     @ResponseHeader(name = "X-PatchVersion",
616                                     description = "Used only to communicate a PATCH version in a response for"
617                                                 + " troubleshooting purposes only, and will not be provided by"
618                                                 + " the client on request",
619                                     response = String.class),
620                     @ResponseHeader(name = "X-LatestVersion",
621                                     description = "Used only to communicate an API's latest version",
622                                     response = String.class),
623                     @ResponseHeader(name = "X-ONAP-RequestID",
624                                     description = "Used to track REST transactions for logging purpose",
625                                     response = UUID.class)
626             },
627             authorizations = @Authorization(value = "basicAuth"),
628             tags = { "Policy", },
629             extensions = {
630                     @Extension(name = "interface info", properties = {
631                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
632                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
633                     })
634             })
635     @ApiResponses(value = {
636             @ApiResponse(code = 401, message = "Authentication Error"),
637             @ApiResponse(code = 403, message = "Authorization Error"),
638             @ApiResponse(code = 404, message = "Resource Not Found"),
639             @ApiResponse(code = 500, message = "Internal Server Error")
640         })
641     public Response getSpecificVersionOfPolicy(
642             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
643             @PathParam("policyTypeVersion")
644                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,
645             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
646             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
647             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
648         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
649             .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion,
650                                                        policyId, policyVersion)).build();
651     }
652
653     /**
654      * Retrieves either latest or deployed version of a particular policy depending on query parameter.
655      *
656      * @param policyTypeId the ID of specified policy type
657      * @param policyTypeVersion the version of specified policy type
658      * @param policyId the ID of specified policy
659      *
660      * @return the Response object containing the results of the API operation
661      */
662     @GET
663     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions")
664     @ApiOperation(value = "Retrieve either latest or deployed version of a particular policy depending on query param",
665             notes = "Returns either latest or deployed version of specified policy depending on query param",
666             response = ToscaPolicyList.class,
667             responseHeaders = {
668                     @ResponseHeader(name = "X-MinorVersion",
669                                     description = "Used to request or communicate a MINOR version back from the client"
670                                                 + " to the server, and from the server back to the client",
671                                     response = String.class),
672                     @ResponseHeader(name = "X-PatchVersion",
673                                     description = "Used only to communicate a PATCH version in a response for"
674                                                 + " troubleshooting purposes only, and will not be provided by"
675                                                 + " the client on request",
676                                     response = String.class),
677                     @ResponseHeader(name = "X-LatestVersion",
678                                     description = "Used only to communicate an API's latest version",
679                                     response = String.class),
680                     @ResponseHeader(name = "X-ONAP-RequestID",
681                                     description = "Used to track REST transactions for logging purpose",
682                                     response = UUID.class)
683             },
684             authorizations = @Authorization(value = "basicAuth"),
685             tags = { "Policy", },
686             extensions = {
687                     @Extension(name = "interface info", properties = {
688                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
689                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
690                     })
691             })
692     @ApiResponses(value = {
693             @ApiResponse(code = 401, message = "Authentication Error"),
694             @ApiResponse(code = 403, message = "Authorization Error"),
695             @ApiResponse(code = 404, message = "Resource Not Found"),
696             @ApiResponse(code = 500, message = "Internal Server Error")
697         })
698     public Response getEitherLatestOrDeployedVersionOfPolicy(
699             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
700             @PathParam("policyTypeVersion")
701                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,
702             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
703             @QueryParam("type")
704                 @ApiParam(value = "Version that can only be 'latest' or 'deployed'", required = true) String type,
705             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
706         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
707             .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion, policyId, type)).build();
708     }
709
710     /**
711      * Creates a new policy for a particular policy type and version.
712      *
713      * @param policyTypeId the ID of specified policy type
714      * @param policyTypeVersion the version of specified policy type
715      * @param body the body of policy following TOSCA definition
716      *
717      * @return the Response object containing the results of the API operation
718      */
719     @POST
720     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")
721     @ApiOperation(value = "Create a new policy for a policy type version",
722             notes = "Client should provide TOSCA body of the new policy",
723             authorizations = @Authorization(value = "basicAuth"),
724             tags = { "Policy", },
725             extensions = {
726                     @Extension(name = "interface info", properties = {
727                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
728                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
729                     })
730             })
731     @ApiResponses(value = {
732             @ApiResponse(code = 201, message = "Resource successfully created",
733             responseHeaders = {
734                     @ResponseHeader(name = "X-MinorVersion",
735                                     description = "Used to request or communicate a MINOR version back from the client"
736                                                 + " to the server, and from the server back to the client",
737                                     response = String.class),
738                     @ResponseHeader(name = "X-PatchVersion",
739                                     description = "Used only to communicate a PATCH version in a response for"
740                                                 + " troubleshooting purposes only, and will not be provided by"
741                                                 + " the client on request",
742                                     response = String.class),
743                     @ResponseHeader(name = "X-LatestVersion",
744                                     description = "Used only to communicate an API's latest version",
745                                     response = String.class),
746                     @ResponseHeader(name = "X-ONAP-RequestID",
747                                     description = "Used to track REST transactions for logging purpose",
748                                     response = UUID.class)
749             }),
750             @ApiResponse(code = 400, message = "Invalid Body"),
751             @ApiResponse(code = 401, message = "Authentication Error"),
752             @ApiResponse(code = 403, message = "Authorization Error"),
753             @ApiResponse(code = 404, message = "Resource Not Found"),
754             @ApiResponse(code = 500, message = "Internal Server Error")
755         })
756     public Response createPolicy(
757             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
758             @PathParam("policyTypeVersion")
759                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,
760             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
761             @ApiParam(value = "Entity body of policy", required = true) ToscaPolicy body) {
762         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
763             .entity(new PolicyProvider().createPolicy(policyTypeId, policyTypeVersion, body)).build();
764     }
765
766     /**
767      * Deletes all versions of a particular policy.
768      *
769      * @param policyTypeId the ID of specified policy type
770      * @param policyTypeVersion the version of specified policy type
771      * @param policyId the ID of specified policy
772      *
773      * @return the Response object containing the results of the API operation
774      */
775     @DELETE
776     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}")
777     @ApiOperation(value = "Delete all versions of a policy",
778             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
779             authorizations = @Authorization(value = "basicAuth"),
780             tags = { "Policy", },
781             extensions = {
782                     @Extension(name = "interface info", properties = {
783                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
784                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
785                     })
786             })
787     @ApiResponses(value = {
788             @ApiResponse(code = 204, message = "Resources successfully deleted, no content returned",
789             responseHeaders = {
790                     @ResponseHeader(name = "X-MinorVersion",
791                                     description = "Used to request or communicate a MINOR version back from the client"
792                                                 + " to the server, and from the server back to the client",
793                                     response = String.class),
794                     @ResponseHeader(name = "X-PatchVersion",
795                                     description = "Used only to communicate a PATCH version in a response for"
796                                                 + " troubleshooting purposes only, and will not be provided by"
797                                                 + " the client on request",
798                                     response = String.class),
799                     @ResponseHeader(name = "X-LatestVersion",
800                                     description = "Used only to communicate an API's latest version",
801                                     response = String.class),
802                     @ResponseHeader(name = "X-ONAP-RequestID",
803                                     description = "Used to track REST transactions for logging purpose",
804                                     response = UUID.class)
805             }),
806             @ApiResponse(code = 401, message = "Authentication Error"),
807             @ApiResponse(code = 403, message = "Authorization Error"),
808             @ApiResponse(code = 404, message = "Resource Not Found"),
809             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
810             @ApiResponse(code = 500, message = "Internal Server Error")
811         })
812     public Response deleteAllVersionsOfPolicy(
813             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,
814             @PathParam("policyTypeVersion")
815                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,
816             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
817             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
818         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
819             .entity(new PolicyProvider().deletePolicies(policyTypeId, policyTypeVersion, policyId, null)).build();
820     }
821
822     /**
823      * Deletes the specified version of a particular policy.
824      *
825      * @param policyTypeId the ID of specified policy type
826      * @param policyTypeVersion the version of specified policy type
827      * @param policyId the ID of specified policy
828      * @param policyVersion the version of specified policy
829      *
830      * @return the Response object containing the results of the API operation
831      */
832     @DELETE
833     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")
834     @ApiOperation(value = "Delete a particular version of a policy",
835             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
836             authorizations = @Authorization(value = "basicAuth"),
837             tags = { "Policy", },
838             extensions = {
839                     @Extension(name = "interface info", properties = {
840                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
841                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
842                     })
843             })
844     @ApiResponses(value = {
845             @ApiResponse(code = 204, message = "Resource successfully deleted, no content returned",
846             responseHeaders = {
847                     @ResponseHeader(name = "X-MinorVersion",
848                                     description = "Used to request or communicate a MINOR version back from the client"
849                                                 + " to the server, and from the server back to the client",
850                                     response = String.class),
851                     @ResponseHeader(name = "X-PatchVersion",
852                                     description = "Used only to communicate a PATCH version in a response for"
853                                                 + " troubleshooting purposes only, and will not be provided by"
854                                                 + " the client on request",
855                                     response = String.class),
856                     @ResponseHeader(name = "X-LatestVersion",
857                                     description = "Used only to communicate an API's latest version",
858                                     response = String.class),
859                     @ResponseHeader(name = "X-ONAP-RequestID",
860                                     description = "Used to track REST transactions for logging purpose",
861                                     response = UUID.class)
862             }),
863             @ApiResponse(code = 401, message = "Authentication Error"),
864             @ApiResponse(code = 403, message = "Authorization Error"),
865             @ApiResponse(code = 404, message = "Resource Not Found"),
866             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
867             @ApiResponse(code = 500, message = "Internal Server Error")
868         })
869     public Response deleteSpecificVersionOfPolicy(
870             @PathParam("policyTypeId") @ApiParam(value = "PolicyType ID", required = true) String policyTypeId,
871             @PathParam("policyTypeVersion")
872                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,
873             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
874             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
875             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
876         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
877             .entity(new PolicyProvider().deletePolicies(policyTypeId, policyTypeVersion,
878                                                         policyId, policyVersion)).build();
879     }
880
881     private ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) {
882         return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0");
883     }
884
885     private ResponseBuilder addLoggingHeaders(ResponseBuilder rb, UUID requestId) {
886         if (requestId == null) {
887             // Generate a random uuid if client does not embed requestId in rest request
888             return rb.header("X-ONAP-RequestID", UUID.randomUUID());
889         }
890         return rb.header("X-ONAP-RequestID", requestId);
891     }
892 }