6dfc86a8874831a8b497bf524403e87aa9e3501f
[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.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;
29
30 import com.orbitz.consul.model.ConsulResponse;
31
32 public class CheckTagAndAutoStopWatchFilter implements
33                 WatchTask.Filter<List<ServiceHealth>> {
34
35         private final static Logger LOGGER = LoggerFactory
36                         .getLogger(CheckTagAndAutoStopWatchFilter.class);
37
38         private final String serviceName;
39
40         public CheckTagAndAutoStopWatchFilter(final String serviceName) {
41                 this.serviceName = serviceName;
42         }
43
44         // from consul,the response data:List<ServiceHealth>
45         // filter ServiceHealth list and find the ServiceHealths which satisfy the
46         // tags conditions
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
50         @Override
51         public boolean filter(ConsulResponse<List<ServiceHealth>> object) {
52                 // TODO Auto-generated method stub
53
54                 // find #ServiceHealth# which satisfy the tag conditions
55                 List<ServiceHealth> satisfyList = getSatisfyList(object);
56
57                 // no satisfied ServiceHealth
58                 if (satisfyList.isEmpty()) {
59                         
60                         LOGGER.info("put delete service["
61                                         + serviceName
62                                         + "] to service queue :because of NO tag meet the conditions");
63                         
64                         // create delete
65                         writeServiceToQueue(object.getResponse(),
66                                         ServiceData.Operate.delete);
67                         // stop watch
68                         //SyncDataManager.stopWatchService(serviceName);
69                         return false;
70                 }
71
72                 LOGGER.info("put update service["
73                                 + serviceName
74                                 + "] to service queue :which tags meet the conditions");
75                 
76                 // put the satisfy list to queue
77                 writeServiceToQueue(satisfyList, ServiceData.Operate.update);
78
79                 return true;
80         }
81
82         private List<ServiceHealth> getSatisfyList(
83                         ConsulResponse<List<ServiceHealth>> object) {
84                 List<ServiceHealth> satisfyList = new ArrayList<ServiceHealth>();
85                 for (ServiceHealth health : object.getResponse()) {
86                 
87                         if (ServiceFilter.getInstance().isFilterCheck(health)) {
88                                 satisfyList.add(health);
89                         }
90                 }
91
92                 return satisfyList;
93         }
94
95         private void writeServiceToQueue(List<ServiceHealth> serviceData,
96                         Operate operate) {
97                 ServiceData<List<ServiceHealth>> data = new ServiceData<List<ServiceHealth>>();
98                 data.setOperate(operate);
99                 data.setDataType(ServiceData.DataType.service);
100                 data.setData(serviceData);
101                 
102                 
103                 try {
104                         QueueManager.getInstance().putIn(data);
105                 } catch (InterruptedException e) {
106                         LOGGER.warn("put " + operate + " service[" + serviceName
107                                         + "]  to  service queue interrupted ", e);
108                 }
109
110         }
111 }