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.ImmutableService;
23 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ImmutableServiceHealth;
24 import org.onap.msb.apiroute.wrapper.consulextend.model.health.Service;
25 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
26 import org.onap.msb.apiroute.wrapper.queue.QueueManager;
27 import org.onap.msb.apiroute.wrapper.queue.ServiceData;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.orbitz.consul.model.ConsulResponse;
32 import com.orbitz.consul.model.health.ImmutableNode;
36 public class CheckServiceDataEmptyAndAutoStopWatchFilter implements
37 WatchTask.Filter<List<ServiceHealth>> {
39 private final static Logger LOGGER = LoggerFactory
40 .getLogger(CheckServiceDataEmptyAndAutoStopWatchFilter.class);
41 private final String serviceName;
43 public CheckServiceDataEmptyAndAutoStopWatchFilter(
44 final String serviceName) {
45 this.serviceName = serviceName;
49 public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
50 // TODO Auto-generated method stub
51 boolean result = check(object);
55 writeServiceToQueue4Del();
57 SyncDataManager.stopWatchService(serviceName);
64 // 1)service had been deleted
65 // 2)service Health check was not passing
66 // single service return [],size==0
67 // stop this service watching task and create delete event
68 private boolean check(ConsulResponse<List<ServiceHealth>> object) {
69 boolean result = true;
71 if (object == null || object.getResponse() == null
72 || object.getResponse().size() == 0) {
73 LOGGER.info("check service-{},its data is empty",
81 private void writeServiceToQueue4Del() {
82 ServiceData<List<ServiceHealth>> data = new ServiceData<List<ServiceHealth>>();
83 data.setDataType(ServiceData.DataType.service);
84 data.setOperate(ServiceData.Operate.delete);
86 // tell the subsequent operation the service name which will be deleted
87 Service service = ImmutableService.builder().id("").port(0).address("")
88 .service(serviceName).addTags("").createIndex(0).modifyIndex(0).build();
89 ServiceHealth serviceHealth = ImmutableServiceHealth.builder()
91 .node(ImmutableNode.builder().node("").address("").build())
93 List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
94 serviceHealthList.add(serviceHealth);
96 data.setData(serviceHealthList);
98 LOGGER.info("put delete service["
100 + "] to service queue :because of deleted ");
103 QueueManager.getInstance().putIn(data);
104 } catch (InterruptedException e) {
105 // TODO Auto-generated catch block
107 "put delete service["
109 + "] to service queue interrupted because of deleted:",