1 /*******************************************************************************
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
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.consulextend.expose;
16 import java.math.BigInteger;
17 import java.util.List;
19 import org.onap.msb.apiroute.wrapper.consulextend.HealthClient;
20 import org.onap.msb.apiroute.wrapper.consulextend.cache.ConsulCache.Listener;
21 import org.onap.msb.apiroute.wrapper.consulextend.cache.ServiceHealthCache;
22 import org.onap.msb.apiroute.wrapper.consulextend.model.health.ServiceHealth;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.orbitz.consul.option.CatalogOptions;
27 import com.orbitz.consul.option.QueryOptions;
29 public class WatchServiceHealthTask extends WatchTask<List<ServiceHealth>> {
30 private final static Logger LOGGER = LoggerFactory.getLogger(WatchServiceHealthTask.class);
32 private ServiceHealthCache serviceHealthCache = null;
33 private String serviceName = "";
35 public String getServiceName() {
39 public WatchServiceHealthTask(final HealthClient healthClient, final String serviceName, final boolean passing,
40 final CatalogOptions catalogOptions, final int watchSeconds, final QueryOptions queryOptions) {
41 initCache(healthClient, serviceName, passing, catalogOptions, watchSeconds, queryOptions);
44 public WatchServiceHealthTask(final HealthClient healthClient, final String serviceName, final boolean passing,
45 final int watchSeconds)
48 initCache(healthClient, serviceName, passing, CatalogOptions.BLANK, watchSeconds, QueryOptions.BLANK);
51 public WatchServiceHealthTask(final HealthClient healthClient, final String serviceName, final int watchSeconds)
54 initCache(healthClient, serviceName, true, CatalogOptions.BLANK, watchSeconds, QueryOptions.BLANK);
57 private ServiceHealthCache initCache(final HealthClient healthClient, final String serviceName,
58 final boolean passing, final CatalogOptions catalogOptions, final int watchSeconds,
59 final QueryOptions queryOptions) {
60 // LOGGER.info("************create {} watch task*****************",serviceName);
61 this.serviceName = serviceName;
62 serviceHealthCache = ServiceHealthCache.newCache(healthClient, serviceName, passing, catalogOptions,
63 watchSeconds, queryOptions);
65 serviceHealthCache.addListener((Listener<List<ServiceHealth>>) new InternalListener());
67 return serviceHealthCache;
70 public boolean startWatch() {
72 if (serviceHealthCache != null) {
74 serviceHealthCache.start();
75 LOGGER.info("************start {} watch task*****************", serviceName);
77 } catch (Exception e) {
78 // TODO Auto-generated catch block
79 LOGGER.warn("start service watch failed:", e);
87 public boolean stopWatch() {
88 if (serviceHealthCache != null) {
90 serviceHealthCache.stop();
91 LOGGER.info("************stop {} watch task*****************", serviceName);
93 } catch (Exception e) {
94 // TODO Auto-generated catch block
95 LOGGER.warn("stop service watch failed:", e);
103 public boolean resetIndex() {
104 if (LOGGER.isDebugEnabled()) {
105 LOGGER.debug("reset " + serviceName + " consul index");
108 // reset consul index
109 serviceHealthCache.updateIndex(BigInteger.valueOf(0));
112 // reset modify index
113 for (WatchTask.Filter<List<ServiceHealth>> filter : getAllFilters()) {
114 if (filter instanceof ServiceModifyIndexFilter) {
115 if (LOGGER.isDebugEnabled()) {
116 LOGGER.debug("reset " + serviceName + " modify index");
118 return ((ServiceModifyIndexFilter) filter).resetModifyIndex();
122 if (LOGGER.isDebugEnabled()) {
123 LOGGER.debug("reset modify index.did not find filter:" + serviceName);