Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / consulextend / expose / CheckTagAndAutoStopWatchFilter.java
1 package org.onap.msb.apiroute.wrapper.consulextend.expose;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.onap.msb.apiroute.SyncDataManager;
7 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
8 import org.onap.msb.apiroute.wrapper.queue.QueueManager;
9 import org.onap.msb.apiroute.wrapper.queue.ServiceData;
10 import org.onap.msb.apiroute.wrapper.queue.ServiceData.Operate;
11 import org.onap.msb.apiroute.wrapper.util.ServiceFilter;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import com.orbitz.consul.model.ConsulResponse;
16
17 public class CheckTagAndAutoStopWatchFilter implements
18                 WatchTask.Filter<List<ServiceHealth>> {
19
20         private final static Logger LOGGER = LoggerFactory
21                         .getLogger(CheckTagAndAutoStopWatchFilter.class);
22
23         private final String serviceName;
24
25         public CheckTagAndAutoStopWatchFilter(final String serviceName) {
26                 this.serviceName = serviceName;
27         }
28
29         // from consul,the response data:List<ServiceHealth>
30         // filter ServiceHealth list and find the ServiceHealths which satisfy the
31         // tags conditions
32         // 1)if all ServiceHealth don't satisfy,create delete event and stop watch
33         // 2)if have some ServiceHealths satisfy the tags conditions,create update
34         // event and send these ServiceHealths
35         @Override
36         public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
37                 // TODO Auto-generated method stub
38
39                 // find #ServiceHealth# which satisfy the tag conditions
40                 List<ServiceHealth> satisfyList = getSatisfyList(object);
41
42                 // no satisfied ServiceHealth
43                 if (satisfyList.isEmpty()) {
44                         
45                         LOGGER.info("put delete service["
46                                         + serviceName
47                                         + "] to service queue :because of NO tag meet the conditions");
48                         
49                         // create delete
50                         writeServiceToQueue(object.getResponse(),
51                                         ServiceData.Operate.delete);
52                         // stop watch
53                         //SyncDataManager.stopWatchService(serviceName);
54                         return false;
55                 }
56
57                 LOGGER.info("put update service["
58                                 + serviceName
59                                 + "] to service queue :which tags meet the conditions");
60                 
61                 // put the satisfy list to queue
62                 writeServiceToQueue(satisfyList, ServiceData.Operate.update);
63
64                 return true;
65         }
66
67         private List<ServiceHealth> getSatisfyList(
68                         ConsulResponse<List<ServiceHealth>> object) {
69                 List<ServiceHealth> satisfyList = new ArrayList<ServiceHealth>();
70                 for (ServiceHealth health : object.getResponse()) {
71                 
72                         if (ServiceFilter.getInstance().isFilterCheck(health)) {
73                                 satisfyList.add(health);
74                         }
75                 }
76
77                 return satisfyList;
78         }
79
80         private void writeServiceToQueue(List<ServiceHealth> serviceData,
81                         Operate operate) {
82                 ServiceData<List<ServiceHealth>> data = new ServiceData<List<ServiceHealth>>();
83                 data.setOperate(operate);
84                 data.setDataType(ServiceData.DataType.service);
85                 data.setData(serviceData);
86                 
87                 
88                 try {
89                         QueueManager.getInstance().putIn(data);
90                 } catch (InterruptedException e) {
91                         LOGGER.warn("put " + operate + " service[" + serviceName
92                                         + "]  to  service queue interrupted ", e);
93                 }
94
95         }
96 }