Incorporate filters in provider functions
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / LegacyApiRestController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest;
24
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiParam;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30 import io.swagger.annotations.Authorization;
31 import io.swagger.annotations.Extension;
32 import io.swagger.annotations.ExtensionProperty;
33 import io.swagger.annotations.ResponseHeader;
34 import java.util.Map;
35 import java.util.UUID;
36 import javax.ws.rs.Consumes;
37 import javax.ws.rs.DELETE;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.HeaderParam;
40 import javax.ws.rs.POST;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.core.Response;
45 import javax.ws.rs.core.Response.ResponseBuilder;
46 import org.onap.policy.api.main.rest.provider.LegacyGuardPolicyProvider;
47 import org.onap.policy.api.main.rest.provider.LegacyOperationalPolicyProvider;
48 import org.onap.policy.models.base.PfModelException;
49 import org.onap.policy.models.base.PfModelRuntimeException;
50 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
51 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
52 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
53
54 /**
55  * Class to provide legacy REST API services.
56  *
57  * @author Chenfei Gao (cgao@research.att.com)
58  */
59 @Path("/policy/api/v1")
60 @Api(value = "Legacy Policy Design API")
61 @Produces("application/json")
62 @Consumes("application/json")
63 public class LegacyApiRestController {
64
65     /**
66      * Retrieves all versions of guard policies.
67      *
68      * @return the Response object containing the results of the API operation
69      */
70     @GET
71     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies")
72     @ApiOperation(value = "Retrieve all versions of guard policies",
73             notes = "Returns a list of all versions of guard policies",
74             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
75             responseHeaders = {
76                     @ResponseHeader(name = "X-MinorVersion",
77                                     description = "Used to request or communicate a MINOR version back from the client"
78                                                 + " to the server, and from the server back to the client",
79                                     response = String.class),
80                     @ResponseHeader(name = "X-PatchVersion",
81                                     description = "Used only to communicate a PATCH version in a response for"
82                                                 + " troubleshooting purposes only, and will not be provided by"
83                                                 + " the client on request",
84                                     response = String.class),
85                     @ResponseHeader(name = "X-LatestVersion",
86                                     description = "Used only to communicate an API's latest version",
87                                     response = String.class),
88                     @ResponseHeader(name = "X-ONAP-RequestID",
89                                     description = "Used to track REST transactions for logging purpose",
90                                     response = UUID.class)
91             },
92             authorizations = @Authorization(value = "basicAuth"),
93             tags = { "Legacy Guard Policy", },
94             extensions = {
95                     @Extension(name = "interface info", properties = {
96                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
97                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
98                     })
99             })
100     @ApiResponses(value = {
101             @ApiResponse(code = 401, message = "Authentication Error"),
102             @ApiResponse(code = 403, message = "Authorization Error"),
103             @ApiResponse(code = 500, message = "Internal Server Error")
104         })
105     public Response getAllGuardPolicies(
106             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
107
108         try {
109             Map<String, LegacyGuardPolicyOutput> policies = new LegacyGuardPolicyProvider()
110                     .fetchGuardPolicy(null, null);
111             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
112                     .entity(policies).build();
113         } catch (PfModelException | PfModelRuntimeException pfme) {
114             return addLoggingHeaders(addVersionControlHeaders(
115                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
116                     .entity(pfme.getErrorResponse()).build();
117         }
118     }
119
120     /**
121      * Retrieves all versions of a particular guard policy.
122      *
123      * @param policyId the ID of specified guard policy
124      *
125      * @return the Response object containing the results of the API operation
126      */
127     @GET
128     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/{policyId}")
129     @ApiOperation(value = "Retrieve all versions of a particular guard policy",
130             notes = "Returns a list of all versions of the specified guard policy",
131             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
132             responseHeaders = {
133                     @ResponseHeader(name = "X-MinorVersion",
134                                     description = "Used to request or communicate a MINOR version back from the client"
135                                                 + " to the server, and from the server back to the client",
136                                     response = String.class),
137                     @ResponseHeader(name = "X-PatchVersion",
138                                     description = "Used only to communicate a PATCH version in a response for"
139                                                 + " troubleshooting purposes only, and will not be provided by"
140                                                 + " the client on request",
141                                     response = String.class),
142                     @ResponseHeader(name = "X-LatestVersion",
143                                     description = "Used only to communicate an API's latest version",
144                                     response = String.class),
145                     @ResponseHeader(name = "X-ONAP-RequestID",
146                                     description = "Used to track REST transactions for logging purpose",
147                                     response = UUID.class)
148             },
149             authorizations = @Authorization(value = "basicAuth"),
150             tags = { "Legacy Guard Policy", },
151             extensions = {
152                     @Extension(name = "interface info", properties = {
153                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
154                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
155                     })
156             })
157     @ApiResponses(value = {
158             @ApiResponse(code = 401, message = "Authentication Error"),
159             @ApiResponse(code = 403, message = "Authorization Error"),
160             @ApiResponse(code = 404, message = "Resource Not Found"),
161             @ApiResponse(code = 500, message = "Internal Server Error")
162         })
163     public Response getAllVersionsOfGuardPolicy(
164             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
165             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
166
167         try {
168             Map<String, LegacyGuardPolicyOutput> policies = new LegacyGuardPolicyProvider()
169                     .fetchGuardPolicy(policyId, null);
170             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
171                     .entity(policies).build();
172         } catch (PfModelException | PfModelRuntimeException pfme) {
173             return addLoggingHeaders(addVersionControlHeaders(
174                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
175                     .entity(pfme.getErrorResponse()).build();
176         }
177     }
178
179     /**
180      * Retrieves the specified version of a particular guard policy.
181      *
182      * @param policyId the ID of specified policy
183      * @param policyVersion the version of specified policy
184      *
185      * @return the Response object containing the results of the API operation
186      */
187     @GET
188     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/{policyId}/versions/{policyVersion}")
189     @ApiOperation(value = "Retrieve one version of a particular guard policy",
190             notes = "Returns a particular version of a specified guard policy",
191             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
192             responseHeaders = {
193                     @ResponseHeader(name = "X-MinorVersion",
194                                     description = "Used to request or communicate a MINOR version back from the client"
195                                                 + " to the server, and from the server back to the client",
196                                     response = String.class),
197                     @ResponseHeader(name = "X-PatchVersion",
198                                     description = "Used only to communicate a PATCH version in a response for"
199                                                 + " troubleshooting purposes only, and will not be provided by"
200                                                 + " the client on request",
201                                     response = String.class),
202                     @ResponseHeader(name = "X-LatestVersion",
203                                     description = "Used only to communicate an API's latest version",
204                                     response = String.class),
205                     @ResponseHeader(name = "X-ONAP-RequestID",
206                                     description = "Used to track REST transactions for logging purpose",
207                                     response = UUID.class)
208             },
209             authorizations = @Authorization(value = "basicAuth"),
210             tags = { "Legacy Guard Policy", },
211             extensions = {
212                     @Extension(name = "interface info", properties = {
213                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
214                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
215                     })
216             })
217     @ApiResponses(value = {
218             @ApiResponse(code = 401, message = "Authentication Error"),
219             @ApiResponse(code = 403, message = "Authorization Error"),
220             @ApiResponse(code = 404, message = "Resource Not Found"),
221             @ApiResponse(code = 500, message = "Internal Server Error")
222         })
223     public Response getSpecificVersionOfGuardPolicy(
224             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
225             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
226             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
227
228         try {
229             Map<String, LegacyGuardPolicyOutput> policies = new LegacyGuardPolicyProvider()
230                     .fetchGuardPolicy(policyId, policyVersion);
231             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
232                     .entity(policies).build();
233         } catch (PfModelException | PfModelRuntimeException pfme) {
234             return addLoggingHeaders(addVersionControlHeaders(
235                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
236                     .entity(pfme.getErrorResponse()).build();
237         }
238     }
239
240     /**
241      * Creates a new guard policy.
242      *
243      * @param body the body of policy
244      *
245      * @return the Response object containing the results of the API operation
246      */
247     @POST
248     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies")
249     @ApiOperation(value = "Create a new guard policy",
250             notes = "Client should provide entity body of the new guard policy",
251             authorizations = @Authorization(value = "basicAuth"),
252             tags = { "Legacy Guard Policy", },
253             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
254             responseHeaders = {
255                     @ResponseHeader(name = "X-MinorVersion",
256                                     description = "Used to request or communicate a MINOR version back from the client"
257                                                 + " to the server, and from the server back to the client",
258                                     response = String.class),
259                     @ResponseHeader(name = "X-PatchVersion",
260                                     description = "Used only to communicate a PATCH version in a response for"
261                                                 + " troubleshooting purposes only, and will not be provided by"
262                                                 + " the client on request",
263                                     response = String.class),
264                     @ResponseHeader(name = "X-LatestVersion",
265                                     description = "Used only to communicate an API's latest version",
266                                     response = String.class),
267                     @ResponseHeader(name = "X-ONAP-RequestID",
268                                     description = "Used to track REST transactions for logging purpose",
269                                     response = UUID.class)
270             },
271             extensions = {
272                     @Extension(name = "interface info", properties = {
273                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
274                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
275                     })
276             })
277     @ApiResponses(value = {
278             @ApiResponse(code = 400, message = "Invalid Body"),
279             @ApiResponse(code = 401, message = "Authentication Error"),
280             @ApiResponse(code = 403, message = "Authorization Error"),
281             @ApiResponse(code = 500, message = "Internal Server Error")
282         })
283     public Response createGuardPolicy(
284             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
285             @ApiParam(value = "Entity body of policy", required = true) LegacyGuardPolicyInput body) {
286
287         try {
288             Map<String, LegacyGuardPolicyOutput> policy = new LegacyGuardPolicyProvider().createGuardPolicy(body);
289             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
290                     .entity(policy).build();
291         } catch (PfModelException | PfModelRuntimeException pfme) {
292             return addLoggingHeaders(addVersionControlHeaders(
293                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
294                     .entity(pfme.getErrorResponse()).build();
295         }
296     }
297
298     /**
299      * Deletes the specified version of a particular guard policy.
300      *
301      * @param policyId the ID of specified policy
302      * @param policyVersion the version of specified policy
303      *
304      * @return the Response object containing the results of the API operation
305      */
306     @DELETE
307     @Path("/policytypes/onap.policy.controlloop.guard/versions/1.0.0/policies/{policyId}/versions/{policyVersion}")
308     @ApiOperation(value = "Delete a particular version of a guard policy",
309             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
310             authorizations = @Authorization(value = "basicAuth"),
311             tags = { "Legacy Guard Policy", },
312             response = LegacyGuardPolicyOutput.class, responseContainer = "Map",
313             responseHeaders = {
314                     @ResponseHeader(name = "X-MinorVersion",
315                                     description = "Used to request or communicate a MINOR version back from the client"
316                                                 + " to the server, and from the server back to the client",
317                                     response = String.class),
318                     @ResponseHeader(name = "X-PatchVersion",
319                                     description = "Used only to communicate a PATCH version in a response for"
320                                                 + " troubleshooting purposes only, and will not be provided by"
321                                                 + " the client on request",
322                                     response = String.class),
323                     @ResponseHeader(name = "X-LatestVersion",
324                                     description = "Used only to communicate an API's latest version",
325                                     response = String.class),
326                     @ResponseHeader(name = "X-ONAP-RequestID",
327                                     description = "Used to track REST transactions for logging purpose",
328                                     response = UUID.class)
329             },
330             extensions = {
331                     @Extension(name = "interface info", properties = {
332                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
333                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
334                     })
335             })
336     @ApiResponses(value = {
337             @ApiResponse(code = 401, message = "Authentication Error"),
338             @ApiResponse(code = 403, message = "Authorization Error"),
339             @ApiResponse(code = 404, message = "Resource Not Found"),
340             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
341             @ApiResponse(code = 500, message = "Internal Server Error")
342         })
343     public Response deleteSpecificVersionOfGuardPolicy(
344             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
345             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
346             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
347
348         try {
349             Map<String, LegacyGuardPolicyOutput> policies = new LegacyGuardPolicyProvider()
350                     .deleteGuardPolicy(policyId, policyVersion);
351             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
352                     .entity(policies).build();
353         } catch (PfModelException | PfModelRuntimeException pfme) {
354             return addLoggingHeaders(addVersionControlHeaders(
355                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
356                     .entity(pfme.getErrorResponse()).build();
357         }
358     }
359
360     /**
361      * Retrieves all versions of operational policies.
362      *
363      * @return the Response object containing the results of the API operation
364      */
365     @GET
366     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/policies")
367     @ApiOperation(value = "Retrieve all versions of operational policies",
368             notes = "Returns a list of all versions of operational policies",
369             response = LegacyOperationalPolicy.class,
370             responseHeaders = {
371                     @ResponseHeader(name = "X-MinorVersion",
372                                     description = "Used to request or communicate a MINOR version back from the client"
373                                                 + " to the server, and from the server back to the client",
374                                     response = String.class),
375                     @ResponseHeader(name = "X-PatchVersion",
376                                     description = "Used only to communicate a PATCH version in a response for"
377                                                 + " troubleshooting purposes only, and will not be provided by"
378                                                 + " the client on request",
379                                     response = String.class),
380                     @ResponseHeader(name = "X-LatestVersion",
381                                     description = "Used only to communicate an API's latest version",
382                                     response = String.class),
383                     @ResponseHeader(name = "X-ONAP-RequestID",
384                                     description = "Used to track REST transactions for logging purpose",
385                                     response = UUID.class)
386             },
387             authorizations = @Authorization(value = "basicAuth"),
388             tags = { "Legacy Operational Policy", },
389             extensions = {
390                     @Extension(name = "interface info", properties = {
391                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
392                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
393                     })
394             })
395     @ApiResponses(value = {
396             @ApiResponse(code = 401, message = "Authentication Error"),
397             @ApiResponse(code = 403, message = "Authorization Error"),
398             @ApiResponse(code = 500, message = "Internal Server Error")
399         })
400     public Response getAllOperationalPolicies(
401             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
402
403         try {
404             LegacyOperationalPolicy policy = new LegacyOperationalPolicyProvider()
405                     .fetchOperationalPolicy(null, null);
406             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
407                     .entity(policy).build();
408         } catch (PfModelException | PfModelRuntimeException pfme) {
409             return addLoggingHeaders(addVersionControlHeaders(
410                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
411                     .entity(pfme.getErrorResponse()).build();
412         }
413     }
414
415     /**
416      * Retrieves all versions of a particular operational policy.
417      *
418      * @param policyId the ID of specified operational policy
419      *
420      * @return the Response object containing the results of the API operation
421      */
422     @GET
423     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/policies/{policyId}")
424     @ApiOperation(value = "Retrieve all versions of a particular operational policy",
425             notes = "Returns a list of all versions of the specified operational policy",
426             response = LegacyOperationalPolicy.class,
427             responseHeaders = {
428                     @ResponseHeader(name = "X-MinorVersion",
429                                     description = "Used to request or communicate a MINOR version back from the client"
430                                                 + " to the server, and from the server back to the client",
431                                     response = String.class),
432                     @ResponseHeader(name = "X-PatchVersion",
433                                     description = "Used only to communicate a PATCH version in a response for"
434                                                 + " troubleshooting purposes only, and will not be provided by"
435                                                 + " the client on request",
436                                     response = String.class),
437                     @ResponseHeader(name = "X-LatestVersion",
438                                     description = "Used only to communicate an API's latest version",
439                                     response = String.class),
440                     @ResponseHeader(name = "X-ONAP-RequestID",
441                                     description = "Used to track REST transactions for logging purpose",
442                                     response = UUID.class)
443             },
444             authorizations = @Authorization(value = "basicAuth"),
445             tags = { "Legacy Operational Policy", },
446             extensions = {
447                     @Extension(name = "interface info", properties = {
448                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
449                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
450                     })
451             })
452     @ApiResponses(value = {
453             @ApiResponse(code = 401, message = "Authentication Error"),
454             @ApiResponse(code = 403, message = "Authorization Error"),
455             @ApiResponse(code = 404, message = "Resource Not Found"),
456             @ApiResponse(code = 500, message = "Internal Server Error")
457         })
458     public Response getAllVersionsOfOperationalPolicy(
459             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
460             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
461
462         try {
463             LegacyOperationalPolicy policy = new LegacyOperationalPolicyProvider()
464                     .fetchOperationalPolicy(policyId, null);
465             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
466                     .entity(policy).build();
467         } catch (PfModelException | PfModelRuntimeException pfme) {
468             return addLoggingHeaders(addVersionControlHeaders(
469                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
470                     .entity(pfme.getErrorResponse()).build();
471         }
472     }
473
474     /**
475      * Retrieves the specified version of a particular operational policy.
476      *
477      * @param policyId the ID of specified policy
478      * @param policyVersion the version of specified policy
479      *
480      * @return the Response object containing the results of the API operation
481      */
482     @GET
483     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/"
484          + "policies/{policyId}/versions/{policyVersion}")
485     @ApiOperation(value = "Retrieve one version of a particular operational policy",
486             notes = "Returns a particular version of a specified operational policy",
487             response = LegacyOperationalPolicy.class,
488             responseHeaders = {
489                     @ResponseHeader(name = "X-MinorVersion",
490                                     description = "Used to request or communicate a MINOR version back from the client"
491                                                 + " to the server, and from the server back to the client",
492                                     response = String.class),
493                     @ResponseHeader(name = "X-PatchVersion",
494                                     description = "Used only to communicate a PATCH version in a response for"
495                                                 + " troubleshooting purposes only, and will not be provided by"
496                                                 + " the client on request",
497                                     response = String.class),
498                     @ResponseHeader(name = "X-LatestVersion",
499                                     description = "Used only to communicate an API's latest version",
500                                     response = String.class),
501                     @ResponseHeader(name = "X-ONAP-RequestID",
502                                     description = "Used to track REST transactions for logging purpose",
503                                     response = UUID.class)
504             },
505             authorizations = @Authorization(value = "basicAuth"),
506             tags = { "Legacy Operational Policy", },
507             extensions = {
508                     @Extension(name = "interface info", properties = {
509                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
510                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
511                     })
512             })
513     @ApiResponses(value = {
514             @ApiResponse(code = 401, message = "Authentication Error"),
515             @ApiResponse(code = 403, message = "Authorization Error"),
516             @ApiResponse(code = 404, message = "Resource Not Found"),
517             @ApiResponse(code = 500, message = "Internal Server Error")
518         })
519     public Response getSpecificVersionOfOperationalPolicy(
520             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
521             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
522             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
523
524         try {
525             LegacyOperationalPolicy policy = new LegacyOperationalPolicyProvider()
526                     .fetchOperationalPolicy(policyId, policyVersion);
527             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
528                     .entity(policy).build();
529         } catch (PfModelException | PfModelRuntimeException pfme) {
530             return addLoggingHeaders(addVersionControlHeaders(
531                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
532                     .entity(pfme.getErrorResponse()).build();
533         }
534     }
535
536     /**
537      * Creates a new operational policy.
538      *
539      * @param body the body of policy
540      *
541      * @return the Response object containing the results of the API operation
542      */
543     @POST
544     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/policies")
545     @ApiOperation(value = "Create a new operational policy",
546             notes = "Client should provide entity body of the new operational policy",
547             authorizations = @Authorization(value = "basicAuth"),
548             tags = { "Legacy Operational Policy", },
549             response = LegacyOperationalPolicy.class,
550             responseHeaders = {
551                     @ResponseHeader(name = "X-MinorVersion",
552                                     description = "Used to request or communicate a MINOR version back from the client"
553                                                 + " to the server, and from the server back to the client",
554                                     response = String.class),
555                     @ResponseHeader(name = "X-PatchVersion",
556                                     description = "Used only to communicate a PATCH version in a response for"
557                                                 + " troubleshooting purposes only, and will not be provided by"
558                                                 + " the client on request",
559                                     response = String.class),
560                     @ResponseHeader(name = "X-LatestVersion",
561                                     description = "Used only to communicate an API's latest version",
562                                     response = String.class),
563                     @ResponseHeader(name = "X-ONAP-RequestID",
564                                     description = "Used to track REST transactions for logging purpose",
565                                     response = UUID.class)
566             },
567             extensions = {
568                     @Extension(name = "interface info", properties = {
569                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
570                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
571                     })
572             })
573     @ApiResponses(value = {
574             @ApiResponse(code = 400, message = "Invalid Body"),
575             @ApiResponse(code = 401, message = "Authentication Error"),
576             @ApiResponse(code = 403, message = "Authorization Error"),
577             @ApiResponse(code = 500, message = "Internal Server Error")
578         })
579     public Response createOperationalPolicy(
580             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
581             @ApiParam(value = "Entity body of policy", required = true) LegacyOperationalPolicy body) {
582
583         try {
584             LegacyOperationalPolicy policy = new LegacyOperationalPolicyProvider()
585                     .createOperationalPolicy(body);
586             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
587                     .entity(policy).build();
588         } catch (PfModelException | PfModelRuntimeException pfme) {
589             return addLoggingHeaders(addVersionControlHeaders(
590                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
591                     .entity(pfme.getErrorResponse()).build();
592         }
593     }
594
595     /**
596      * Deletes the specified version of a particular operational policy.
597      *
598      * @param policyId the ID of specified policy
599      * @param policyVersion the version of specified policy
600      *
601      * @return the Response object containing the results of the API operation
602      */
603     @DELETE
604     @Path("/policytypes/onap.policy.controlloop.operational/versions/1.0.0/"
605          + "policies/{policyId}/versions/{policyVersion}")
606     @ApiOperation(value = "Delete a particular version of a specified operational policy",
607             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
608             authorizations = @Authorization(value = "basicAuth"),
609             tags = { "Legacy Operational Policy", },
610             response = LegacyOperationalPolicy.class,
611             responseHeaders = {
612                     @ResponseHeader(name = "X-MinorVersion",
613                                     description = "Used to request or communicate a MINOR version back from the client"
614                                                 + " to the server, and from the server back to the client",
615                                     response = String.class),
616                     @ResponseHeader(name = "X-PatchVersion",
617                                     description = "Used only to communicate a PATCH version in a response for"
618                                                 + " troubleshooting purposes only, and will not be provided by"
619                                                 + " the client on request",
620                                     response = String.class),
621                     @ResponseHeader(name = "X-LatestVersion",
622                                     description = "Used only to communicate an API's latest version",
623                                     response = String.class),
624                     @ResponseHeader(name = "X-ONAP-RequestID",
625                                     description = "Used to track REST transactions for logging purpose",
626                                     response = UUID.class)
627             },
628             extensions = {
629                     @Extension(name = "interface info", properties = {
630                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
631                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
632                     })
633             })
634     @ApiResponses(value = {
635             @ApiResponse(code = 401, message = "Authentication Error"),
636             @ApiResponse(code = 403, message = "Authorization Error"),
637             @ApiResponse(code = 404, message = "Resource Not Found"),
638             @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
639             @ApiResponse(code = 500, message = "Internal Server Error")
640         })
641     public Response deleteSpecificVersionOfOperationalPolicy(
642             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
643             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
644             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
645
646         try {
647             LegacyOperationalPolicy policy = new LegacyOperationalPolicyProvider()
648                     .deleteOperationalPolicy(policyId, policyVersion);
649             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
650                     .entity(policy).build();
651         } catch (PfModelException | PfModelRuntimeException pfme) {
652             return addLoggingHeaders(addVersionControlHeaders(
653                     Response.status(pfme.getErrorResponse().getResponseCode())), requestId)
654                     .entity(pfme.getErrorResponse()).build();
655         }
656     }
657
658     private ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) {
659         return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0");
660     }
661
662     private ResponseBuilder addLoggingHeaders(ResponseBuilder rb, UUID requestId) {
663         if (requestId == null) {
664             // Generate a random uuid if client does not embed requestId in rest request
665             return rb.header("X-ONAP-RequestID", UUID.randomUUID());
666         }
667         return rb.header("X-ONAP-RequestID", requestId);
668     }
669 }