1 /*******************************************************************************
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.
15 ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.consulextend.cache;
18 import java.math.BigInteger;
19 import java.util.List;
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;
25 import com.orbitz.consul.option.CatalogOptions;
26 import com.orbitz.consul.option.QueryOptions;
28 public class ServiceHealthCache extends ConsulCache<List<ServiceHealth>> {
29 private ServiceHealthCache(CallbackConsumer<List<ServiceHealth>> callbackConsumer) {
30 super(callbackConsumer);
34 * Factory method to construct a string/{@link ServiceHealth} map for a particular service.
36 * Keys will be a {@link HostAndPort} object made up of the service's address/port combo
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
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) {
51 CallbackConsumer<List<ServiceHealth>> callbackConsumer = new CallbackConsumer<List<ServiceHealth>>() {
53 public void consume(BigInteger index,
54 ConsulResponseCallback<List<ServiceHealth>> callback) {
55 // TODO Auto-generated method stub
56 QueryOptions params = watchParams(index, watchSeconds, queryOptions);
58 healthClient.getHealthyServiceInstances(serviceName, catalogOptions, params, callback);
60 healthClient.getAllServiceInstances(serviceName, catalogOptions, params, callback);
65 return new ServiceHealthCache(callbackConsumer);
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);
77 public static ServiceHealthCache newCache(final HealthClient healthClient, final String serviceName) {
78 return newCache(healthClient, serviceName, true, CatalogOptions.BLANK, 10);