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.ArrayList;
19 import java.util.List;
21 import org.onap.msb.apiroute.SyncDataManager;
22 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
23 import org.onap.msb.apiroute.wrapper.queue.QueueManager;
24 import org.onap.msb.apiroute.wrapper.queue.ServiceData;
25 import org.onap.msb.apiroute.wrapper.queue.ServiceData.Operate;
26 import org.onap.msb.apiroute.wrapper.util.ServiceFilter;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.orbitz.consul.model.ConsulResponse;
32 public class CheckTagAndAutoStopWatchFilter implements
33 WatchTask.Filter<List<ServiceHealth>> {
35 private final static Logger LOGGER = LoggerFactory
36 .getLogger(CheckTagAndAutoStopWatchFilter.class);
38 private final String serviceName;
40 public CheckTagAndAutoStopWatchFilter(final String serviceName) {
41 this.serviceName = serviceName;
44 // from consul,the response data:List<ServiceHealth>
45 // filter ServiceHealth list and find the ServiceHealths which satisfy the
47 // 1)if all ServiceHealth don't satisfy,create delete event and stop watch
48 // 2)if have some ServiceHealths satisfy the tags conditions,create update
49 // event and send these ServiceHealths
51 public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
52 // TODO Auto-generated method stub
54 // find #ServiceHealth# which satisfy the tag conditions
55 List<ServiceHealth> satisfyList = getSatisfyList(object);
57 // no satisfied ServiceHealth
58 if (satisfyList.isEmpty()) {
60 LOGGER.info("put delete service["
62 + "] to service queue :because of NO tag meet the conditions");
65 writeServiceToQueue(object.getResponse(),
66 ServiceData.Operate.delete);
68 //SyncDataManager.stopWatchService(serviceName);
72 LOGGER.info("put update service["
74 + "] to service queue :which tags meet the conditions");
76 // put the satisfy list to queue
77 writeServiceToQueue(satisfyList, ServiceData.Operate.update);
82 private List<ServiceHealth> getSatisfyList(
83 ConsulResponse<List<ServiceHealth>> object) {
84 List<ServiceHealth> satisfyList = new ArrayList<ServiceHealth>();
85 for (ServiceHealth health : object.getResponse()) {
87 if (ServiceFilter.getInstance().isFilterCheck(health)) {
88 satisfyList.add(health);
95 private void writeServiceToQueue(List<ServiceHealth> serviceData,
97 ServiceData<List<ServiceHealth>> data = new ServiceData<List<ServiceHealth>>();
98 data.setOperate(operate);
99 data.setDataType(ServiceData.DataType.service);
100 data.setData(serviceData);
104 QueueManager.getInstance().putIn(data);
105 } catch (InterruptedException e) {
106 LOGGER.warn("put " + operate + " service[" + serviceName
107 + "] to service queue interrupted ", e);