Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / consulextend / expose / ServiceModifyIndexFilter.java
1 package org.onap.msb.apiroute.wrapper.consulextend.expose;
2
3 import java.util.List;
4 import java.util.concurrent.atomic.AtomicReference;
5
6 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
7 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
8 import org.onap.msb.apiroute.wrapper.util.ServiceFilter;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import com.google.common.collect.ImmutableList;
13 import com.orbitz.consul.model.ConsulResponse;
14 import com.orbitz.consul.model.health.HealthCheck;
15
16 public class ServiceModifyIndexFilter implements WatchTask.Filter<List<ServiceHealth>> {
17
18   private final AtomicReference<ImmutableList<ServiceHealth>> lastResponse =
19       new AtomicReference<ImmutableList<ServiceHealth>>(ImmutableList.<ServiceHealth>of());
20
21   private final static Logger LOGGER = LoggerFactory.getLogger(ServiceModifyIndexFilter.class);
22
23
24   @Override
25   public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
26     // TODO Auto-generated method stub
27
28     List<ServiceHealth> newList=object.getResponse();
29     if(realFilter(newList)){
30       lastResponse.set(ImmutableList.copyOf(newList));
31       return true;
32     }
33     
34     return false;
35   }
36
37   private boolean realFilter(List<ServiceHealth> newList) {
38     // 1)判断list的size,不等则改变
39     if (newList.size() != lastResponse.get().size()) {
40       // 第一次不打印
41       if (lastResponse.get().size() != 0) {
42         LOGGER.info(newList.get(0).getService().getService()
43             + " instance count is different.new_count:" + newList.size() + " old_count:"
44             + lastResponse.get().size());
45       }
46
47       return true;
48     }
49     
50     
51     // 2)循环服务实例判断服务内容和健康检查是否改变
52     for (ServiceHealth newData : newList) {
53       ServiceHealth sameIdOldData = findSameIdInOldList(newData);
54       // 若在oldlist中不存在,则改变
55       if (sameIdOldData == null) {
56
57           LOGGER.info(newData.getService().getId()
58                   + " is a new service instance.the createindex:"
59                   + newData.getService().getCreateIndex()
60                   + " the modifyIndex:"
61                   + newData.getService().getModifyIndex());
62
63           return true;
64       }
65
66       // 若在oldlist中存在,则比较ModifyIndex的值和健康检查状态.不等则改变
67       if(!compareService(newData,sameIdOldData)){
68         LOGGER.info(newData.getService().getId() +" instance  is change because of modifyIndex  or health check" );
69         return true;
70       }
71     }
72     
73     return false;
74
75
76   }
77
78
79   private boolean compareService(ServiceHealth oldData,ServiceHealth newData) {
80     
81     return compareServiceInfo(oldData.getService(),newData.getService()) && 
82         compareServiceHealthStatus(oldData.getChecks(),newData.getChecks());
83     
84   }
85   
86
87
88   private boolean compareServiceInfo(Service oldServiceInfo, Service newServiceInfo) {
89     if (oldServiceInfo.getModifyIndex() != newServiceInfo.getModifyIndex()) {
90       LOGGER.info(newServiceInfo.getId() + " new_modifyIndex:"
91           + newServiceInfo.getModifyIndex() + " old_modifyIndex:"
92           + oldServiceInfo.getModifyIndex());
93       return false;
94     }
95      return true;
96   }
97   
98   private boolean compareServiceHealthStatus(List<HealthCheck>  oldData, List<HealthCheck>  newData) {
99     boolean oldHealthCheck=ServiceFilter.getInstance().isFilterHealthCheck(oldData);
100     boolean newHealthCheck=ServiceFilter.getInstance().isFilterHealthCheck(newData);
101     return oldHealthCheck==newHealthCheck;     
102      
103   }
104
105
106   private ServiceHealth findSameIdInOldList(ServiceHealth newData) {
107     for (ServiceHealth oldData : lastResponse.get()) {
108       if (oldData.getService().getId().equals(newData.getService().getId())) {
109         return oldData;
110       }
111     }
112
113     return null;
114   }
115
116   public boolean resetModifyIndex() {
117     // clear last response
118     lastResponse.set(ImmutableList.<ServiceHealth>of());
119     return true;
120   }
121 }