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