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