2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.api.main.rest;
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiParam;
29 import io.swagger.annotations.ApiResponse;
30 import io.swagger.annotations.ApiResponses;
31 import io.swagger.annotations.Authorization;
32 import io.swagger.annotations.BasicAuthDefinition;
33 import io.swagger.annotations.Info;
34 import io.swagger.annotations.SecurityDefinition;
35 import io.swagger.annotations.SwaggerDefinition;
36 import io.swagger.annotations.Tag;
37 import javax.ws.rs.Consumes;
38 import javax.ws.rs.DELETE;
39 import javax.ws.rs.GET;
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.QueryParam;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47 import org.onap.policy.api.main.rest.provider.HealthCheckProvider;
48 import org.onap.policy.api.main.rest.provider.PolicyProvider;
49 import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;
50 import org.onap.policy.api.main.rest.provider.StatisticsProvider;
51 import org.onap.policy.common.endpoints.report.HealthCheckReport;
52 import org.onap.policy.model.tosca.ToscaPolicy;
53 import org.onap.policy.model.tosca.ToscaPolicyList;
54 import org.onap.policy.model.tosca.ToscaPolicyType;
55 import org.onap.policy.model.tosca.ToscaPolicyTypeList;
58 * Class to provide REST API services.
60 @Path("/policy/api/v1")
61 @Api(value = "Policy Design API")
62 @Produces(MediaType.APPLICATION_JSON)
63 @Consumes(MediaType.APPLICATION_JSON)
64 @SwaggerDefinition(info = @Info(
65 description = "Policy Design API is publicly exposed for clients to Create/Read/Update/Delete"
66 + "policy types, policy type implementation and policies which can be recognized"
67 + "and executable by incorporated policy engines XACML, Drools and APEX. It is a"
68 + "standalone component running rest service that takes all policy design API calls"
69 + "from clients and then assign them to different API working functions. Besides"
70 + "that, API is also exposed for clients to retrieve healthcheck status of this API"
71 + "rest service and the statistics report including the counters of API invocation.",
72 version = "v1.0", title = "Policy Design"),
73 consumes = { MediaType.APPLICATION_JSON }, produces = { MediaType.APPLICATION_JSON },
74 schemes = { SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS },
75 tags = { @Tag(name = "policy-api", description = "Policy API Service Operations") },
76 securityDefinition = @SecurityDefinition(basicAuthDefinitions = { @BasicAuthDefinition(key = "basicAuth") }))
77 public class ApiRestController {
80 * Retrieves the healthcheck status of the API component.
82 * @return the Response object containing the results of the API operation
86 @ApiOperation(value = "Perform a system healthcheck",
87 notes = "Returns healthy status of the Policy API component",
88 response = HealthCheckReport.class,
89 authorizations = @Authorization(value = "basicAuth"),
90 tags = { "HealthCheck", })
91 @ApiResponses(value = {
92 @ApiResponse(code = 401, message = "Authentication Error"),
93 @ApiResponse(code = 403, message = "Authorization Error"),
94 @ApiResponse(code = 500, message = "Internal Server Error")
96 public Response getHealthCheck() {
97 return Response.status(Response.Status.OK).entity(new HealthCheckProvider().performHealthCheck()).build();
101 * Retrieves the statistics report of the API component.
103 * @return the Response object containing the results of the API operation
107 @ApiOperation(value = "Retrieve current statistics",
108 notes = "Returns current statistics including the counters of API invocation",
109 response = StatisticsReport.class,
110 authorizations = @Authorization(value = "basicAuth"),
111 tags = { "Statistics", })
112 @ApiResponses(value = {
113 @ApiResponse(code = 401, message = "Authentication Error"),
114 @ApiResponse(code = 403, message = "Authorization Error"),
115 @ApiResponse(code = 500, message = "Internal Server Error")
117 public Response getStatistics() {
118 return Response.status(Response.Status.OK).entity(new StatisticsProvider().fetchCurrentStatistics()).build();
122 * Retrieves all available policy types.
124 * @return the Response object containing the results of the API operation
127 @Path("/policytypes")
128 @ApiOperation(value = "Retrieve existing policy types",
129 notes = "Returns a list of existing policy types stored in Policy Framework",
130 response = ToscaPolicyTypeList.class,
131 authorizations = @Authorization(value = "basicAuth"),
132 tags = { "PolicyType", })
133 @ApiResponses(value = {
134 @ApiResponse(code = 401, message = "Authentication Error"),
135 @ApiResponse(code = 403, message = "Authorization Error"),
136 @ApiResponse(code = 500, message = "Internal Server Error")
138 public Response getAllPolicyTypes() {
139 return Response.status(Response.Status.OK)
140 .entity(new PolicyTypeProvider().fetchPolicyTypes(null, null)).build();
144 * Retrieves all versions of a particular policy type.
146 * @param policyTypeId the ID of specified policy type
148 * @return the Response object containing the results of the API operation
151 @Path("/policytypes/{policyTypeId}")
152 @ApiOperation(value = "Retrieve all available versions of a policy type",
153 notes = "Returns a list of all available versions for the specified policy type",
154 response = ToscaPolicyTypeList.class,
155 authorizations = @Authorization(value = "basicAuth"),
156 tags = { "PolicyType", })
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")
163 public Response getAllVersionsOfPolicyType(
164 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId) {
165 return Response.status(Response.Status.OK)
166 .entity(new PolicyTypeProvider().fetchPolicyTypes(policyTypeId, null)).build();
170 * Retrieves specified version of a particular policy type.
172 * @param policyTypeId the ID of specified policy type
173 * @param versionId the version of specified policy type
175 * @return the Response object containing the results of the API operation
178 @Path("/policytypes/{policyTypeId}/versions/{versionId}")
179 @ApiOperation(value = "Retrieve one particular version of a policy type",
180 notes = "Returns a particular version for the specified policy type",
181 response = ToscaPolicyTypeList.class,
182 authorizations = @Authorization(value = "basicAuth"),
183 tags = { "PolicyType", })
184 @ApiResponses(value = {
185 @ApiResponse(code = 401, message = "Authentication Error"),
186 @ApiResponse(code = 403, message = "Authorization Error"),
187 @ApiResponse(code = 404, message = "Resource Not Found"),
188 @ApiResponse(code = 500, message = "Internal Server Error")
190 public Response getSpecificVersionOfPolicyType(
191 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
192 @PathParam("versionId") @ApiParam("ID of version") String versionId) {
193 return Response.status(Response.Status.OK)
194 .entity(new PolicyTypeProvider().fetchPolicyTypes(policyTypeId, versionId)).build();
198 * Creates a new policy type.
200 * @param body the body of policy type following TOSCA definition
202 * @return the Response object containing the results of the API operation
205 @Path("/policytypes")
206 @ApiOperation(value = "Create a new policy type",
207 notes = "Client should provide TOSCA body of the new policy type",
208 authorizations = @Authorization(value = "basicAuth"),
209 tags = { "PolicyType", })
210 @ApiResponses(value = {
211 @ApiResponse(code = 400, message = "Invalid Body"),
212 @ApiResponse(code = 401, message = "Authentication Error"),
213 @ApiResponse(code = 403, message = "Authorization Error"),
214 @ApiResponse(code = 500, message = "Internal Server Error")
216 public Response createPolicyType(ToscaPolicyType body) {
217 return Response.status(Response.Status.OK).entity(new PolicyTypeProvider().createPolicyType(body)).build();
221 * Deletes all versions of a particular policy type.
223 * @param policyTypeId the ID of specified policy type
225 * @return the Response object containing the results of the API operation
228 @Path("/policytypes/{policyTypeId}")
229 @ApiOperation(value = "Delete all versions of a policy type",
230 notes = "Rule 1: pre-defined policy types cannot be deleted;"
231 + "Rule 2: policy types that are in use (parameterized by a TOSCA policy) cannot be deleted."
232 + "The parameterizing TOSCA policies must be deleted first;",
233 authorizations = @Authorization(value = "basicAuth"),
234 tags = { "PolicyType", })
235 @ApiResponses(value = {
236 @ApiResponse(code = 401, message = "Authentication Error"),
237 @ApiResponse(code = 403, message = "Authorization Error"),
238 @ApiResponse(code = 404, message = "Resource Not Found"),
239 @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
240 @ApiResponse(code = 500, message = "Internal Server Error")
242 public Response deleteAllVersionsOfPolicyType(
243 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId) {
244 return Response.status(Response.Status.OK)
245 .entity(new PolicyTypeProvider().deletePolicyTypes(policyTypeId, null)).build();
249 * Deletes specified version of a particular policy type.
251 * @param policyTypeId the ID of specified policy type
252 * @param versionId the version of specified policy type
254 * @return the Response object containing the results of the API operation
257 @Path("/policytypes/{policyTypeId}/versions/{versionId}")
258 @ApiOperation(value = "Delete one version of a policy type",
259 notes = "Rule 1: pre-defined policy types cannot be deleted;"
260 + "Rule 2: policy types that are in use (parameterized by a TOSCA policy) cannot be deleted."
261 + "The parameterizing TOSCA policies must be deleted first;",
262 authorizations = @Authorization(value = "basicAuth"),
263 tags = { "PolicyType", })
264 @ApiResponses(value = {
265 @ApiResponse(code = 401, message = "Authentication Error"),
266 @ApiResponse(code = 403, message = "Authorization Error"),
267 @ApiResponse(code = 404, message = "Resource Not Found"),
268 @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
269 @ApiResponse(code = 500, message = "Internal Server Error")
271 public Response deleteSpecificVersionOfPolicyType(
272 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
273 @PathParam("versionId") @ApiParam("ID of version") String versionId) {
274 return Response.status(Response.Status.OK)
275 .entity(new PolicyTypeProvider().deletePolicyTypes(policyTypeId, versionId)).build();
279 * Retrieves all versions of a particular policy.
281 * @param policyTypeId the ID of specified policy type
282 * @param policyTypeVersion the version of specified policy type
284 * @return the Response object containing the results of the API operation
287 @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")
288 @ApiOperation(value = "Retrieve all versions of a policy created for a particular policy type version",
289 notes = "Returns a list of all versions of specified policy created for the specified policy type version",
290 response = ToscaPolicyList.class,
291 authorizations = @Authorization(value = "basicAuth"),
292 tags = { "Policy", })
293 @ApiResponses(value = {
294 @ApiResponse(code = 401, message = "Authentication Error"),
295 @ApiResponse(code = 403, message = "Authorization Error"),
296 @ApiResponse(code = 404, message = "Resource Not Found"),
297 @ApiResponse(code = 500, message = "Internal Server Error")
299 public Response getAllPolicies(
300 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
301 @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion) {
302 return Response.status(Response.Status.OK)
303 .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion, null, null)).build();
307 * Retrieves all versions of a particular policy.
309 * @param policyTypeId the ID of specified policy type
310 * @param policyTypeVersion the version of specified policy type
311 * @param policyId the ID of specified policy
313 * @return the Response object containing the results of the API operation
316 @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}")
317 @ApiOperation(value = "Retrieve all version details of a policy created for a particular policy type version",
318 notes = "Returns a list of all version details of the specified policy",
319 response = ToscaPolicyList.class,
320 authorizations = @Authorization(value = "basicAuth"),
321 tags = { "Policy", })
322 @ApiResponses(value = {
323 @ApiResponse(code = 401, message = "Authentication Error"),
324 @ApiResponse(code = 403, message = "Authorization Error"),
325 @ApiResponse(code = 404, message = "Resource Not Found"),
326 @ApiResponse(code = 500, message = "Internal Server Error")
328 public Response getAllVersionsOfPolicy(
329 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
330 @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
331 @PathParam("policyId") @ApiParam("ID of policy") String policyId) {
332 return Response.status(Response.Status.OK)
333 .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion, policyId, null)).build();
337 * Retrieves the specified version of a particular policy.
339 * @param policyTypeId the ID of specified policy type
340 * @param policyTypeVersion the version of specified policy type
341 * @param policyId the ID of specified policy
342 * @param policyVersion the version of specified policy
344 * @return the Response object containing the results of the API operation
347 @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")
348 @ApiOperation(value = "Retrieve one version of a policy created for a particular policy type version",
349 notes = "Returns a particular version of specified policy created for the specified policy type version",
350 response = ToscaPolicyList.class,
351 authorizations = @Authorization(value = "basicAuth"),
352 tags = { "Policy", })
353 @ApiResponses(value = {
354 @ApiResponse(code = 401, message = "Authentication Error"),
355 @ApiResponse(code = 403, message = "Authorization Error"),
356 @ApiResponse(code = 404, message = "Resource Not Found"),
357 @ApiResponse(code = 500, message = "Internal Server Error")
359 public Response getSpecificVersionOfPolicy(
360 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
361 @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
362 @PathParam("policyId") @ApiParam("ID of policy") String policyId,
363 @PathParam("policyVersion") @ApiParam("ID of policy version") String policyVersion) {
364 return Response.status(Response.Status.OK)
365 .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion,
366 policyId, policyVersion)).build();
370 * Retrieves either latest or deployed version of a particular policy depending on query parameter.
372 * @param policyTypeId the ID of specified policy type
373 * @param policyTypeVersion the version of specified policy type
374 * @param policyId the ID of specified policy
376 * @return the Response object containing the results of the API operation
379 @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions")
380 @ApiOperation(value = "Retrieve either latest or deployed version of a particular policy depending on query param",
381 notes = "Returns either latest or deployed version of specified policy depending on query param",
382 response = ToscaPolicyList.class,
383 authorizations = @Authorization(value = "basicAuth"),
384 tags = { "Policy", })
385 @ApiResponses(value = {
386 @ApiResponse(code = 401, message = "Authentication Error"),
387 @ApiResponse(code = 403, message = "Authorization Error"),
388 @ApiResponse(code = 404, message = "Resource Not Found"),
389 @ApiResponse(code = 500, message = "Internal Server Error")
391 public Response getEitherLatestOrDeployedVersionOfPolicy(
392 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
393 @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
394 @PathParam("policyId") @ApiParam("ID of policy") String policyId,
395 @QueryParam("type") @ApiParam("Type of version that can only be 'latest' or 'deployed'") String type) {
396 return Response.status(Response.Status.OK)
397 .entity(new PolicyProvider().fetchPolicies(policyTypeId, policyTypeVersion, policyId, type)).build();
401 * Creates a new policy for a particular policy type and version.
403 * @param policyTypeId the ID of specified policy type
404 * @param policyTypeVersion the version of specified policy type
405 * @param body the body of policy following TOSCA definition
407 * @return the Response object containing the results of the API operation
410 @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies")
411 @ApiOperation(value = "Create a new policy for a policy type version",
412 notes = "Client should provide TOSCA body of the new policy",
413 authorizations = @Authorization(value = "basicAuth"),
414 tags = { "Policy", })
415 @ApiResponses(value = {
416 @ApiResponse(code = 400, message = "Invalid Body"),
417 @ApiResponse(code = 401, message = "Authentication Error"),
418 @ApiResponse(code = 403, message = "Authorization Error"),
419 @ApiResponse(code = 404, message = "Resource Not Found"),
420 @ApiResponse(code = 500, message = "Internal Server Error")
422 public Response createPolicy(
423 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
424 @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
426 return Response.status(Response.Status.OK)
427 .entity(new PolicyProvider().createPolicy(policyTypeId, policyTypeVersion, body)).build();
431 * Deletes all versions of a particular policy.
433 * @param policyTypeId the ID of specified policy type
434 * @param policyTypeVersion the version of specified policy type
435 * @param policyId the ID of specified policy
437 * @return the Response object containing the results of the API operation
440 @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}")
441 @ApiOperation(value = "Delete all versions of a policy",
442 notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
443 authorizations = @Authorization(value = "basicAuth"),
444 tags = { "Policy", })
445 @ApiResponses(value = {
446 @ApiResponse(code = 401, message = "Authentication Error"),
447 @ApiResponse(code = 403, message = "Authorization Error"),
448 @ApiResponse(code = 404, message = "Resource Not Found"),
449 @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
450 @ApiResponse(code = 500, message = "Internal Server Error")
452 public Response deleteAllVersionsOfPolicy(
453 @PathParam("policyTypeId") @ApiParam("ID of policy type") String policyTypeId,
454 @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
455 @PathParam("policyId") @ApiParam("ID of policy") String policyId) {
456 return Response.status(Response.Status.OK)
457 .entity(new PolicyProvider().deletePolicies(policyTypeId, policyTypeVersion, policyId, null)).build();
461 * Deletes the specified version of a particular policy.
463 * @param policyTypeId the ID of specified policy type
464 * @param policyTypeVersion the version of specified policy type
465 * @param policyId the ID of specified policy
466 * @param policyVersion the version of specified policy
468 * @return the Response object containing the results of the API operation
471 @Path("/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies/{policyId}/versions/{policyVersion}")
472 @ApiOperation(value = "Delete a particular version of a policy",
473 notes = "Rule: the version that has been deployed in PDP group(s) cannot be deleted",
474 authorizations = @Authorization(value = "basicAuth"),
475 tags = { "Policy", })
476 @ApiResponses(value = {
477 @ApiResponse(code = 401, message = "Authentication Error"),
478 @ApiResponse(code = 403, message = "Authorization Error"),
479 @ApiResponse(code = 404, message = "Resource Not Found"),
480 @ApiResponse(code = 409, message = "Delete Conflict, Rule Violation"),
481 @ApiResponse(code = 500, message = "Internal Server Error")
483 public Response deleteSpecificVersionOfPolicy(
484 @PathParam("policyTypeId") @ApiParam("PolicyType ID") String policyTypeId,
485 @PathParam("policyTypeVersion") @ApiParam("ID of policy type version") String policyTypeVersion,
486 @PathParam("policyId") @ApiParam("ID of policy") String policyId,
487 @PathParam("policyVersion") @ApiParam("ID of policy version") String policyVersion) {
488 return Response.status(Response.Status.OK)
489 .entity(new PolicyProvider().deletePolicies(policyTypeId, policyTypeVersion,
490 policyId, policyVersion)).build();