2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
\r
6 * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
\r
7 * Modifications Copyright (C) 2020-2023 Nordix Foundation.
\r
8 * Modifications Copyright (C) 2020-2023 Bell Canada. All rights reserved.
\r
9 * ================================================================================
\r
10 * Licensed under the Apache License, Version 2.0 (the "License");
\r
11 * you may not use this file except in compliance with the License.
\r
12 * You may obtain a copy of the License at
\r
14 * http://www.apache.org/licenses/LICENSE-2.0
\r
16 * Unless required by applicable law or agreed to in writing, software
\r
17 * distributed under the License is distributed on an "AS IS" BASIS,
\r
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
19 * See the License for the specific language governing permissions and
\r
20 * limitations under the License.
\r
22 * SPDX-License-Identifier: Apache-2.0
\r
23 * ============LICENSE_END=========================================================
\r
26 package org.onap.policy.api.main.rest;
\r
28 import jakarta.ws.rs.core.Response.Status;
\r
29 import java.util.List;
\r
30 import java.util.UUID;
\r
31 import lombok.RequiredArgsConstructor;
\r
32 import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
\r
33 import org.onap.policy.api.main.rest.genapi.PolicyDesignApi;
\r
34 import org.onap.policy.api.main.rest.provider.healthcheck.HealthCheckProvider;
\r
35 import org.onap.policy.api.main.service.ToscaServiceTemplateService;
\r
36 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
\r
37 import org.onap.policy.common.endpoints.report.HealthCheckReport;
\r
38 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
\r
39 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
\r
40 import org.onap.policy.models.base.PfModelException;
\r
41 import org.onap.policy.models.base.PfModelRuntimeException;
\r
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
43 import org.springframework.context.annotation.Profile;
\r
44 import org.springframework.http.ResponseEntity;
\r
45 import org.springframework.web.bind.annotation.RestController;
\r
48 * Class to provide REST API services.
\r
50 * @author Chenfei Gao (cgao@research.att.com)
\r
53 @RequiredArgsConstructor
\r
55 public class ApiRestController extends CommonRestController implements PolicyDesignApi {
\r
57 private final ToscaServiceTemplateService toscaServiceTemplateService;
\r
58 private final HealthCheckProvider healthCheckProvider;
\r
61 * Retrieves the healthcheck status of the API component.
\r
63 * @return the Response object containing the results of the API operation
\r
66 public ResponseEntity<HealthCheckReport> getHealthCheck(UUID requestId) {
\r
67 final var report = healthCheckProvider.performHealthCheck();
\r
68 return makeResponse(requestId, report, report.getCode());
\r
72 * Retrieves all available policy types.
\r
74 * @return the Response object containing the results of the API operation
\r
77 public ResponseEntity<ToscaServiceTemplate> getAllPolicyTypes(UUID requestId) {
\r
79 ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(null, null);
\r
80 return makeOkResponse(requestId, serviceTemplate);
\r
81 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
82 final var msg = "GET /policytypes";
\r
83 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
88 * Retrieves all versions of a particular policy type.
\r
90 * @param policyTypeId the ID of specified policy type
\r
91 * @return the Response object containing the results of the API operation
\r
94 public ResponseEntity<ToscaServiceTemplate> getAllVersionsOfPolicyType(
\r
95 String policyTypeId,
\r
98 ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(policyTypeId, null);
\r
99 return makeOkResponse(requestId, serviceTemplate);
\r
100 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
101 var msg = String.format("GET /policytypes/%s", policyTypeId);
\r
102 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
107 * Retrieves specified version of a particular policy type.
\r
109 * @param policyTypeId the ID of specified policy type
\r
110 * @param versionId the version of specified policy type
\r
111 * @return the Response object containing the results of the API operation
\r
114 public ResponseEntity<ToscaServiceTemplate> getSpecificVersionOfPolicyType(
\r
115 String policyTypeId,
\r
119 ToscaServiceTemplate serviceTemplate =
\r
120 toscaServiceTemplateService.fetchPolicyTypes(policyTypeId, versionId);
\r
121 return makeOkResponse(requestId, serviceTemplate);
\r
122 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
123 var msg = String.format("GET /policytypes/%s/versions/%s", policyTypeId, versionId);
\r
124 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
129 * Retrieves latest version of a particular policy type.
\r
131 * @param policyTypeId the ID of specified policy type
\r
132 * @return the Response object containing the results of the API operation
\r
135 public ResponseEntity<ToscaServiceTemplate> getLatestVersionOfPolicyType(
\r
136 String policyTypeId,
\r
139 ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchLatestPolicyTypes(policyTypeId);
\r
140 return makeOkResponse(requestId, serviceTemplate);
\r
141 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
142 var msg = String.format("GET /policytypes/%s/versions/latest", policyTypeId);
\r
143 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
148 * Creates a new policy type.
\r
150 * @param body the body of policy type following TOSCA definition
\r
151 * @return the Response object containing the results of the API operation
\r
154 public ResponseEntity<ToscaServiceTemplate> createPolicyType(
\r
155 ToscaServiceTemplate body,
\r
157 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
\r
158 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policytypes", toJson(body));
\r
162 ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicyType(body);
\r
163 return makeOkResponse(requestId, serviceTemplate);
\r
164 } catch (PfModelRuntimeException pfme) {
\r
165 final var msg = "POST /policytypes";
\r
166 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
167 } catch (InterruptedException e) {
\r
168 Thread.currentThread().interrupt();
\r
169 throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);
\r
176 * Deletes specified version of a particular policy type.
\r
178 * @param policyTypeId the ID of specified policy type
\r
179 * @param versionId the version of specified policy type
\r
180 * @return the Response object containing the results of the API operation
\r
183 public ResponseEntity<ToscaServiceTemplate> deleteSpecificVersionOfPolicyType(
\r
184 String policyTypeId,
\r
189 ToscaServiceTemplate serviceTemplate =
\r
190 toscaServiceTemplateService.deletePolicyType(policyTypeId, versionId);
\r
191 return makeOkResponse(requestId, serviceTemplate);
\r
192 } catch (PfModelRuntimeException pfme) {
\r
193 var msg = String.format("DELETE /policytypes/%s/versions/%s", policyTypeId, versionId);
\r
194 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
195 } catch (InterruptedException e) {
\r
196 Thread.currentThread().interrupt();
\r
197 throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);
\r
204 * Retrieves all versions of a particular policy.
\r
206 * @param policyTypeId the ID of specified policy type
\r
207 * @param policyTypeVersion the version of specified policy type
\r
208 * @return the Response object containing the results of the API operation
\r
211 public ResponseEntity<ToscaServiceTemplate> getAllPolicies(
\r
212 String policyTypeId,
\r
213 String policyTypeVersion,
\r
214 PolicyFetchMode mode,
\r
217 ToscaServiceTemplate serviceTemplate =
\r
218 toscaServiceTemplateService.fetchPolicies(policyTypeId, policyTypeVersion, null, null, mode);
\r
219 return makeOkResponse(requestId, serviceTemplate);
\r
220 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
221 var msg = String.format("GET /policytypes/%s/versions/%s/policies", policyTypeId, policyTypeVersion);
\r
222 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
227 * Retrieves all versions of a particular policy.
\r
229 * @param policyTypeId the ID of specified policy type
\r
230 * @param policyTypeVersion the version of specified policy type
\r
231 * @param policyId the ID of specified policy
\r
232 * @return the Response object containing the results of the API operation
\r
235 public ResponseEntity<ToscaServiceTemplate> getAllVersionsOfPolicy(
\r
237 String policyTypeId,
\r
238 String policyTypeVersion,
\r
239 PolicyFetchMode mode,
\r
242 ToscaServiceTemplate serviceTemplate =
\r
243 toscaServiceTemplateService.fetchPolicies(policyTypeId, policyTypeVersion, policyId, null, mode);
\r
244 return makeOkResponse(requestId, serviceTemplate);
\r
245 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
246 var msg = String.format("/policytypes/%s/versions/%s/policies/%s",
\r
247 policyTypeId, policyTypeVersion, policyId);
\r
248 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
253 * Retrieves the specified version of a particular policy.
\r
255 * @param policyTypeId the ID of specified policy type
\r
256 * @param policyTypeVersion the version of specified policy type
\r
257 * @param policyId the ID of specified policy
\r
258 * @param policyVersion the version of specified policy
\r
259 * @return the Response object containing the results of the API operation
\r
262 public ResponseEntity<ToscaServiceTemplate> getSpecificVersionOfPolicy(
\r
264 String policyTypeId,
\r
265 String policyTypeVersion,
\r
266 String policyVersion,
\r
267 PolicyFetchMode mode,
\r
270 ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService
\r
271 .fetchPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode);
\r
272 return makeOkResponse(requestId, serviceTemplate);
\r
273 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
274 var msg = String.format("GET /policytypes/%s/versions/%s/policies/%s/versions/%s",
\r
275 policyTypeId, policyTypeVersion, policyId, policyVersion);
\r
276 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
281 * Retrieves the latest version of a particular policy.
\r
283 * @param policyTypeId the ID of specified policy type
\r
284 * @param policyTypeVersion the version of specified policy type
\r
285 * @param policyId the ID of specified policy
\r
286 * @return the Response object containing the results of the API operation
\r
289 public ResponseEntity<ToscaServiceTemplate> getLatestVersionOfPolicy(
\r
291 String policyTypeId,
\r
292 String policyTypeVersion,
\r
293 PolicyFetchMode mode,
\r
296 ToscaServiceTemplate serviceTemplate =
\r
297 toscaServiceTemplateService.fetchLatestPolicies(policyTypeId, policyTypeVersion, policyId, mode);
\r
298 return makeOkResponse(requestId, serviceTemplate);
\r
299 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
300 var msg = String.format("GET /policytypes/%s/versions/%s/policies/%s/versions/latest",
\r
301 policyTypeId, policyTypeVersion, policyId);
\r
302 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
307 * Creates a new policy for a particular policy type and version.
\r
309 * @param policyTypeId the ID of specified policy type
\r
310 * @param policyTypeVersion the version of specified policy type
\r
311 * @param body the body of policy following TOSCA definition
\r
312 * @return the Response object containing the results of the API operation
\r
315 public ResponseEntity<ToscaServiceTemplate> createPolicy(
\r
316 String policyTypeId,
\r
317 String policyTypeVersion,
\r
318 ToscaServiceTemplate body,
\r
320 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
\r
321 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST,
\r
322 "/policytypes/" + policyTypeId + "/versions/" + policyTypeVersion + "/policies", toJson(body));
\r
326 ToscaServiceTemplate serviceTemplate =
\r
327 toscaServiceTemplateService.createPolicy(policyTypeId, policyTypeVersion, body);
\r
328 return makeOkResponse(requestId, serviceTemplate);
\r
329 } catch (PfModelRuntimeException pfme) {
\r
330 var msg = String.format("POST /policytypes/%s/versions/%s/policies", policyTypeId, policyTypeVersion);
\r
331 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
332 } catch (InterruptedException e) {
\r
333 Thread.currentThread().interrupt();
\r
334 throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);
\r
341 * Deletes the specified version of a particular policy.
\r
343 * @param policyTypeId the ID of specified policy type
\r
344 * @param policyTypeVersion the version of specified policy type
\r
345 * @param policyId the ID of specified policy
\r
346 * @param policyVersion the version of specified policy
\r
347 * @return the Response object containing the results of the API operation
\r
350 public ResponseEntity<ToscaServiceTemplate> deleteSpecificVersionOfPolicy(
\r
351 String policyTypeId,
\r
352 String policyTypeVersion,
\r
354 String policyVersion,
\r
358 ToscaServiceTemplate serviceTemplate =
\r
359 toscaServiceTemplateService.deletePolicy(policyTypeId, policyTypeVersion, policyId, policyVersion);
\r
360 return makeOkResponse(requestId, serviceTemplate);
\r
361 } catch (PfModelRuntimeException pfme) {
\r
362 var msg = String.format("DELETE /policytypes/%s/versions/%s/policies/%s/versions/%s",
\r
363 policyTypeId, policyTypeVersion, policyId, policyVersion);
\r
364 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
365 } catch (InterruptedException e) {
\r
366 Thread.currentThread().interrupt();
\r
367 throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);
\r
374 * Retrieves all the available policies.
\r
376 * @return the Response object containing the results of the API operation
\r
379 public ResponseEntity<ToscaServiceTemplate> getPolicies(
\r
380 PolicyFetchMode mode,
\r
383 ToscaServiceTemplate serviceTemplate =
\r
384 toscaServiceTemplateService.fetchPolicies(null, null, null, null, mode);
\r
385 return makeOkResponse(requestId, serviceTemplate);
\r
386 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
387 final var msg = "GET /policies/ --";
\r
388 if (pfme.getErrorResponse().getResponseCode().equals(Status.NOT_FOUND)) {
\r
389 pfme.getErrorResponse().setErrorMessage(ERROR_MESSAGE_NO_POLICIES_FOUND);
\r
390 pfme.getErrorResponse().setErrorDetails(List.of(ERROR_MESSAGE_NO_POLICIES_FOUND));
\r
392 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
397 * Retrieves the specified version of a particular policy.
\r
399 * @param policyId the Name of specified policy
\r
400 * @param policyVersion the version of specified policy
\r
401 * @return the Response object containing the results of the API operation
\r
404 public ResponseEntity<ToscaServiceTemplate> getSpecificPolicy(
\r
406 String policyVersion,
\r
407 PolicyFetchMode mode,
\r
410 ToscaServiceTemplate serviceTemplate =
\r
411 toscaServiceTemplateService.fetchPolicies(null, null, policyId, policyVersion, mode);
\r
412 return makeOkResponse(requestId, serviceTemplate);
\r
413 } catch (PfModelException | PfModelRuntimeException pfme) {
\r
414 var msg = String.format("GET /policies/%s/versions/%s", policyId, policyVersion);
\r
415 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
420 * Creates one or more new policies in one call.
\r
422 * @param body the body of policy following TOSCA definition
\r
423 * @return the Response object containing the results of the API operation
\r
426 public ResponseEntity<ToscaServiceTemplate> createPolicies(
\r
427 ToscaServiceTemplate body,
\r
429 if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
\r
430 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policies", toJson(body));
\r
434 ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicies(body);
\r
435 return makeOkResponse(requestId, serviceTemplate);
\r
436 } catch (PfModelRuntimeException pfme) {
\r
437 final var msg = "POST /policies";
\r
438 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
439 } catch (InterruptedException e) {
\r
440 Thread.currentThread().interrupt();
\r
441 throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);
\r
448 * Deletes the specified version of a particular policy.
\r
450 * @param policyId the ID of specified policy
\r
451 * @param policyVersion the version of specified policy
\r
452 * @return the Response object containing the results of the API operation
\r
455 public ResponseEntity<ToscaServiceTemplate> deleteSpecificPolicy(
\r
457 String policyVersion,
\r
461 ToscaServiceTemplate serviceTemplate =
\r
462 toscaServiceTemplateService.deletePolicy(null, null, policyId, policyVersion);
\r
463 return makeOkResponse(requestId, serviceTemplate);
\r
464 } catch (PfModelRuntimeException pfme) {
\r
465 var msg = String.format("DELETE /policies/%s/versions/%s", policyId, policyVersion);
\r
466 throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
\r
467 } catch (InterruptedException e) {
\r
468 Thread.currentThread().interrupt();
\r
469 throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);
\r