added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / service / impl / TestDefinitionDeploymentServiceImpl.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 package org.oran.otf.service.impl;\r
18 \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
27 \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
41 \r
42 @Service\r
43 public class TestDefinitionDeploymentServiceImpl implements TestDefinitionDeploymentService {\r
44 \r
45     private static Logger logger = LoggerFactory.getLogger(TestDefinitionDeploymentServiceImpl.class);\r
46 \r
47 \r
48     private RepositoryService repositoryService;\r
49 \r
50     private TestDefinitionDeploymentServiceImpl() {\r
51         // prohibit instantiation\r
52     }\r
53 \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
58         }\r
59     }\r
60 \r
61     public Response deployTestStrategyWithResources(InputStream bpmn, InputStream resourcesZip) {\r
62 \r
63         if (bpmn == null) {\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
66         }\r
67         DeploymentEntity deployment = null;\r
68         try {\r
69             InputStream processDefinitionStream = bpmn;\r
70             BpmnModelInstance bpmnModelInstance = null;\r
71             try {\r
72                 bpmnModelInstance = Bpmn.readModelFromStream(processDefinitionStream);\r
73                 Bpmn.validateModel(bpmnModelInstance);\r
74             }\r
75             catch(Exception e){\r
76                 e.printStackTrace();\r
77                 return ResponseUtility.Build.badRequestWithMessage("Unable to deploy BPMN: " + e.getMessage());\r
78             }\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
85             } else {\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
90                 } else {\r
91                     ZipInputStream zis = new ZipInputStream(resourcesZip);\r
92 \r
93                     deployment = (DeploymentEntity) repositoryService.createDeployment()\r
94                             .addModelInstance(processDefinitionKey + ".bpmn", bpmnModelInstance)\r
95                             .addZipInputStream(zis).deploy();\r
96                 }\r
97             }\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
101         }\r
102         return Response.ok(generateResponseFromDeployment(deployment)).build();\r
103     }\r
104 \r
105     @Override\r
106     public Response isProcessDefinitionDeployed(String processDefinitionKey) {\r
107         try {\r
108             ProcessDefinition definition =\r
109                     repositoryService\r
110                             .createProcessDefinitionQuery()\r
111                             .processDefinitionKey(processDefinitionKey)\r
112                             .latestVersion()\r
113                             .singleResult();\r
114             if (definition != null) {\r
115                 return ResponseUtility.Build.okRequest();\r
116             }\r
117             return ResponseUtility.Build.notFound();\r
118         }\r
119         catch (Exception e){\r
120             return ResponseUtility.Build.internalServerErrorWithMessage(e.getMessage());\r
121         }\r
122     }\r
123 \r
124     private OTFDeploymentResponse generateResponseFromDeployment(DeploymentEntity deployment) {\r
125         if (deployment == null) {\r
126             return new OTFDeploymentResponse(null, null, null, -1);\r
127         }\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
133     }\r
134 }\r