--- /dev/null
+/* 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.configuration.OtfCamundaConfiguration;\r
+import org.oran.otf.common.model.local.OTFDeploymentResponse;\r
+import org.oran.otf.common.utility.http.ResponseUtility;\r
+import org.oran.otf.service.TestDefinitionDeploymentService;\r
+import java.io.InputStream;\r
+import java.util.List;\r
+import java.util.zip.ZipInputStream;\r
+import javax.ws.rs.core.Response;\r
+\r
+import org.camunda.bpm.BpmPlatform;\r
+import org.camunda.bpm.engine.RepositoryService;\r
+import org.camunda.bpm.engine.impl.persistence.entity.DeploymentEntity;\r
+import org.camunda.bpm.engine.repository.ProcessDefinition;\r
+import org.camunda.bpm.model.bpmn.Bpmn;\r
+import org.camunda.bpm.model.bpmn.BpmnModelInstance;\r
+import org.camunda.bpm.model.xml.instance.DomElement;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.boot.context.event.ApplicationReadyEvent;\r
+import org.springframework.context.event.EventListener;\r
+import org.springframework.stereotype.Service;\r
+\r
+@Service\r
+public class TestDefinitionDeploymentServiceImpl implements TestDefinitionDeploymentService {\r
+\r
+ private static Logger logger = LoggerFactory.getLogger(TestDefinitionDeploymentServiceImpl.class);\r
+\r
+\r
+ private RepositoryService repositoryService;\r
+\r
+ private TestDefinitionDeploymentServiceImpl() {\r
+ // prohibit instantiation\r
+ }\r
+\r
+ @EventListener(ApplicationReadyEvent.class)\r
+ private void initialize(){\r
+ if(this.repositoryService == null){\r
+ this.repositoryService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRepositoryService();\r
+ }\r
+ }\r
+\r
+ public Response deployTestStrategyWithResources(InputStream bpmn, InputStream resourcesZip) {\r
+\r
+ if (bpmn == null) {\r
+ logger.error("no bpmn file provided with name 'bpmn' in multipart form");\r
+ return ResponseUtility.Build.badRequestWithMessage("No bpmn file provided with name 'bpmn' in multipart form");\r
+ }\r
+ DeploymentEntity deployment = null;\r
+ try {\r
+ InputStream processDefinitionStream = bpmn;\r
+ BpmnModelInstance bpmnModelInstance = null;\r
+ try {\r
+ bpmnModelInstance = Bpmn.readModelFromStream(processDefinitionStream);\r
+ Bpmn.validateModel(bpmnModelInstance);\r
+ }\r
+ catch(Exception e){\r
+ e.printStackTrace();\r
+ return ResponseUtility.Build.badRequestWithMessage("Unable to deploy BPMN: " + e.getMessage());\r
+ }\r
+ String namespace = bpmnModelInstance.getDefinitions().getDomElement().getNamespaceURI();\r
+ List<DomElement> bpmnProcess =\r
+ bpmnModelInstance.getDocument().getElementsByNameNs(namespace, "process");\r
+ if (bpmnProcess.size() != 1) {\r
+ logger.info("Invalid number of bpmn process tags");\r
+ return ResponseUtility.Build.internalServerErrorWithMessage("Invalid number of bpmn process tags");\r
+ } else {\r
+ String processDefinitionKey = bpmnProcess.get(0).getAttribute("id");\r
+ if (resourcesZip == null) {\r
+ deployment = (DeploymentEntity) repositoryService.createDeployment()\r
+ .addModelInstance(processDefinitionKey + ".bpmn", bpmnModelInstance).deploy();\r
+ } else {\r
+ ZipInputStream zis = new ZipInputStream(resourcesZip);\r
+\r
+ deployment = (DeploymentEntity) repositoryService.createDeployment()\r
+ .addModelInstance(processDefinitionKey + ".bpmn", bpmnModelInstance)\r
+ .addZipInputStream(zis).deploy();\r
+ }\r
+ }\r
+ } catch (Exception e) {\r
+ logger.info("Error: Creating Deployment: " + e.getMessage());\r
+ return ResponseUtility.Build.internalServerErrorWithMessage("Error: Creating Deployment: " + e.getMessage());\r
+ }\r
+ return Response.ok(generateResponseFromDeployment(deployment)).build();\r
+ }\r
+\r
+ @Override\r
+ public Response isProcessDefinitionDeployed(String processDefinitionKey) {\r
+ try {\r
+ ProcessDefinition definition =\r
+ repositoryService\r
+ .createProcessDefinitionQuery()\r
+ .processDefinitionKey(processDefinitionKey)\r
+ .latestVersion()\r
+ .singleResult();\r
+ if (definition != null) {\r
+ return ResponseUtility.Build.okRequest();\r
+ }\r
+ return ResponseUtility.Build.notFound();\r
+ }\r
+ catch (Exception e){\r
+ return ResponseUtility.Build.internalServerErrorWithMessage(e.getMessage());\r
+ }\r
+ }\r
+\r
+ private OTFDeploymentResponse generateResponseFromDeployment(DeploymentEntity deployment) {\r
+ if (deployment == null) {\r
+ return new OTFDeploymentResponse(null, null, null, -1);\r
+ }\r
+ String deploymentId = deployment.getId();\r
+ String id = deployment.getDeployedProcessDefinitions().get(0).getId();\r
+ String key = deployment.getDeployedProcessDefinitions().get(0).getKey();\r
+ int version = deployment.getDeployedProcessDefinitions().get(0).getVersion();\r
+ return new OTFDeploymentResponse(deploymentId, key, id, version);\r
+ }\r
+}\r