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