2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest;
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;
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;
58 * Class to provide legacy REST API services.
60 * @author Chenfei Gao (cgao@research.att.com)
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 {
68 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyApiRestController.class);
71 * Retrieves the latest version of a particular operational policy.
73 * @param policyId the ID of specified operational policy
75 * @return the Response object containing the results of the API operation
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,
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) {
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);
119 * Retrieves the specified version of a particular operational policy.
121 * @param policyId the ID of specified policy
122 * @param policyVersion the version of specified policy
124 * @return the Response object containing the results of the API operation
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,
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) {
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);
170 * Retrieves deployed versions of a particular operational policy in PDP groups.
172 * @param policyId the ID of specified policy
174 * @return the Response object containing the results of the API operation
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",
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) {
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);
220 * Creates a new operational policy.
222 * @param body the body of policy
224 * @return the Response object containing the results of the API operation
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,
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)
250 @Extension(name = "interface info", properties = {
251 @ExtensionProperty(name = "api-version", value = "1.0.0"),
252 @ExtensionProperty(name = "last-mod-release", value = "Dublin")
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")
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) {
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));
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);
281 * Deletes the specified version of a particular operational policy.
283 * @param policyId the ID of specified policy
284 * @param policyVersion the version of specified policy
286 * @return the Response object containing the results of the API operation
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,
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) {
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);