8d6e133a1cf49d877f7d462421157a33b1d150d7
[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.logging.filter.spring.MDCTaskDecorator;
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.logger.LoggingAnchor;
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     private static final int CANNOT_INVOKE_COMMAND = 126;
83
84
85     private static void setLogsDir() {
86         if (System.getProperty(LOGS_DIR) == null) {
87             System.getProperties().setProperty(LOGS_DIR, "./logs/bpmn/");
88         }
89     }
90
91     public static void main(String... args) {
92         try {
93             SpringApplication.run(MSOInfrastructureApplication.class, args);
94             System.getProperties().setProperty("mso.config.path", ".");
95             setLogsDir();
96         } catch (Exception e) {
97             logger.error("Exception has occurred during application startup. App will exit. ", e);
98             System.exit(CANNOT_INVOKE_COMMAND);
99         }
100     }
101
102     @PostConstruct
103     public void postConstruct() {
104         try {
105             DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
106             deployCustomWorkflows(deploymentBuilder);
107         } catch (Exception e) {
108             logger.warn("Unable to invoke deploymentBuilder ", e);
109         }
110     }
111
112     @PreUndeploy
113     public void cleanup(ProcessEngine processEngine, ProcessApplicationInfo processApplicationInfo,
114             List<ProcessEngine> processEngines) {}
115
116     @Bean
117     @Primary
118     public Executor asyncExecutor() {
119         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
120         executor.setTaskDecorator(new MDCTaskDecorator());
121         executor.setCorePoolSize(corePoolSize);
122         executor.setMaxPoolSize(maxPoolSize);
123         executor.setQueueCapacity(queueCapacity);
124         executor.setThreadNamePrefix("Camunda-");
125         executor.initialize();
126         return executor;
127     }
128
129     public void deployCustomWorkflows(DeploymentBuilder deploymentBuilder) {
130         logger.debug("Attempting to deploy custom workflows");
131         try {
132             List<Workflow> workflows = catalogDbClient.findWorkflowBySource(SDC_SOURCE);
133             if (workflows != null && !workflows.isEmpty()) {
134                 for (Workflow workflow : workflows) {
135                     String workflowName = workflow.getName();
136                     String workflowBody = workflow.getBody();
137                     if (!workflowName.endsWith(BPMN_SUFFIX)) {
138                         workflowName += BPMN_SUFFIX;
139                     }
140                     if (workflowBody != null) {
141                         logger.info(LoggingAnchor.TWO, "Deploying custom workflow", workflowName);
142                         deploymentBuilder.addString(workflowName, workflowBody);
143                     }
144                     deploymentBuilder.enableDuplicateFiltering(true);
145                 }
146                 deploymentBuilder.deploy();
147             }
148         } catch (Exception e) {
149             logger.warn("Unable to deploy custom workflows ", e);
150         }
151     }
152 }