added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / service / impl / TestControlUnitServiceImpl.java
diff --git a/otf-camunda/src/main/java/org/oran/otf/service/impl/TestControlUnitServiceImpl.java b/otf-camunda/src/main/java/org/oran/otf/service/impl/TestControlUnitServiceImpl.java
new file mode 100644 (file)
index 0000000..a607d5c
--- /dev/null
@@ -0,0 +1,119 @@
+/*  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.service.impl;\r
+\r
+import org.oran.otf.camunda.delegate.otf.common.runnable.AsynchronousTestInstanceCallable;\r
+import org.oran.otf.camunda.delegate.otf.common.runnable.SynchronousTestInstanceCallable;\r
+import org.oran.otf.camunda.exception.WorkflowProcessorException;\r
+import org.oran.otf.camunda.workflow.WorkflowProcessor;\r
+import org.oran.otf.camunda.workflow.WorkflowRequest;\r
+import org.oran.otf.common.model.TestExecution;\r
+import org.oran.otf.common.model.TestInstance;\r
+import org.oran.otf.common.repository.TestExecutionRepository;\r
+import org.oran.otf.common.repository.TestInstanceRepository;\r
+import org.oran.otf.common.utility.gson.Convert;\r
+import org.oran.otf.common.utility.http.ResponseUtility;\r
+import org.oran.otf.service.TestControlUnitService;\r
+import com.fasterxml.jackson.core.type.TypeReference;\r
+import java.io.IOException;\r
+import javax.ws.rs.core.Context;\r
+import javax.ws.rs.core.HttpHeaders;\r
+import javax.ws.rs.core.MultivaluedMap;\r
+import javax.ws.rs.core.Response;\r
+import org.bson.types.ObjectId;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.data.mongodb.core.MongoTemplate;\r
+import org.springframework.stereotype.Service;\r
+import org.springframework.web.bind.annotation.RequestHeader;\r
+\r
+@Service\r
+public class TestControlUnitServiceImpl implements TestControlUnitService {\r
+\r
+  private static Logger logger = LoggerFactory.getLogger(TestControlUnitServiceImpl.class);\r
+\r
+  @Autowired\r
+  TestInstanceRepository testInstanceRepository;\r
+\r
+  @Autowired\r
+  TestExecutionRepository testExecutionRepository;\r
+\r
+  @Autowired\r
+  MongoTemplate mongoOperation;\r
+\r
+  @Autowired\r
+  WorkflowProcessor processor;\r
+\r
+  @Override\r
+  public Response executeByTestInstanceId(String testInstanceId) {\r
+    try {\r
+      TestInstance testInstance = testInstanceRepository.findById(testInstanceId).orElse(null);\r
+      if (testInstance == null) {\r
+        return Response.status(404).entity("Test Instance not found.").build();\r
+      }\r
+\r
+      WorkflowRequest req = new WorkflowRequest();\r
+      req.setAsync(false);\r
+      req.setExecutorId(new ObjectId("5cb72a7e10ba2a0042e6282a"));\r
+      req.setTestInstanceId(testInstance.get_id());\r
+      req.setTestData(testInstance.getTestData());\r
+      req.setVthInput(testInstance.getVthInput());\r
+      req.setPfloInput(testInstance.getPfloInput());\r
+      req.setMaxExecutionTimeInMillis(testInstance.getMaxExecutionTimeInMillis());\r
+      return processWorkflowRequest(req);\r
+    } catch (Exception e) {\r
+      return ResponseUtility.Build.internalServerErrorWithMessage(e.getMessage());\r
+    }\r
+  }\r
+\r
+  @Override\r
+  public Response executeByWorkflowRequest(String workflowRequestJson) {\r
+    try {\r
+      WorkflowRequest workflowRequest =\r
+          Convert.jsonToObject(workflowRequestJson, new TypeReference<WorkflowRequest>() {\r
+          });\r
+\r
+      return processWorkflowRequest(workflowRequest);\r
+    } catch (IOException e) {\r
+      logger.error(e.getMessage());\r
+      return ResponseUtility.Build.badRequestWithMessage(e.getMessage());\r
+    }\r
+  }\r
+\r
+  private Response processWorkflowRequest(WorkflowRequest request) {\r
+    TestExecution testExecution = null;\r
+    int statusCode = 200;\r
+    try {\r
+      if (request.isAsync()) {\r
+        AsynchronousTestInstanceCallable asynchronousTestInstanceCallable =\r
+            new AsynchronousTestInstanceCallable(\r
+                request, testExecutionRepository, processor, mongoOperation);\r
+        testExecution = asynchronousTestInstanceCallable.call();\r
+      } else {\r
+        SynchronousTestInstanceCallable synchronousTestInstanceCallable =\r
+            new SynchronousTestInstanceCallable(\r
+                request, testExecutionRepository, processor, mongoOperation);\r
+        testExecution = synchronousTestInstanceCallable.call();\r
+      }\r
+    } catch (WorkflowProcessorException e) {\r
+      testExecution = e.getWorkflowResponse().getTestExecution();\r
+      statusCode = e.getWorkflowResponse().getMessageCode();\r
+    }\r
+    return ResponseUtility.Build.genericWithMessage(statusCode, testExecution.toString());\r
+  }\r
+}\r