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