modify copyright year
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / consul / cache / HealthCache.java
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.sdclient.wrapper.consul.cache;
17
18 import java.math.BigInteger;
19 import java.util.List;
20
21 import org.onap.msb.sdclient.wrapper.consul.HealthClient;
22 import org.onap.msb.sdclient.wrapper.consul.async.ConsulResponseCallback;
23 import org.onap.msb.sdclient.wrapper.consul.model.health.ServiceHealth;
24
25 import com.google.common.base.Function;
26
27 public class HealthCache extends ConsulCache<String, ServiceHealth>{
28     
29     private  final String serviceName;
30
31     private  HealthCache(Function<ServiceHealth, String> keyConversion,
32             ConsulCache.CallbackConsumer<ServiceHealth> callbackConsumer,String serviceName) {
33         super(keyConversion, callbackConsumer);
34         this.serviceName=serviceName;
35         // TODO Auto-generated constructor stub
36     }
37     
38     
39     public static HealthCache newCache(
40         final HealthClient healthClient,
41         final String serviceName,
42         final int watchSeconds){
43        Function<ServiceHealth,String> keyExtractor = new Function<ServiceHealth, String>() {
44            @Override
45            public String apply(ServiceHealth input) {
46                //return input.getKey().substring(rootPath.length() + 1);
47                return input.getService().getId();
48            }
49        };  
50        
51        final CallbackConsumer<ServiceHealth> callbackConsumer = new CallbackConsumer<ServiceHealth>() {
52            @Override
53            public void consume(BigInteger index, ConsulResponseCallback<List<ServiceHealth>> callback) {
54                healthClient.getHealthyServiceInstances(serviceName,  watchParams(index, watchSeconds),callback);
55            }
56        };
57        
58         
59        return new HealthCache(keyExtractor, callbackConsumer,serviceName);
60         
61         
62     }
63     
64     public String getServiceName(){
65         return this.serviceName;
66     }
67     
68
69 }