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.configuration.OtfCamundaConfiguration;
\r
20 import org.oran.otf.common.model.local.OTFDeploymentResponse;
\r
21 import org.oran.otf.common.utility.http.ResponseUtility;
\r
22 import org.oran.otf.service.TestDefinitionDeploymentService;
\r
23 import java.io.InputStream;
\r
24 import java.util.List;
\r
25 import java.util.zip.ZipInputStream;
\r
26 import javax.ws.rs.core.Response;
\r
28 import org.camunda.bpm.BpmPlatform;
\r
29 import org.camunda.bpm.engine.RepositoryService;
\r
30 import org.camunda.bpm.engine.impl.persistence.entity.DeploymentEntity;
\r
31 import org.camunda.bpm.engine.repository.ProcessDefinition;
\r
32 import org.camunda.bpm.model.bpmn.Bpmn;
\r
33 import org.camunda.bpm.model.bpmn.BpmnModelInstance;
\r
34 import org.camunda.bpm.model.xml.instance.DomElement;
\r
35 import org.slf4j.Logger;
\r
36 import org.slf4j.LoggerFactory;
\r
37 import org.springframework.beans.factory.annotation.Autowired;
\r
38 import org.springframework.boot.context.event.ApplicationReadyEvent;
\r
39 import org.springframework.context.event.EventListener;
\r
40 import org.springframework.stereotype.Service;
\r
43 public class TestDefinitionDeploymentServiceImpl implements TestDefinitionDeploymentService {
\r
45 private static Logger logger = LoggerFactory.getLogger(TestDefinitionDeploymentServiceImpl.class);
\r
48 private RepositoryService repositoryService;
\r
50 private TestDefinitionDeploymentServiceImpl() {
\r
51 // prohibit instantiation
\r
54 @EventListener(ApplicationReadyEvent.class)
\r
55 private void initialize(){
\r
56 if(this.repositoryService == null){
\r
57 this.repositoryService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRepositoryService();
\r
61 public Response deployTestStrategyWithResources(InputStream bpmn, InputStream resourcesZip) {
\r
64 logger.error("no bpmn file provided with name 'bpmn' in multipart form");
\r
65 return ResponseUtility.Build.badRequestWithMessage("No bpmn file provided with name 'bpmn' in multipart form");
\r
67 DeploymentEntity deployment = null;
\r
69 InputStream processDefinitionStream = bpmn;
\r
70 BpmnModelInstance bpmnModelInstance = null;
\r
72 bpmnModelInstance = Bpmn.readModelFromStream(processDefinitionStream);
\r
73 Bpmn.validateModel(bpmnModelInstance);
\r
76 e.printStackTrace();
\r
77 return ResponseUtility.Build.badRequestWithMessage("Unable to deploy BPMN: " + e.getMessage());
\r
79 String namespace = bpmnModelInstance.getDefinitions().getDomElement().getNamespaceURI();
\r
80 List<DomElement> bpmnProcess =
\r
81 bpmnModelInstance.getDocument().getElementsByNameNs(namespace, "process");
\r
82 if (bpmnProcess.size() != 1) {
\r
83 logger.info("Invalid number of bpmn process tags");
\r
84 return ResponseUtility.Build.internalServerErrorWithMessage("Invalid number of bpmn process tags");
\r
86 String processDefinitionKey = bpmnProcess.get(0).getAttribute("id");
\r
87 if (resourcesZip == null) {
\r
88 deployment = (DeploymentEntity) repositoryService.createDeployment()
\r
89 .addModelInstance(processDefinitionKey + ".bpmn", bpmnModelInstance).deploy();
\r
91 ZipInputStream zis = new ZipInputStream(resourcesZip);
\r
93 deployment = (DeploymentEntity) repositoryService.createDeployment()
\r
94 .addModelInstance(processDefinitionKey + ".bpmn", bpmnModelInstance)
\r
95 .addZipInputStream(zis).deploy();
\r
98 } catch (Exception e) {
\r
99 logger.info("Error: Creating Deployment: " + e.getMessage());
\r
100 return ResponseUtility.Build.internalServerErrorWithMessage("Error: Creating Deployment: " + e.getMessage());
\r
102 return Response.ok(generateResponseFromDeployment(deployment)).build();
\r
106 public Response isProcessDefinitionDeployed(String processDefinitionKey) {
\r
108 ProcessDefinition definition =
\r
110 .createProcessDefinitionQuery()
\r
111 .processDefinitionKey(processDefinitionKey)
\r
114 if (definition != null) {
\r
115 return ResponseUtility.Build.okRequest();
\r
117 return ResponseUtility.Build.notFound();
\r
119 catch (Exception e){
\r
120 return ResponseUtility.Build.internalServerErrorWithMessage(e.getMessage());
\r
124 private OTFDeploymentResponse generateResponseFromDeployment(DeploymentEntity deployment) {
\r
125 if (deployment == null) {
\r
126 return new OTFDeploymentResponse(null, null, null, -1);
\r
128 String deploymentId = deployment.getId();
\r
129 String id = deployment.getDeployedProcessDefinitions().get(0).getId();
\r
130 String key = deployment.getDeployedProcessDefinitions().get(0).getKey();
\r
131 int version = deployment.getDeployedProcessDefinitions().get(0).getVersion();
\r
132 return new OTFDeploymentResponse(deploymentId, key, id, version);
\r