Modify Example code
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / msb / MSBServiceClient.java
1 /*******************************************************************************
2  * Copyright 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.sdk.httpclient.msb;
15
16 import java.util.concurrent.Callable;
17
18 import org.onap.msb.sdk.discovery.MSBService;
19 import org.onap.msb.sdk.discovery.common.RouteException;
20 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
21 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
22 import org.onap.msb.sdk.discovery.entity.NodeAddress;
23 import org.onap.msb.sdk.discovery.entity.RouteResult;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 public class MSBServiceClient {
29
30   private static final Logger logger = LoggerFactory.getLogger(MSBServiceClient.class);
31
32   private String msbSvrAddress;
33
34   private MSBService msbService = new MSBService();
35
36   public MSBServiceClient(String msbSvrIp, int msbSvrPort) {
37     super();
38     this.msbSvrAddress = msbSvrIp + ":" + msbSvrPort;
39
40     logger.info("msb service info:msbSvrAddress:{}", this.msbSvrAddress);
41   }
42
43   public MicroServiceFullInfo queryMicroServiceInfo(String serviceName, String version)
44       throws RouteException {
45     return invokeMsb(() -> msbService.queryMicroServiceInfo(msbSvrAddress, serviceName, version));
46   }
47
48   private <V> V invokeMsb(Callable<V> callable) throws RouteException {
49     try {
50       return callable.call();
51     } catch (Exception e) {
52       logger.error("msb service info:msbSvrAddress:" + this.msbSvrAddress, e);
53       if (e instanceof RouteException) {
54         throw (RouteException) e;
55       } else {
56         throw new RuntimeException(e);
57       }
58     }
59
60   }
61
62
63   public MicroServiceFullInfo registerMicroServiceInfo(MicroServiceInfo msinfo)
64       throws RouteException {
65
66     return invokeMsb(() -> msbService.registerMicroServiceInfo(msbSvrAddress, msinfo));
67   }
68
69
70
71   public MicroServiceFullInfo registerMicroServiceInfo(MicroServiceInfo msinfo,
72       boolean createOrUpdate) throws RouteException {
73
74     return invokeMsb(
75         () -> msbService.registerMicroServiceInfo(msbSvrAddress, msinfo, createOrUpdate));
76   }
77
78
79
80   /**
81    * unregister all the instances of a service
82    */
83   public RouteResult cancelMicroServiceInfo(String serviceName, String version)
84       throws RouteException {
85     return invokeMsb(() -> msbService.cancelMicroServiceInfo(msbSvrAddress, serviceName, version));
86   }
87
88
89
90   /**
91    * unregister the specified instance of a service
92    */
93
94   public RouteResult cancelMicroServiceInfo(String serviceName, String version, String ip,
95       String port) throws RouteException {
96
97     return invokeMsb(
98         () -> msbService.cancelMicroServiceInfo(msbSvrAddress, serviceName, version, ip, port));
99   }
100
101
102   public String getMsbSvrAddress() {
103     return msbSvrAddress;
104   }
105
106 }