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