add license file for CII Badge application
[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.RegExpTestUtil;
26
27
28
29 public class MSBService {
30
31   /**
32    * @Title queryMicroServiceInfo
33    * @Description TODO(查询单个微服务)
34    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
35    * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
36    * @param version 版本号[若无版本号,传空字符串]
37    * @throws RouteException
38    * @return MicroServiceFullInfo
39    */
40   public MicroServiceFullInfo queryMicroServiceInfo(String msbAddress, String serviceName,
41       String version) throws RouteException {
42
43     // 服务名空值检查
44     if (StringUtils.isBlank(serviceName)) {
45       throw new RouteException("ServiceName  can't be empty", "DATA_FORMAT_ERROR");
46     }
47
48     // 版本号格式检查
49     if (StringUtils.isNotBlank(version)) {
50       if (!RegExpTestUtil.versionRegExpTest(version)) {
51         throw new RouteException("version is not a valid  format", "DATA_FORMAT_ERROR");
52       }
53     } else {
54       version = "null";
55     }
56
57     MicroServiceFullInfo microServiceInfo = null;
58
59
60     String apiRouteUrl = (new StringBuilder().append("http://").append(msbAddress)
61         .append(RouteConst.MSB_ROUTE_URL).append("/").append(serviceName).append("/version/")
62         .append(version).append("?ifPassStatus=true")).toString();
63
64     String resultJson = HttpClientUtil.httpGet(apiRouteUrl);
65     microServiceInfo =
66         (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
67
68     return microServiceInfo;
69   }
70
71
72   /**
73    * @Title registerMicroServiceInfo
74    * @Description TODO(注册微服务-默认追加方式)
75    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
76    * @param microServiceInfo 微服务注册实体类
77    * @throws RouteException
78    * @return MicroServiceFullInfo
79    */
80   public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
81       MicroServiceInfo microServiceInfo) throws RouteException {
82     return this.registerMicroServiceInfo(msbAddress, microServiceInfo, true);
83
84   }
85
86   /**
87    * @Title registerMicroServiceInfo
88    * @Description TODO(注册微服务)
89    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
90    * @param microServiceInfo 微服务注册实体类
91    * @param createOrUpdate true:新增或追加更新 ,false:重新添加
92    * @throws RouteException
93    * @return MicroServiceFullInfo
94    */
95   public MicroServiceFullInfo registerMicroServiceInfo(String msbAddress,
96       MicroServiceInfo microServiceInfo, boolean createOrUpdate) throws RouteException {
97
98     // 必填项空值检查
99     if (StringUtils.isBlank(microServiceInfo.getServiceName())
100         || StringUtils.isBlank(microServiceInfo.getProtocol())
101         || microServiceInfo.getNodes().size() == 0) {
102
103       throw new RouteException(
104           "register MicroServiceInfo FAIL: Some MicroServiceInfo's required fields are empty",
105           "DATA_FORMAT_ERROR");
106
107     }
108
109
110     // 版本号格式检查
111     if (StringUtils.isNotBlank(microServiceInfo.getVersion())) {
112       if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) {
113         throw new RouteException("register MicroServiceInfo FAIL:version is not a valid  format",
114             "DATA_FORMAT_ERROR");
115
116       }
117     }
118
119
120
121     // 服务协议取值范围检查
122     if (!RouteConst.checkExistProtocol(microServiceInfo.getProtocol().trim())) {
123       throw new RouteException("register MicroServiceInfo FAIL:Protocol is wrong,value range:("
124           + RouteConst.listProtocol() + ")", "DATA_FORMAT_ERROR");
125
126     }
127
128
129
130     String apiRouteJson = JacksonJsonUtil.beanToJson(microServiceInfo);
131
132     String apiRouteUrl =
133         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
134             .append("?createOrUpdate=").append(createOrUpdate)).toString();
135
136     String resultJson = HttpClientUtil.httpPostWithJSON(apiRouteUrl, apiRouteJson);
137
138     MicroServiceFullInfo microServiceFullInfo =
139         (MicroServiceFullInfo) JacksonJsonUtil.jsonToBean(resultJson, MicroServiceFullInfo.class);
140
141     return microServiceFullInfo;
142   }
143
144   /**
145    * @Title cancelMicroServiceInfo
146    * @Description TODO(注销全部微服务)
147    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
148    * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
149    * @param version 版本号[若无版本号,传空字符串]
150    * @throws RouteException
151    * @return RouteResult
152    */
153   public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version)
154       throws RouteException {
155     RouteResult result = new RouteResult();
156
157     // 服务名空值检查
158     if (StringUtils.isBlank(serviceName)) {
159       throw new RouteException("cancel MicroServiceInfo FAIL:ServiceName  can't be empty",
160           "DATA_FORMAT_ERROR");
161
162     }
163
164     // 版本号格式检查
165     if (StringUtils.isNotBlank(version)) {
166       if (!RegExpTestUtil.versionRegExpTest(version)) {
167         throw new RouteException("cancel MicroServiceInfo FAIL:version is not a valid  format",
168             "DATA_FORMAT_ERROR");
169
170       }
171     } else {
172       version = "null";
173     }
174
175
176     String url =
177         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
178             .append("/").append(serviceName).append("/version/").append(version).toString();
179
180     HttpClientUtil.delete(url, null);
181
182
183     result.setResult(RouteConst.REQUEST_SUCCESS);
184     result.setInfo("cancel MicroServiceInfo success");
185
186
187     return result;
188   }
189
190   /**
191    * @Title cancelMicroServiceInfo
192    * @Description TODO(注销单个微服务)
193    * @param msbAddress 微服务服务器地址( ip:port 或 域名地址)
194    * @param serviceName 服务名[必填,若自定义服务名包含/,用*代替]
195    * @param version 版本号[若无版本号,传空字符串]
196    * @param ip
197    * @param port
198    * @throws RouteException
199    * @return RouteResult
200    */
201   public RouteResult cancelMicroServiceInfo(String msbAddress, String serviceName, String version,
202       String ip, String port) throws RouteException {
203
204     RouteResult result = new RouteResult();
205
206     // 服务名空值检查
207     if (StringUtils.isBlank(serviceName)) {
208       throw new RouteException("cancel MicroServiceInfo FAIL:ServiceName  can't be empty",
209           "DATA_FORMAT_ERROR");
210
211     }
212
213
214     // HOST空值和格式检查
215     if (StringUtils.isBlank(ip)) {
216       throw new RouteException("cancel MicroServiceInfo FAIL:ip can't be empty",
217           "DATA_FORMAT_ERROR");
218     }
219
220     if (StringUtils.isBlank(port)) {
221       throw new RouteException("cancel MicroServiceInfo FAIL:port can't be empty",
222           "DATA_FORMAT_ERROR");
223
224     }
225
226     // 版本号格式检查
227     if (StringUtils.isNotBlank(version)) {
228       if (!RegExpTestUtil.versionRegExpTest(version)) {
229         throw new RouteException("cancel MicroServiceInfo FAIL:version is not a valid  format",
230             "DATA_FORMAT_ERROR");
231       }
232     } else {
233       version = "null";
234     }
235
236
237     String url =
238         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL))
239             .append("/").append(serviceName).append("/version/").append(version).append("/nodes/")
240             .append(ip).append("/").append(port).toString();
241
242     HttpClientUtil.delete(url, null);
243
244
245     result.setResult(RouteConst.REQUEST_SUCCESS);
246     result.setInfo("cancel MicroServiceInfo success");
247
248     return result;
249   }
250
251
252   /**
253    * @Title healthCheckbyTTL
254    * @Description TODO(请求服务实例TTL健康检查)
255    * @param msbAddress
256    * @param serviceName 服务名
257    * @param version 版本号[若无版本号,传空字符串]
258    * @param ip 实例IP
259    * @param port 实例端口
260    * @throws RouteException
261    * @return CheckNode
262    */
263   public NodeAddress healthCheckbyTTL(String msbAddress, String serviceName, String version,
264       String ip, String port) throws RouteException {
265
266     // 服务名空值检查
267     if (StringUtils.isBlank(serviceName)) {
268       throw new RouteException("ServiceName  can't be empty", "DATA_FORMAT_ERROR");
269     }
270
271     // 版本号格式检查
272     if (StringUtils.isNotBlank(version)) {
273       if (!RegExpTestUtil.versionRegExpTest(version)) {
274         throw new RouteException("version is not a valid  format", "DATA_FORMAT_ERROR");
275       }
276     } else {
277       version = "null";
278     }
279
280
281     // HOST空值和格式检查
282     if (StringUtils.isBlank(ip)) {
283       throw new RouteException("healthCheck by TTL FAIL:ip can't be empty", "DATA_FORMAT_ERROR");
284
285     }
286
287     if (StringUtils.isBlank(port)) {
288       throw new RouteException("healthCheck by TTL FAIL:port can't be empty", "DATA_FORMAT_ERROR");
289
290     }
291
292
293
294     NodeAddress checkNode = new NodeAddress(ip, port);
295
296
297     String healthCheckJson = JacksonJsonUtil.beanToJson(checkNode);
298
299     String healthCheckUrl =
300         (new StringBuilder().append("http://").append(msbAddress).append(RouteConst.MSB_ROUTE_URL)
301             .append("/").append(serviceName).append("/version/").append(version).append("/ttl"))
302                 .toString();
303
304     HttpClientUtil.httpPutWithJSON(healthCheckUrl, healthCheckJson);
305
306     return checkNode;
307   }
308
309
310
311 }