msb protocol synch change
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / consul / cache / CatalogCache.java
1 /**
2  * Copyright 2016 ZTE Corporation.
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.openo.msb.wrapper.consul.cache;
17
18 import java.math.BigInteger;
19 import java.util.List;
20
21 import org.openo.msb.wrapper.consul.CatalogClient;
22 import org.openo.msb.wrapper.consul.async.ConsulResponseCallback;
23 import org.openo.msb.wrapper.consul.model.catalog.CatalogService;
24
25 import com.google.common.base.Function;
26
27 public class CatalogCache extends ConsulCache<String, CatalogService>{
28     
29     private  final String serviceName;
30
31     private  CatalogCache(Function<CatalogService, String> keyConversion,
32             ConsulCache.CallbackConsumer<CatalogService> callbackConsumer,String serviceName) {
33         super(keyConversion, callbackConsumer);
34         this.serviceName=serviceName;
35         // TODO Auto-generated constructor stub
36     }
37     
38     
39     public static CatalogCache newCache(
40         final CatalogClient catalogClient,
41         final String serviceName,
42         final int watchSeconds){
43        Function<CatalogService,String> keyExtractor = new Function<CatalogService, String>() {
44            @Override
45            public String apply(CatalogService input) {
46                //return input.getKey().substring(rootPath.length() + 1);
47                return input.getServiceId();
48            }
49        };  
50        
51        final CallbackConsumer<CatalogService> callbackConsumer = new CallbackConsumer<CatalogService>() {
52            @Override
53            public void consume(BigInteger index, ConsulResponseCallback<List<CatalogService>> callback) {
54                catalogClient.getService(serviceName,  watchParams(index, watchSeconds),callback);
55            }
56        };
57        
58         
59        return new CatalogCache(keyExtractor, callbackConsumer,serviceName);
60         
61         
62     }
63     
64     public String getServiceName(){
65         return this.serviceName;
66     }
67     
68
69 }