--- /dev/null
+/**\r
+* Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+* http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+package org.openo.msb.wrapper;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.apache.commons.lang3.StringUtils;\r
+import org.openo.msb.api.IuiRouteInfo;\r
+import org.openo.msb.api.RouteServer;\r
+import org.openo.msb.api.exception.ExtendedInternalServerErrorException;\r
+import org.openo.msb.api.exception.ExtendedNotFoundException;\r
+import org.openo.msb.api.exception.ExtendedNotSupportedException;\r
+import org.openo.msb.wrapper.util.JedisUtil;\r
+import org.openo.msb.wrapper.util.RegExpTestUtil;\r
+import org.openo.msb.wrapper.util.RouteUtil;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import redis.clients.jedis.Jedis;\r
+\r
+public class IuiRouteServiceWrapper {\r
+\r
+\r
+ private static final Logger LOGGER = LoggerFactory.getLogger(IuiRouteServiceWrapper.class);\r
+\r
+ private static IuiRouteServiceWrapper instance = new IuiRouteServiceWrapper();\r
+\r
+ private IuiRouteServiceWrapper() {}\r
+\r
+ public static IuiRouteServiceWrapper getInstance() {\r
+ return instance;\r
+ }\r
+\r
+\r
+ /**\r
+ * @Title: getAllIuiRouteService\r
+ * @Description: TODO(获取全部内容服务列表)\r
+ * @param: @return\r
+ * @return: IuiRouteInfo[]\r
+ */\r
+ public IuiRouteInfo[] getAllIuiRouteInstances() {\r
+\r
+\r
+ Jedis jedis = null;\r
+ IuiRouteInfo[] iuiRouteList = null;\r
+ try {\r
+ jedis = JedisUtil.borrowJedisInstance();\r
+ if (jedis == null) {\r
+ throw new ExtendedInternalServerErrorException(\r
+ "fetch from jedis pool failed,null object!");\r
+ }\r
+\r
+ // 获取全部服务列表\r
+ String routekey =\r
+ RouteUtil\r
+ .getPrefixedKey("", RouteUtil.IUIROUTE, "*", RouteUtil.ROUTE_PATH_INFO);\r
+ Set<String> routeSet = jedis.keys(routekey);\r
+ iuiRouteList = new IuiRouteInfo[routeSet.size()];\r
+\r
+ int i = 0;\r
+ for (String routePath : routeSet) {\r
+ String[] routePathArray = routePath.split(":");\r
+ IuiRouteInfo iuiRoute = getIuiRouteInstance(routePathArray[3], jedis);\r
+ iuiRouteList[i] = iuiRoute;\r
+ i++;\r
+ }\r
+\r
+\r
+ } catch (Exception e) {\r
+ LOGGER.error("call redis throw exception", e);\r
+ throw new ExtendedInternalServerErrorException("call redis throw exception:"\r
+ + e.getMessage());\r
+ } finally {\r
+ JedisUtil.returnJedisInstance(jedis);\r
+ }\r
+\r
+ return iuiRouteList;\r
+ }\r
+\r
+\r
+\r
+ /**\r
+ * @Title: getIuiRouteInstance\r
+ * @Description: TODO(通过服务名获取单个内容服务对象信息)\r
+ * @param: @param serviceName\r
+ * @param: @return\r
+ * @return: IuiRouteInfo\r
+ */\r
+ public IuiRouteInfo getIuiRouteInstance(String serviceName) {\r
+\r
+ if (StringUtils.isBlank(serviceName)) {\r
+ throw new ExtendedNotSupportedException("serviceName can't be empty");\r
+ }\r
+\r
+ IuiRouteInfo iuiRouteInfo = null;\r
+\r
+ Jedis jedis = null;\r
+ try {\r
+ jedis = JedisUtil.borrowJedisInstance();\r
+ if (jedis == null) {\r
+ throw new ExtendedInternalServerErrorException(\r
+ "fetch from jedis pool failed,null object!");\r
+ }\r
+\r
+ iuiRouteInfo = getIuiRouteInstance(serviceName, jedis);\r
+\r
+\r
+ } catch (Exception e) {\r
+ LOGGER.error("call redis throw exception", e);\r
+ throw new ExtendedInternalServerErrorException("call redis throw exception:"\r
+ + e.getMessage());\r
+ } finally {\r
+ JedisUtil.returnJedisInstance(jedis);\r
+ }\r
+\r
+ if (null == iuiRouteInfo) {\r
+ String errInfo = "iuiRouteInfo not found: serviceName-" + serviceName;\r
+ LOGGER.warn(errInfo);\r
+ throw new ExtendedNotFoundException(errInfo);\r
+\r
+ }\r
+\r
+ return iuiRouteInfo;\r
+\r
+ }\r
+\r
+ public IuiRouteInfo getIuiRouteInstance(String serviceName, Jedis jedis) throws Exception {\r
+\r
+\r
+ IuiRouteInfo iuiRouteInfo = null;\r
+\r
+\r
+ // 获取info信息\r
+ String routekey =\r
+ RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName,\r
+ RouteUtil.ROUTE_PATH_INFO);\r
+ Map<String, String> infomap = jedis.hgetAll(routekey);\r
+ if (!infomap.isEmpty()) {\r
+ iuiRouteInfo = new IuiRouteInfo();\r
+ iuiRouteInfo.setServiceName(serviceName);\r
+ iuiRouteInfo.setUrl(infomap.get("url"));\r
+ iuiRouteInfo.setControl(infomap.get("control"));\r
+ iuiRouteInfo.setStatus(infomap.get("status"));\r
+ iuiRouteInfo.setVisualRange(infomap.get("visualRange"));\r
+ iuiRouteInfo.setUseOwnUpstream(infomap.get("useOwnUpstream"));\r
+\r
+\r
+ // 获取负载均衡信息\r
+ String serviceLBkey =\r
+ RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName,\r
+ RouteUtil.ROUTE_PATH_LOADBALANCE);\r
+ Set<String> serviceLBset = jedis.keys(serviceLBkey + ":*");\r
+ int serverNum = serviceLBset.size();\r
+ RouteServer[] iuiRouteServerList = new RouteServer[serverNum];\r
+ int i = 0;\r
+ for (String serviceInfo : serviceLBset) {\r
+ Map<String, String> serviceLBmap = jedis.hgetAll(serviceInfo);\r
+ RouteServer server = new RouteServer();\r
+ server.setIp(serviceLBmap.get("ip"));\r
+ server.setPort(serviceLBmap.get("port"));\r
+ server.setWeight(Integer.parseInt(serviceLBmap.get("weight")));\r
+ iuiRouteServerList[i] = server;\r
+ i++;\r
+ }\r
+\r
+ iuiRouteInfo.setServers(iuiRouteServerList);\r
+ }\r
+\r
+\r
+ return iuiRouteInfo;\r
+ }\r
+\r
+ /**\r
+ * @Title: updateIuiRouteInstance\r
+ * @Description: TODO(更新单个服务信息)\r
+ * @param: @param serviceName\r
+ * @param: @param IuiRouteInfo\r
+ * @param: @return\r
+ * @return: IuiRouteInfo\r
+ */\r
+ public synchronized IuiRouteInfo updateIuiRouteInstance(String serviceName,\r
+ IuiRouteInfo iuiRouteInfo) {\r
+\r
+ if (StringUtils.isBlank(serviceName)) {\r
+ throw new ExtendedNotSupportedException("serviceName can't be empty");\r
+ }\r
+\r
+ try {\r
+ if (serviceName.equals(iuiRouteInfo.getServiceName())) {\r
+ // 删除已存在负载均衡服务器信息\r
+ deleteIuiRoute(serviceName, RouteUtil.ROUTE_PATH_LOADBALANCE + "*");\r
+\r
+ } else {\r
+ // 如果已修改服务名,先删除此服务全部已有信息\r
+ deleteIuiRoute(serviceName, "*");\r
+ }\r
+ saveIuiRouteInstance(iuiRouteInfo);\r
+\r
+ } catch (ExtendedNotSupportedException e) {\r
+ throw e;\r
+ } catch (Exception e) {\r
+ LOGGER.error("updateIuiRoute throw exception", e);\r
+ throw new ExtendedInternalServerErrorException("update IuiRouteInfo throw exception"\r
+ + e.getMessage());\r
+ }\r
+\r
+ return iuiRouteInfo;\r
+\r
+ }\r
+\r
+ /**\r
+ * @Title updateIuiRouteStatus\r
+ * @Description TODO(更新单个服务状态)\r
+ * @param serviceName\r
+ * @param status\r
+ * @return\r
+ * @return RouteResult\r
+ */\r
+ public synchronized IuiRouteInfo updateIuiRouteStatus(String serviceName, String status) {\r
+\r
+\r
+ if (StringUtils.isBlank(serviceName)) {\r
+ throw new ExtendedNotSupportedException("serviceName can't be empty");\r
+ }\r
+\r
+ if (!RouteUtil.contain(RouteUtil.statusRangeMatches, status)) {\r
+ throw new ExtendedNotSupportedException(\r
+ "save IuiRouteInfo Status FAIL:status is wrong,value range:("\r
+ + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");\r
+ }\r
+\r
+ IuiRouteInfo new_iuiRouteInfo = getIuiRouteInstance(serviceName);\r
+\r
+ // 准备info信息\r
+ String serviceInfokey =\r
+ RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName,\r
+ RouteUtil.ROUTE_PATH_INFO);\r
+ Map<String, String> serviceInfoMap = new HashMap<String, String>();\r
+ serviceInfoMap.put("status", status);\r
+\r
+\r
+ Jedis jedis = null;\r
+ try {\r
+ jedis = JedisUtil.borrowJedisInstance();\r
+ if (jedis == null) {\r
+ throw new ExtendedInternalServerErrorException(\r
+ "fetch from jedis pool failed,null object!");\r
+ }\r
+ // 保存info信息\r
+ jedis.hmset(serviceInfokey, serviceInfoMap);\r
+ new_iuiRouteInfo.setStatus(status);\r
+\r
+ } catch (Exception e) {\r
+ LOGGER.error("update IuiRoute status throw exception", e);\r
+ throw new ExtendedInternalServerErrorException(\r
+ "update IuiRouteInfo status throw exception" + e.getMessage());\r
+\r
+ } finally {\r
+ JedisUtil.returnJedisInstance(jedis);\r
+ }\r
+\r
+ return new_iuiRouteInfo;\r
+ }\r
+\r
+ /**\r
+ * @Title: saveIuiRouteInstance\r
+ * @Description: TODO(存储单个服务信息)\r
+ * @param: @param IuiRouteInfo\r
+ * @param: @return\r
+ * @return: IuiRouteInfo\r
+ */\r
+ public synchronized IuiRouteInfo saveIuiRouteInstance(IuiRouteInfo iuiRouteInfo) {\r
+\r
+ if (StringUtils.isBlank(iuiRouteInfo.getServiceName())\r
+ || iuiRouteInfo.getServers().length == 0) {\r
+ throw new ExtendedNotSupportedException(\r
+ "save iuiRouteInfo FAIL: Some required fields are empty");\r
+ }\r
+\r
+ if (StringUtils.isNotBlank(iuiRouteInfo.getUrl())){\r
+ if (!RegExpTestUtil.urlRegExpTest(iuiRouteInfo.getUrl())) {\r
+ throw new ExtendedNotSupportedException(\r
+ "save iuiRouteInfo FAIL:url is not a valid format(url must be begin with /)");\r
+ \r
+ }\r
+ }\r
+\r
+ if (!RouteUtil.contain(RouteUtil.visualRangeRange, iuiRouteInfo.getVisualRange())) {\r
+ throw new ExtendedNotSupportedException(\r
+ "save iuiRouteInfo FAIL:VisualRange is wrong,value range:("\r
+ + RouteUtil.show(RouteUtil.visualRangeMatches) + ")");\r
+ }\r
+\r
+ if (!RouteUtil.contain(RouteUtil.controlRangeMatches, iuiRouteInfo.getControl())) {\r
+ throw new ExtendedNotSupportedException(\r
+ "save iuiRouteInfo FAIL:control is wrong,value range:("\r
+ + RouteUtil.show(RouteUtil.controlRangeMatches) + ")");\r
+ }\r
+\r
+ if (!RouteUtil.contain(RouteUtil.statusRangeMatches, iuiRouteInfo.getStatus())) {\r
+ throw new ExtendedNotSupportedException(\r
+ "save iuiRouteInfo FAIL:status is wrong,value range:("\r
+ + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");\r
+ }\r
+\r
+ if (!RouteUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, iuiRouteInfo.getUseOwnUpstream())) {\r
+ throw new ExtendedNotSupportedException(\r
+ "save apiRouteInfo FAIL:useOwnUpstream is wrong,value range:("\r
+ + RouteUtil.show(RouteUtil.useOwnUpstreamRangeMatches) + ")");\r
+ }\r
+\r
+ // 检查服务实例格式\r
+ RouteServer[] serverList = iuiRouteInfo.getServers();\r
+ for (int i = 0; i < serverList.length; i++) {\r
+ RouteServer server = serverList[i];\r
+ if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {\r
+ throw new ExtendedNotSupportedException("save iuiRouteInfo FAIL:IP("\r
+ + server.getIp() + ")is not a valid ip address");\r
+ }\r
+\r
+ if (!RegExpTestUtil.portRegExpTest(server.getPort())) {\r
+ throw new ExtendedNotSupportedException("save iuiRouteInfo FAIL:Port("\r
+ + server.getPort() + ")is not a valid Port address");\r
+ }\r
+ }\r
+\r
+\r
+ // 准备info信息\r
+ String serviceInfokey =\r
+ RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, iuiRouteInfo.getServiceName().trim(),\r
+ RouteUtil.ROUTE_PATH_INFO);\r
+ Map<String, String> serviceInfoMap = new HashMap<String, String>();\r
+ serviceInfoMap.put("url", "/".equals(iuiRouteInfo.getUrl().trim()) ? "" : iuiRouteInfo\r
+ .getUrl().trim());\r
+ serviceInfoMap.put("control", iuiRouteInfo.getControl());\r
+ serviceInfoMap.put("status", iuiRouteInfo.getStatus());\r
+ serviceInfoMap.put("visualRange", iuiRouteInfo.getVisualRange());\r
+ serviceInfoMap.put("useOwnUpstream", iuiRouteInfo.getUseOwnUpstream());\r
+\r
+\r
+ // 准备负载均衡信息\r
+ String serviceLBkey =\r
+ RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, iuiRouteInfo.getServiceName(),\r
+ RouteUtil.ROUTE_PATH_LOADBALANCE);\r
+\r
+\r
+ Jedis jedis = null;\r
+ try {\r
+ jedis = JedisUtil.borrowJedisInstance();\r
+ if (jedis == null) {\r
+ throw new ExtendedInternalServerErrorException(\r
+ "fetch from jedis pool failed,null object!");\r
+ }\r
+ // 保存info信息\r
+ jedis.hmset(serviceInfokey, serviceInfoMap);\r
+\r
+ // 保存负载均衡信息\r
+ for (int i = 0; i < serverList.length; i++) {\r
+ Map<String, String> servermap = new HashMap<String, String>();\r
+ RouteServer server = serverList[i];\r
+\r
+ servermap.put("ip", server.getIp());\r
+ servermap.put("port", server.getPort());\r
+ servermap.put("weight", Integer.toString(server.getWeight()));\r
+\r
+ jedis.hmset(serviceLBkey + ":server" + (i + 1), servermap);\r
+ }\r
+\r
+\r
+ } catch (Exception e) {\r
+ LOGGER.error("call redis throw exception", e);\r
+ throw new ExtendedInternalServerErrorException("call redis throw exception:"\r
+ + e.getMessage());\r
+ } finally {\r
+ JedisUtil.returnJedisInstance(jedis);;\r
+ }\r
+\r
+ return iuiRouteInfo;\r
+ }\r
+\r
+\r
+\r
+ /**\r
+ * @Title: deleteIuiRoute\r
+ * @Description: TODO(删除单个服务信息)\r
+ * @param: @param type\r
+ * @param: @param serviceName\r
+ * @param: @param delKey\r
+ * @param: @return\r
+ * @return: void\r
+ */\r
+ public synchronized void deleteIuiRoute(String serviceName, String delKey) {\r
+\r
+ if (StringUtils.isBlank(serviceName)) {\r
+ throw new ExtendedNotSupportedException("serviceName can't be empty");\r
+ }\r
+\r
+ Jedis jedis = null;\r
+ try {\r
+ jedis = JedisUtil.borrowJedisInstance();\r
+ if (jedis == null) {\r
+ throw new ExtendedInternalServerErrorException(\r
+ "fetch from jedis pool failed,null object!");\r
+ }\r
+\r
+ // 获取info信息\r
+ String routekey = RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName, delKey);\r
+ Set<String> infoSet = jedis.keys(routekey);\r
+\r
+ if (infoSet.isEmpty()) {\r
+ throw new ExtendedNotFoundException("delete IuiRoute FAIL:serviceName-"\r
+ + serviceName + " not fond ");\r
+ }\r
+\r
+ String[] paths = new String[infoSet.size()];\r
+\r
+ // Set-->数组\r
+ infoSet.toArray(paths);\r
+\r
+ jedis.del(paths);\r
+\r
+\r
+ } catch (ExtendedNotFoundException e) {\r
+ throw e;\r
+ } catch (Exception e) {\r
+ LOGGER.error("delete IuiRoute throw exception", e);\r
+ throw new ExtendedInternalServerErrorException("delete IuiRoute throw exception:"\r
+ + e.getMessage());\r
+ } finally {\r
+ JedisUtil.returnJedisInstance(jedis);\r
+ }\r
+\r
+\r
+ }\r
+\r
+\r
+\r
+}\r