1 package org.onap.msb.apiroute.wrapper.consulextend.expose;
4 import java.util.concurrent.atomic.AtomicReference;
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;
12 import com.google.common.collect.ImmutableList;
13 import com.orbitz.consul.model.ConsulResponse;
14 import com.orbitz.consul.model.health.HealthCheck;
16 public class ServiceModifyIndexFilter implements WatchTask.Filter<List<ServiceHealth>> {
18 private final AtomicReference<ImmutableList<ServiceHealth>> lastResponse =
19 new AtomicReference<ImmutableList<ServiceHealth>>(ImmutableList.<ServiceHealth>of());
21 private final static Logger LOGGER = LoggerFactory.getLogger(ServiceModifyIndexFilter.class);
25 public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
26 // TODO Auto-generated method stub
28 List<ServiceHealth> newList=object.getResponse();
29 if(realFilter(newList)){
30 lastResponse.set(ImmutableList.copyOf(newList));
37 private boolean realFilter(List<ServiceHealth> newList) {
38 // 1)判断list的size,不等则改变
39 if (newList.size() != lastResponse.get().size()) {
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());
51 // 2)循环服务实例判断服务内容和健康检查是否改变
52 for (ServiceHealth newData : newList) {
53 ServiceHealth sameIdOldData = findSameIdInOldList(newData);
55 if (sameIdOldData == null) {
57 LOGGER.info(newData.getService().getId()
58 + " is a new service instance.the createindex:"
59 + newData.getService().getCreateIndex()
61 + newData.getService().getModifyIndex());
66 // 若在oldlist中存在,则比较ModifyIndex的值和健康检查状态.不等则改变
67 if(!compareService(newData,sameIdOldData)){
68 LOGGER.info(newData.getService().getId() +" instance is change because of modifyIndex or health check" );
79 private boolean compareService(ServiceHealth oldData,ServiceHealth newData) {
81 return compareServiceInfo(oldData.getService(),newData.getService()) &&
82 compareServiceHealthStatus(oldData.getChecks(),newData.getChecks());
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());
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;
106 private ServiceHealth findSameIdInOldList(ServiceHealth newData) {
107 for (ServiceHealth oldData : lastResponse.get()) {
108 if (oldData.getService().getId().equals(newData.getService().getId())) {
116 public boolean resetModifyIndex() {
117 // clear last response
118 lastResponse.set(ImmutableList.<ServiceHealth>of());