2 * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.msb.wrapper;
18 import java.util.HashMap;
22 import org.apache.commons.lang3.StringUtils;
23 import org.openo.msb.api.CustomRouteInfo;
24 import org.openo.msb.api.RouteServer;
25 import org.openo.msb.api.exception.ExtendedInternalServerErrorException;
26 import org.openo.msb.api.exception.ExtendedNotFoundException;
27 import org.openo.msb.api.exception.ExtendedNotSupportedException;
28 import org.openo.msb.wrapper.util.JedisUtil;
29 import org.openo.msb.wrapper.util.RegExpTestUtil;
30 import org.openo.msb.wrapper.util.RouteUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import redis.clients.jedis.Jedis;
36 public class CustomRouteServiceWrapper {
39 private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteServiceWrapper.class);
41 private static CustomRouteServiceWrapper instance = new CustomRouteServiceWrapper();
43 private CustomRouteServiceWrapper() {}
45 public static CustomRouteServiceWrapper getInstance() {
51 * @Title: getAllCustomRouteService
52 * @Description: TODO(获取全部内容服务列表)
54 * @return: CustomRouteInfo[]
56 public CustomRouteInfo[] getAllCustomRouteInstances() {
60 CustomRouteInfo[] customRouteList = null;
62 jedis = JedisUtil.borrowJedisInstance();
64 throw new ExtendedInternalServerErrorException(
65 "fetch from jedis pool failed,null object!");
70 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, "*",
71 RouteUtil.ROUTE_PATH_INFO);
72 Set<String> routeSet = jedis.keys(routekey);
73 customRouteList = new CustomRouteInfo[routeSet.size()];
76 for (String routePath : routeSet) {
77 String[] routePathArray = routePath.split(":");
78 CustomRouteInfo customRoute = getCustomRouteInstance(routePathArray[3], jedis);
79 customRouteList[i] = customRoute;
84 } catch (Exception e) {
85 LOGGER.error("call redis throw exception", e);
86 throw new ExtendedInternalServerErrorException("call redis throw exception:"
90 JedisUtil.returnJedisInstance(jedis);
93 return customRouteList;
99 * @Title: getCustomRouteInstance
100 * @Description: TODO(通过服务名获取单个内容服务对象信息)
101 * @param: @param serviceName
103 * @return: CustomRouteInfo
105 public CustomRouteInfo getCustomRouteInstance(String serviceName) {
107 if (StringUtils.isBlank(serviceName)) {
108 throw new ExtendedNotSupportedException("serviceName can't be empty");
111 CustomRouteInfo customRouteInfo;
115 jedis = JedisUtil.borrowJedisInstance();
117 throw new ExtendedInternalServerErrorException(
118 "fetch from jedis pool failed,null object!");
121 customRouteInfo = getCustomRouteInstance(serviceName, jedis);
124 } catch (Exception e) {
125 LOGGER.error("call redis throw exception", e);
126 throw new ExtendedInternalServerErrorException("call redis throw exception:"
129 JedisUtil.returnJedisInstance(jedis);
132 if (null == customRouteInfo) {
133 String errInfo = "customRouteInfo not found: serviceName-" + serviceName;
134 LOGGER.warn(errInfo);
135 throw new ExtendedNotFoundException(errInfo);
139 return customRouteInfo;
143 public CustomRouteInfo getCustomRouteInstance(String serviceName, Jedis jedis) throws Exception {
146 CustomRouteInfo customRouteInfo = null;
151 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,
152 RouteUtil.ROUTE_PATH_INFO);
153 Map<String, String> infomap = jedis.hgetAll(routekey);
154 if (!infomap.isEmpty()) {
155 customRouteInfo = new CustomRouteInfo();
156 customRouteInfo.setServiceName(serviceName);
157 customRouteInfo.setUrl(infomap.get("url"));
158 customRouteInfo.setControl(infomap.get("control"));
159 customRouteInfo.setStatus(infomap.get("status"));
160 customRouteInfo.setVisualRange(infomap.get("visualRange"));
161 customRouteInfo.setUseOwnUpstream(infomap.get("useOwnUpstream"));
165 String serviceLBkey =
166 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,
167 RouteUtil.ROUTE_PATH_LOADBALANCE);
168 Set<String> serviceLBset = jedis.keys(serviceLBkey + ":*");
169 int serverNum = serviceLBset.size();
170 RouteServer[] CustomRouteServerList = new RouteServer[serverNum];
172 for (String serviceInfo : serviceLBset) {
173 Map<String, String> serviceLBmap = jedis.hgetAll(serviceInfo);
174 RouteServer server = new RouteServer();
175 server.setIp(serviceLBmap.get("ip"));
176 server.setPort(serviceLBmap.get("port"));
177 server.setWeight(Integer.parseInt(serviceLBmap.get("weight")));
178 CustomRouteServerList[i] = server;
182 customRouteInfo.setServers(CustomRouteServerList);
186 return customRouteInfo;
190 * @Title: updateCustomRouteInstance
191 * @Description: TODO(更新单个服务信息)
192 * @param: @param serviceName
193 * @param: @param CustomRouteInfo
195 * @return: CustomRouteInfo
197 public synchronized CustomRouteInfo updateCustomRouteInstance(String serviceName,
198 CustomRouteInfo customRouteInfo, String serverPort) {
199 if (StringUtils.isBlank(serviceName)) {
200 throw new ExtendedNotSupportedException("serviceName can't be empty");
205 if (serviceName.equals(customRouteInfo.getServiceName())) {
207 deleteCustomRoute(serviceName, RouteUtil.ROUTE_PATH_LOADBALANCE + "*", serverPort);
209 // 如果已修改服务名,先删除此服务全部已有信息
210 deleteCustomRoute(serviceName, "*", serverPort);
214 saveCustomRouteInstance(customRouteInfo, serverPort);
218 } catch (ExtendedNotSupportedException e) {
220 } catch (Exception e) {
221 LOGGER.error("updateCustomRoute throw exception", e);
222 throw new ExtendedInternalServerErrorException("update CustomRoute throw exception"
227 return customRouteInfo;
232 * @Title updateCustomRouteStatus
233 * @Description TODO(更新单个服务状态)
237 * @return RouteResult
239 public synchronized CustomRouteInfo updateCustomRouteStatus(String serviceName, String status) {
241 if (StringUtils.isBlank(serviceName)) {
242 throw new ExtendedNotSupportedException("serviceName can't be empty");
245 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, status)) {
246 throw new ExtendedNotSupportedException(
247 "save CustomRouteInfo Status FAIL:status is wrong,value range:("
248 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
251 CustomRouteInfo new_customRouteInfo = getCustomRouteInstance(serviceName);
256 String serviceInfokey =
257 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,
258 RouteUtil.ROUTE_PATH_INFO);
259 Map<String, String> serviceInfoMap = new HashMap<String, String>();
260 serviceInfoMap.put("status", status);
265 jedis = JedisUtil.borrowJedisInstance();
267 throw new ExtendedInternalServerErrorException(
268 "fetch from jedis pool failed,null object!");
271 jedis.hmset(serviceInfokey, serviceInfoMap);
272 new_customRouteInfo.setStatus(status);
274 } catch (Exception e) {
276 LOGGER.error("update CustomRoute status throw exception", e);
277 throw new ExtendedInternalServerErrorException(
278 "update CustomRoute status throw exception" + e.getMessage());
281 JedisUtil.returnJedisInstance(jedis);
284 return new_customRouteInfo;
288 * @Title: saveCustomRouteInstance
289 * @Description: TODO(存储单个服务信息)
290 * @param: @param CustomRouteInfo
292 * @return: CustomRouteInfo
294 public synchronized CustomRouteInfo saveCustomRouteInstance(CustomRouteInfo customRouteInfo,
297 if (StringUtils.isBlank(customRouteInfo.getServiceName())
298 || customRouteInfo.getServers().length == 0) {
299 throw new ExtendedNotSupportedException(
300 "save CustomRouteInfo FAIL: Some required fields are empty");
304 if (!RegExpTestUtil.urlRegExpTest(customRouteInfo.getServiceName())) {
305 throw new ExtendedNotSupportedException(
306 "save CustomRouteInfo FAIL: ServiceName is not a valid format(ServiceName must be begin with /)");
310 if (StringUtils.isNotBlank(customRouteInfo.getUrl())){
311 if (!RegExpTestUtil.urlRegExpTest(customRouteInfo.getUrl())) {
312 throw new ExtendedNotSupportedException(
313 "save CustomRouteInfo FAIL:url is not a valid format(url must be begin with /)");
318 if (!RouteUtil.contain(RouteUtil.visualRangeRange, customRouteInfo.getVisualRange())) {
319 throw new ExtendedNotSupportedException(
320 "save CustomRouteInfo FAIL:VisualRange is wrong,value range:("
321 + RouteUtil.show(RouteUtil.visualRangeMatches) + ")");
324 if (!RouteUtil.contain(RouteUtil.controlRangeMatches, customRouteInfo.getControl())) {
325 throw new ExtendedNotSupportedException(
326 "save CustomRouteInfo FAIL:control is wrong,value range:("
327 + RouteUtil.show(RouteUtil.controlRangeMatches) + ")");
330 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, customRouteInfo.getStatus())) {
331 throw new ExtendedNotSupportedException(
332 "save CustomRouteInfo FAIL:status is wrong,value range:("
333 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
336 if (!RouteUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, customRouteInfo.getUseOwnUpstream())) {
337 throw new ExtendedNotSupportedException(
338 "save apiRouteInfo FAIL:useOwnUpstream is wrong,value range:("
339 + RouteUtil.show(RouteUtil.useOwnUpstreamRangeMatches) + ")");
343 RouteServer[] serverList = customRouteInfo.getServers();
344 for (int i = 0; i < serverList.length; i++) {
345 RouteServer server = serverList[i];
346 if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {
347 throw new ExtendedNotSupportedException("save CustomRouteInfo FAIL:IP("
348 + server.getIp() + ")is not a valid ip address");
351 if (!RegExpTestUtil.portRegExpTest(server.getPort())) {
352 throw new ExtendedNotSupportedException("save CustomRouteInfo FAIL:Port("
353 + server.getPort() + ")is not a valid Port address");
359 String serviceInfokey =
360 RouteUtil.getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE,
361 customRouteInfo.getServiceName().trim(), RouteUtil.ROUTE_PATH_INFO);
362 Map<String, String> serviceInfoMap = new HashMap<String, String>();
363 serviceInfoMap.put("url", "/".equals(customRouteInfo.getUrl().trim())
365 : customRouteInfo.getUrl().trim());
366 serviceInfoMap.put("control", customRouteInfo.getControl());
367 serviceInfoMap.put("status", customRouteInfo.getStatus());
368 serviceInfoMap.put("visualRange", customRouteInfo.getVisualRange());
369 serviceInfoMap.put("useOwnUpstream", customRouteInfo.getUseOwnUpstream());
374 String serviceLBkey =
375 RouteUtil.getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE,
376 customRouteInfo.getServiceName(), RouteUtil.ROUTE_PATH_LOADBALANCE);
381 jedis = JedisUtil.borrowJedisInstance();
383 throw new ExtendedInternalServerErrorException(
384 "fetch from jedis pool failed,null object!");
387 jedis.hmset(serviceInfokey, serviceInfoMap);
391 for (int i = 0; i < serverList.length; i++) {
392 Map<String, String> servermap = new HashMap<String, String>();
393 RouteServer server = serverList[i];
395 servermap.put("ip", server.getIp());
396 servermap.put("port", server.getPort());
397 servermap.put("weight", Integer.toString(server.getWeight()));
399 jedis.hmset(serviceLBkey + ":server" + (i + 1), servermap);
403 } catch (Exception e) {
404 LOGGER.error("call redis throw exception", e);
405 throw new ExtendedInternalServerErrorException("call redis throw exception:"
409 JedisUtil.returnJedisInstance(jedis);;
412 return customRouteInfo;
418 * @Title: deleteCustomRoute
419 * @Description: TODO(删除单个服务信息)
420 * @param: @param type
421 * @param: @param serviceName
422 * @param: @param delKey
426 public synchronized void deleteCustomRoute(String serviceName, String delKey, String serverPort) {
428 if (StringUtils.isBlank(serviceName)) {
429 throw new ExtendedNotSupportedException("serviceName can't be empty");
435 jedis = JedisUtil.borrowJedisInstance();
437 throw new ExtendedInternalServerErrorException(
438 "fetch from jedis pool failed,null object!");
444 .getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE, serviceName, delKey);
445 Set<String> infoSet = jedis.keys(routekey);
447 if (infoSet.isEmpty()) {
448 throw new ExtendedNotFoundException("delete CustomRoute FAIL:serviceName-"
449 + serviceName + " not fond ");
453 String[] paths = new String[infoSet.size()];
456 infoSet.toArray(paths);
460 } catch (ExtendedNotFoundException e) {
462 } catch (Exception e) {
464 LOGGER.error("delete CustomRoute throw exception", e);
465 throw new ExtendedInternalServerErrorException("delete CustomRoute throw exception:"
469 JedisUtil.returnJedisInstance(jedis);