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