1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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;
18 import java.util.List;
19 import java.util.concurrent.atomic.AtomicReference;
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;
27 import com.google.common.collect.ImmutableList;
28 import com.orbitz.consul.model.ConsulResponse;
29 import com.orbitz.consul.model.health.HealthCheck;
31 public class ServiceModifyIndexFilter implements WatchTask.Filter<List<ServiceHealth>> {
33 private final AtomicReference<ImmutableList<ServiceHealth>> lastResponse =
34 new AtomicReference<ImmutableList<ServiceHealth>>(ImmutableList.<ServiceHealth>of());
36 private final static Logger LOGGER = LoggerFactory.getLogger(ServiceModifyIndexFilter.class);
40 public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
41 // TODO Auto-generated method stub
43 List<ServiceHealth> newList=object.getResponse();
44 if(realFilter(newList)){
45 lastResponse.set(ImmutableList.copyOf(newList));
52 private boolean realFilter(List<ServiceHealth> newList) {
53 // 1)判断list的size,不等则改变
54 if (newList.size() != lastResponse.get().size()) {
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());
66 // 2)循环服务实例判断服务内容和健康检查是否改变
67 for (ServiceHealth newData : newList) {
68 ServiceHealth sameIdOldData = findSameIdInOldList(newData);
70 if (sameIdOldData == null) {
72 LOGGER.info(newData.getService().getId()
73 + " is a new service instance.the createindex:"
74 + newData.getService().getCreateIndex()
76 + newData.getService().getModifyIndex());
81 // 若在oldlist中存在,则比较ModifyIndex的值和健康检查状态.不等则改变
82 if(!compareService(newData,sameIdOldData)){
83 LOGGER.info(newData.getService().getId() +" instance is change because of modifyIndex or health check" );
94 private boolean compareService(ServiceHealth oldData,ServiceHealth newData) {
96 return compareServiceInfo(oldData.getService(),newData.getService()) &&
97 compareServiceHealthStatus(oldData.getChecks(),newData.getChecks());
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());
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;
121 private ServiceHealth findSameIdInOldList(ServiceHealth newData) {
122 for (ServiceHealth oldData : lastResponse.get()) {
123 if (oldData.getService().getId().equals(newData.getService().getId())) {
131 public boolean resetModifyIndex() {
132 // clear last response
133 lastResponse.set(ImmutableList.<ServiceHealth>of());