Removing legacy guard from policy/api
[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-2020 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.List;
35 import java.util.Map;
36 import java.util.UUID;
37 import javax.ws.rs.Consumes;
38 import javax.ws.rs.DELETE;
39 import javax.ws.rs.GET;
40 import javax.ws.rs.HeaderParam;
41 import javax.ws.rs.POST;
42 import javax.ws.rs.Path;
43 import javax.ws.rs.PathParam;
44 import javax.ws.rs.Produces;
45 import javax.ws.rs.core.Response;
46 import org.apache.commons.lang3.tuple.Pair;
47 import org.onap.policy.api.main.rest.provider.LegacyOperationalPolicyProvider;
48 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
49 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
50 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
51 import org.onap.policy.models.base.PfModelException;
52 import org.onap.policy.models.base.PfModelRuntimeException;
53 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * Class to provide legacy REST API services.
59  *
60  * @author Chenfei Gao (cgao@research.att.com)
61  */
62 @Path("/policy/api/v1")
63 @Api(value = "Legacy Policy Design API")
64 @Produces({"application/json", "application/yaml"})
65 @Consumes({"application/json", "application/yaml"})
66 public class LegacyApiRestController extends CommonRestController {
67
68     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyApiRestController.class);
69
70     /**
71      * Retrieves the latest version of a particular operational policy.
72      *
73      * @param policyId the ID of specified operational policy
74      *
75      * @return the Response object containing the results of the API operation
76      */
77     @GET
78     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/{policyId}/versions/latest")
79     @ApiOperation(value = "Retrieve the latest version of a particular operational policy",
80             notes = "Returns the latest version of the specified operational policy",
81             response = LegacyOperationalPolicy.class,
82             responseHeaders = {
83                 @ResponseHeader(name = "X-MinorVersion",
84                         description = "Used to request or communicate a MINOR version back from the client"
85                                 + " to the server, and from the server back to the client",
86                         response = String.class),
87                 @ResponseHeader(name = "X-PatchVersion",
88                         description = "Used only to communicate a PATCH version in a response for"
89                                 + " troubleshooting purposes only, and will not be provided by"
90                                 + " the client on request",
91                         response = String.class),
92                 @ResponseHeader(name = "X-LatestVersion",
93                         description = "Used only to communicate an API's latest version", response = String.class),
94                 @ResponseHeader(name = "X-ONAP-RequestID",
95                         description = "Used to track REST transactions for logging purpose", response = UUID.class)},
96             authorizations = @Authorization(value = "basicAuth"), tags = {"Legacy Operational Policy",},
97             extensions = {@Extension(name = "interface info",
98                     properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),
99                         @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})
100     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),
101         @ApiResponse(code = 403, message = "Authorization Error"),
102         @ApiResponse(code = 404, message = "Resource Not Found"),
103         @ApiResponse(code = 500, message = "Internal Server Error")})
104     public Response getLatestVersionOfOperationalPolicy(
105             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
106             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
107
108         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
109             LegacyOperationalPolicy policy = operationalPolicyProvider.fetchOperationalPolicy(policyId, null);
110             return makeOkResponse(requestId, policy);
111         } catch (PfModelException | PfModelRuntimeException pfme) {
112             LOGGER.debug("GET /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies/{}"
113                     + "/versions/latest", policyId, pfme);
114             return makeErrorResponse(requestId, pfme);
115         }
116     }
117
118     /**
119      * Retrieves the specified version of a particular operational policy.
120      *
121      * @param policyId the ID of specified policy
122      * @param policyVersion the version of specified policy
123      *
124      * @return the Response object containing the results of the API operation
125      */
126     @GET
127     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
128             + "policies/{policyId}/versions/{policyVersion}")
129     @ApiOperation(value = "Retrieve one version of a particular operational policy",
130             notes = "Returns a particular version of a specified operational policy",
131             response = LegacyOperationalPolicy.class,
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", response = String.class),
144                 @ResponseHeader(name = "X-ONAP-RequestID",
145                         description = "Used to track REST transactions for logging purpose", response = UUID.class)},
146             authorizations = @Authorization(value = "basicAuth"), tags = {"Legacy Operational Policy",},
147             extensions = {@Extension(name = "interface info",
148                     properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),
149                         @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})
150     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),
151         @ApiResponse(code = 403, message = "Authorization Error"),
152         @ApiResponse(code = 404, message = "Resource Not Found"),
153         @ApiResponse(code = 500, message = "Internal Server Error")})
154     public Response getSpecificVersionOfOperationalPolicy(
155             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
156             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
157             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
158
159         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
160             LegacyOperationalPolicy policy = operationalPolicyProvider.fetchOperationalPolicy(policyId, policyVersion);
161             return makeOkResponse(requestId, policy);
162         } catch (PfModelException | PfModelRuntimeException pfme) {
163             LOGGER.debug("GET /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
164                     + "policies/{}/versions/{}", policyId, policyVersion, pfme);
165             return makeErrorResponse(requestId, pfme);
166         }
167     }
168
169     /**
170      * Retrieves deployed versions of a particular operational policy in PDP groups.
171      *
172      * @param policyId the ID of specified policy
173      *
174      * @return the Response object containing the results of the API operation
175      */
176     @GET
177     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
178             + "policies/{policyId}/versions/deployed")
179     @ApiOperation(value = "Retrieve deployed versions of a particular operational policy in pdp groups",
180             notes = "Returns deployed versions of a specified operational policy in pdp groups",
181             response = LegacyOperationalPolicy.class, responseContainer = "List",
182             responseHeaders = {
183                 @ResponseHeader(name = "X-MinorVersion",
184                         description = "Used to request or communicate a MINOR version back from the client"
185                                 + " to the server, and from the server back to the client",
186                         response = String.class),
187                 @ResponseHeader(name = "X-PatchVersion",
188                         description = "Used only to communicate a PATCH version in a response for"
189                                 + " troubleshooting purposes only, and will not be provided by"
190                                 + " the client on request",
191                         response = String.class),
192                 @ResponseHeader(name = "X-LatestVersion",
193                         description = "Used only to communicate an API's latest version", response = String.class),
194                 @ResponseHeader(name = "X-ONAP-RequestID",
195                         description = "Used to track REST transactions for logging purpose", response = UUID.class)},
196             authorizations = @Authorization(value = "basicAuth"), tags = {"Legacy Operational Policy",},
197             extensions = {@Extension(name = "interface info",
198                     properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),
199                         @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})
200     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),
201         @ApiResponse(code = 403, message = "Authorization Error"),
202         @ApiResponse(code = 404, message = "Resource Not Found"),
203         @ApiResponse(code = 500, message = "Internal Server Error")})
204     public Response getDeployedVersionsOfOperationalPolicy(
205             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
206             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
207
208         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
209             Map<Pair<String, String>, List<LegacyOperationalPolicy>> deployedOperationalPolicies =
210                     operationalPolicyProvider.fetchDeployedOperationalPolicies(policyId);
211             return makeOkResponse(requestId, deployedOperationalPolicies);
212         } catch (PfModelException | PfModelRuntimeException pfme) {
213             LOGGER.debug("GET /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
214                     + "policies/{}/versions/deployed", policyId, pfme);
215             return makeErrorResponse(requestId, pfme);
216         }
217     }
218
219     /**
220      * Creates a new operational policy.
221      *
222      * @param body the body of policy
223      *
224      * @return the Response object containing the results of the API operation
225      */
226     @POST
227     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies")
228     @ApiOperation(value = "Create a new operational policy",
229             notes = "Client should provide entity body of the new operational policy",
230             authorizations = @Authorization(value = "basicAuth"), tags = {"Legacy Operational Policy",},
231             response = LegacyOperationalPolicy.class,
232             responseHeaders = {
233                     @ResponseHeader(name = "X-MinorVersion",
234                                     description = "Used to request or communicate a MINOR version back from the client"
235                                                 + " to the server, and from the server back to the client",
236                                     response = String.class),
237                     @ResponseHeader(name = "X-PatchVersion",
238                                     description = "Used only to communicate a PATCH version in a response for"
239                                                 + " troubleshooting purposes only, and will not be provided by"
240                                                 + " the client on request",
241                                     response = String.class),
242                     @ResponseHeader(name = "X-LatestVersion",
243                                     description = "Used only to communicate an API's latest version",
244                                     response = String.class),
245                     @ResponseHeader(name = "X-ONAP-RequestID",
246                                     description = "Used to track REST transactions for logging purpose",
247                                     response = UUID.class)
248             },
249             extensions = {
250                     @Extension(name = "interface info", properties = {
251                             @ExtensionProperty(name = "api-version", value = "1.0.0"),
252                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")
253                     })
254             })
255     @ApiResponses(value = {
256             @ApiResponse(code = 400, message = "Invalid Body"),
257             @ApiResponse(code = 401, message = "Authentication Error"),
258             @ApiResponse(code = 403, message = "Authorization Error"),
259             @ApiResponse(code = 406, message = "Not Acceptable Payload"),
260             @ApiResponse(code = 500, message = "Internal Server Error")
261         })
262     public Response createOperationalPolicy(
263             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId,
264             @ApiParam(value = "Entity body of policy", required = true) LegacyOperationalPolicy body) {
265
266         if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
267             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST,
268                     "/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies", toJson(body));
269         }
270
271         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
272             LegacyOperationalPolicy policy = operationalPolicyProvider.createOperationalPolicy(body);
273             return makeOkResponse(requestId, policy);
274         } catch (PfModelException | PfModelRuntimeException pfme) {
275             LOGGER.debug("POST /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/policies", pfme);
276             return makeErrorResponse(requestId, pfme);
277         }
278     }
279
280     /**
281      * Deletes the specified version of a particular operational policy.
282      *
283      * @param policyId the ID of specified policy
284      * @param policyVersion the version of specified policy
285      *
286      * @return the Response object containing the results of the API operation
287      */
288     @DELETE
289     @Path("/policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
290             + "policies/{policyId}/versions/{policyVersion}")
291     @ApiOperation(value = "Delete a particular version of a specified operational policy",
292             notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
293             authorizations = @Authorization(value = "basicAuth"), tags = {"Legacy Operational Policy",},
294             response = LegacyOperationalPolicy.class,
295             responseHeaders = {
296                 @ResponseHeader(name = "X-MinorVersion",
297                         description = "Used to request or communicate a MINOR version back from the client"
298                                 + " to the server, and from the server back to the client",
299                         response = String.class),
300                 @ResponseHeader(name = "X-PatchVersion",
301                         description = "Used only to communicate a PATCH version in a response for"
302                                 + " troubleshooting purposes only, and will not be provided by"
303                                 + " the client on request",
304                         response = String.class),
305                 @ResponseHeader(name = "X-LatestVersion",
306                         description = "Used only to communicate an API's latest version", response = String.class),
307                 @ResponseHeader(name = "X-ONAP-RequestID",
308                         description = "Used to track REST transactions for logging purpose", response = UUID.class)},
309             extensions = {@Extension(name = "interface info",
310                     properties = {@ExtensionProperty(name = "api-version", value = "1.0.0"),
311                         @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})
312     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),
313         @ApiResponse(code = 403, message = "Authorization Error"),
314         @ApiResponse(code = 404, message = "Resource Not Found"),
315         @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
316         @ApiResponse(code = 500, message = "Internal Server Error")})
317     public Response deleteSpecificVersionOfOperationalPolicy(
318             @PathParam("policyId") @ApiParam(value = "ID of policy", required = true) String policyId,
319             @PathParam("policyVersion") @ApiParam(value = "Version of policy", required = true) String policyVersion,
320             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
321
322         try (LegacyOperationalPolicyProvider operationalPolicyProvider = new LegacyOperationalPolicyProvider()) {
323             LegacyOperationalPolicy policy = operationalPolicyProvider.deleteOperationalPolicy(policyId, policyVersion);
324             return makeOkResponse(requestId, policy);
325         } catch (PfModelException | PfModelRuntimeException pfme) {
326             LOGGER.debug("DELETE /policytypes/onap.policies.controlloop.Operational/versions/1.0.0/"
327                     + "policies/{}/versions/{}", policyId, policyVersion, pfme);
328             return makeErrorResponse(requestId, pfme);
329         }
330     }
331 }