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.expose;
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.cache.ServiceHealthCache;
23 import org.onap.msb.apiroute.wrapper.consulextend.cache.ConsulCache.Listener;
24 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.orbitz.consul.option.CatalogOptions;
29 import com.orbitz.consul.option.QueryOptions;
31 public class WatchServiceHealthTask extends WatchTask<List<ServiceHealth>> {
32 private final static Logger LOGGER = LoggerFactory
33 .getLogger(WatchServiceHealthTask.class);
35 private ServiceHealthCache serviceHealthCache = null;
36 private String serviceName="";
38 public String getServiceName() {
42 public WatchServiceHealthTask(final HealthClient healthClient,
43 final String serviceName,final boolean passing,
44 final CatalogOptions catalogOptions, final int watchSeconds,
45 final QueryOptions queryOptions) {
46 initCache(healthClient, serviceName, passing, catalogOptions,
47 watchSeconds, queryOptions);
50 public WatchServiceHealthTask(final HealthClient healthClient,
51 final String serviceName,final boolean passing,
52 final int watchSeconds)
55 initCache(healthClient, serviceName, passing, CatalogOptions.BLANK,
56 watchSeconds, QueryOptions.BLANK);
59 public WatchServiceHealthTask(final HealthClient healthClient,
60 final String serviceName, final int watchSeconds)
63 initCache(healthClient, serviceName, true, CatalogOptions.BLANK,
64 watchSeconds, QueryOptions.BLANK);
67 private ServiceHealthCache initCache(final HealthClient healthClient,
68 final String serviceName,final boolean passing,
69 final CatalogOptions catalogOptions, final int watchSeconds,
70 final QueryOptions queryOptions) {
71 // LOGGER.info("************create {} watch task*****************",serviceName);
72 this.serviceName = serviceName;
73 serviceHealthCache = ServiceHealthCache.newCache(healthClient,
74 serviceName, passing, catalogOptions, watchSeconds,
78 .addListener((Listener<List<ServiceHealth>>) new InternalListener());
80 return serviceHealthCache;
83 public boolean startWatch() {
85 if(serviceHealthCache!=null)
88 serviceHealthCache.start();
89 LOGGER.info("************start {} watch task*****************",serviceName);
91 } catch (Exception e) {
92 // TODO Auto-generated catch block
93 LOGGER.warn("start service watch failed:", e);
101 public boolean stopWatch(){
102 if (serviceHealthCache != null) {
104 serviceHealthCache.stop();
105 LOGGER.info("************stop {} watch task*****************",serviceName);
107 } catch (Exception e) {
108 // TODO Auto-generated catch block
109 LOGGER.warn("stop service watch failed:", e);
117 public boolean resetIndex()
119 if (LOGGER.isDebugEnabled()) {
120 LOGGER.debug("reset " + serviceName + " consul index");
124 serviceHealthCache.updateIndex(BigInteger.valueOf(0));
128 for (WatchTask.Filter<List<ServiceHealth>> filter : getAllFilters()) {
129 if (filter instanceof ServiceModifyIndexFilter) {
130 if (LOGGER.isDebugEnabled()) {
131 LOGGER.debug("reset " + serviceName + " modify index");
133 return ((ServiceModifyIndexFilter) filter).resetModifyIndex();
137 if (LOGGER.isDebugEnabled()) {
138 LOGGER.debug("reset modify index.did not find filter:" + serviceName);