Implement policy provider functions
[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 {\r
232             ToscaServiceTemplate serviceTemplate = new 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 {\r
292             ToscaServiceTemplate serviceTemplate = new PolicyTypeProvider()\r
293                     .fetchPolicyTypes(policyTypeId, null);\r
294             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET);\r
295             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
296                     .entity(serviceTemplate).build();\r
297         } catch (PfModelException | PfModelRuntimeException pfme) {\r
298             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET);\r
299             return addLoggingHeaders(addVersionControlHeaders(\r
300                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
301                     .entity(pfme.getErrorResponse()).build();\r
302         }\r
303     }\r
304 \r
305     /**\r
306      * Retrieves specified version of a particular policy type.\r
307      *\r
308      * @param policyTypeId the ID of specified policy type\r
309      * @param versionId the version of specified policy type\r
310      *\r
311      * @return the Response object containing the results of the API operation\r
312      */\r
313     @GET\r
314     @Path("/policytypes/{policyTypeId}/versions/{versionId}")\r
315     @ApiOperation(value = "Retrieve one particular version of a policy type",\r
316             notes = "Returns a particular version for the specified policy type",\r
317             response = ToscaServiceTemplate.class,\r
318             responseHeaders = {\r
319                     @ResponseHeader(name = "X-MinorVersion",\r
320                                     description = "Used to request or communicate a MINOR version back from the client"\r
321                                                 + " to the server, and from the server back to the client",\r
322                                     response = String.class),\r
323                     @ResponseHeader(name = "X-PatchVersion",\r
324                                     description = "Used only to communicate a PATCH version in a response for"\r
325                                                 + " troubleshooting purposes only, and will not be provided by"\r
326                                                 + " the client on request",\r
327                                     response = String.class),\r
328                     @ResponseHeader(name = "X-LatestVersion",\r
329                                     description = "Used only to communicate an API's latest version",\r
330                                     response = String.class),\r
331                     @ResponseHeader(name = "X-ONAP-RequestID",\r
332                                     description = "Used to track REST transactions for logging purpose",\r
333                                     response = UUID.class)\r
334             },\r
335             authorizations = @Authorization(value = "basicAuth"),\r
336             tags = { "PolicyType", },\r
337             extensions = {\r
338                     @Extension(name = "interface info", properties = {\r
339                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
340                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
341                     })\r
342             })\r
343     @ApiResponses(value = {\r
344             @ApiResponse(code = 401, message = "Authentication Error"),\r
345             @ApiResponse(code = 403, message = "Authorization Error"),\r
346             @ApiResponse(code = 404, message = "Resource Not Found"),\r
347             @ApiResponse(code = 500, message = "Internal Server Error")\r
348         })\r
349     public Response getSpecificVersionOfPolicyType(\r
350             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
351             @PathParam("versionId") @ApiParam(value = "Version of policy type", required = true) String versionId,\r
352             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
353 \r
354         try {\r
355             ToscaServiceTemplate serviceTemplate = new PolicyTypeProvider()\r
356                     .fetchPolicyTypes(policyTypeId, versionId);\r
357             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.GET);\r
358             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
359                     .entity(serviceTemplate).build();\r
360         } catch (PfModelException | PfModelRuntimeException pfme) {\r
361             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.GET);\r
362             return addLoggingHeaders(addVersionControlHeaders(\r
363                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
364                     .entity(pfme.getErrorResponse()).build();\r
365         }\r
366     }\r
367 \r
368     /**\r
369      * Creates a new policy type.\r
370      *\r
371      * @param body the body of policy type following TOSCA definition\r
372      *\r
373      * @return the Response object containing the results of the API operation\r
374      */\r
375     @POST\r
376     @Path("/policytypes")\r
377     @ApiOperation(value = "Create a new policy type",\r
378             notes = "Client should provide TOSCA body of the new policy type",\r
379             authorizations = @Authorization(value = "basicAuth"),\r
380             tags = { "PolicyType", },\r
381             response = ToscaServiceTemplate.class,\r
382             responseHeaders = {\r
383                     @ResponseHeader(name = "X-MinorVersion",\r
384                                     description = "Used to request or communicate a MINOR version back from the client"\r
385                                                 + " to the server, and from the server back to the client",\r
386                                     response = String.class),\r
387                     @ResponseHeader(name = "X-PatchVersion",\r
388                                     description = "Used only to communicate a PATCH version in a response for"\r
389                                                 + " troubleshooting purposes only, and will not be provided by"\r
390                                                 + " the client on request",\r
391                                     response = String.class),\r
392                     @ResponseHeader(name = "X-LatestVersion",\r
393                                     description = "Used only to communicate an API's latest version",\r
394                                     response = String.class),\r
395                     @ResponseHeader(name = "X-ONAP-RequestID",\r
396                                     description = "Used to track REST transactions for logging purpose",\r
397                                     response = UUID.class)\r
398             },\r
399             extensions = {\r
400                     @Extension(name = "interface info", properties = {\r
401                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
402                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
403                     })\r
404             })\r
405     @ApiResponses(value = {\r
406             @ApiResponse(code = 400, message = "Invalid Body"),\r
407             @ApiResponse(code = 401, message = "Authentication Error"),\r
408             @ApiResponse(code = 403, message = "Authorization Error"),\r
409             @ApiResponse(code = 500, message = "Internal Server Error")\r
410         })\r
411     public Response createPolicyType(\r
412             @ApiParam(value = "Entity body of policy type", required = true) ToscaServiceTemplate body,\r
413             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
414 \r
415         try {\r
416             ToscaServiceTemplate serviceTemplate = new PolicyTypeProvider().createPolicyType(body);\r
417             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.SUCCESS, HttpMethod.POST);\r
418             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
419                     .entity(serviceTemplate).build();\r
420         } catch (PfModelException | PfModelRuntimeException pfme) {\r
421             updateApiStatisticsCounter(Target.POLICY_TYPE, Result.FAILURE, HttpMethod.POST);\r
422             return addLoggingHeaders(addVersionControlHeaders(\r
423                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
424                     .entity(pfme.getErrorResponse()).build();\r
425         }\r
426     }\r
427 \r
428     /**\r
429      * Deletes specified version of a particular policy type.\r
430      *\r
431      * @param policyTypeId the ID of specified policy type\r
432      * @param versionId the version of specified policy type\r
433      *\r
434      * @return the Response object containing the results of the API operation\r
435      */\r
436     @DELETE\r
437     @Path("/policytypes/{policyTypeId}/versions/{versionId}")\r
438     @ApiOperation(value = "Delete one version of a policy type",\r
439             notes = "Rule 1: pre-defined policy types cannot be deleted;"\r
440                   + "Rule 2: policy types that are in use (parameterized by a TOSCA policy) cannot be deleted."\r
441                   + "The parameterizing TOSCA policies must be deleted first;",\r
442             authorizations = @Authorization(value = "basicAuth"),\r
443             tags = { "PolicyType", },\r
444             response = ToscaServiceTemplate.class,\r
445             responseHeaders = {\r
446                     @ResponseHeader(name = "X-MinorVersion",\r
447                                     description = "Used to request or communicate a MINOR version back from the client"\r
448                                                 + " to the server, and from the server back to the client",\r
449                                     response = String.class),\r
450                     @ResponseHeader(name = "X-PatchVersion",\r
451                                     description = "Used only to communicate a PATCH version in a response for"\r
452                                                 + " troubleshooting purposes only, and will not be provided by"\r
453                                                 + " the client on request",\r
454                                     response = String.class),\r
455                     @ResponseHeader(name = "X-LatestVersion",\r
456                                     description = "Used only to communicate an API's latest version",\r
457                                     response = String.class),\r
458                     @ResponseHeader(name = "X-ONAP-RequestID",\r
459                                     description = "Used to track REST transactions for logging purpose",\r
460                                     response = UUID.class)\r
461             },\r
462             extensions = {\r
463                     @Extension(name = "interface info", properties = {\r
464                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
465                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
466                     })\r
467             })\r
468     @ApiResponses(value = {\r
469             @ApiResponse(code = 401, message = "Authentication Error"),\r
470             @ApiResponse(code = 403, message = "Authorization Error"),\r
471             @ApiResponse(code = 404, message = "Resource Not Found"),\r
472             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),\r
473             @ApiResponse(code = 500, message = "Internal Server Error")\r
474         })\r
475     public Response deleteSpecificVersionOfPolicyType(\r
476             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
477             @PathParam("versionId") @ApiParam(value = "Version of policy type", required = true) String versionId,\r
478             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
479 \r
480         try {\r
481             ToscaServiceTemplate serviceTemplate = new PolicyTypeProvider()\r
482                     .deletePolicyType(policyTypeId, versionId);\r
483             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
484                     .entity(serviceTemplate).build();\r
485         } catch (PfModelException | PfModelRuntimeException pfme) {\r
486             return addLoggingHeaders(addVersionControlHeaders(\r
487                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
488                     .entity(pfme.getErrorResponse()).build();\r
489         }\r
490     }\r
491 \r
492     /**\r
493      * Retrieves all versions of a particular policy.\r
494      *\r
495      * @param policyTypeId the ID of specified policy type\r
496      * @param policyTypeVersion the version of specified policy type\r
497      *\r
498      * @return the Response object containing the results of the API operation\r
499      */\r
500     @GET\r
501     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")\r
502     @ApiOperation(value = "Retrieve all versions of a policy created for a particular policy type version",\r
503             notes = "Returns a list of all versions of specified policy created for the specified policy type version",\r
504             response = ToscaServiceTemplate.class,\r
505             responseHeaders = {\r
506                     @ResponseHeader(name = "X-MinorVersion",\r
507                                     description = "Used to request or communicate a MINOR version back from the client"\r
508                                                 + " to the server, and from the server back to the client",\r
509                                     response = String.class),\r
510                     @ResponseHeader(name = "X-PatchVersion",\r
511                                     description = "Used only to communicate a PATCH version in a response for"\r
512                                                 + " troubleshooting purposes only, and will not be provided by"\r
513                                                 + " the client on request",\r
514                                     response = String.class),\r
515                     @ResponseHeader(name = "X-LatestVersion",\r
516                                     description = "Used only to communicate an API's latest version",\r
517                                     response = String.class),\r
518                     @ResponseHeader(name = "X-ONAP-RequestID",\r
519                                     description = "Used to track REST transactions for logging purpose",\r
520                                     response = UUID.class)\r
521             },\r
522             authorizations = @Authorization(value = "basicAuth"),\r
523             tags = { "Policy", },\r
524             extensions = {\r
525                     @Extension(name = "interface info", properties = {\r
526                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
527                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
528                     })\r
529             })\r
530     @ApiResponses(value = {\r
531             @ApiResponse(code = 401, message = "Authentication Error"),\r
532             @ApiResponse(code = 403, message = "Authorization Error"),\r
533             @ApiResponse(code = 404, message = "Resource Not Found"),\r
534             @ApiResponse(code = 500, message = "Internal Server Error")\r
535         })\r
536     public Response getAllPolicies(\r
537             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
538             @PathParam("policyTypeVersion")\r
539                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,\r
540             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
541 \r
542         try {\r
543             ToscaServiceTemplate serviceTemplate = new PolicyProvider()\r
544                     .fetchPolicies(policyTypeId, policyTypeVersion, null, null);\r
545             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET);\r
546             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
547                     .entity(serviceTemplate).build();\r
548         } catch (PfModelException | PfModelRuntimeException pfme) {\r
549             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET);\r
550             return addLoggingHeaders(addVersionControlHeaders(\r
551                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
552                     .entity(pfme.getErrorResponse()).build();\r
553         }\r
554     }\r
555 \r
556     /**\r
557      * Retrieves all versions of a particular policy.\r
558      *\r
559      * @param policyTypeId the ID of specified policy type\r
560      * @param policyTypeVersion the version of specified policy type\r
561      * @param policyId the ID of specified policy\r
562      *\r
563      * @return the Response object containing the results of the API operation\r
564      */\r
565     @GET\r
566     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}")\r
567     @ApiOperation(value = "Retrieve all version details of a policy created for a particular policy type version",\r
568             notes = "Returns a list of all version details of the specified policy",\r
569             response = ToscaServiceTemplate.class,\r
570             responseHeaders = {\r
571                     @ResponseHeader(name = "X-MinorVersion",\r
572                                     description = "Used to request or communicate a MINOR version back from the client"\r
573                                                 + " to the server, and from the server back to the client",\r
574                                     response = String.class),\r
575                     @ResponseHeader(name = "X-PatchVersion",\r
576                                     description = "Used only to communicate a PATCH version in a response for"\r
577                                                 + " troubleshooting purposes only, and will not be provided by"\r
578                                                 + " the client on request",\r
579                                     response = String.class),\r
580                     @ResponseHeader(name = "X-LatestVersion",\r
581                                     description = "Used only to communicate an API's latest version",\r
582                                     response = String.class),\r
583                     @ResponseHeader(name = "X-ONAP-RequestID",\r
584                                     description = "Used to track REST transactions for logging purpose",\r
585                                     response = UUID.class)\r
586             },\r
587             authorizations = @Authorization(value = "basicAuth"),\r
588             tags = { "Policy", },\r
589             extensions = {\r
590                     @Extension(name = "interface info", properties = {\r
591                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
592                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
593                     })\r
594             })\r
595     @ApiResponses(value = {\r
596             @ApiResponse(code = 401, message = "Authentication Error"),\r
597             @ApiResponse(code = 403, message = "Authorization Error"),\r
598             @ApiResponse(code = 404, message = "Resource Not Found"),\r
599             @ApiResponse(code = 500, message = "Internal Server Error")\r
600         })\r
601     public Response getAllVersionsOfPolicy(\r
602             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
603             @PathParam("policyTypeVersion")\r
604                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,\r
605             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,\r
606             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
607 \r
608         try {\r
609             ToscaServiceTemplate serviceTemplate = new PolicyProvider()\r
610                     .fetchPolicies(policyTypeId, policyTypeVersion, policyId, null);\r
611             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET);\r
612             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
613                     .entity(serviceTemplate).build();\r
614         } catch (PfModelException | PfModelRuntimeException pfme) {\r
615             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET);\r
616             return addLoggingHeaders(addVersionControlHeaders(\r
617                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
618                     .entity(pfme.getErrorResponse()).build();\r
619         }\r
620     }\r
621 \r
622     /**\r
623      * Retrieves the specified version of a particular policy.\r
624      *\r
625      * @param policyTypeId the ID of specified policy type\r
626      * @param policyTypeVersion the version of specified policy type\r
627      * @param policyId the ID of specified policy\r
628      * @param policyVersion the version of specified policy\r
629      *\r
630      * @return the Response object containing the results of the API operation\r
631      */\r
632     @GET\r
633     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")\r
634     @ApiOperation(value = "Retrieve one version of a policy created for a particular policy type version",\r
635             notes = "Returns a particular version of specified policy created for the specified policy type version",\r
636             response = ToscaServiceTemplate.class,\r
637             responseHeaders = {\r
638                     @ResponseHeader(name = "X-MinorVersion",\r
639                                     description = "Used to request or communicate a MINOR version back from the client"\r
640                                                 + " to the server, and from the server back to the client",\r
641                                     response = String.class),\r
642                     @ResponseHeader(name = "X-PatchVersion",\r
643                                     description = "Used only to communicate a PATCH version in a response for"\r
644                                                 + " troubleshooting purposes only, and will not be provided by"\r
645                                                 + " the client on request",\r
646                                     response = String.class),\r
647                     @ResponseHeader(name = "X-LatestVersion",\r
648                                     description = "Used only to communicate an API's latest version",\r
649                                     response = String.class),\r
650                     @ResponseHeader(name = "X-ONAP-RequestID",\r
651                                     description = "Used to track REST transactions for logging purpose",\r
652                                     response = UUID.class)\r
653             },\r
654             authorizations = @Authorization(value = "basicAuth"),\r
655             tags = { "Policy", },\r
656             extensions = {\r
657                     @Extension(name = "interface info", properties = {\r
658                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
659                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
660                     })\r
661             })\r
662     @ApiResponses(value = {\r
663             @ApiResponse(code = 401, message = "Authentication Error"),\r
664             @ApiResponse(code = 403, message = "Authorization Error"),\r
665             @ApiResponse(code = 404, message = "Resource Not Found"),\r
666             @ApiResponse(code = 500, message = "Internal Server Error")\r
667         })\r
668     public Response getSpecificVersionOfPolicy(\r
669             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
670             @PathParam("policyTypeVersion")\r
671                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,\r
672             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,\r
673             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,\r
674             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
675 \r
676         try {\r
677             ToscaServiceTemplate serviceTemplate = new PolicyProvider()\r
678                     .fetchPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
679             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET);\r
680             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
681                     .entity(serviceTemplate).build();\r
682         } catch (PfModelException | PfModelRuntimeException pfme) {\r
683             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET);\r
684             return addLoggingHeaders(addVersionControlHeaders(\r
685                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
686                     .entity(pfme.getErrorResponse()).build();\r
687         }\r
688     }\r
689 \r
690     /**\r
691      * Retrieves the latest version of a particular policy.\r
692      *\r
693      * @param policyTypeId the ID of specified policy type\r
694      * @param policyTypeVersion the version of specified policy type\r
695      * @param policyId the ID of specified policy\r
696      *\r
697      * @return the Response object containing the results of the API operation\r
698      */\r
699     @GET\r
700     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/latest")\r
701     @ApiOperation(value = "Retrieve the latest version of a particular policy",\r
702             notes = "Returns the latest version of specified policy",\r
703             response = ToscaServiceTemplate.class,\r
704             responseHeaders = {\r
705                     @ResponseHeader(name = "X-MinorVersion",\r
706                                     description = "Used to request or communicate a MINOR version back from the client"\r
707                                                 + " to the server, and from the server back to the client",\r
708                                     response = String.class),\r
709                     @ResponseHeader(name = "X-PatchVersion",\r
710                                     description = "Used only to communicate a PATCH version in a response for"\r
711                                                 + " troubleshooting purposes only, and will not be provided by"\r
712                                                 + " the client on request",\r
713                                     response = String.class),\r
714                     @ResponseHeader(name = "X-LatestVersion",\r
715                                     description = "Used only to communicate an API's latest version",\r
716                                     response = String.class),\r
717                     @ResponseHeader(name = "X-ONAP-RequestID",\r
718                                     description = "Used to track REST transactions for logging purpose",\r
719                                     response = UUID.class)\r
720             },\r
721             authorizations = @Authorization(value = "basicAuth"),\r
722             tags = { "Policy", },\r
723             extensions = {\r
724                     @Extension(name = "interface info", properties = {\r
725                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
726                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
727                     })\r
728             })\r
729     @ApiResponses(value = {\r
730             @ApiResponse(code = 401, message = "Authentication Error"),\r
731             @ApiResponse(code = 403, message = "Authorization Error"),\r
732             @ApiResponse(code = 404, message = "Resource Not Found"),\r
733             @ApiResponse(code = 500, message = "Internal Server Error")\r
734         })\r
735     public Response getLatestVersionOfPolicy(\r
736             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
737             @PathParam("policyTypeVersion")\r
738                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,\r
739             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,\r
740             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
741 \r
742         try {\r
743             ToscaServiceTemplate serviceTemplate =\r
744                     new PolicyProvider().fetchLatestPolicies(policyTypeId, policyTypeVersion, policyId);\r
745             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET);\r
746             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
747                     .entity(serviceTemplate).build();\r
748         } catch (PfModelException | PfModelRuntimeException pfme) {\r
749             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET);\r
750             return addLoggingHeaders(addVersionControlHeaders(\r
751                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
752                     .entity(pfme.getErrorResponse()).build();\r
753         }\r
754     }\r
755 \r
756     /**\r
757      * Retrieves deployed versions of a particular policy in pdp groups.\r
758      *\r
759      * @param policyTypeId the ID of specified policy type\r
760      * @param policyTypeVersion the version of specified policy type\r
761      * @param policyId the ID of specified policy\r
762      *\r
763      * @return the Response object containing the results of the API operation\r
764      */\r
765     @GET\r
766     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/deployed")\r
767     @ApiOperation(value = "Retrieve deployed versions of a particular policy in pdp groups",\r
768             notes = "Returns deployed versions of specified policy in pdp groups",\r
769             response = ToscaPolicy.class, responseContainer = "List",\r
770             responseHeaders = {\r
771                     @ResponseHeader(name = "X-MinorVersion",\r
772                                     description = "Used to request or communicate a MINOR version back from the client"\r
773                                                 + " to the server, and from the server back to the client",\r
774                                     response = String.class),\r
775                     @ResponseHeader(name = "X-PatchVersion",\r
776                                     description = "Used only to communicate a PATCH version in a response for"\r
777                                                 + " troubleshooting purposes only, and will not be provided by"\r
778                                                 + " the client on request",\r
779                                     response = String.class),\r
780                     @ResponseHeader(name = "X-LatestVersion",\r
781                                     description = "Used only to communicate an API's latest version",\r
782                                     response = String.class),\r
783                     @ResponseHeader(name = "X-ONAP-RequestID",\r
784                                     description = "Used to track REST transactions for logging purpose",\r
785                                     response = UUID.class)\r
786             },\r
787             authorizations = @Authorization(value = "basicAuth"),\r
788             tags = { "Policy", },\r
789             extensions = {\r
790                     @Extension(name = "interface info", properties = {\r
791                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
792                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
793                     })\r
794             })\r
795     @ApiResponses(value = {\r
796             @ApiResponse(code = 401, message = "Authentication Error"),\r
797             @ApiResponse(code = 403, message = "Authorization Error"),\r
798             @ApiResponse(code = 404, message = "Resource Not Found"),\r
799             @ApiResponse(code = 500, message = "Internal Server Error")\r
800         })\r
801     public Response getDeployedVersionsOfPolicy(\r
802             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
803             @PathParam("policyTypeVersion")\r
804                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,\r
805             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,\r
806             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
807 \r
808         try {\r
809             Map<Pair<String, String>, List<ToscaPolicy>> deployedPolicies = new PolicyProvider()\r
810                     .fetchDeployedPolicies(policyTypeId, policyTypeVersion, policyId);\r
811             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.GET);\r
812             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
813                     .entity(deployedPolicies).build();\r
814         } catch (PfModelException | PfModelRuntimeException pfme) {\r
815             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.GET);\r
816             return addLoggingHeaders(addVersionControlHeaders(\r
817                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
818                     .entity(pfme.getErrorResponse()).build();\r
819         }\r
820     }\r
821 \r
822     /**\r
823      * Creates a new policy for a particular policy type and version.\r
824      *\r
825      * @param policyTypeId the ID of specified policy type\r
826      * @param policyTypeVersion the version of specified policy type\r
827      * @param body the body of policy following TOSCA definition\r
828      *\r
829      * @return the Response object containing the results of the API operation\r
830      */\r
831     @POST\r
832     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")\r
833     @ApiOperation(value = "Create a new policy for a policy type version",\r
834             notes = "Client should provide TOSCA body of the new policy",\r
835             authorizations = @Authorization(value = "basicAuth"),\r
836             tags = { "Policy", },\r
837             response = ToscaServiceTemplate.class,\r
838             responseHeaders = {\r
839                     @ResponseHeader(name = "X-MinorVersion",\r
840                                     description = "Used to request or communicate a MINOR version back from the client"\r
841                                                 + " to the server, and from the server back to the client",\r
842                                     response = String.class),\r
843                     @ResponseHeader(name = "X-PatchVersion",\r
844                                     description = "Used only to communicate a PATCH version in a response for"\r
845                                                 + " troubleshooting purposes only, and will not be provided by"\r
846                                                 + " the client on request",\r
847                                     response = String.class),\r
848                     @ResponseHeader(name = "X-LatestVersion",\r
849                                     description = "Used only to communicate an API's latest version",\r
850                                     response = String.class),\r
851                     @ResponseHeader(name = "X-ONAP-RequestID",\r
852                                     description = "Used to track REST transactions for logging purpose",\r
853                                     response = UUID.class)\r
854             },\r
855             extensions = {\r
856                     @Extension(name = "interface info", properties = {\r
857                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
858                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
859                     })\r
860             })\r
861     @ApiResponses(value = {\r
862             @ApiResponse(code = 400, message = "Invalid Body"),\r
863             @ApiResponse(code = 401, message = "Authentication Error"),\r
864             @ApiResponse(code = 403, message = "Authorization Error"),\r
865             @ApiResponse(code = 404, message = "Resource Not Found"),\r
866             @ApiResponse(code = 500, message = "Internal Server Error")\r
867         })\r
868     public Response createPolicy(\r
869             @PathParam("policyTypeId") @ApiParam(value = "ID of policy type", required = true) String policyTypeId,\r
870             @PathParam("policyTypeVersion")\r
871                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,\r
872             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,\r
873             @ApiParam(value = "Entity body of policy", required = true) ToscaServiceTemplate body) {\r
874 \r
875         try {\r
876             ToscaServiceTemplate serviceTemplate = new PolicyProvider()\r
877                     .createPolicy(policyTypeId, policyTypeVersion, body);\r
878             updateApiStatisticsCounter(Target.POLICY, Result.SUCCESS, HttpMethod.POST);\r
879             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
880                     .entity(serviceTemplate).build();\r
881         } catch (PfModelException | PfModelRuntimeException pfme) {\r
882             updateApiStatisticsCounter(Target.POLICY, Result.FAILURE, HttpMethod.POST);\r
883             return addLoggingHeaders(addVersionControlHeaders(\r
884                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
885                     .entity(pfme.getErrorResponse()).build();\r
886         }\r
887     }\r
888 \r
889     /**\r
890      * Deletes the specified version of a particular policy.\r
891      *\r
892      * @param policyTypeId the ID of specified policy type\r
893      * @param policyTypeVersion the version of specified policy type\r
894      * @param policyId the ID of specified policy\r
895      * @param policyVersion the version of specified policy\r
896      *\r
897      * @return the Response object containing the results of the API operation\r
898      */\r
899     @DELETE\r
900     @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")\r
901     @ApiOperation(value = "Delete a particular version of a policy",\r
902             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",\r
903             authorizations = @Authorization(value = "basicAuth"),\r
904             tags = { "Policy", },\r
905             response = ToscaServiceTemplate.class,\r
906             responseHeaders = {\r
907                     @ResponseHeader(name = "X-MinorVersion",\r
908                                     description = "Used to request or communicate a MINOR version back from the client"\r
909                                                 + " to the server, and from the server back to the client",\r
910                                     response = String.class),\r
911                     @ResponseHeader(name = "X-PatchVersion",\r
912                                     description = "Used only to communicate a PATCH version in a response for"\r
913                                                 + " troubleshooting purposes only, and will not be provided by"\r
914                                                 + " the client on request",\r
915                                     response = String.class),\r
916                     @ResponseHeader(name = "X-LatestVersion",\r
917                                     description = "Used only to communicate an API's latest version",\r
918                                     response = String.class),\r
919                     @ResponseHeader(name = "X-ONAP-RequestID",\r
920                                     description = "Used to track REST transactions for logging purpose",\r
921                                     response = UUID.class)\r
922             },\r
923             extensions = {\r
924                     @Extension(name = "interface info", properties = {\r
925                             @ExtensionProperty(name = "api-version", value = "1.0.0"),\r
926                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")\r
927                     })\r
928             })\r
929     @ApiResponses(value = {\r
930             @ApiResponse(code = 401, message = "Authentication Error"),\r
931             @ApiResponse(code = 403, message = "Authorization Error"),\r
932             @ApiResponse(code = 404, message = "Resource Not Found"),\r
933             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),\r
934             @ApiResponse(code = 500, message = "Internal Server Error")\r
935         })\r
936     public Response deleteSpecificVersionOfPolicy(\r
937             @PathParam("policyTypeId") @ApiParam(value = "PolicyType ID", required = true) String policyTypeId,\r
938             @PathParam("policyTypeVersion")\r
939                 @ApiParam(value = "Version of policy type", required = true) String policyTypeVersion,\r
940             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,\r
941             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,\r
942             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {\r
943 \r
944         try {\r
945             ToscaServiceTemplate serviceTemplate = new PolicyProvider()\r
946                     .deletePolicy(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
947             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)\r
948                     .entity(serviceTemplate).build();\r
949         } catch (PfModelException | PfModelRuntimeException pfme) {\r
950             return addLoggingHeaders(addVersionControlHeaders(\r
951                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)\r
952                     .entity(pfme.getErrorResponse()).build();\r
953         }\r
954     }\r
955 \r
956     private ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) {\r
957         return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0");\r
958     }\r
959 \r
960     private ResponseBuilder addLoggingHeaders(ResponseBuilder rb, UUID requestId) {\r
961         if (requestId == null) {\r
962             // Generate a random uuid if client does not embed requestId in rest request\r
963             return rb.header("X-ONAP-RequestID", UUID.randomUUID());\r
964         }\r
965         return rb.header("X-ONAP-RequestID", requestId);\r
966     }\r
967 \r
968     private enum Target {\r
969         POLICY, POLICY_TYPE, OTHER\r
970     }\r
971 \r
972     private enum Result {\r
973         SUCCESS, FAILURE\r
974     }\r
975 \r
976     private enum HttpMethod {\r
977         POST, GET\r
978     }\r
979 \r
980     private void updateApiStatisticsCounter(Target target, Result result, HttpMethod http) {\r
981 \r
982         ApiStatisticsManager.updateTotalApiCallCount();\r
983         if (target == Target.POLICY) {\r
984             if (result == Result.SUCCESS) {\r
985                 if (http == HttpMethod.GET) {\r
986                     ApiStatisticsManager.updateApiCallSuccessCount();\r
987                     ApiStatisticsManager.updateTotalPolicyGetCount();\r
988                     ApiStatisticsManager.updatePolicyGetSuccessCount();\r
989                 } else if (http == HttpMethod.POST) {\r
990                     ApiStatisticsManager.updateApiCallSuccessCount();\r
991                     ApiStatisticsManager.updateTotalPolicyPostCount();\r
992                     ApiStatisticsManager.updatePolicyPostSuccessCount();\r
993                 }\r
994             } else {\r
995                 if (http == HttpMethod.GET) {\r
996                     ApiStatisticsManager.updateApiCallFailureCount();\r
997                     ApiStatisticsManager.updateTotalPolicyGetCount();\r
998                     ApiStatisticsManager.updatePolicyGetFailureCount();\r
999                 } else {\r
1000                     ApiStatisticsManager.updateApiCallFailureCount();\r
1001                     ApiStatisticsManager.updateTotalPolicyPostCount();\r
1002                     ApiStatisticsManager.updatePolicyPostFailureCount();\r
1003                 }\r
1004             }\r
1005         } else if (target == Target.POLICY_TYPE) {\r
1006             if (result == Result.SUCCESS) {\r
1007                 if (http == HttpMethod.GET) {\r
1008                     ApiStatisticsManager.updateApiCallSuccessCount();\r
1009                     ApiStatisticsManager.updateTotalPolicyTypeGetCount();\r
1010                     ApiStatisticsManager.updatePolicyTypeGetSuccessCount();\r
1011                 } else if (http == HttpMethod.POST) {\r
1012                     ApiStatisticsManager.updateApiCallSuccessCount();\r
1013                     ApiStatisticsManager.updatePolicyTypePostSuccessCount();\r
1014                     ApiStatisticsManager.updatePolicyTypePostSuccessCount();\r
1015                 }\r
1016             } else {\r
1017                 if (http == HttpMethod.GET) {\r
1018                     ApiStatisticsManager.updateApiCallFailureCount();\r
1019                     ApiStatisticsManager.updateTotalPolicyTypeGetCount();\r
1020                     ApiStatisticsManager.updatePolicyTypeGetFailureCount();\r
1021                 } else {\r
1022                     ApiStatisticsManager.updateApiCallFailureCount();\r
1023                     ApiStatisticsManager.updateTotalPolicyTypePostCount();\r
1024                     ApiStatisticsManager.updatePolicyTypePostFailureCount();\r
1025                 }\r
1026             }\r
1027         } else {\r
1028             ApiStatisticsManager.updateApiCallSuccessCount();\r
1029         }\r
1030     }\r
1031 }