Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / consulextend / cache / ServiceHealthCache.java
1 package org.onap.msb.apiroute.wrapper.consulextend.cache;
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.async.ConsulResponseCallback;
8 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
9
10 import com.orbitz.consul.option.CatalogOptions;
11 import com.orbitz.consul.option.QueryOptions;
12
13 public class ServiceHealthCache extends ConsulCache<List<ServiceHealth>> {
14     private ServiceHealthCache(CallbackConsumer<List<ServiceHealth>> callbackConsumer) {
15         super(callbackConsumer);
16     }
17
18     /**
19      * Factory method to construct a string/{@link ServiceHealth} map for a particular service.
20      * <p/>
21      * Keys will be a {@link HostAndPort} object made up of the service's address/port combo
22      *
23      * @param healthClient the {@link HealthClient}
24      * @param serviceName  the name of the service
25      * @param passing      include only passing services?
26      * @return a cache object
27      */
28     public static ServiceHealthCache newCache(
29             final HealthClient healthClient,
30             final String serviceName,
31             final boolean passing,
32             final CatalogOptions catalogOptions,
33             final int watchSeconds,
34             final QueryOptions queryOptions) {
35
36         CallbackConsumer<List<ServiceHealth>> callbackConsumer = new CallbackConsumer<List<ServiceHealth>>() {
37                         @Override
38                         public void consume(BigInteger index,
39                                         ConsulResponseCallback<List<ServiceHealth>> callback) {
40                                 // TODO Auto-generated method stub
41                 QueryOptions params = watchParams(index, watchSeconds, queryOptions);
42                 if (passing) {
43                     healthClient.getHealthyServiceInstances(serviceName, catalogOptions, params, callback);
44                 } else {
45                     healthClient.getAllServiceInstances(serviceName, catalogOptions, params, callback);
46                 }
47                         }
48         };
49
50         return new ServiceHealthCache(callbackConsumer);
51     }
52
53     public static ServiceHealthCache newCache(
54             final HealthClient healthClient,
55             final String serviceName,
56             final boolean passing,
57             final CatalogOptions catalogOptions,
58             final int watchSeconds) {
59         return newCache(healthClient, serviceName, passing, catalogOptions, watchSeconds, QueryOptions.BLANK);
60     }
61
62     public static ServiceHealthCache newCache(final HealthClient healthClient, final String serviceName) {
63         return newCache(healthClient, serviceName, true, CatalogOptions.BLANK, 10);
64     }
65 }