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);