c61808ebb1b2b55b0f8585e201b5155d8751f3ee
[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 org.camunda.bpm.application.PostDeploy;
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.data.repository.WorkflowRepository;
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.boot.autoconfigure.domain.EntityScan;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.context.annotation.ComponentScan;
45 import org.springframework.context.annotation.ComponentScan.Filter;
46 import org.springframework.context.annotation.FilterType;
47 import org.springframework.context.annotation.Primary;
48 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
49 import org.springframework.scheduling.annotation.EnableAsync;
50 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
51
52 /**
53  * @since Version 1.0
54  *
55  */
56
57 @SpringBootApplication
58 @EnableAsync
59 @EnableJpaRepositories("org.onap.so.db.catalog.data.repository")
60 @EntityScan({"org.onap.so.db.catalog.beans"})
61 @ComponentScan(basePackages = {"org.onap"}, nameGenerator = DefaultToShortClassNameBeanNameGenerator.class,
62         excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = SpringBootApplication.class)})
63
64 public class MSOInfrastructureApplication {
65
66     private static final Logger logger = LoggerFactory.getLogger(MSOInfrastructureApplication.class);
67
68     @Autowired
69     private WorkflowRepository workflowRepository;
70
71     @Value("${mso.async.core-pool-size}")
72     private int corePoolSize;
73
74     @Value("${mso.async.max-pool-size}")
75     private int maxPoolSize;
76
77     @Value("${mso.async.queue-capacity}")
78     private int queueCapacity;
79
80     private static final String LOGS_DIR = "logs_dir";
81     private static final String BPMN_SUFFIX = ".bpmn";
82
83
84     private static void setLogsDir() {
85         if (System.getProperty(LOGS_DIR) == null) {
86             System.getProperties().setProperty(LOGS_DIR, "./logs/bpmn/");
87         }
88     }
89
90     public static void main(String... args) {
91         SpringApplication.run(MSOInfrastructureApplication.class, args);
92         System.getProperties().setProperty("mso.config.path", ".");
93         setLogsDir();
94     }
95
96     @PostDeploy
97     public void postDeploy(ProcessEngine processEngineInstance) {
98         DeploymentBuilder deploymentBuilder = processEngineInstance.getRepositoryService().createDeployment();
99         deployCustomWorkflows(deploymentBuilder);
100     }
101
102     @PreUndeploy
103     public void cleanup(ProcessEngine processEngine, ProcessApplicationInfo processApplicationInfo,
104             List<ProcessEngine> processEngines) {}
105
106     @Bean
107     @Primary
108     public Executor asyncExecutor() {
109         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
110         executor.setTaskDecorator(new MDCTaskDecorator());
111         executor.setCorePoolSize(corePoolSize);
112         executor.setMaxPoolSize(maxPoolSize);
113         executor.setQueueCapacity(queueCapacity);
114         executor.setThreadNamePrefix("Camunda-");
115         executor.initialize();
116         return executor;
117     }
118
119     public void deployCustomWorkflows(DeploymentBuilder deploymentBuilder) {
120         if (workflowRepository == null) {
121             return;
122         }
123         List<Workflow> workflows = workflowRepository.findAll();
124         if (workflows != null && workflows.size() != 0) {
125             for (Workflow workflow : workflows) {
126                 String workflowName = workflow.getName();
127                 String workflowBody = workflow.getBody();
128                 if (!workflowName.endsWith(BPMN_SUFFIX)) {
129                     workflowName += BPMN_SUFFIX;
130                 }
131                 if (workflowBody != null) {
132                     logger.info("{} {}", "Deploying custom workflow", workflowName);
133                     deploymentBuilder.addString(workflowName, workflowBody);
134                 }
135             }
136             deploymentBuilder.deploy();
137         }
138     }
139 }