--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.api.handler;\r
+\r
+import org.oran.otf.api.Utilities;\r
+import org.oran.otf.api.Utilities.LogLevel;\r
+import org.oran.otf.common.model.TestExecution;\r
+import org.oran.otf.common.model.local.OTFApiResponse;\r
+import org.oran.otf.common.model.local.WorkflowRequest;\r
+import org.oran.otf.common.utility.gson.Convert;\r
+import org.oran.otf.common.utility.http.ResponseUtility;\r
+import com.fasterxml.jackson.core.type.TypeReference;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import javax.ws.rs.core.MediaType;\r
+import javax.ws.rs.core.Response;\r
+import org.apache.http.HttpEntity;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.conn.HttpHostConnectException;\r
+import org.apache.http.util.EntityUtils;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class CamundaProcessExecutionHandler {\r
+ private static final Logger logger =\r
+ LoggerFactory.getLogger(CamundaProcessExecutionHandler.class);\r
+\r
+ private CamundaProcessExecutionHandler() {\r
+ // prevent instantiation\r
+ }\r
+\r
+ public Response startProcessInstance(WorkflowRequest request) throws Exception {\r
+ try {\r
+ // if (!Utilities.Camunda.isCamundaOnline()) {\r
+ // Utilities.Http.BuildResponse.internalServerErrorWithMessage(\r
+ // "Unable to start process instance because the test control unit is\r
+ // unavailable.");\r
+ // }\r
+\r
+ // Read necessary environment variables - Avoiding using Spring dependencies (@Value)\r
+ String host = System.getenv("otf.camunda.host");\r
+ String path = System.getenv("otf.camunda.uri.execute-test");\r
+ int port = Utilities.TryGetEnvironmentVariable("otf.camunda.port");\r
+\r
+ if (!Utilities.isHostValid(host)) {\r
+ logger.error(String.format("Host (%s) must use either the http or https protocol.", host));\r
+ return null;\r
+ }\r
+\r
+ if (!Utilities.isPortValid(port)) {\r
+ logger.error(\r
+ String.format(\r
+ "Invalid port (%s) specified as environment variable 'otf.camunda.port'.",\r
+ System.getenv("otf.camunda.port")));\r
+ return null;\r
+ }\r
+\r
+ // Form the URL\r
+ String postUrl = String.format("%s:%s/%s", host, port, path);\r
+\r
+ // Send and store the response\r
+ HttpResponse response = Utilities.Http.httpPostJsonUsingAAF(postUrl, request.toString());\r
+ // Get the entity and attempt to convert it to a TestExecution object.\r
+ HttpEntity entity = response.getEntity();\r
+ String rawEntity = EntityUtils.toString(entity);\r
+ ObjectMapper mapper = new ObjectMapper();\r
+ OTFApiResponse otfApiResponse = mapper.readValue(rawEntity, OTFApiResponse.class);\r
+\r
+ if (otfApiResponse.getStatusCode() == 400) {\r
+ return Response.status(400)\r
+ .type(MediaType.APPLICATION_JSON_TYPE)\r
+ .entity(otfApiResponse.toString())\r
+ .build();\r
+ }\r
+\r
+ String jsonMessage = otfApiResponse.getMessage();\r
+ TestExecution testExecution =\r
+ Convert.jsonToObject(jsonMessage, new TypeReference<TestExecution>() {});\r
+ return Response.status(otfApiResponse.getStatusCode())\r
+ .entity(testExecution.toString())\r
+ .build();\r
+\r
+ } catch (HttpHostConnectException e) {\r
+ return ResponseUtility.Build.serviceUnavailableWithMessage(e.getMessage());\r
+ } catch (Exception e) {\r
+ Utilities.printStackTrace(e, LogLevel.ERROR);\r
+ return ResponseUtility.Build.internalServerError();\r
+ }\r
+ }\r
+}\r