aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/OrchestrationTasks.java
blob: 7961cb08860f3d7f194b0942b84787a10e4fba67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.apihandlerinfra;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.apache.http.HttpStatus;
import org.json.JSONObject;
import org.onap.so.apihandler.common.ErrorNumbers;
import org.onap.so.apihandler.common.ResponseBuilder;
import org.onap.so.apihandlerinfra.exceptions.ApiException;
import org.onap.so.db.request.beans.OrchestrationTask;
import org.onap.so.db.request.client.RequestsDbClient;
import org.onap.so.logger.ErrorCode;
import org.onap.so.logger.LoggingAnchor;
import org.onap.so.logger.MessageEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.onap.so.apihandlerinfra.Constants.MSO_PROP_APIHANDLER_INFRA;

@Path("/onap/so/infra/orchestrationTasks")
@OpenAPIDefinition(
        info = @Info(title = "onap/so/infra/orchestrationTasks", description = "API Requests for Orchestration Task"))
@Component
public class OrchestrationTasks {

    private static Logger logger = LoggerFactory.getLogger(OrchestrationTasks.class);

    @Autowired
    private MsoRequest msoRequest;

    @Autowired
    private CamundaRequestHandler camundaRequestHandler;

    @Autowired
    private RequestsDbClient requestsDbClient;

    @Autowired
    private ResponseBuilder builder;

    private ObjectMapper mapper = new ObjectMapper();

    @GET
    @Path("/{version:[vV][4-7]}/")
    @Operation(description = "Find All Orchestrated Task", responses = @ApiResponse(
            content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
    @Produces(MediaType.APPLICATION_JSON)
    @Transactional
    public Response getAllOrchestrationTasks(@QueryParam("status") String status,
            @PathParam("version") String version) {
        List<OrchestrationTask> orchestrationTaskList = requestsDbClient.getAllOrchestrationTasks();
        if (status != null && !status.isEmpty()) {
            for (Iterator<OrchestrationTask> it = orchestrationTaskList.iterator(); it.hasNext();) {
                OrchestrationTask task = it.next();
                if (!status.equals(task.getStatus())) {
                    it.remove();
                }
            }
        }
        return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTaskList, version);
    }

    @GET
    @Path("/{version:[vV][4-7]}/{taskId}")
    @Operation(description = "Find Orchestrated Task for a given TaskId", responses = @ApiResponse(
            content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
    @Produces(MediaType.APPLICATION_JSON)
    @Transactional
    public Response getOrchestrationTask(@PathParam("taskId") String taskId, @PathParam("version") String version)
            throws ApiException {
        try {
            OrchestrationTask orchestrationTask = requestsDbClient.getOrchestrationTask(taskId);
            return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTask, version);
        } catch (Exception e) {
            logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
                    ErrorCode.AvailabilityError.getValue(),
                    "Exception while communciate with Request DB - Orchestration Task Lookup", e);
            Response response =
                    msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND, MsoException.ServiceException,
                            e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null, version);
            return response;
        }
    }

}