1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 package org.oran.otf.api.handler;
\r
19 import org.oran.otf.api.Utilities;
\r
20 import org.oran.otf.api.Utilities.LogLevel;
\r
21 import org.oran.otf.common.model.TestExecution;
\r
22 import org.oran.otf.common.model.local.OTFApiResponse;
\r
23 import org.oran.otf.common.model.local.WorkflowRequest;
\r
24 import org.oran.otf.common.utility.gson.Convert;
\r
25 import org.oran.otf.common.utility.http.ResponseUtility;
\r
26 import com.fasterxml.jackson.core.type.TypeReference;
\r
27 import com.fasterxml.jackson.databind.ObjectMapper;
\r
28 import javax.ws.rs.core.MediaType;
\r
29 import javax.ws.rs.core.Response;
\r
30 import org.apache.http.HttpEntity;
\r
31 import org.apache.http.HttpResponse;
\r
32 import org.apache.http.conn.HttpHostConnectException;
\r
33 import org.apache.http.util.EntityUtils;
\r
34 import org.slf4j.Logger;
\r
35 import org.slf4j.LoggerFactory;
\r
36 import org.springframework.stereotype.Component;
\r
39 public class CamundaProcessExecutionHandler {
\r
40 private static final Logger logger =
\r
41 LoggerFactory.getLogger(CamundaProcessExecutionHandler.class);
\r
43 private CamundaProcessExecutionHandler() {
\r
44 // prevent instantiation
\r
47 public Response startProcessInstance(WorkflowRequest request) throws Exception {
\r
49 // if (!Utilities.Camunda.isCamundaOnline()) {
\r
50 // Utilities.Http.BuildResponse.internalServerErrorWithMessage(
\r
51 // "Unable to start process instance because the test control unit is
\r
55 // Read necessary environment variables - Avoiding using Spring dependencies (@Value)
\r
56 String host = System.getenv("otf.camunda.host");
\r
57 String path = System.getenv("otf.camunda.uri.execute-test");
\r
58 int port = Utilities.TryGetEnvironmentVariable("otf.camunda.port");
\r
60 if (!Utilities.isHostValid(host)) {
\r
61 logger.error(String.format("Host (%s) must use either the http or https protocol.", host));
\r
65 if (!Utilities.isPortValid(port)) {
\r
68 "Invalid port (%s) specified as environment variable 'otf.camunda.port'.",
\r
69 System.getenv("otf.camunda.port")));
\r
74 String postUrl = String.format("%s:%s/%s", host, port, path);
\r
76 // Send and store the response
\r
77 HttpResponse response = Utilities.Http.httpPostJsonUsingAAF(postUrl, request.toString());
\r
78 // Get the entity and attempt to convert it to a TestExecution object.
\r
79 HttpEntity entity = response.getEntity();
\r
80 String rawEntity = EntityUtils.toString(entity);
\r
81 ObjectMapper mapper = new ObjectMapper();
\r
82 OTFApiResponse otfApiResponse = mapper.readValue(rawEntity, OTFApiResponse.class);
\r
84 if (otfApiResponse.getStatusCode() == 400) {
\r
85 return Response.status(400)
\r
86 .type(MediaType.APPLICATION_JSON_TYPE)
\r
87 .entity(otfApiResponse.toString())
\r
91 String jsonMessage = otfApiResponse.getMessage();
\r
92 TestExecution testExecution =
\r
93 Convert.jsonToObject(jsonMessage, new TypeReference<TestExecution>() {});
\r
94 return Response.status(otfApiResponse.getStatusCode())
\r
95 .entity(testExecution.toString())
\r
98 } catch (HttpHostConnectException e) {
\r
99 return ResponseUtility.Build.serviceUnavailableWithMessage(e.getMessage());
\r
100 } catch (Exception e) {
\r
101 Utilities.printStackTrace(e, LogLevel.ERROR);
\r
102 return ResponseUtility.Build.internalServerError();
\r