X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=otf-camunda%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fservice%2Fimpl%2FTestControlUnitServiceImpl.java;fp=otf-camunda%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fservice%2Fimpl%2FTestControlUnitServiceImpl.java;h=a607d5ca4c6ee685bd8d0c5cf7b18cd9f48741fa;hb=14f6f95c84a4a1fa8774190db4a03fd0214ec55f;hp=0000000000000000000000000000000000000000;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b;p=it%2Fotf.git 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 index 0000000..a607d5c --- /dev/null +++ b/otf-camunda/src/main/java/org/oran/otf/service/impl/TestControlUnitServiceImpl.java @@ -0,0 +1,119 @@ +/* Copyright (c) 2019 AT&T Intellectual Property. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +##############################################################################*/ + + +package org.oran.otf.service.impl; + +import org.oran.otf.camunda.delegate.otf.common.runnable.AsynchronousTestInstanceCallable; +import org.oran.otf.camunda.delegate.otf.common.runnable.SynchronousTestInstanceCallable; +import org.oran.otf.camunda.exception.WorkflowProcessorException; +import org.oran.otf.camunda.workflow.WorkflowProcessor; +import org.oran.otf.camunda.workflow.WorkflowRequest; +import org.oran.otf.common.model.TestExecution; +import org.oran.otf.common.model.TestInstance; +import org.oran.otf.common.repository.TestExecutionRepository; +import org.oran.otf.common.repository.TestInstanceRepository; +import org.oran.otf.common.utility.gson.Convert; +import org.oran.otf.common.utility.http.ResponseUtility; +import org.oran.otf.service.TestControlUnitService; +import com.fasterxml.jackson.core.type.TypeReference; +import java.io.IOException; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import org.bson.types.ObjectId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestHeader; + +@Service +public class TestControlUnitServiceImpl implements TestControlUnitService { + + private static Logger logger = LoggerFactory.getLogger(TestControlUnitServiceImpl.class); + + @Autowired + TestInstanceRepository testInstanceRepository; + + @Autowired + TestExecutionRepository testExecutionRepository; + + @Autowired + MongoTemplate mongoOperation; + + @Autowired + WorkflowProcessor processor; + + @Override + public Response executeByTestInstanceId(String testInstanceId) { + try { + TestInstance testInstance = testInstanceRepository.findById(testInstanceId).orElse(null); + if (testInstance == null) { + return Response.status(404).entity("Test Instance not found.").build(); + } + + WorkflowRequest req = new WorkflowRequest(); + req.setAsync(false); + req.setExecutorId(new ObjectId("5cb72a7e10ba2a0042e6282a")); + req.setTestInstanceId(testInstance.get_id()); + req.setTestData(testInstance.getTestData()); + req.setVthInput(testInstance.getVthInput()); + req.setPfloInput(testInstance.getPfloInput()); + req.setMaxExecutionTimeInMillis(testInstance.getMaxExecutionTimeInMillis()); + return processWorkflowRequest(req); + } catch (Exception e) { + return ResponseUtility.Build.internalServerErrorWithMessage(e.getMessage()); + } + } + + @Override + public Response executeByWorkflowRequest(String workflowRequestJson) { + try { + WorkflowRequest workflowRequest = + Convert.jsonToObject(workflowRequestJson, new TypeReference() { + }); + + return processWorkflowRequest(workflowRequest); + } catch (IOException e) { + logger.error(e.getMessage()); + return ResponseUtility.Build.badRequestWithMessage(e.getMessage()); + } + } + + private Response processWorkflowRequest(WorkflowRequest request) { + TestExecution testExecution = null; + int statusCode = 200; + try { + if (request.isAsync()) { + AsynchronousTestInstanceCallable asynchronousTestInstanceCallable = + new AsynchronousTestInstanceCallable( + request, testExecutionRepository, processor, mongoOperation); + testExecution = asynchronousTestInstanceCallable.call(); + } else { + SynchronousTestInstanceCallable synchronousTestInstanceCallable = + new SynchronousTestInstanceCallable( + request, testExecutionRepository, processor, mongoOperation); + testExecution = synchronousTestInstanceCallable.call(); + } + } catch (WorkflowProcessorException e) { + testExecution = e.getWorkflowResponse().getTestExecution(); + statusCode = e.getWorkflowResponse().getMessageCode(); + } + return ResponseUtility.Build.genericWithMessage(statusCode, testExecution.toString()); + } +}