1 /*******************************************************************************
2 * Copyright 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.sdk.httpclient.msb;
16 import java.util.concurrent.Callable;
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;
28 public class MSBServiceClient {
30 private static final Logger logger = LoggerFactory.getLogger(MSBServiceClient.class);
32 private String msbSvrAddress;
34 private MSBService msbService = new MSBService();
36 public MSBServiceClient(String msbSvrIp, int msbSvrPort) {
38 this.msbSvrAddress = msbSvrIp + ":" + msbSvrPort;
40 logger.info("msb service info:msbSvrAddress:{}", this.msbSvrAddress);
43 public MicroServiceFullInfo queryMicroServiceInfo(String serviceName, String version)
44 throws RouteException {
45 return invokeMsb(() -> msbService.queryMicroServiceInfo(msbSvrAddress, serviceName, version));
48 private <V> V invokeMsb(Callable<V> callable) throws RouteException {
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;
56 throw new RuntimeException(e);
63 public MicroServiceFullInfo registerMicroServiceInfo(MicroServiceInfo msinfo)
64 throws RouteException {
66 return invokeMsb(() -> msbService.registerMicroServiceInfo(msbSvrAddress, msinfo));
71 public MicroServiceFullInfo registerMicroServiceInfo(MicroServiceInfo msinfo,
72 boolean createOrUpdate) throws RouteException {
75 () -> msbService.registerMicroServiceInfo(msbSvrAddress, msinfo, createOrUpdate));
81 * unregister all the instances of a service
83 public RouteResult cancelMicroServiceInfo(String serviceName, String version)
84 throws RouteException {
85 return invokeMsb(() -> msbService.cancelMicroServiceInfo(msbSvrAddress, serviceName, version));
91 * unregister the specified instance of a service
94 public RouteResult cancelMicroServiceInfo(String serviceName, String version, String ip,
95 String port) throws RouteException {
98 () -> msbService.cancelMicroServiceInfo(msbSvrAddress, serviceName, version, ip, port));
102 public String getMsbSvrAddress() {
103 return msbSvrAddress;