added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / service / impl / DeveloperServiceImpl.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 package org.oran.otf.service.impl;\r
18 \r
19 import org.oran.otf.camunda.configuration.OtfCamundaConfiguration;\r
20 import org.oran.otf.camunda.service.CamundaShutdown;\r
21 import org.oran.otf.camunda.service.OtfExternalTaskService;\r
22 import org.oran.otf.camunda.service.OtfWorkflowTaskCleanupService;\r
23 import org.oran.otf.camunda.workflow.utility.WorkflowTask;\r
24 import org.oran.otf.common.utility.http.ResponseUtility;\r
25 import org.oran.otf.service.DeveloperService;\r
26 import com.google.common.base.Strings;\r
27 \r
28 import java.util.Arrays;\r
29 import java.util.Set;\r
30 \r
31 import javax.servlet.http.HttpServletRequest;\r
32 import javax.ws.rs.core.Response;\r
33 \r
34 import org.springframework.beans.factory.annotation.Autowired;\r
35 import org.springframework.beans.factory.annotation.Value;\r
36 import org.springframework.context.event.ContextClosedEvent;\r
37 import org.springframework.context.event.EventListener;\r
38 \r
39 import org.camunda.bpm.BpmPlatform;\r
40 import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;\r
41 import org.camunda.bpm.engine.impl.jobexecutor.JobExecutor;\r
42 import org.slf4j.Logger;\r
43 import org.slf4j.LoggerFactory;\r
44 import org.springframework.stereotype.Service;\r
45 \r
46 @Service\r
47 public class DeveloperServiceImpl implements DeveloperService {\r
48         \r
49   private Logger logger = LoggerFactory.getLogger(DeveloperServiceImpl.class);\r
50   \r
51   @Autowired\r
52   CamundaShutdown camundaShutdown;\r
53   \r
54   @Value("${otf.camunda.graceful-shutdown.wait-time}")\r
55   private int gracefulWaitTime;\r
56 \r
57   private boolean gracefulShutdown = true;\r
58 \r
59   @Override\r
60   public Response workflowTaskCleanup(String enabled) {\r
61     if (Strings.isNullOrEmpty(enabled))\r
62       return ResponseUtility.Build.badRequestWithMessage(\r
63           "Path parameter, enabled, cannot be null or empty.");\r
64 \r
65     OtfWorkflowTaskCleanupService.isEnabled = enabled.equalsIgnoreCase("true");\r
66     return ResponseUtility.Build.okRequestWithMessage(\r
67         "Clean up service set to " + OtfWorkflowTaskCleanupService.isEnabled);\r
68   }\r
69 \r
70   @Override\r
71   public Response externalTaskWorker(String enabled) {\r
72     if (Strings.isNullOrEmpty(enabled)) {\r
73       return ResponseUtility.Build.badRequestWithMessage(\r
74           "Path parameter, enabled, cannot be null or empty.");\r
75     }\r
76 \r
77     OtfExternalTaskService.isEnabled = enabled.equalsIgnoreCase("true");\r
78     return ResponseUtility.Build.okRequestWithMessage(\r
79         "OTF External Task set to " + OtfExternalTaskService.isEnabled);\r
80   }\r
81 \r
82   @Override\r
83   public Response printThreads(HttpServletRequest request) {\r
84     //Logger logger = LoggerFactory.getLogger(HealthServiceImpl.class);\r
85     String message = String.format("Health request from %s.", request.getRemoteAddr());\r
86     logger.info(message);\r
87 \r
88     WorkflowTask.printWorkflowTaskResources();\r
89     logger.info("");\r
90     logger.info("");\r
91     WorkflowTask.printThreadInformation();\r
92 \r
93     return ResponseUtility.Build.okRequestWithMessage(message);\r
94   }\r
95   \r
96   @Override\r
97   public Response activateJobExecutor() {\r
98         JobExecutor jobExecutor = ((ProcessEngineConfigurationImpl) (BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName)).getProcessEngineConfiguration()).getJobExecutor();\r
99     if (!jobExecutor.isActive()) {\r
100                 jobExecutor.start();\r
101     }\r
102         return ResponseUtility.Build.okRequest();\r
103   }\r
104 \r
105   @Override\r
106   public Response deActivateJobExecutor() {\r
107         JobExecutor jobExecutor = ((ProcessEngineConfigurationImpl) (BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName)).getProcessEngineConfiguration()).getJobExecutor();\r
108         if (jobExecutor.isActive()) {\r
109                 jobExecutor.shutdown();\r
110         }\r
111         return ResponseUtility.Build.okRequest();\r
112   }\r
113   \r
114   @Override\r
115   public Response gracefulShutdown() {\r
116           return this.gracefulShutdown ? ResponseUtility.Build.okRequestWithMessage(shutdown()) : ResponseUtility.Build.okRequestWithMessage("Graceful shutdown is disabled.");\r
117   }\r
118 \r
119     @Override\r
120     public Response disableGracefulShutdown() {\r
121         this.gracefulShutdown = false;\r
122         return ResponseUtility.Build.okRequest();\r
123     }\r
124 \r
125     @Override\r
126     public Response enableGracefulShutdown() {\r
127         this.gracefulShutdown = true;\r
128         return ResponseUtility.Build.okRequest();\r
129     }\r
130 \r
131     @EventListener(ContextClosedEvent.class)\r
132   private String shutdown() {\r
133           String message = "Graceful shutdown:";\r
134           String returnMessage = "Graceful shutdown processes terminated: ";\r
135           try {\r
136               //disable external task service\r
137           OtfExternalTaskService.isEnabled = false;\r
138           //disable job executor\r
139                   deActivateJobExecutor();\r
140           logger.info("Disabled job executor and external task service.");\r
141           logger.info("Starting to sleep...");\r
142           Thread.sleep(gracefulWaitTime);\r
143           logger.info("ending to sleep...calling termination service");\r
144                   // Call Termination service\r
145                   Set<String> processInterrupted = camundaShutdown.gracefulShutdown();\r
146           returnMessage = returnMessage + " " + processInterrupted.size();\r
147                   message += String.format("processesInterrupted %s ",\r
148                         Arrays.toString(processInterrupted.toArray()));\r
149 \r
150                   logger.info(message += String.format("processesInterrupted %s ",\r
151                   Arrays.toString(processInterrupted.toArray())));\r
152           } catch (InterruptedException e) {\r
153                   return "Graceful shutdown processes encountered an error.";\r
154           }\r
155           return returnMessage;\r
156   }\r
157 \r
158 \r
159 \r
160   \r
161 }\r