6f90b8004126a3fb44f78c6d861a1fd1b4a2f895
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.consulextend.expose;
17
18 import java.util.List;
19 import java.util.concurrent.atomic.AtomicReference;
20
21 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
22 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
23 import org.onap.msb.apiroute.wrapper.util.ServiceFilter;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.collect.ImmutableList;
28 import com.orbitz.consul.model.ConsulResponse;
29 import com.orbitz.consul.model.health.HealthCheck;
30
31 public class ServiceModifyIndexFilter implements WatchTask.Filter<List<ServiceHealth>> {
32
33   private final AtomicReference<ImmutableList<ServiceHealth>> lastResponse =
34       new AtomicReference<ImmutableList<ServiceHealth>>(ImmutableList.<ServiceHealth>of());
35
36   private final static Logger LOGGER = LoggerFactory.getLogger(ServiceModifyIndexFilter.class);
37
38
39   @Override
40   public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
41     // TODO Auto-generated method stub
42
43     List<ServiceHealth> newList=object.getResponse();
44     if(realFilter(newList)){
45       lastResponse.set(ImmutableList.copyOf(newList));
46       return true;
47     }
48     
49     return false;
50   }
51
52   private boolean realFilter(List<ServiceHealth> newList) {
53     // 1)判断list的size,不等则改变
54     if (newList.size() != lastResponse.get().size()) {
55       // 第一次不打印
56       if (lastResponse.get().size() != 0) {
57         LOGGER.info(newList.get(0).getService().getService()
58             + " instance count is different.new_count:" + newList.size() + " old_count:"
59             + lastResponse.get().size());
60       }
61
62       return true;
63     }
64     
65     
66     // 2)循环服务实例判断服务内容和健康检查是否改变
67     for (ServiceHealth newData : newList) {
68       ServiceHealth sameIdOldData = findSameIdInOldList(newData);
69       // 若在oldlist中不存在,则改变
70       if (sameIdOldData == null) {
71
72           LOGGER.info(newData.getService().getId()
73                   + " is a new service instance.the createindex:"
74                   + newData.getService().getCreateIndex()
75                   + " the modifyIndex:"
76                   + newData.getService().getModifyIndex());
77
78           return true;
79       }
80
81       // 若在oldlist中存在,则比较ModifyIndex的值和健康检查状态.不等则改变
82       if(!compareService(newData,sameIdOldData)){
83         LOGGER.info(newData.getService().getId() +" instance  is change because of modifyIndex  or health check" );
84         return true;
85       }
86     }
87     
88     return false;
89
90
91   }
92
93
94   private boolean compareService(ServiceHealth oldData,ServiceHealth newData) {
95     
96     return compareServiceInfo(oldData.getService(),newData.getService()) && 
97         compareServiceHealthStatus(oldData.getChecks(),newData.getChecks());
98     
99   }
100   
101
102
103   private boolean compareServiceInfo(Service oldServiceInfo, Service newServiceInfo) {
104     if (oldServiceInfo.getModifyIndex() != newServiceInfo.getModifyIndex()) {
105       LOGGER.info(newServiceInfo.getId() + " new_modifyIndex:"
106           + newServiceInfo.getModifyIndex() + " old_modifyIndex:"
107           + oldServiceInfo.getModifyIndex());
108       return false;
109     }
110      return true;
111   }
112   
113   private boolean compareServiceHealthStatus(List<HealthCheck>  oldData, List<HealthCheck>  newData) {
114     boolean oldHealthCheck=ServiceFilter.getInstance().isFilterHealthCheck(oldData);
115     boolean newHealthCheck=ServiceFilter.getInstance().isFilterHealthCheck(newData);
116     return oldHealthCheck==newHealthCheck;     
117      
118   }
119
120
121   private ServiceHealth findSameIdInOldList(ServiceHealth newData) {
122     for (ServiceHealth oldData : lastResponse.get()) {
123       if (oldData.getService().getId().equals(newData.getService().getId())) {
124         return oldData;
125       }
126     }
127
128     return null;
129   }
130
131   public boolean resetModifyIndex() {
132     // clear last response
133     lastResponse.set(ImmutableList.<ServiceHealth>of());
134     return true;
135   }
136 }