Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / consulextend / expose / WatchServiceHealthTask.java
1 package org.onap.msb.apiroute.wrapper.consulextend.expose;
2
3 import java.math.BigInteger;
4 import java.util.List;
5
6 import org.onap.msb.apiroute.wrapper.consulextend.HealthClient;
7 import org.onap.msb.apiroute.wrapper.consulextend.cache.ServiceHealthCache;
8 import org.onap.msb.apiroute.wrapper.consulextend.cache.ConsulCache.Listener;
9 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import com.orbitz.consul.option.CatalogOptions;
14 import com.orbitz.consul.option.QueryOptions;
15
16 public class WatchServiceHealthTask extends WatchTask<List<ServiceHealth>> {
17         private final static Logger LOGGER = LoggerFactory
18                         .getLogger(WatchServiceHealthTask.class);
19         
20         private ServiceHealthCache serviceHealthCache = null;
21         private String serviceName="";
22
23         public String getServiceName() {
24                 return serviceName;
25         }
26
27         public WatchServiceHealthTask(final HealthClient healthClient,
28                         final String serviceName,final boolean passing,
29                         final CatalogOptions catalogOptions, final int watchSeconds,
30                         final QueryOptions queryOptions) {
31                 initCache(healthClient, serviceName, passing, catalogOptions,
32                                 watchSeconds, queryOptions);
33         }
34
35         public WatchServiceHealthTask(final HealthClient healthClient,
36                         final String serviceName,final boolean passing,
37                         final int watchSeconds)
38
39         {
40                 initCache(healthClient, serviceName, passing, CatalogOptions.BLANK,
41                                 watchSeconds, QueryOptions.BLANK);
42         }
43
44         public WatchServiceHealthTask(final HealthClient healthClient,
45                         final String serviceName, final int watchSeconds)
46
47         {
48                 initCache(healthClient, serviceName, true, CatalogOptions.BLANK,
49                                 watchSeconds, QueryOptions.BLANK);
50         }
51
52         private ServiceHealthCache initCache(final HealthClient healthClient,
53                         final String serviceName,final boolean passing,
54                         final CatalogOptions catalogOptions, final int watchSeconds,
55                         final QueryOptions queryOptions) {
56 //              LOGGER.info("************create {} watch task*****************",serviceName);
57                 this.serviceName = serviceName;         
58                 serviceHealthCache = ServiceHealthCache.newCache(healthClient,
59                                 serviceName, passing, catalogOptions, watchSeconds,
60                                 queryOptions);
61
62                 serviceHealthCache
63                                 .addListener((Listener<List<ServiceHealth>>) new InternalListener());
64
65                 return serviceHealthCache;
66         }
67
68         public boolean startWatch() {
69                 
70                 if(serviceHealthCache!=null)
71                 {
72                         try {
73                                 serviceHealthCache.start();
74                                 LOGGER.info("************start {} watch task*****************",serviceName);
75                                 return true;
76                         } catch (Exception e) {
77                                 // TODO Auto-generated catch block
78                                 LOGGER.warn("start service watch failed:", e);
79                         }
80                 }
81                 
82                 return false;
83                 
84         }
85
86         public boolean stopWatch(){
87                 if (serviceHealthCache != null) {
88                         try {
89                                 serviceHealthCache.stop();
90                                 LOGGER.info("************stop {} watch task*****************",serviceName);
91                                 return true;
92                         } catch (Exception e) {
93                                 // TODO Auto-generated catch block
94                                 LOGGER.warn("stop service watch failed:", e);
95                         }
96                 }
97                 
98                 return false;
99         }
100
101
102         public boolean resetIndex()
103         {
104                 if (LOGGER.isDebugEnabled()) {
105                         LOGGER.debug("reset " + serviceName + " consul index");
106                 }
107                 
108                 //reset consul index
109                 serviceHealthCache.updateIndex(BigInteger.valueOf(0));
110                 
111                 
112                 //reset modify index
113                 for (WatchTask.Filter<List<ServiceHealth>> filter : getAllFilters()) {
114                         if (filter instanceof ServiceModifyIndexFilter) {
115                                 if (LOGGER.isDebugEnabled()) {
116                                         LOGGER.debug("reset " + serviceName + " modify index");
117                                 }
118                                 return ((ServiceModifyIndexFilter) filter).resetModifyIndex();
119                         }
120                 }
121                 
122                 if (LOGGER.isDebugEnabled()) {
123                         LOGGER.debug("reset modify index.did not find filter:" + serviceName);
124                 }
125                 
126                 return false;
127         }
128 }