2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Huawei Technologies Co., Ltd. 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.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.apihandlerinfra;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.info.Info;
27 import io.swagger.v3.oas.annotations.media.ArraySchema;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.responses.ApiResponse;
31 import org.apache.http.HttpStatus;
32 import org.json.JSONObject;
33 import org.onap.so.apihandler.common.ErrorNumbers;
34 import org.onap.so.apihandler.common.ResponseBuilder;
35 import org.onap.so.apihandlerinfra.exceptions.ApiException;
36 import org.onap.so.db.request.beans.OrchestrationTask;
37 import org.onap.so.db.request.client.RequestsDbClient;
38 import org.onap.so.logger.ErrorCode;
39 import org.onap.so.logger.LoggingAnchor;
40 import org.onap.so.logger.MessageEnum;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
45 import javax.transaction.Transactional;
47 import javax.ws.rs.core.MediaType;
48 import javax.ws.rs.core.Response;
49 import java.util.HashMap;
50 import java.util.Iterator;
51 import java.util.List;
53 import static org.onap.so.apihandlerinfra.Constants.MSO_PROP_APIHANDLER_INFRA;
55 @Path("/onap/so/infra/orchestrationTasks")
57 info = @Info(title = "onap/so/infra/orchestrationTasks", description = "API Requests for Orchestration Task"))
59 public class OrchestrationTasks {
61 private static Logger logger = LoggerFactory.getLogger(OrchestrationTasks.class);
64 private MsoRequest msoRequest;
67 private CamundaRequestHandler camundaRequestHandler;
70 private RequestsDbClient requestsDbClient;
73 private ResponseBuilder builder;
75 private ObjectMapper mapper = new ObjectMapper();
78 @Path("/{version:[vV][4-7]}/")
79 @Operation(description = "Find All Orchestrated Task", responses = @ApiResponse(
80 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
81 @Produces(MediaType.APPLICATION_JSON)
83 public Response getAllOrchestrationTasks(@QueryParam("status") String status,
84 @PathParam("version") String version) {
85 List<OrchestrationTask> orchestrationTaskList = requestsDbClient.getAllOrchestrationTasks();
86 if (status != null && !status.isEmpty()) {
87 for (Iterator<OrchestrationTask> it = orchestrationTaskList.iterator(); it.hasNext();) {
88 OrchestrationTask task = it.next();
89 if (!status.equals(task.getStatus())) {
94 return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTaskList, version);
98 @Path("/{version:[vV][4-7]}/{taskId}")
99 @Operation(description = "Find Orchestrated Task for a given TaskId", responses = @ApiResponse(
100 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
101 @Produces(MediaType.APPLICATION_JSON)
103 public Response getOrchestrationTask(@PathParam("taskId") String taskId, @PathParam("version") String version)
104 throws ApiException {
106 OrchestrationTask orchestrationTask = requestsDbClient.getOrchestrationTask(taskId);
107 return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTask, version);
108 } catch (Exception e) {
109 logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
110 ErrorCode.AvailabilityError.getValue(),
111 "Exception while communciate with Request DB - Orchestration Task Lookup", e);
113 msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND, MsoException.ServiceException,
114 e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null, version);
120 @Path("/{version:[vV][4-7]}/")
121 @Operation(description = "Create an Orchestrated Task", responses = @ApiResponse(
122 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
123 @Produces(MediaType.APPLICATION_JSON)
125 public Response CreateOrchestrationTask(String requestJson, @PathParam("version") String version) {
127 OrchestrationTask orchestrationTask = mapper.readValue(requestJson, OrchestrationTask.class);
128 requestsDbClient.createOrchestrationTask(orchestrationTask);
129 return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTask, version);
130 } catch (Exception e) {
131 logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
132 ErrorCode.AvailabilityError.getValue(),
133 "Exception while communciate with Request DB - Orchestration Task Create", e);
134 Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
135 MsoException.ServiceException, e.getMessage(), ErrorNumbers.COULD_NOT_WRITE_TO_REQUESTS_DB, null,
142 @Path("/{version:[vV][4-7]}/{taskId}")
143 @Operation(description = "Update an Orchestrated Task", responses = @ApiResponse(
144 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
145 @Produces(MediaType.APPLICATION_JSON)
147 public Response UpdateOrchestrationTask(@PathParam("taskId") String taskId, String requestJson,
148 @PathParam("version") String version) {
150 OrchestrationTask orchestrationTask = requestsDbClient.getOrchestrationTask(taskId);
151 } catch (Exception e) {
152 logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
153 ErrorCode.AvailabilityError.getValue(),
154 "Exception while communciate with Request DB - Orchestration Task Update", e);
156 msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND, MsoException.ServiceException,
157 e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null, version);
162 OrchestrationTask orchestrationTask = mapper.readValue(requestJson, OrchestrationTask.class);
163 requestsDbClient.updateOrchestrationTask(taskId, orchestrationTask);
164 return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTask, version);
165 } catch (Exception e) {
166 logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
167 ErrorCode.AvailabilityError.getValue(),
168 "Exception while communciate with Request DB - Orchestration Task Update", e);
169 Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
170 MsoException.ServiceException, e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null,
177 @Path("/{version:[vV][4-7]}/{taskId}")
178 @Operation(description = "Delete an Orchestrated Task", responses = @ApiResponse(
179 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
180 @Produces(MediaType.APPLICATION_JSON)
182 public Response DeleteOrchestrationTask(@PathParam("taskId") String taskId, @PathParam("version") String version) {
184 OrchestrationTask orchestrationTask = requestsDbClient.getOrchestrationTask(taskId);
185 } catch (Exception e) {
186 logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
187 ErrorCode.AvailabilityError.getValue(),
188 "Exception while communciate with Request DB - Orchestration Task Delete", e);
190 msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND, MsoException.ServiceException,
191 e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null, version);
196 requestsDbClient.deleteOrchestrationTask(taskId);
197 return builder.buildResponse(HttpStatus.SC_OK, null, null, version);
198 } catch (Exception e) {
199 logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
200 ErrorCode.AvailabilityError.getValue(),
201 "Exception while communciate with Request DB - Orchestration Task Delete", e);
202 Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
203 MsoException.ServiceException, e.getMessage(), ErrorNumbers.COULD_NOT_WRITE_TO_REQUESTS_DB, null,