2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.infrastructure;
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;
56 @SpringBootApplication
58 @ComponentScan(basePackages = {"org.onap"}, nameGenerator = DefaultToShortClassNameBeanNameGenerator.class,
59 excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = SpringBootApplication.class)})
61 public class MSOInfrastructureApplication {
63 private static final Logger logger = LoggerFactory.getLogger(MSOInfrastructureApplication.class);
65 private ProcessEngine processEngine;
68 private CatalogDbClient catalogDbClient;
70 @Value("${mso.async.core-pool-size}")
71 private int corePoolSize;
73 @Value("${mso.async.max-pool-size}")
74 private int maxPoolSize;
76 @Value("${mso.async.queue-capacity}")
77 private int queueCapacity;
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;
85 private static void setLogsDir() {
86 if (System.getProperty(LOGS_DIR) == null) {
87 System.getProperties().setProperty(LOGS_DIR, "./logs/bpmn/");
91 public static void main(String... args) {
93 SpringApplication.run(MSOInfrastructureApplication.class, args);
94 System.getProperties().setProperty("mso.config.path", ".");
96 } catch (Exception e) {
97 logger.error("Exception has occurred during application startup. App will exit. ", e);
98 System.exit(CANNOT_INVOKE_COMMAND);
103 public void postConstruct() {
105 DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
106 deployCustomWorkflows(deploymentBuilder);
107 } catch (Exception e) {
108 logger.warn("Unable to invoke deploymentBuilder ", e);
113 public void cleanup(ProcessEngine processEngine, ProcessApplicationInfo processApplicationInfo,
114 List<ProcessEngine> processEngines) {}
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();
129 public void deployCustomWorkflows(DeploymentBuilder deploymentBuilder) {
130 logger.debug("Attempting to deploy custom workflows");
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;
140 if (workflowBody != null) {
141 logger.info(LoggingAnchor.TWO, "Deploying custom workflow", workflowName);
142 deploymentBuilder.addString(workflowName, workflowBody);
144 deploymentBuilder.enableDuplicateFiltering(true);
146 deploymentBuilder.deploy();
148 } catch (Exception e) {
149 logger.warn("Unable to deploy custom workflows ", e);