added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / api / handler / CamundaProcessExecutionHandler.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 package org.oran.otf.api.handler;\r
18 \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
37 \r
38 @Component\r
39 public class CamundaProcessExecutionHandler {\r
40   private static final Logger logger =\r
41       LoggerFactory.getLogger(CamundaProcessExecutionHandler.class);\r
42 \r
43   private CamundaProcessExecutionHandler() {\r
44     // prevent instantiation\r
45   }\r
46 \r
47   public Response startProcessInstance(WorkflowRequest request) throws Exception {\r
48     try {\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
52       // unavailable.");\r
53       //      }\r
54 \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
59 \r
60       if (!Utilities.isHostValid(host)) {\r
61         logger.error(String.format("Host (%s) must use either the http or https protocol.", host));\r
62         return null;\r
63       }\r
64 \r
65       if (!Utilities.isPortValid(port)) {\r
66         logger.error(\r
67             String.format(\r
68                 "Invalid port (%s) specified as environment variable 'otf.camunda.port'.",\r
69                 System.getenv("otf.camunda.port")));\r
70         return null;\r
71       }\r
72 \r
73       // Form the URL\r
74       String postUrl = String.format("%s:%s/%s", host, port, path);\r
75 \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
83 \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
88             .build();\r
89       }\r
90 \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
96           .build();\r
97 \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
103     }\r
104   }\r
105 }\r