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