added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / service / impl / TestControlUnitServiceImpl.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.service.impl;\r
18 \r
19 import org.oran.otf.camunda.delegate.otf.common.runnable.AsynchronousTestInstanceCallable;\r
20 import org.oran.otf.camunda.delegate.otf.common.runnable.SynchronousTestInstanceCallable;\r
21 import org.oran.otf.camunda.exception.WorkflowProcessorException;\r
22 import org.oran.otf.camunda.workflow.WorkflowProcessor;\r
23 import org.oran.otf.camunda.workflow.WorkflowRequest;\r
24 import org.oran.otf.common.model.TestExecution;\r
25 import org.oran.otf.common.model.TestInstance;\r
26 import org.oran.otf.common.repository.TestExecutionRepository;\r
27 import org.oran.otf.common.repository.TestInstanceRepository;\r
28 import org.oran.otf.common.utility.gson.Convert;\r
29 import org.oran.otf.common.utility.http.ResponseUtility;\r
30 import org.oran.otf.service.TestControlUnitService;\r
31 import com.fasterxml.jackson.core.type.TypeReference;\r
32 import java.io.IOException;\r
33 import javax.ws.rs.core.Context;\r
34 import javax.ws.rs.core.HttpHeaders;\r
35 import javax.ws.rs.core.MultivaluedMap;\r
36 import javax.ws.rs.core.Response;\r
37 import org.bson.types.ObjectId;\r
38 import org.slf4j.Logger;\r
39 import org.slf4j.LoggerFactory;\r
40 import org.springframework.beans.factory.annotation.Autowired;\r
41 import org.springframework.data.mongodb.core.MongoTemplate;\r
42 import org.springframework.stereotype.Service;\r
43 import org.springframework.web.bind.annotation.RequestHeader;\r
44 \r
45 @Service\r
46 public class TestControlUnitServiceImpl implements TestControlUnitService {\r
47 \r
48   private static Logger logger = LoggerFactory.getLogger(TestControlUnitServiceImpl.class);\r
49 \r
50   @Autowired\r
51   TestInstanceRepository testInstanceRepository;\r
52 \r
53   @Autowired\r
54   TestExecutionRepository testExecutionRepository;\r
55 \r
56   @Autowired\r
57   MongoTemplate mongoOperation;\r
58 \r
59   @Autowired\r
60   WorkflowProcessor processor;\r
61 \r
62   @Override\r
63   public Response executeByTestInstanceId(String testInstanceId) {\r
64     try {\r
65       TestInstance testInstance = testInstanceRepository.findById(testInstanceId).orElse(null);\r
66       if (testInstance == null) {\r
67         return Response.status(404).entity("Test Instance not found.").build();\r
68       }\r
69 \r
70       WorkflowRequest req = new WorkflowRequest();\r
71       req.setAsync(false);\r
72       req.setExecutorId(new ObjectId("5cb72a7e10ba2a0042e6282a"));\r
73       req.setTestInstanceId(testInstance.get_id());\r
74       req.setTestData(testInstance.getTestData());\r
75       req.setVthInput(testInstance.getVthInput());\r
76       req.setPfloInput(testInstance.getPfloInput());\r
77       req.setMaxExecutionTimeInMillis(testInstance.getMaxExecutionTimeInMillis());\r
78       return processWorkflowRequest(req);\r
79     } catch (Exception e) {\r
80       return ResponseUtility.Build.internalServerErrorWithMessage(e.getMessage());\r
81     }\r
82   }\r
83 \r
84   @Override\r
85   public Response executeByWorkflowRequest(String workflowRequestJson) {\r
86     try {\r
87       WorkflowRequest workflowRequest =\r
88           Convert.jsonToObject(workflowRequestJson, new TypeReference<WorkflowRequest>() {\r
89           });\r
90 \r
91       return processWorkflowRequest(workflowRequest);\r
92     } catch (IOException e) {\r
93       logger.error(e.getMessage());\r
94       return ResponseUtility.Build.badRequestWithMessage(e.getMessage());\r
95     }\r
96   }\r
97 \r
98   private Response processWorkflowRequest(WorkflowRequest request) {\r
99     TestExecution testExecution = null;\r
100     int statusCode = 200;\r
101     try {\r
102       if (request.isAsync()) {\r
103         AsynchronousTestInstanceCallable asynchronousTestInstanceCallable =\r
104             new AsynchronousTestInstanceCallable(\r
105                 request, testExecutionRepository, processor, mongoOperation);\r
106         testExecution = asynchronousTestInstanceCallable.call();\r
107       } else {\r
108         SynchronousTestInstanceCallable synchronousTestInstanceCallable =\r
109             new SynchronousTestInstanceCallable(\r
110                 request, testExecutionRepository, processor, mongoOperation);\r
111         testExecution = synchronousTestInstanceCallable.call();\r
112       }\r
113     } catch (WorkflowProcessorException e) {\r
114       testExecution = e.getWorkflowResponse().getTestExecution();\r
115       statusCode = e.getWorkflowResponse().getMessageCode();\r
116     }\r
117     return ResponseUtility.Build.genericWithMessage(statusCode, testExecution.toString());\r
118   }\r
119 }\r