1 package org.onap.msb.apiroute.wrapper.consulextend.expose;
3 import java.util.ArrayList;
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;
15 import com.orbitz.consul.model.ConsulResponse;
17 public class CheckTagAndAutoStopWatchFilter implements
18 WatchTask.Filter<List<ServiceHealth>> {
20 private final static Logger LOGGER = LoggerFactory
21 .getLogger(CheckTagAndAutoStopWatchFilter.class);
23 private final String serviceName;
25 public CheckTagAndAutoStopWatchFilter(final String serviceName) {
26 this.serviceName = serviceName;
29 // from consul,the response data:List<ServiceHealth>
30 // filter ServiceHealth list and find the ServiceHealths which satisfy the
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
36 public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
37 // TODO Auto-generated method stub
39 // find #ServiceHealth# which satisfy the tag conditions
40 List<ServiceHealth> satisfyList = getSatisfyList(object);
42 // no satisfied ServiceHealth
43 if (satisfyList.isEmpty()) {
45 LOGGER.info("put delete service["
47 + "] to service queue :because of NO tag meet the conditions");
50 writeServiceToQueue(object.getResponse(),
51 ServiceData.Operate.delete);
53 //SyncDataManager.stopWatchService(serviceName);
57 LOGGER.info("put update service["
59 + "] to service queue :which tags meet the conditions");
61 // put the satisfy list to queue
62 writeServiceToQueue(satisfyList, ServiceData.Operate.update);
67 private List<ServiceHealth> getSatisfyList(
68 ConsulResponse<List<ServiceHealth>> object) {
69 List<ServiceHealth> satisfyList = new ArrayList<ServiceHealth>();
70 for (ServiceHealth health : object.getResponse()) {
72 if (ServiceFilter.getInstance().isFilterCheck(health)) {
73 satisfyList.add(health);
80 private void writeServiceToQueue(List<ServiceHealth> serviceData,
82 ServiceData<List<ServiceHealth>> data = new ServiceData<List<ServiceHealth>>();
83 data.setOperate(operate);
84 data.setDataType(ServiceData.DataType.service);
85 data.setData(serviceData);
89 QueueManager.getInstance().putIn(data);
90 } catch (InterruptedException e) {
91 LOGGER.warn("put " + operate + " service[" + serviceName
92 + "] to service queue interrupted ", e);