package org.gcube.portlets.user.dataminermanagertester.server; import java.util.HashMap; ***REMOVED*** import java.util.Queue; import java.util.UUID; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; ***REMOVED*** ***REMOVED*** import org.gcube.data.analysis.dataminermanagercl.server.util.ServiceCredentials; import org.gcube.data.analysis.dataminermanagercl.shared.data.OutputData; import org.gcube.data.analysis.dataminermanagercl.shared.data.computations.ComputationId; import org.gcube.data.analysis.dataminermanagercl.shared.parameters.Parameter; import org.gcube.data.analysis.dataminermanagercl.shared.process.ComputationStatus; import org.gcube.data.analysis.dataminermanagercl.shared.process.ComputationStatus.Status; ***REMOVED*** ***REMOVED*** import org.gcube.portlets.user.dataminermanagertester.client.rpc.DataMinerTesterService; import org.gcube.portlets.user.dataminermanagertester.server.task.TaskRequest; import org.gcube.portlets.user.dataminermanagertester.server.task.TaskWrapper; import org.gcube.portlets.user.dataminermanagertester.server.testbuild.TestBuilder; import org.gcube.portlets.user.dataminermanagertester.server.testconfig.DMTest; import org.gcube.portlets.user.dataminermanagertester.shared.config.DMBatchConfig; import org.gcube.portlets.user.dataminermanagertester.shared.config.DMConfig; import org.gcube.portlets.user.dataminermanagertester.shared.config.DMDiffConfig; import org.gcube.portlets.user.dataminermanagertester.shared.config.TestType; ***REMOVED*** import org.gcube.portlets.user.dataminermanagertester.shared.result.BatchTestResult; import org.gcube.portlets.user.dataminermanagertester.shared.session.UserInfo; import org.gcube.portlets.user.dataminermanagertester.shared.task.TaskStatus; ***REMOVED*** ***REMOVED*** import com.google.gwt.user.server.rpc.RemoteServiceServlet; /** * * @author Giancarlo Panichi * * */ @SuppressWarnings("serial") public class DataMinerTesterServiceImpl extends RemoteServiceServlet implements DataMinerTesterService { private static Logger logger = LoggerFactory.getLogger(DataMinerTesterServiceImpl.class); ***REMOVED*** ***REMOVED***{@inheritDoc***REMOVED*** ***REMOVED*** @Override public void init() throws ServletException { super.init(); logger.info("DataMiner Tester Service started!"); ***REMOVED*** ***REMOVED*** ***REMOVED*** ***REMOVED***{@inheritDoc***REMOVED*** ***REMOVED*** ***REMOVED*** @Override public UserInfo hello(String token) throws ServiceException { ***REMOVED*** ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(this.getThreadLocalRequest(), token); logger.debug("hello()"); UserInfo userInfo = new UserInfo(serviceCredentials.getUserName(), serviceCredentials.getGroupId(), serviceCredentials.getGroupName(), serviceCredentials.getScope(), serviceCredentials.getEmail(), serviceCredentials.getFullName()); logger.debug("UserInfo: " + userInfo); return userInfo; ***REMOVED*** ***REMOVED*** throw e; ***REMOVED*** ***REMOVED*** logger.error("Hello(): " + e.getLocalizedMessage(), e); throw new ServiceException(e.getLocalizedMessage(), e); ***REMOVED*** ***REMOVED*** @Override public String getCapabilities(String token, DMConfig dmConfig) throws ServiceException { ***REMOVED*** HttpServletRequest httpRequest = this.getThreadLocalRequest(); @SuppressWarnings("unused") ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(httpRequest, token); logger.debug("getCapabilities()"); SClient sClient = new DataMinerService().getClient(dmConfig.getToken(), dmConfig.getDmUrl()); List operatorsClassifications = sClient.getOperatorsClassifications(); logger.debug("OperatorsClassifications: " + operatorsClassifications); StringBuilder result = new StringBuilder(); result.append("Success\n"); for (OperatorsClassification opClass : operatorsClassifications) { for (Operator op : opClass.getOperators()) { result.append("Operator[Name=" + op.getName() + "]"); ***REMOVED*** ***REMOVED*** return result.toString(); ***REMOVED*** ***REMOVED*** throw e; ***REMOVED*** ***REMOVED*** throw new ServiceException(e.getLocalizedMessage(), e); ***REMOVED*** ***REMOVED*** @Override public String getOperatorsDiff(String token, DMDiffConfig operatorsDiffConfig) throws ServiceException { ***REMOVED*** logger.debug("getOperatorsDiff: dm1=" + operatorsDiffConfig.getDm1() + ", dm2=" + operatorsDiffConfig.getDm2()); HttpServletRequest httpRequest = this.getThreadLocalRequest(); @SuppressWarnings("unused") ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(httpRequest, token); SClient sClient1 = new DataMinerService().getClient(operatorsDiffConfig.getToken1(), operatorsDiffConfig.getDmUrl1()); List operatorsClassifications1 = sClient1.getOperatorsClassifications(); logger.debug("OperatorsClassifications1: " + operatorsClassifications1); SClient sClient2 = new DataMinerService().getClient(operatorsDiffConfig.getToken2(), operatorsDiffConfig.getDmUrl2()); List operatorsClassifications2 = sClient2.getOperatorsClassifications(); logger.debug("OperatorsClassifications2: " + operatorsClassifications2); StringBuilder result = new StringBuilder(); result.append("Success\n"); result.append("----------------------------------\n"); result.append("Operators in: " + operatorsDiffConfig.getDm1() + " \n"); result.append("and not in: " + operatorsDiffConfig.getDm2() + " \n"); for (OperatorsClassification opClass1 : operatorsClassifications1) { for (Operator op1 : opClass1.getOperators()) { boolean op1Found = false; ***REMOVED*** ***REMOVED*** if (op1.getId().compareTo(op2.getId()) == 0) { op1Found = true; ***REMOVED*** ***REMOVED*** ***REMOVED*** if (op1Found) { break; ***REMOVED*** ***REMOVED*** if (!op1Found) { result.append("Op: " + op1.getId() + " \n"); ***REMOVED*** ***REMOVED*** ***REMOVED*** result.append("----------------------------------\n"); result.append("Operators in: " + operatorsDiffConfig.getDm2() + " \n"); result.append("and not in: " + operatorsDiffConfig.getDm1() + " \n"); for (OperatorsClassification opClass2 : operatorsClassifications2) { for (Operator op2 : opClass2.getOperators()) { boolean op2Found = false; ***REMOVED*** ***REMOVED*** if (op2.getId().compareTo(op1.getId()) == 0) { op2Found = true; ***REMOVED*** ***REMOVED*** ***REMOVED*** if (op2Found) { break; ***REMOVED*** ***REMOVED*** if (!op2Found) { result.append("Op: " + op2.getId() + " \n"); ***REMOVED*** ***REMOVED*** ***REMOVED*** return result.toString(); ***REMOVED*** ***REMOVED*** throw e; ***REMOVED*** ***REMOVED*** throw new ServiceException(e.getLocalizedMessage(), e); ***REMOVED*** ***REMOVED*** @Override public ComputationId startSimpleTest(String token, DMConfig dmConfig) throws ServiceException { ***REMOVED*** HttpServletRequest httpRequest = this.getThreadLocalRequest(); @SuppressWarnings("unused") ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(httpRequest, token); logger.debug("startSimpleTest()"); TestType t = null; if (dmConfig == null || dmConfig.getTestType() == null || dmConfig.getTestType().isEmpty()) { throw new ServiceException("Invalid test type: " + dmConfig.getTestType()); ***REMOVED*** t = TestType.getTypeFromId(dmConfig.getTestType()); ***REMOVED*** TestBuilder testBuilder = new TestBuilder(); DMTest dmTest = testBuilder.build(t); SClient sClient = new DataMinerService().getClient(dmConfig.getToken(), dmConfig.getDmUrl()); List operatorsClassifications = sClient.getOperatorsClassifications(); logger.debug("OperatorsClassifications: " + operatorsClassifications); Operator operator = sClient.getOperatorById(dmTest.getId()); if (operator == null) { logger.error("Operator not found"); throw new ServiceException("Operator: " + dmTest.getId() + " not found"); ***REMOVED*** logger.debug("Operator Name: " + operator.getName() + " (" + operator.getId() + ")"); logger.debug("Operator: " + operator); List parameters = sClient.getInputParameters(operator); logger.debug("Parameters: " + parameters); for (Parameter parameter : parameters) { logger.debug( "Parameter:[Name=" + parameter.getName() + ", Typology=" + parameter.getTypology() + "]"); ***REMOVED*** dmTest.createRequest(operator); logger.debug("Start Computation"); ComputationId computationId = sClient.startComputation(operator); logger.debug("Started ComputationId: " + computationId); return computationId; ***REMOVED*** ***REMOVED*** ***REMOVED*** throw e; ***REMOVED*** logger.error("An error occurred starting simple test: " + e.getLocalizedMessage(), e); ***REMOVED*** throw new ServiceException(e.getLocalizedMessage(), e); ***REMOVED*** ***REMOVED*** @Override public Status monitorSimpleTest(String token, DMConfig dmConfig, ComputationId computationId) throws ServiceException { ***REMOVED*** HttpServletRequest httpRequest = this.getThreadLocalRequest(); @SuppressWarnings("unused") ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(httpRequest, token); SClient sClient = new DataMinerService().getClient(dmConfig.getToken(), dmConfig.getDmUrl()); logger.debug("Requesting operation progress"); ComputationStatus computationStatus = null; ***REMOVED*** computationStatus = sClient.getComputationStatus(computationId); ***REMOVED*** catch (Exception e) { logger.error("Error retrieving computation Status:" + e.getLocalizedMessage(), e); throw new ServiceException("Error retrieving computation Status:" + e.getLocalizedMessage(), e); ***REMOVED*** logger.debug("ComputationStatus: " + computationStatus); if (computationStatus == null) { logger.error("ComputationStatus is null"); throw new ServiceException("Error retrieving computation Status: ComputationStatus is null"); ***REMOVED*** Status status = computationStatus.getStatus(); if (status == null) { logger.error("Status is null"); throw new ServiceException("Error retrieving computation Status: Status is null"); ***REMOVED*** return status; // monitoringComputation(computationId, sClient); ***REMOVED*** ***REMOVED*** throw e; ***REMOVED*** logger.error("An error occurred monitoring simple test: " + e.getLocalizedMessage(), e); ***REMOVED*** throw new ServiceException(e.getLocalizedMessage(), e); ***REMOVED*** ***REMOVED*** @Override public String retrieveOutputForSimpleTest(String token, DMConfig dmConfig, ComputationId computationId) throws ServiceException { ***REMOVED*** HttpServletRequest httpRequest = this.getThreadLocalRequest(); @SuppressWarnings("unused") ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(httpRequest, token); logger.debug("retrieveOutputForSimpleTest()"); TestType t = null; if (dmConfig == null || dmConfig.getTestType() == null || dmConfig.getTestType().isEmpty()) { throw new ServiceException("Invalid test type: " + dmConfig.getTestType()); ***REMOVED*** t = TestType.getTypeFromId(dmConfig.getTestType()); ***REMOVED*** TestBuilder testBuilder = new TestBuilder(); DMTest dmTest = testBuilder.build(t); SClient sClient = new DataMinerService().getClient(dmConfig.getToken(), dmConfig.getDmUrl()); OutputData outputData = sClient.getOutputDataByComputationId(computationId); if (dmTest.isValidResult(outputData)) { return dmTest.getResult(outputData); ***REMOVED*** throw new ServiceException("Invalid output data for test: " + dmTest.getId()); ***REMOVED*** ***REMOVED*** ***REMOVED*** throw e; ***REMOVED*** logger.error("An error occurred monitoring simple test: " + e.getLocalizedMessage(), e); ***REMOVED*** throw new ServiceException(e.getLocalizedMessage(), e); ***REMOVED*** ***REMOVED*** @Override public String startBatchTest(String token, DMBatchConfig dmBatchConfig) throws ServiceException { ***REMOVED*** HttpServletRequest httpRequest = this.getThreadLocalRequest(); ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(httpRequest, token); logger.debug("startBatchTest()"); String operationId = UUID.randomUUID().toString(); logger.info("DataMinerBatchTest Task Operation Id: " + operationId); logger.info("Session Id: " + httpRequest.getSession()); TaskRequest taskRequest = new TaskRequest(operationId, httpRequest.getSession(), serviceCredentials, dmBatchConfig); ServletContext appScope = httpRequest.getServletContext(); @SuppressWarnings("unchecked") Queue queue = ((Queue) appScope .getAttribute(SessionConstants.TASK_REQUEST_QUEUE)); queue.add(taskRequest); logger.debug("operationId: " + operationId); return operationId; ***REMOVED*** ***REMOVED*** throw e; ***REMOVED*** logger.error("An error occurred starting batch test: " + e.getLocalizedMessage(), e); ***REMOVED*** throw new ServiceException(e.getLocalizedMessage(), e); ***REMOVED*** ***REMOVED*** @Override public TaskStatus monitorBatchTest(String token, String operationId) throws ServiceException { ***REMOVED*** HttpServletRequest httpRequest = this.getThreadLocalRequest(); ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(httpRequest, token); HashMap taskWrapperMap = SessionUtil.getTaskWrapperMap(httpRequest.getSession(), serviceCredentials); if (taskWrapperMap == null) { return null; ***REMOVED*** if (taskWrapperMap.containsKey(operationId)) { TaskWrapper taskWrapper = taskWrapperMap.get(operationId); return taskWrapper.getTaskStatus(); ***REMOVED*** else { return null; ***REMOVED*** ***REMOVED*** ***REMOVED*** ***REMOVED*** throw e; ***REMOVED*** logger.error("MonitorBatchTest(): " + e.getLocalizedMessage(), e); throw new ServiceException(e.getLocalizedMessage()); ***REMOVED*** ***REMOVED*** @Override public BatchTestResult retrieveOutputForBatchTest(String token, String operationId) throws ServiceException { ***REMOVED*** HttpServletRequest httpRequest = this.getThreadLocalRequest(); ServiceCredentials serviceCredentials = SessionUtil.getServiceCredentials(httpRequest, token); logger.debug("retrieveOutputForBatchTest()"); HashMap taskWrapperMap = SessionUtil.getTaskWrapperMap(httpRequest.getSession(), serviceCredentials); if (taskWrapperMap == null) { return null; ***REMOVED*** if (taskWrapperMap.containsKey(operationId)) { TaskWrapper taskWrapper = taskWrapperMap.get(operationId); return taskWrapper.getResult(); ***REMOVED*** else { return null; ***REMOVED*** ***REMOVED*** ***REMOVED*** ***REMOVED*** throw e; ***REMOVED*** logger.error("RetrieveOutputForBatchTest(): " + e.getLocalizedMessage(), e); throw new ServiceException(e.getLocalizedMessage()); ***REMOVED*** ***REMOVED*** ***REMOVED***