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