5730b4b3f25999c46d062adede905078e34d1619
[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.ArrayList;
19 import java.util.List;
20
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;
30
31 import com.orbitz.consul.model.ConsulResponse;
32 import com.orbitz.consul.model.health.ImmutableNode;
33
34
35
36 public class CheckServiceDataEmptyAndAutoStopWatchFilter implements
37                 WatchTask.Filter<List<ServiceHealth>> {
38
39         private final static Logger LOGGER = LoggerFactory
40                         .getLogger(CheckServiceDataEmptyAndAutoStopWatchFilter.class);
41         private final String serviceName;
42
43         public CheckServiceDataEmptyAndAutoStopWatchFilter(
44                         final String serviceName) {
45                 this.serviceName = serviceName;
46         }
47
48         @Override
49         public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
50                 // TODO Auto-generated method stub
51                 boolean result = check(object);
52
53                 if (!result) {
54                         // create delete
55                         writeServiceToQueue4Del();
56                         // stop watch
57                         SyncDataManager.stopWatchService(serviceName);
58                 }
59
60                 return result;
61         }
62
63         // when:
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;
70
71                 if (object == null || object.getResponse() == null
72                                 || object.getResponse().size() == 0) {
73                         LOGGER.info("check service-{},its data is empty",
74                                         serviceName);
75                         return false;
76                 }
77
78                 return result;
79         }
80
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);
85                 
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()
90                                 .service(service)
91                                 .node(ImmutableNode.builder().node("").address("").build())
92                                 .build();
93                 List<ServiceHealth> serviceHealthList = new ArrayList<ServiceHealth>();
94                 serviceHealthList.add(serviceHealth);
95
96                 data.setData(serviceHealthList);
97
98                 LOGGER.info("put delete service["
99                                 + serviceName
100                                 + "] to service queue :because of deleted ");
101
102                 try {
103                         QueueManager.getInstance().putIn(data);
104                 } catch (InterruptedException e) {
105                         // TODO Auto-generated catch block
106                         LOGGER.warn(
107                                         "put delete service["
108                                                         + serviceName
109                                                         + "]  to  service queue interrupted because of deleted:",
110                                         e);
111                 }
112         }
113 }