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.discovery;
16 import org.apache.commons.lang3.StringUtils;
17 import org.onap.msb.sdk.discovery.common.RouteConst;
18 import org.onap.msb.sdk.discovery.common.RouteException;
19 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
20 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
21 import org.onap.msb.sdk.discovery.entity.NodeAddress;
22 import org.onap.msb.sdk.discovery.entity.RouteResult;
23 import org.onap.msb.sdk.discovery.util.HttpClientUtil;
24 import org.onap.msb.sdk.discovery.util.JacksonJsonUtil;
25 import org.onap.msb.sdk.discovery.util.MsbUtil;
26 import org.onap.msb.sdk.discovery.util.RegExpTestUtil;
30 public class MSBService {
33 * @Title queryMicroServiceInfo
34 * @Description Get information of a service
35 * @param msbAddress MSB Address
37 * @param version null if no version
38 * @throws RouteException
39 * @return MicroServiceFullInfo
41 public MicroServiceFullInfo queryMicroServiceInfo(String msbAddress, String serviceName,
42 String version) throws RouteException {
44 MsbUtil.checkServiceName(serviceName);
45 version=MsbUtil.checkVersion(version);
47 MicroServiceFullInfo microServiceInfo = null;
50 String apiRouteUrl = (new StringBuilder().append("http://").append(msbAddress)
51 .append(RouteConst.MSB_ROUTE_URL).append("/").append(serviceName).append("/version/")
52 .append(version).append("?ifPassStatus=true")).toString();
54 String resultJson = HttpClientUtil.httpGet(apiRouteUrl);
56 (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
58 return microServiceInfo;
63 * @Title registerMicroServiceInfo
66 * @param microServiceInfo
67 * @throws RouteException
68 * @return MicroServiceFullInfo
70 public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
71 MicroServiceInfo microServiceInfo) throws RouteException {
72 return this.registerMicroServiceInfo(msbAddress, microServiceInfo, true);
77 * @Title registerMicroServiceInfo
80 * @param microServiceInfo
81 * @param createOrUpdate true
82 * @throws RouteException
83 * @return MicroServiceFullInfo
85 public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
86 MicroServiceInfo microServiceInfo, boolean createOrUpdate) throws RouteException {
88 if (StringUtils.isBlank(microServiceInfo.getServiceName())
89 || StringUtils.isBlank(microServiceInfo.getProtocol())
90 || microServiceInfo.getNodes().size() == 0) {
92 throw new RouteException("register MicroServiceInfo FAIL: Some MicroServiceInfo's required fields are empty","DATA_FORMAT_ERROR");
97 if (StringUtils.isNotBlank(microServiceInfo.getVersion())) {
98 if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) {
99 throw new RouteException("register MicroServiceInfo FAIL:version is not a valid format","DATA_FORMAT_ERROR");
105 if (!RouteConst.checkExistProtocol(microServiceInfo.getProtocol().trim())) {
106 throw new RouteException("register MicroServiceInfo FAIL:Protocol is wrong,value range:("+ RouteConst.listProtocol() + ")", "DATA_FORMAT_ERROR");
112 String apiRouteJson = JacksonJsonUtil.beanToJson(microServiceInfo);
115 (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
116 .append("?createOrUpdate=").append(createOrUpdate)).toString();
118 String resultJson = HttpClientUtil.httpPostWithJSON(apiRouteUrl, apiRouteJson);
120 MicroServiceFullInfo microServiceFullInfo =
121 (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
123 return microServiceFullInfo;
127 * @Title cancelMicroServiceInfo
128 * @Description deregister a service
132 * @throws RouteException
133 * @return RouteResult
135 public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version)
136 throws RouteException {
137 RouteResult result = new RouteResult();
139 MsbUtil.checkServiceName(serviceName);
140 version=MsbUtil.checkVersion(version);
145 (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
146 .append("/").append(serviceName).append("/version/").append(version).toString();
148 HttpClientUtil.delete(url, null);
151 result.setResult(RouteConst.REQUEST_SUCCESS);
152 result.setInfo("cancel MicroServiceInfo success");
159 * @Title cancelMicroServiceInfo
160 * @Description deregister a service
166 * @throws RouteException
167 * @return RouteResult
169 public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version,
170 String ip, String port) throws RouteException {
172 RouteResult result = new RouteResult();
174 MsbUtil.checkServiceName(serviceName);
175 version=MsbUtil.checkVersion(version);
178 (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
179 .append("/").append(serviceName).append("/version/").append(version).append("/nodes/")
180 .append(ip).append("/").append(port).toString();
182 HttpClientUtil.delete(url, null);
185 result.setResult(RouteConst.REQUEST_SUCCESS);
186 result.setInfo("cancel MicroServiceInfo success");
193 * @Title healthCheckbyTTL
200 * @throws RouteException
203 public NodeAddress healthCheckbyTTL(String msbAddress, String serviceName, String version,
204 String ip, String port) throws RouteException {
206 MsbUtil.checkServiceName(serviceName);
207 version=MsbUtil.checkVersion(version);
208 MsbUtil.checkHost(ip,port);
211 NodeAddress checkNode = new NodeAddress(ip, port);
214 String healthCheckJson = JacksonJsonUtil.beanToJson(checkNode);
216 String healthCheckUrl =
217 (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
218 .append("/").append(serviceName).append("/version/").append(version).append("/ttl"))
221 HttpClientUtil.httpPutWithJSON(healthCheckUrl, healthCheckJson);