Merge "50% Code Coverage-MSB Java SDK"
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / discovery / MSBService.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.discovery;
15
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;
27
28
29
30 public class MSBService {
31
32   /**
33    * @Title queryMicroServiceInfo
34    * @Description TODO(查询单个微服务)
35    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
36    * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
37    * @param version 版本号[若无版本号,传空字符串]
38    * @throws RouteException
39    * @return MicroServiceFullInfo
40    */
41   public MicroServiceFullInfo queryMicroServiceInfo(String msbAddress, String serviceName,
42       String version) throws RouteException {
43
44           MsbUtil.checkServiceName(serviceName);
45           version=MsbUtil.checkVersion(version);    
46
47     MicroServiceFullInfo microServiceInfo = null;
48
49
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();
53
54     String resultJson = HttpClientUtil.httpGet(apiRouteUrl);
55     microServiceInfo =
56         (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
57
58     return microServiceInfo;
59   }
60
61
62   /**
63    * @Title registerMicroServiceInfo
64    * @Description TODO(注册微服务-默认追加方式)
65    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
66    * @param microServiceInfo 微服务注册实体类
67    * @throws RouteException
68    * @return MicroServiceFullInfo
69    */
70   public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
71       MicroServiceInfo microServiceInfo) throws RouteException {
72     return this.registerMicroServiceInfo(msbAddress, microServiceInfo, true);
73
74   }
75
76   /**
77    * @Title registerMicroServiceInfo
78    * @Description TODO(注册微服务)
79    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
80    * @param microServiceInfo 微服务注册实体类
81    * @param createOrUpdate true:新增或追加更新 ,false:重新添加
82    * @throws RouteException
83    * @return MicroServiceFullInfo
84    */
85   public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
86       MicroServiceInfo microServiceInfo, boolean createOrUpdate) throws RouteException {
87
88     // 必填项空值检查
89     if (StringUtils.isBlank(microServiceInfo.getServiceName())
90         || StringUtils.isBlank(microServiceInfo.getProtocol())
91         || microServiceInfo.getNodes().size() == 0) {
92
93       throw new RouteException("register MicroServiceInfo FAIL: Some MicroServiceInfo's required fields are empty","DATA_FORMAT_ERROR");
94
95     }
96
97
98     // 版本号格式检查
99     if (StringUtils.isNotBlank(microServiceInfo.getVersion())) {
100       if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) {
101         throw new RouteException("register MicroServiceInfo FAIL:version is not a valid  format","DATA_FORMAT_ERROR");
102       }
103     }
104
105
106
107     // 服务协议取值范围检查
108     if (!RouteConst.checkExistProtocol(microServiceInfo.getProtocol().trim())) {
109       throw new RouteException("register MicroServiceInfo FAIL:Protocol is wrong,value range:("+ RouteConst.listProtocol() + ")", "DATA_FORMAT_ERROR");
110
111     }
112
113
114
115     String apiRouteJson = JacksonJsonUtil.beanToJson(microServiceInfo);
116
117     String apiRouteUrl =
118         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
119             .append("?createOrUpdate=").append(createOrUpdate)).toString();
120
121     String resultJson = HttpClientUtil.httpPostWithJSON(apiRouteUrl, apiRouteJson);
122
123     MicroServiceFullInfo microServiceFullInfo =
124         (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
125
126     return microServiceFullInfo;
127   }
128
129   /**
130    * @Title cancelMicroServiceInfo
131    * @Description TODO(注销全部微服务)
132    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
133    * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
134    * @param version 版本号[若无版本号,传空字符串]
135    * @throws RouteException
136    * @return RouteResult
137    */
138   public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version)
139       throws RouteException {
140     RouteResult result = new RouteResult();
141
142     MsbUtil.checkServiceName(serviceName);
143     version=MsbUtil.checkVersion(version);  
144     
145
146
147     String url =
148         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
149             .append("/").append(serviceName).append("/version/").append(version).toString();
150
151     HttpClientUtil.delete(url, null);
152
153
154     result.setResult(RouteConst.REQUEST_SUCCESS);
155     result.setInfo("cancel MicroServiceInfo success");
156
157
158     return result;
159   }
160
161   /**
162    * @Title cancelMicroServiceInfo
163    * @Description TODO(注销单个微服务)
164    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
165    * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
166    * @param version 版本号[若无版本号,传空字符串]
167    * @param ip
168    * @param port
169    * @throws RouteException
170    * @return RouteResult
171    */
172   public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version,
173       String ip, String port) throws RouteException {
174
175     RouteResult result = new RouteResult();
176
177     MsbUtil.checkServiceName(serviceName);
178     version=MsbUtil.checkVersion(version);  
179     
180     MsbUtil.checkHost(ip,port);  
181
182    
183
184
185     String url =
186         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
187             .append("/").append(serviceName).append("/version/").append(version).append("/nodes/")
188             .append(ip).append("/").append(port).toString();
189
190     HttpClientUtil.delete(url, null);
191
192
193     result.setResult(RouteConst.REQUEST_SUCCESS);
194     result.setInfo("cancel MicroServiceInfo success");
195
196     return result;
197   }
198
199
200   /**
201    * @Title healthCheckbyTTL
202    * @Description TODO(请求服务实例TTL健康检查)
203    * @param msbAddress
204    * @param serviceName 服务名
205    * @param version 版本号[若无版本号,传空字符串]
206    * @param ip 实例IP
207    * @param port 实例端口
208    * @throws RouteException
209    * @return CheckNode
210    */
211   public NodeAddress healthCheckbyTTL(String msbAddress, String serviceName, String version,
212       String ip, String port) throws RouteException {
213
214           MsbUtil.checkServiceName(serviceName);
215           version=MsbUtil.checkVersion(version);  
216           MsbUtil.checkHost(ip,port);  
217
218
219     NodeAddress checkNode = new NodeAddress(ip, port);
220
221
222     String healthCheckJson = JacksonJsonUtil.beanToJson(checkNode);
223
224     String healthCheckUrl =
225         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
226             .append("/").append(serviceName).append("/version/").append(version).append("/ttl"))
227                 .toString();
228
229     HttpClientUtil.httpPutWithJSON(healthCheckUrl, healthCheckJson);
230
231     return checkNode;
232   }
233
234
235
236 }