73a51763389761988d7ba8e3fad6bea1a25dd327
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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;
17
18 import java.math.BigInteger;
19 import java.util.List;
20
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;
27
28 import com.orbitz.consul.option.CatalogOptions;
29 import com.orbitz.consul.option.QueryOptions;
30
31 public class WatchServiceHealthTask extends WatchTask<List<ServiceHealth>> {
32         private final static Logger LOGGER = LoggerFactory
33                         .getLogger(WatchServiceHealthTask.class);
34         
35         private ServiceHealthCache serviceHealthCache = null;
36         private String serviceName="";
37
38         public String getServiceName() {
39                 return serviceName;
40         }
41
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);
48         }
49
50         public WatchServiceHealthTask(final HealthClient healthClient,
51                         final String serviceName,final boolean passing,
52                         final int watchSeconds)
53
54         {
55                 initCache(healthClient, serviceName, passing, CatalogOptions.BLANK,
56                                 watchSeconds, QueryOptions.BLANK);
57         }
58
59         public WatchServiceHealthTask(final HealthClient healthClient,
60                         final String serviceName, final int watchSeconds)
61
62         {
63                 initCache(healthClient, serviceName, true, CatalogOptions.BLANK,
64                                 watchSeconds, QueryOptions.BLANK);
65         }
66
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,
75                                 queryOptions);
76
77                 serviceHealthCache
78                                 .addListener((Listener<List<ServiceHealth>>) new InternalListener());
79
80                 return serviceHealthCache;
81         }
82
83         public boolean startWatch() {
84                 
85                 if(serviceHealthCache!=null)
86                 {
87                         try {
88                                 serviceHealthCache.start();
89                                 LOGGER.info("************start {} watch task*****************",serviceName);
90                                 return true;
91                         } catch (Exception e) {
92                                 // TODO Auto-generated catch block
93                                 LOGGER.warn("start service watch failed:", e);
94                         }
95                 }
96                 
97                 return false;
98                 
99         }
100
101         public boolean stopWatch(){
102                 if (serviceHealthCache != null) {
103                         try {
104                                 serviceHealthCache.stop();
105                                 LOGGER.info("************stop {} watch task*****************",serviceName);
106                                 return true;
107                         } catch (Exception e) {
108                                 // TODO Auto-generated catch block
109                                 LOGGER.warn("stop service watch failed:", e);
110                         }
111                 }
112                 
113                 return false;
114         }
115
116
117         public boolean resetIndex()
118         {
119                 if (LOGGER.isDebugEnabled()) {
120                         LOGGER.debug("reset " + serviceName + " consul index");
121                 }
122                 
123                 //reset consul index
124                 serviceHealthCache.updateIndex(BigInteger.valueOf(0));
125                 
126                 
127                 //reset modify index
128                 for (WatchTask.Filter<List<ServiceHealth>> filter : getAllFilters()) {
129                         if (filter instanceof ServiceModifyIndexFilter) {
130                                 if (LOGGER.isDebugEnabled()) {
131                                         LOGGER.debug("reset " + serviceName + " modify index");
132                                 }
133                                 return ((ServiceModifyIndexFilter) filter).resetModifyIndex();
134                         }
135                 }
136                 
137                 if (LOGGER.isDebugEnabled()) {
138                         LOGGER.debug("reset modify index.did not find filter:" + serviceName);
139                 }
140                 
141                 return false;
142         }
143 }