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.EnableAutoConfiguration;
43 import org.springframework.boot.autoconfigure.SpringBootApplication;
44 import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
45 import org.springframework.boot.builder.SpringApplicationBuilder;
46 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
47 import org.springframework.context.annotation.Bean;
48 import org.springframework.context.annotation.ComponentScan;
49 import org.springframework.context.annotation.ComponentScan.Filter;
50 import org.springframework.context.annotation.FilterType;
51 import org.springframework.context.annotation.Primary;
52 import org.springframework.scheduling.annotation.EnableAsync;
53 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
55 import javax.annotation.PostConstruct;
56 import java.util.List;
57 import java.util.concurrent.Executor;
59 import static java.util.Collections.singletonMap;
60 import static org.springframework.boot.context.config.ConfigFileApplicationListener.*;
66 @SpringBootApplication
68 @ComponentScan(basePackages = {"org.onap"}, nameGenerator = DefaultToShortClassNameBeanNameGenerator.class,
69 excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = SpringBootApplication.class)})
70 @EnableAutoConfiguration(exclude= FreeMarkerAutoConfiguration.class)
71 public class MSOInfrastructureApplication extends SpringBootServletInitializer {
73 private static final String ADDITIONAL_CONFIG = "file:/camunda/app/config/override.yaml";
74 private static final Logger logger = LoggerFactory.getLogger(MSOInfrastructureApplication.class);
77 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
79 .sources(MSOInfrastructureApplication.class)
80 .properties(singletonMap(CONFIG_ADDITIONAL_LOCATION_PROPERTY, ADDITIONAL_CONFIG));
83 private ProcessEngine processEngine;
86 private CatalogDbClient catalogDbClient;
88 @Value("${mso.async.core-pool-size}")
89 private int corePoolSize;
91 @Value("${mso.async.max-pool-size}")
92 private int maxPoolSize;
94 @Value("${mso.async.queue-capacity}")
95 private int queueCapacity;
97 private static final String LOGS_DIR = "logs_dir";
98 private static final String BPMN_SUFFIX = ".bpmn";
99 private static final String SDC_SOURCE = "sdc";
100 private static final int CANNOT_INVOKE_COMMAND = 126;
103 private static void setLogsDir() {
104 if (System.getProperty(LOGS_DIR) == null) {
105 System.getProperties().setProperty(LOGS_DIR, "./logs/bpmn/");
109 public static void main(String... args) {
111 SpringApplication.run(MSOInfrastructureApplication.class, args);
112 System.getProperties().setProperty("mso.config.path", ".");
114 } catch (Exception e) {
115 logger.error("Exception has occurred during application startup. App will exit. ", e);
116 System.exit(CANNOT_INVOKE_COMMAND);
121 public void postConstruct() {
122 DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
124 // DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
125 // deployCustomWorkflows(deploymentBuilder);
126 // } catch (Exception e) {
127 // logger.warn("Unable to invoke deploymentBuilder: " + e.getMessage());
132 // public void cleanup(ProcessEngine processEngine, ProcessApplicationInfo processApplicationInfo,
133 // List<ProcessEngine> processEngines) {}
137 public Executor asyncExecutor() {
138 ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
139 executor.setTaskDecorator(new MDCTaskDecorator());
140 executor.setCorePoolSize(corePoolSize);
141 executor.setMaxPoolSize(maxPoolSize);
142 executor.setQueueCapacity(queueCapacity);
143 executor.setThreadNamePrefix("Camunda-");
144 executor.initialize();
148 public void deployCustomWorkflows(DeploymentBuilder deploymentBuilder) {
149 logger.info("Attempting to deploy custom workflows");
151 List<Workflow> workflows = catalogDbClient.findWorkflowBySource(SDC_SOURCE);
152 logger.info("SDC workflows: {}", workflows );
153 if (workflows != null && !workflows.isEmpty()) {
154 for (Workflow workflow : workflows) {
155 String workflowName = workflow.getName();
156 String workflowBody = workflow.getBody();
157 if (!workflowName.endsWith(BPMN_SUFFIX)) {
158 workflowName += BPMN_SUFFIX;
160 if (workflowBody != null) {
161 logger.info(LoggingAnchor.TWO, "Deploying custom workflow", workflowName);
162 deploymentBuilder.addString(workflowName, workflowBody);
164 deploymentBuilder.enableDuplicateFiltering(true);
166 deploymentBuilder.deploy();
168 } catch (Exception e) {
169 logger.error("Unable to deploy custom workflows ", e);