2 * Copyright 2016-2017 ZTE, Inc. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.msb.sdclient.wrapper.consul.cache;
18 import java.math.BigInteger;
19 import java.util.List;
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;
25 import com.google.common.base.Function;
27 public class HealthCache extends ConsulCache<String, ServiceHealth>{
29 private final String serviceName;
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
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>() {
45 public String apply(ServiceHealth input) {
46 //return input.getKey().substring(rootPath.length() + 1);
47 return input.getService().getId();
51 final CallbackConsumer<ServiceHealth> callbackConsumer = new CallbackConsumer<ServiceHealth>() {
53 public void consume(BigInteger index, ConsulResponseCallback<List<ServiceHealth>> callback) {
54 healthClient.getHealthyServiceInstances(serviceName, watchParams(index, watchSeconds),callback);
59 return new HealthCache(keyExtractor, callbackConsumer,serviceName);
64 public String getServiceName(){
65 return this.serviceName;