2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.msb.sdclient.wrapper.consul.cache;
16 import java.math.BigInteger;
17 import java.util.List;
19 import org.onap.msb.sdclient.wrapper.consul.HealthClient;
20 import org.onap.msb.sdclient.wrapper.consul.async.ConsulResponseCallback;
21 import org.onap.msb.sdclient.wrapper.consul.model.health.ServiceHealth;
23 import com.google.common.base.Function;
25 public class HealthCache extends ConsulCache<String, ServiceHealth> {
27 private final String serviceName;
29 private HealthCache(Function<ServiceHealth, String> keyConversion,
30 ConsulCache.CallbackConsumer<ServiceHealth> callbackConsumer, String serviceName) {
31 super(keyConversion, callbackConsumer);
32 this.serviceName = serviceName;
33 // TODO Auto-generated constructor stub
37 public static HealthCache newCache(final HealthClient healthClient, final String serviceName,
38 final int watchSeconds) {
39 Function<ServiceHealth, String> keyExtractor = new Function<ServiceHealth, String>() {
41 public String apply(ServiceHealth input) {
42 // return input.getKey().substring(rootPath.length() + 1);
43 return input.getService().getId();
47 final CallbackConsumer<ServiceHealth> callbackConsumer = new CallbackConsumer<ServiceHealth>() {
49 public void consume(BigInteger index, ConsulResponseCallback<List<ServiceHealth>> callback) {
50 healthClient.getHealthyServiceInstances(serviceName, watchParams(index, watchSeconds), callback);
55 return new HealthCache(keyExtractor, callbackConsumer, serviceName);
60 public String getServiceName() {
61 return this.serviceName;