6900f3099dee04719407523e6d1bc70d1e2eacf6
[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 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;
54
55 import javax.annotation.PostConstruct;
56 import java.util.List;
57 import java.util.concurrent.Executor;
58
59 import static java.util.Collections.singletonMap;
60 import static org.springframework.boot.context.config.ConfigFileApplicationListener.*;
61 /**
62  * @since Version 1.0
63  *
64  */
65
66 @SpringBootApplication
67 @EnableAsync
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 {
72
73     private static final Logger logger = LoggerFactory.getLogger(MSOInfrastructureApplication.class);
74     
75     @Override
76     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
77         return application
78                 .sources(MSOInfrastructureApplication.class);
79     }
80     @Autowired
81     private ProcessEngine processEngine;
82
83     @Autowired
84     private CatalogDbClient catalogDbClient;
85
86     @Value("${mso.async.core-pool-size}")
87     private int corePoolSize;
88
89     @Value("${mso.async.max-pool-size}")
90     private int maxPoolSize;
91
92     @Value("${mso.async.queue-capacity}")
93     private int queueCapacity;
94
95     private static final String LOGS_DIR = "logs_dir";
96     private static final String BPMN_SUFFIX = ".bpmn";
97     private static final String SDC_SOURCE = "sdc";
98     private static final int CANNOT_INVOKE_COMMAND = 126;
99
100
101     private static void setLogsDir() {
102         if (System.getProperty(LOGS_DIR) == null) {
103             System.getProperties().setProperty(LOGS_DIR, "./logs/bpmn/");
104         }
105     }
106
107     public static void main(String... args) {
108         try {
109             SpringApplication.run(MSOInfrastructureApplication.class, args);
110             System.getProperties().setProperty("mso.config.path", ".");
111             setLogsDir();
112         } catch (Exception e) {
113             logger.error("Exception has occurred during application startup. App will exit. ", e);
114             System.exit(CANNOT_INVOKE_COMMAND);
115         }
116     }
117
118     @PostConstruct
119     public void postConstruct() {
120         DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
121 //        try {
122 //            DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
123 //            deployCustomWorkflows(deploymentBuilder);
124 //        } catch (Exception e) {
125 //            logger.warn("Unable to invoke deploymentBuilder: " + e.getMessage());
126 //        }
127     }
128
129 //    @PreUndeploy
130 //    public void cleanup(ProcessEngine processEngine, ProcessApplicationInfo processApplicationInfo,
131 //            List<ProcessEngine> processEngines) {}
132
133     @Bean
134     @Primary
135     public Executor asyncExecutor() {
136         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
137         executor.setTaskDecorator(new MDCTaskDecorator());
138         executor.setCorePoolSize(corePoolSize);
139         executor.setMaxPoolSize(maxPoolSize);
140         executor.setQueueCapacity(queueCapacity);
141         executor.setThreadNamePrefix("Camunda-");
142         executor.initialize();
143         return executor;
144     }
145
146     public void deployCustomWorkflows(DeploymentBuilder deploymentBuilder) {
147         logger.info("Attempting to deploy custom workflows");
148         try {
149             List<Workflow> workflows = catalogDbClient.findWorkflowBySource(SDC_SOURCE);
150                         logger.info("SDC workflows: {}", workflows );
151             if (workflows != null && !workflows.isEmpty()) {
152                 for (Workflow workflow : workflows) {
153                     String workflowName = workflow.getName();
154                     String workflowBody = workflow.getBody();
155                     if (!workflowName.endsWith(BPMN_SUFFIX)) {
156                         workflowName += BPMN_SUFFIX;
157                     }
158                     if (workflowBody != null) {
159                         logger.info(LoggingAnchor.TWO, "Deploying custom workflow", workflowName);
160                         deploymentBuilder.addString(workflowName, workflowBody);
161                     }
162                     deploymentBuilder.enableDuplicateFiltering(true);
163                 }
164                 deploymentBuilder.deploy();
165             }
166         } catch (Exception e) {
167             logger.error("Unable to deploy custom workflows ", e);
168         }
169     }
170 }