Fix deployment of workflows on start
[so.git] / bpmn / mso-infrastructure-bpmn / src / main / java / org / onap / so / bpmn / infrastructure / MSOInfrastructureApplication.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure;
24
25 import java.util.List;
26 import java.util.concurrent.Executor;
27 import javax.annotation.PostConstruct;
28 import org.camunda.bpm.application.PreUndeploy;
29 import org.camunda.bpm.application.ProcessApplicationInfo;
30 import org.camunda.bpm.engine.ProcessEngine;
31 import org.camunda.bpm.engine.repository.DeploymentBuilder;
32 import org.onap.so.bpmn.common.DefaultToShortClassNameBeanNameGenerator;
33 import org.onap.so.db.catalog.beans.Workflow;
34 import org.onap.so.db.catalog.client.CatalogDbClient;
35 import org.onap.so.logging.jaxrs.filter.MDCTaskDecorator;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.boot.SpringApplication;
41 import org.springframework.boot.autoconfigure.SpringBootApplication;
42 import org.springframework.context.annotation.Bean;
43 import org.springframework.context.annotation.ComponentScan;
44 import org.springframework.context.annotation.ComponentScan.Filter;
45 import org.springframework.context.annotation.FilterType;
46 import org.springframework.context.annotation.Primary;
47 import org.springframework.scheduling.annotation.EnableAsync;
48 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
49
50 /**
51  * @since Version 1.0
52  *
53  */
54
55 @SpringBootApplication
56 @EnableAsync
57 @ComponentScan(basePackages = {"org.onap"}, nameGenerator = DefaultToShortClassNameBeanNameGenerator.class,
58         excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = SpringBootApplication.class)})
59
60 public class MSOInfrastructureApplication {
61
62     private static final Logger logger = LoggerFactory.getLogger(MSOInfrastructureApplication.class);
63     @Autowired
64     private ProcessEngine processEngine;
65
66     @Autowired
67     private CatalogDbClient catalogDbClient;
68
69     @Value("${mso.async.core-pool-size}")
70     private int corePoolSize;
71
72     @Value("${mso.async.max-pool-size}")
73     private int maxPoolSize;
74
75     @Value("${mso.async.queue-capacity}")
76     private int queueCapacity;
77
78     private static final String LOGS_DIR = "logs_dir";
79     private static final String BPMN_SUFFIX = ".bpmn";
80     private static final String SDC_SOURCE = "sdc";
81
82
83     private static void setLogsDir() {
84         if (System.getProperty(LOGS_DIR) == null) {
85             System.getProperties().setProperty(LOGS_DIR, "./logs/bpmn/");
86         }
87     }
88
89     public static void main(String... args) {
90         SpringApplication.run(MSOInfrastructureApplication.class, args);
91         System.getProperties().setProperty("mso.config.path", ".");
92         setLogsDir();
93     }
94
95     @PostConstruct
96     public void postConstruct() {
97         try {
98             DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
99             deployCustomWorkflows(deploymentBuilder);
100         } catch (Exception e) {
101             logger.warn("Unable to invoke deploymentBuilder: " + e.getMessage());
102         }
103     }
104
105     @PreUndeploy
106     public void cleanup(ProcessEngine processEngine, ProcessApplicationInfo processApplicationInfo,
107             List<ProcessEngine> processEngines) {}
108
109     @Bean
110     @Primary
111     public Executor asyncExecutor() {
112         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
113         executor.setTaskDecorator(new MDCTaskDecorator());
114         executor.setCorePoolSize(corePoolSize);
115         executor.setMaxPoolSize(maxPoolSize);
116         executor.setQueueCapacity(queueCapacity);
117         executor.setThreadNamePrefix("Camunda-");
118         executor.initialize();
119         return executor;
120     }
121
122     public void deployCustomWorkflows(DeploymentBuilder deploymentBuilder) {
123         logger.debug("Attempting to deploy custom workflows");
124         try {
125             List<Workflow> workflows = catalogDbClient.findWorkflowBySource(SDC_SOURCE);
126             if (workflows != null && workflows.size() != 0) {
127                 for (Workflow workflow : workflows) {
128                     String workflowName = workflow.getName();
129                     String workflowBody = workflow.getBody();
130                     if (!workflowName.endsWith(BPMN_SUFFIX)) {
131                         workflowName += BPMN_SUFFIX;
132                     }
133                     if (workflowBody != null) {
134                         logger.info("{} {}", "Deploying custom workflow", workflowName);
135                         deploymentBuilder.addString(workflowName, workflowBody);
136                     }
137                 }
138                 deploymentBuilder.enableDuplicateFiltering(true);
139                 deploymentBuilder.deploy();
140             }
141         } catch (Exception e) {
142             logger.warn("Unable to deploy custom workflows, " + e.getMessage());
143         }
144     }
145 }