Merge "Add REST api to query policy status from PAP"
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyStatusControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest;
22
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiParam;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27 import io.swagger.annotations.Authorization;
28 import io.swagger.annotations.Extension;
29 import io.swagger.annotations.ExtensionProperty;
30 import io.swagger.annotations.ResponseHeader;
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.UUID;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.core.Response;
39 import org.onap.policy.common.utils.services.Registry;
40 import org.onap.policy.models.pap.concepts.PolicyStatus;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
42 import org.onap.policy.pap.main.PapConstants;
43 import org.onap.policy.pap.main.notification.PolicyNotifier;
44
45 /**
46  * Class to provide REST end points for PAP component to retrieve the status of deployed
47  * policies.
48  */
49 public class PolicyStatusControllerV1 extends PapRestControllerV1 {
50     private final PolicyNotifier notifier;
51
52     public PolicyStatusControllerV1() {
53         this.notifier = Registry.get(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class);
54     }
55
56     /**
57      * Queries status of all deployed policies.
58      *
59      * @param requestId request ID used in ONAP logging
60      * @return a response
61      */
62     // @formatter:off
63     @GET
64     @Path("policies/deployed")
65     @ApiOperation(value = "Queries status of all deployed policies",
66         notes = "Queries status of all deployed policies, returning success and failure counts of the PDPs",
67         responseContainer = "List", response = PolicyStatus.class,
68         tags = {"Policy Administration (PAP) API"},
69         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
70         responseHeaders = {
71             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
72                             response = String.class),
73             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
74                             response = String.class),
75             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
76                             response = String.class),
77             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
78                             response = UUID.class)},
79         extensions = {@Extension(name = EXTENSION_NAME,
80             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
81                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
82     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
83                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
84                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
85     // @formatter:on
86
87     public Response queryAllDeployedPolicies(
88                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
89
90         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
91                         .entity(notifier.getStatus()).build();
92     }
93
94
95     /**
96      * Queries status of specific deployed policies.
97      *
98      * @param requestId request ID used in ONAP logging
99      * @return a response
100      */
101     // @formatter:off
102     @GET
103     @Path("policies/deployed/{name}")
104     @ApiOperation(value = "Queries status of specific deployed policies",
105         notes = "Queries status of specific deployed policies, returning success and failure counts of the PDPs",
106         responseContainer = "List", response = PolicyStatus.class,
107         tags = {"Policy Administration (PAP) API"},
108         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
109         responseHeaders = {
110             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
111                             response = String.class),
112             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
113                             response = String.class),
114             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
115                             response = String.class),
116             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
117                             response = UUID.class)},
118         extensions = {@Extension(name = EXTENSION_NAME,
119             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
120                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
121     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
122                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
123                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
124     // @formatter:on
125
126     public Response queryDeployedPolicies(
127                     @ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
128                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
129
130         List<PolicyStatus> result = notifier.getStatus(name);
131         if (result.isEmpty()) {
132             return makeNotFoundResponse(requestId);
133
134         } else {
135             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
136                             .entity(result).build();
137         }
138     }
139
140
141     /**
142      * Queries status of a specific deployed policy.
143      *
144      * @param requestId request ID used in ONAP logging
145      * @return a response
146      */
147     // @formatter:off
148     @GET
149     @Path("policies/deployed/{name}/{version}")
150     @ApiOperation(value = "Queries status of a specific deployed policy",
151         notes = "Queries status of a specific deployed policy, returning success and failure counts of the PDPs",
152         response = PolicyStatus.class,
153         tags = {"Policy Administration (PAP) API"},
154         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
155         responseHeaders = {
156             @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
157                             response = String.class),
158             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
159                             response = String.class),
160             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
161                             response = String.class),
162             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
163                             response = UUID.class)},
164         extensions = {@Extension(name = EXTENSION_NAME,
165             properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
166                 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
167     @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
168                     @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
169                     @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
170     // @formatter:on
171
172     public Response queryDeployedPolicy(@ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
173                     @ApiParam(value = "Policy Version", required = true) @PathParam("version") String version,
174                     @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
175
176         ToscaPolicyIdentifier ident = new ToscaPolicyIdentifier(name, version);
177         Optional<PolicyStatus> result = notifier.getStatus(ident);
178         if (result.isPresent()) {
179             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
180                             .entity(result.get()).build();
181
182         } else {
183             return makeNotFoundResponse(requestId);
184         }
185     }
186
187
188     /**
189      * Makes a "not found" response.
190      *
191      * @param requestId request ID
192      * @return a "not found" response
193      */
194     private Response makeNotFoundResponse(final UUID requestId) {
195         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.NOT_FOUND)), requestId)
196                         .build();
197     }
198 }