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.service.impl;
\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
46 public class TestControlUnitServiceImpl implements TestControlUnitService {
\r
48 private static Logger logger = LoggerFactory.getLogger(TestControlUnitServiceImpl.class);
\r
51 TestInstanceRepository testInstanceRepository;
\r
54 TestExecutionRepository testExecutionRepository;
\r
57 MongoTemplate mongoOperation;
\r
60 WorkflowProcessor processor;
\r
63 public Response executeByTestInstanceId(String testInstanceId) {
\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
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
85 public Response executeByWorkflowRequest(String workflowRequestJson) {
\r
87 WorkflowRequest workflowRequest =
\r
88 Convert.jsonToObject(workflowRequestJson, new TypeReference<WorkflowRequest>() {
\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
98 private Response processWorkflowRequest(WorkflowRequest request) {
\r
99 TestExecution testExecution = null;
\r
100 int statusCode = 200;
\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
108 SynchronousTestInstanceCallable synchronousTestInstanceCallable =
\r
109 new SynchronousTestInstanceCallable(
\r
110 request, testExecutionRepository, processor, mongoOperation);
\r
111 testExecution = synchronousTestInstanceCallable.call();
\r
113 } catch (WorkflowProcessorException e) {
\r
114 testExecution = e.getWorkflowResponse().getTestExecution();
\r
115 statusCode = e.getWorkflowResponse().getMessageCode();
\r
117 return ResponseUtility.Build.genericWithMessage(statusCode, testExecution.toString());
\r