Fix java check style warning
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / consulextend / expose / WatchServiceHealthTask.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.consulextend.expose;
15
16 import java.math.BigInteger;
17 import java.util.List;
18
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;
25
26 import com.orbitz.consul.option.CatalogOptions;
27 import com.orbitz.consul.option.QueryOptions;
28
29 public class WatchServiceHealthTask extends WatchTask<List<ServiceHealth>> {
30     private final static Logger LOGGER = LoggerFactory.getLogger(WatchServiceHealthTask.class);
31
32     private ServiceHealthCache serviceHealthCache = null;
33     private String serviceName = "";
34
35     public String getServiceName() {
36         return serviceName;
37     }
38
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);
42     }
43
44     public WatchServiceHealthTask(final HealthClient healthClient, final String serviceName, final boolean passing,
45                     final int watchSeconds)
46
47     {
48         initCache(healthClient, serviceName, passing, CatalogOptions.BLANK, watchSeconds, QueryOptions.BLANK);
49     }
50
51     public WatchServiceHealthTask(final HealthClient healthClient, final String serviceName, final int watchSeconds)
52
53     {
54         initCache(healthClient, serviceName, true, CatalogOptions.BLANK, watchSeconds, QueryOptions.BLANK);
55     }
56
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);
64
65         serviceHealthCache.addListener((Listener<List<ServiceHealth>>) new InternalListener());
66
67         return serviceHealthCache;
68     }
69
70     public boolean startWatch() {
71
72         if (serviceHealthCache != null) {
73             try {
74                 serviceHealthCache.start();
75                 LOGGER.info("************start {} watch task*****************", serviceName);
76                 return true;
77             } catch (Exception e) {
78                 // TODO Auto-generated catch block
79                 LOGGER.warn("start service watch failed:", e);
80             }
81         }
82
83         return false;
84
85     }
86
87     public boolean stopWatch() {
88         if (serviceHealthCache != null) {
89             try {
90                 serviceHealthCache.stop();
91                 LOGGER.info("************stop {} watch task*****************", serviceName);
92                 return true;
93             } catch (Exception e) {
94                 // TODO Auto-generated catch block
95                 LOGGER.warn("stop service watch failed:", e);
96             }
97         }
98
99         return false;
100     }
101
102
103     public boolean resetIndex() {
104         if (LOGGER.isDebugEnabled()) {
105             LOGGER.debug("reset " + serviceName + " consul index");
106         }
107
108         // reset consul index
109         serviceHealthCache.updateIndex(BigInteger.valueOf(0));
110
111
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");
117                 }
118                 return ((ServiceModifyIndexFilter) filter).resetModifyIndex();
119             }
120         }
121
122         if (LOGGER.isDebugEnabled()) {
123             LOGGER.debug("reset modify index.did not find filter:" + serviceName);
124         }
125
126         return false;
127     }
128 }