Initial code import
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / CustomRouteServiceWrapper.java
1 /**\r
2 * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 * http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 package org.openo.msb.wrapper;\r
17 \r
18 import java.util.HashMap;\r
19 import java.util.Map;\r
20 import java.util.Set;\r
21 \r
22 import org.apache.commons.lang3.StringUtils;\r
23 import org.openo.msb.api.CustomRouteInfo;\r
24 import org.openo.msb.api.RouteServer;\r
25 import org.openo.msb.api.exception.ExtendedInternalServerErrorException;\r
26 import org.openo.msb.api.exception.ExtendedNotFoundException;\r
27 import org.openo.msb.api.exception.ExtendedNotSupportedException;\r
28 import org.openo.msb.wrapper.util.JedisUtil;\r
29 import org.openo.msb.wrapper.util.RegExpTestUtil;\r
30 import org.openo.msb.wrapper.util.RouteUtil;\r
31 import org.slf4j.Logger;\r
32 import org.slf4j.LoggerFactory;\r
33 \r
34 import redis.clients.jedis.Jedis;\r
35 \r
36 public class CustomRouteServiceWrapper {\r
37 \r
38 \r
39     private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteServiceWrapper.class);\r
40 \r
41     private static CustomRouteServiceWrapper instance = new CustomRouteServiceWrapper();\r
42 \r
43     private CustomRouteServiceWrapper() {}\r
44 \r
45     public static CustomRouteServiceWrapper getInstance() {\r
46         return instance;\r
47     }\r
48 \r
49 \r
50     /**\r
51      * @Title: getAllCustomRouteService\r
52      * @Description: TODO(获取全部内容服务列表)\r
53      * @param: @return\r
54      * @return: CustomRouteInfo[]\r
55      */\r
56     public CustomRouteInfo[] getAllCustomRouteInstances() {\r
57 \r
58 \r
59         Jedis jedis = null;\r
60         CustomRouteInfo[] customRouteList = null;\r
61         try {\r
62             jedis = JedisUtil.borrowJedisInstance();\r
63             if (jedis == null) {\r
64                 throw new ExtendedInternalServerErrorException(\r
65                         "fetch from jedis pool failed,null object!");\r
66             }\r
67 \r
68             // 获取全部服务列表\r
69             String routekey =\r
70                     RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, "*",\r
71                             RouteUtil.ROUTE_PATH_INFO);\r
72             Set<String> routeSet = jedis.keys(routekey);\r
73             customRouteList = new CustomRouteInfo[routeSet.size()];\r
74 \r
75             int i = 0;\r
76             for (String routePath : routeSet) {\r
77                 String[] routePathArray = routePath.split(":");\r
78                 CustomRouteInfo customRoute = getCustomRouteInstance(routePathArray[3], jedis);\r
79                 customRouteList[i] = customRoute;\r
80                 i++;\r
81             }\r
82 \r
83 \r
84         } catch (Exception e) {\r
85             LOGGER.error("call redis throw exception", e);\r
86             throw new ExtendedInternalServerErrorException("call redis throw exception:"\r
87                     + e.getMessage());\r
88 \r
89         } finally {\r
90             JedisUtil.returnJedisInstance(jedis);\r
91         }\r
92 \r
93         return customRouteList;\r
94     }\r
95 \r
96 \r
97 \r
98     /**\r
99      * @Title: getCustomRouteInstance\r
100      * @Description: TODO(通过服务名获取单个内容服务对象信息)\r
101      * @param: @param serviceName\r
102      * @param: @return\r
103      * @return: CustomRouteInfo\r
104      */\r
105     public CustomRouteInfo getCustomRouteInstance(String serviceName) {\r
106 \r
107         if (StringUtils.isBlank(serviceName)) {\r
108             throw new ExtendedNotSupportedException("serviceName  can't be empty");\r
109         }\r
110 \r
111         CustomRouteInfo customRouteInfo;\r
112 \r
113         Jedis jedis = null;\r
114         try {\r
115             jedis = JedisUtil.borrowJedisInstance();\r
116             if (jedis == null) {\r
117                 throw new ExtendedInternalServerErrorException(\r
118                         "fetch from jedis pool failed,null object!");\r
119             }\r
120 \r
121             customRouteInfo = getCustomRouteInstance(serviceName, jedis);\r
122 \r
123 \r
124         } catch (Exception e) {\r
125             LOGGER.error("call redis throw exception", e);\r
126             throw new ExtendedInternalServerErrorException("call redis throw exception:"\r
127                     + e.getMessage());\r
128         } finally {\r
129             JedisUtil.returnJedisInstance(jedis);\r
130         }\r
131 \r
132         if (null == customRouteInfo) {\r
133             String errInfo = "customRouteInfo not found: serviceName-" + serviceName;\r
134             LOGGER.warn(errInfo);\r
135             throw new ExtendedNotFoundException(errInfo);\r
136 \r
137         }\r
138 \r
139         return customRouteInfo;\r
140 \r
141     }\r
142 \r
143     public CustomRouteInfo getCustomRouteInstance(String serviceName, Jedis jedis) throws Exception {\r
144 \r
145 \r
146         CustomRouteInfo customRouteInfo = null;\r
147 \r
148 \r
149         // 获取info信息\r
150         String routekey =\r
151                 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,\r
152                         RouteUtil.ROUTE_PATH_INFO);\r
153         Map<String, String> infomap = jedis.hgetAll(routekey);\r
154         if (!infomap.isEmpty()) {\r
155             customRouteInfo = new CustomRouteInfo();\r
156             customRouteInfo.setServiceName(serviceName);\r
157             customRouteInfo.setUrl(infomap.get("url"));\r
158             customRouteInfo.setControl(infomap.get("control"));\r
159             customRouteInfo.setStatus(infomap.get("status"));\r
160             customRouteInfo.setVisualRange(infomap.get("visualRange"));\r
161             customRouteInfo.setUseOwnUpstream(infomap.get("useOwnUpstream"));\r
162 \r
163 \r
164             // 获取负载均衡信息\r
165             String serviceLBkey =\r
166                     RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,\r
167                             RouteUtil.ROUTE_PATH_LOADBALANCE);\r
168             Set<String> serviceLBset = jedis.keys(serviceLBkey + ":*");\r
169             int serverNum = serviceLBset.size();\r
170             RouteServer[] CustomRouteServerList = new RouteServer[serverNum];\r
171             int i = 0;\r
172             for (String serviceInfo : serviceLBset) {\r
173                 Map<String, String> serviceLBmap = jedis.hgetAll(serviceInfo);\r
174                 RouteServer server = new RouteServer();\r
175                 server.setIp(serviceLBmap.get("ip"));\r
176                 server.setPort(serviceLBmap.get("port"));\r
177                 server.setWeight(Integer.parseInt(serviceLBmap.get("weight")));\r
178                 CustomRouteServerList[i] = server;\r
179                 i++;\r
180             }\r
181 \r
182             customRouteInfo.setServers(CustomRouteServerList);\r
183         }\r
184 \r
185 \r
186         return customRouteInfo;\r
187     }\r
188 \r
189     /**\r
190      * @Title: updateCustomRouteInstance\r
191      * @Description: TODO(更新单个服务信息)\r
192      * @param: @param serviceName\r
193      * @param: @param CustomRouteInfo\r
194      * @param: @return\r
195      * @return: CustomRouteInfo\r
196      */\r
197     public synchronized CustomRouteInfo updateCustomRouteInstance(String serviceName,\r
198             CustomRouteInfo customRouteInfo, String serverPort) {\r
199         if (StringUtils.isBlank(serviceName)) {\r
200             throw new ExtendedNotSupportedException("serviceName  can't be empty");\r
201         }\r
202 \r
203         try {\r
204 \r
205             if (serviceName.equals(customRouteInfo.getServiceName())) {\r
206                 // 删除已存在负载均衡服务器信息\r
207                 deleteCustomRoute(serviceName, RouteUtil.ROUTE_PATH_LOADBALANCE + "*", serverPort);\r
208             } else {\r
209                 // 如果已修改服务名,先删除此服务全部已有信息\r
210                 deleteCustomRoute(serviceName, "*", serverPort);\r
211             }\r
212 \r
213 \r
214             saveCustomRouteInstance(customRouteInfo, serverPort);\r
215 \r
216 \r
217 \r
218         } catch (ExtendedNotSupportedException e) {\r
219             throw e;\r
220         } catch (Exception e) {\r
221             LOGGER.error("updateCustomRoute throw exception", e);\r
222             throw new ExtendedInternalServerErrorException("update CustomRoute throw exception"\r
223                     + e.getMessage());\r
224 \r
225         }\r
226 \r
227         return customRouteInfo;\r
228 \r
229     }\r
230 \r
231     /**\r
232      * @Title updateCustomRouteStatus\r
233      * @Description TODO(更新单个服务状态)\r
234      * @param serviceName\r
235      * @param status\r
236      * @return\r
237      * @return RouteResult\r
238      */\r
239     public synchronized CustomRouteInfo updateCustomRouteStatus(String serviceName, String status) {\r
240 \r
241         if (StringUtils.isBlank(serviceName)) {\r
242             throw new ExtendedNotSupportedException("serviceName  can't be empty");\r
243         }\r
244 \r
245         if (!RouteUtil.contain(RouteUtil.statusRangeMatches, status)) {\r
246             throw new ExtendedNotSupportedException(\r
247                     "save CustomRouteInfo Status FAIL:status is wrong,value range:("\r
248                             + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");\r
249         }\r
250 \r
251         CustomRouteInfo new_customRouteInfo = getCustomRouteInstance(serviceName);\r
252 \r
253 \r
254 \r
255         // 准备info信息\r
256         String serviceInfokey =\r
257                 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,\r
258                         RouteUtil.ROUTE_PATH_INFO);\r
259         Map<String, String> serviceInfoMap = new HashMap<String, String>();\r
260         serviceInfoMap.put("status", status);\r
261 \r
262 \r
263         Jedis jedis = null;\r
264         try {\r
265             jedis = JedisUtil.borrowJedisInstance();\r
266             if (jedis == null) {\r
267                 throw new ExtendedInternalServerErrorException(\r
268                         "fetch from jedis pool failed,null object!");\r
269             }\r
270             // 保存info信息\r
271             jedis.hmset(serviceInfokey, serviceInfoMap);\r
272             new_customRouteInfo.setStatus(status);\r
273 \r
274         } catch (Exception e) {\r
275 \r
276             LOGGER.error("update CustomRoute status throw exception", e);\r
277             throw new ExtendedInternalServerErrorException(\r
278                     "update CustomRoute status throw exception" + e.getMessage());\r
279 \r
280         } finally {\r
281             JedisUtil.returnJedisInstance(jedis);\r
282         }\r
283 \r
284         return new_customRouteInfo;\r
285     }\r
286 \r
287     /**\r
288      * @Title: saveCustomRouteInstance\r
289      * @Description: TODO(存储单个服务信息)\r
290      * @param: @param CustomRouteInfo\r
291      * @param: @return\r
292      * @return: CustomRouteInfo\r
293      */\r
294     public synchronized CustomRouteInfo saveCustomRouteInstance(CustomRouteInfo customRouteInfo,\r
295             String serverPort) {\r
296 \r
297         if (StringUtils.isBlank(customRouteInfo.getServiceName())\r
298                 || customRouteInfo.getServers().length == 0) {\r
299             throw new ExtendedNotSupportedException(\r
300                     "save CustomRouteInfo FAIL: Some required fields are empty");\r
301         }\r
302 \r
303        \r
304             if (!RegExpTestUtil.urlRegExpTest(customRouteInfo.getServiceName())) {\r
305                 throw new ExtendedNotSupportedException(\r
306                         "save CustomRouteInfo FAIL: ServiceName is not a valid format(ServiceName must be begin with /)");\r
307     \r
308             }\r
309         \r
310             if (StringUtils.isNotBlank(customRouteInfo.getUrl())){\r
311                 if (!RegExpTestUtil.urlRegExpTest(customRouteInfo.getUrl())) {\r
312                     throw new ExtendedNotSupportedException(\r
313                             "save CustomRouteInfo FAIL:url is not a valid format(url must be begin with /)");\r
314         \r
315                 }\r
316             }\r
317 \r
318         if (!RouteUtil.contain(RouteUtil.visualRangeRange, customRouteInfo.getVisualRange())) {\r
319             throw new ExtendedNotSupportedException(\r
320                     "save CustomRouteInfo FAIL:VisualRange is wrong,value range:("\r
321                             + RouteUtil.show(RouteUtil.visualRangeMatches) + ")");\r
322         }\r
323 \r
324         if (!RouteUtil.contain(RouteUtil.controlRangeMatches, customRouteInfo.getControl())) {\r
325             throw new ExtendedNotSupportedException(\r
326                     "save CustomRouteInfo FAIL:control is wrong,value range:("\r
327                             + RouteUtil.show(RouteUtil.controlRangeMatches) + ")");\r
328         }\r
329 \r
330         if (!RouteUtil.contain(RouteUtil.statusRangeMatches, customRouteInfo.getStatus())) {\r
331             throw new ExtendedNotSupportedException(\r
332                     "save CustomRouteInfo FAIL:status is wrong,value range:("\r
333                             + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");\r
334         }\r
335 \r
336         if (!RouteUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, customRouteInfo.getUseOwnUpstream())) {\r
337             throw new ExtendedNotSupportedException(\r
338                     "save apiRouteInfo FAIL:useOwnUpstream is wrong,value range:("\r
339                             + RouteUtil.show(RouteUtil.useOwnUpstreamRangeMatches) + ")");\r
340         }\r
341 \r
342         // 检查服务实例格式\r
343         RouteServer[] serverList = customRouteInfo.getServers();\r
344         for (int i = 0; i < serverList.length; i++) {\r
345             RouteServer server = serverList[i];\r
346             if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {\r
347                 throw new ExtendedNotSupportedException("save CustomRouteInfo FAIL:IP("\r
348                         + server.getIp() + ")is not a valid ip address");\r
349             }\r
350 \r
351             if (!RegExpTestUtil.portRegExpTest(server.getPort())) {\r
352                 throw new ExtendedNotSupportedException("save CustomRouteInfo FAIL:Port("\r
353                         + server.getPort() + ")is not a valid Port address");\r
354             }\r
355         }\r
356 \r
357 \r
358         // 准备info信息\r
359         String serviceInfokey =\r
360                 RouteUtil.getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE,\r
361                         customRouteInfo.getServiceName().trim(), RouteUtil.ROUTE_PATH_INFO);\r
362         Map<String, String> serviceInfoMap = new HashMap<String, String>();\r
363         serviceInfoMap.put("url", "/".equals(customRouteInfo.getUrl().trim())\r
364                 ? ""\r
365                 : customRouteInfo.getUrl().trim());\r
366         serviceInfoMap.put("control", customRouteInfo.getControl());\r
367         serviceInfoMap.put("status", customRouteInfo.getStatus());\r
368         serviceInfoMap.put("visualRange", customRouteInfo.getVisualRange());\r
369         serviceInfoMap.put("useOwnUpstream", customRouteInfo.getUseOwnUpstream());\r
370 \r
371 \r
372 \r
373         // 准备负载均衡信息\r
374         String serviceLBkey =\r
375                 RouteUtil.getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE,\r
376                         customRouteInfo.getServiceName(), RouteUtil.ROUTE_PATH_LOADBALANCE);\r
377 \r
378 \r
379         Jedis jedis = null;\r
380         try {\r
381             jedis = JedisUtil.borrowJedisInstance();\r
382             if (jedis == null) {\r
383                 throw new ExtendedInternalServerErrorException(\r
384                         "fetch from jedis pool failed,null object!");\r
385             }\r
386             // 保存info信息\r
387             jedis.hmset(serviceInfokey, serviceInfoMap);\r
388 \r
389             // 保存负载均衡信息\r
390 \r
391             for (int i = 0; i < serverList.length; i++) {\r
392                 Map<String, String> servermap = new HashMap<String, String>();\r
393                 RouteServer server = serverList[i];\r
394 \r
395                 servermap.put("ip", server.getIp());\r
396                 servermap.put("port", server.getPort());\r
397                 servermap.put("weight", Integer.toString(server.getWeight()));\r
398 \r
399                 jedis.hmset(serviceLBkey + ":server" + (i + 1), servermap);\r
400             }\r
401 \r
402 \r
403         } catch (Exception e) {\r
404             LOGGER.error("call redis throw exception", e);\r
405             throw new ExtendedInternalServerErrorException("call redis throw exception:"\r
406                     + e.getMessage());\r
407 \r
408         } finally {\r
409             JedisUtil.returnJedisInstance(jedis);;\r
410         }\r
411 \r
412         return customRouteInfo;\r
413     }\r
414 \r
415 \r
416 \r
417     /**\r
418      * @Title: deleteCustomRoute\r
419      * @Description: TODO(删除单个服务信息)\r
420      * @param: @param type\r
421      * @param: @param serviceName\r
422      * @param: @param delKey\r
423      * @param: @return\r
424      * @return: void\r
425      */\r
426     public synchronized void deleteCustomRoute(String serviceName, String delKey, String serverPort) {\r
427 \r
428         if (StringUtils.isBlank(serviceName)) {\r
429             throw new ExtendedNotSupportedException("serviceName  can't be empty");\r
430         }\r
431 \r
432         Jedis jedis = null;\r
433 \r
434         try {\r
435             jedis = JedisUtil.borrowJedisInstance();\r
436             if (jedis == null) {\r
437                 throw new ExtendedInternalServerErrorException(\r
438                         "fetch from jedis pool failed,null object!");\r
439             }\r
440 \r
441             // 获取info信息\r
442             String routekey =\r
443                     RouteUtil\r
444                             .getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE, serviceName, delKey);\r
445             Set<String> infoSet = jedis.keys(routekey);\r
446 \r
447             if (infoSet.isEmpty()) {\r
448                 throw new ExtendedNotFoundException("delete CustomRoute FAIL:serviceName-"\r
449                         + serviceName + " not fond ");\r
450             }\r
451 \r
452 \r
453             String[] paths = new String[infoSet.size()];\r
454 \r
455             // Set-->数组\r
456             infoSet.toArray(paths);\r
457 \r
458             jedis.del(paths);\r
459 \r
460         } catch (ExtendedNotFoundException e) {\r
461             throw e;\r
462         } catch (Exception e) {\r
463 \r
464             LOGGER.error("delete CustomRoute throw exception", e);\r
465             throw new ExtendedInternalServerErrorException("delete CustomRoute throw exception:"\r
466                     + e.getMessage());\r
467 \r
468         } finally {\r
469             JedisUtil.returnJedisInstance(jedis);\r
470         }\r
471 \r
472 \r
473     }\r
474 }\r