050a2af06a270b8633070397dfd056f534c187f4
[so.git] /
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.onap.so.logger.LoggingAnchor;
28 import javax.annotation.PostConstruct;
29 import org.camunda.bpm.application.PreUndeploy;
30 import org.camunda.bpm.application.ProcessApplicationInfo;
31 import org.camunda.bpm.engine.ProcessEngine;
32 import org.camunda.bpm.engine.repository.DeploymentBuilder;
33 import org.onap.so.bpmn.common.DefaultToShortClassNameBeanNameGenerator;
34 import org.onap.so.db.catalog.beans.Workflow;
35 import org.onap.so.db.catalog.client.CatalogDbClient;
36 import org.onap.so.logging.jaxrs.filter.MDCTaskDecorator;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.boot.SpringApplication;
42 import org.springframework.boot.autoconfigure.SpringBootApplication;
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.scheduling.annotation.EnableAsync;
49 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
50
51 /**
52  * @since Version 1.0
53  *
54  */
55
56 @SpringBootApplication
57 @EnableAsync
58 @ComponentScan(basePackages = {"org.onap"}, nameGenerator = DefaultToShortClassNameBeanNameGenerator.class,
59         excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = SpringBootApplication.class)})
60
61 public class MSOInfrastructureApplication {
62
63     private static final Logger logger = LoggerFactory.getLogger(MSOInfrastructureApplication.class);
64     @Autowired
65     private ProcessEngine processEngine;
66
67     @Autowired
68     private CatalogDbClient catalogDbClient;
69
70     @Value("${mso.async.core-pool-size}")
71     private int corePoolSize;
72
73     @Value("${mso.async.max-pool-size}")
74     private int maxPoolSize;
75
76     @Value("${mso.async.queue-capacity}")
77     private int queueCapacity;
78
79     private static final String LOGS_DIR = "logs_dir";
80     private static final String BPMN_SUFFIX = ".bpmn";
81     private static final String SDC_SOURCE = "sdc";
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     @PostConstruct
97     public void postConstruct() {
98         try {
99             DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
100             deployCustomWorkflows(deploymentBuilder);
101         } catch (Exception e) {
102             logger.warn("Unable to invoke deploymentBuilder: " + e.getMessage());
103         }
104     }
105
106     @PreUndeploy
107     public void cleanup(ProcessEngine processEngine, ProcessApplicationInfo processApplicationInfo,
108             List<ProcessEngine> processEngines) {}
109
110     @Bean
111     @Primary
112     public Executor asyncExecutor() {
113         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
114         executor.setTaskDecorator(new MDCTaskDecorator());
115         executor.setCorePoolSize(corePoolSize);
116         executor.setMaxPoolSize(maxPoolSize);
117         executor.setQueueCapacity(queueCapacity);
118         executor.setThreadNamePrefix("Camunda-");
119         executor.initialize();
120         return executor;
121     }
122
123     public void deployCustomWorkflows(DeploymentBuilder deploymentBuilder) {
124         logger.debug("Attempting to deploy custom workflows");
125         try {
126             List<Workflow> workflows = catalogDbClient.findWorkflowBySource(SDC_SOURCE);
127             if (workflows != null && !workflows.isEmpty()) {
128                 for (Workflow workflow : workflows) {
129                     String workflowName = workflow.getName();
130                     String workflowBody = workflow.getBody();
131                     if (!workflowName.endsWith(BPMN_SUFFIX)) {
132                         workflowName += BPMN_SUFFIX;
133                     }
134                     if (workflowBody != null) {
135                         logger.info(LoggingAnchor.TWO, "Deploying custom workflow", workflowName);
136                         deploymentBuilder.addString(workflowName, workflowBody);
137                     }
138                     deploymentBuilder.enableDuplicateFiltering(true);
139                 }
140                 deploymentBuilder.deploy();
141             }
142         } catch (Exception e) {
143             logger.warn("Unable to deploy custom workflows, " + e.getMessage());
144         }
145     }
146 }