2 * Copyright 2016 ZTE, Inc. and others.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 package org.openo.msb.wrapper;
\r
18 import java.util.HashMap;
\r
19 import java.util.Map;
\r
20 import java.util.Set;
\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
34 import redis.clients.jedis.Jedis;
\r
36 public class CustomRouteServiceWrapper {
\r
39 private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteServiceWrapper.class);
\r
41 private static CustomRouteServiceWrapper instance = new CustomRouteServiceWrapper();
\r
43 private CustomRouteServiceWrapper() {}
\r
45 public static CustomRouteServiceWrapper getInstance() {
\r
51 * @Title: getAllCustomRouteService
\r
52 * @Description: TODO(获取全部内容服务列表)
\r
54 * @return: CustomRouteInfo[]
\r
56 public CustomRouteInfo[] getAllCustomRouteInstances() {
\r
60 CustomRouteInfo[] customRouteList = null;
\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
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
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
84 } catch (Exception e) {
\r
85 LOGGER.error("call redis throw exception", e);
\r
86 throw new ExtendedInternalServerErrorException("call redis throw exception:"
\r
90 JedisUtil.returnJedisInstance(jedis);
\r
93 return customRouteList;
\r
99 * @Title: getCustomRouteInstance
\r
100 * @Description: TODO(通过服务名获取单个内容服务对象信息)
\r
101 * @param: @param serviceName
\r
103 * @return: CustomRouteInfo
\r
105 public CustomRouteInfo getCustomRouteInstance(String serviceName) {
\r
107 if (StringUtils.isBlank(serviceName)) {
\r
108 throw new ExtendedNotSupportedException("serviceName can't be empty");
\r
111 CustomRouteInfo customRouteInfo;
\r
113 Jedis jedis = null;
\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
121 customRouteInfo = getCustomRouteInstance(serviceName, jedis);
\r
124 } catch (Exception e) {
\r
125 LOGGER.error("call redis throw exception", e);
\r
126 throw new ExtendedInternalServerErrorException("call redis throw exception:"
\r
129 JedisUtil.returnJedisInstance(jedis);
\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
139 return customRouteInfo;
\r
143 public CustomRouteInfo getCustomRouteInstance(String serviceName, Jedis jedis) throws Exception {
\r
146 CustomRouteInfo customRouteInfo = null;
\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
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
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
182 customRouteInfo.setServers(CustomRouteServerList);
\r
186 return customRouteInfo;
\r
190 * @Title: updateCustomRouteInstance
\r
191 * @Description: TODO(更新单个服务信息)
\r
192 * @param: @param serviceName
\r
193 * @param: @param CustomRouteInfo
\r
195 * @return: CustomRouteInfo
\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
205 if (serviceName.equals(customRouteInfo.getServiceName())) {
\r
207 deleteCustomRoute(serviceName, RouteUtil.ROUTE_PATH_LOADBALANCE + "*", serverPort);
\r
209 // 如果已修改服务名,先删除此服务全部已有信息
\r
210 deleteCustomRoute(serviceName, "*", serverPort);
\r
214 saveCustomRouteInstance(customRouteInfo, serverPort);
\r
218 } catch (ExtendedNotSupportedException e) {
\r
220 } catch (Exception e) {
\r
221 LOGGER.error("updateCustomRoute throw exception", e);
\r
222 throw new ExtendedInternalServerErrorException("update CustomRoute throw exception"
\r
227 return customRouteInfo;
\r
232 * @Title updateCustomRouteStatus
\r
233 * @Description TODO(更新单个服务状态)
\r
234 * @param serviceName
\r
237 * @return RouteResult
\r
239 public synchronized CustomRouteInfo updateCustomRouteStatus(String serviceName, String status) {
\r
241 if (StringUtils.isBlank(serviceName)) {
\r
242 throw new ExtendedNotSupportedException("serviceName can't be empty");
\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
251 CustomRouteInfo new_customRouteInfo = getCustomRouteInstance(serviceName);
\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
263 Jedis jedis = null;
\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
271 jedis.hmset(serviceInfokey, serviceInfoMap);
\r
272 new_customRouteInfo.setStatus(status);
\r
274 } catch (Exception e) {
\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
281 JedisUtil.returnJedisInstance(jedis);
\r
284 return new_customRouteInfo;
\r
288 * @Title: saveCustomRouteInstance
\r
289 * @Description: TODO(存储单个服务信息)
\r
290 * @param: @param CustomRouteInfo
\r
292 * @return: CustomRouteInfo
\r
294 public synchronized CustomRouteInfo saveCustomRouteInstance(CustomRouteInfo customRouteInfo,
\r
295 String serverPort) {
\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
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
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
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
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
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
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
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
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
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
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
374 String serviceLBkey =
\r
375 RouteUtil.getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE,
\r
376 customRouteInfo.getServiceName(), RouteUtil.ROUTE_PATH_LOADBALANCE);
\r
379 Jedis jedis = null;
\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
387 jedis.hmset(serviceInfokey, serviceInfoMap);
\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
395 servermap.put("ip", server.getIp());
\r
396 servermap.put("port", server.getPort());
\r
397 servermap.put("weight", Integer.toString(server.getWeight()));
\r
399 jedis.hmset(serviceLBkey + ":server" + (i + 1), servermap);
\r
403 } catch (Exception e) {
\r
404 LOGGER.error("call redis throw exception", e);
\r
405 throw new ExtendedInternalServerErrorException("call redis throw exception:"
\r
409 JedisUtil.returnJedisInstance(jedis);;
\r
412 return customRouteInfo;
\r
418 * @Title: deleteCustomRoute
\r
419 * @Description: TODO(删除单个服务信息)
\r
420 * @param: @param type
\r
421 * @param: @param serviceName
\r
422 * @param: @param delKey
\r
426 public synchronized void deleteCustomRoute(String serviceName, String delKey, String serverPort) {
\r
428 if (StringUtils.isBlank(serviceName)) {
\r
429 throw new ExtendedNotSupportedException("serviceName can't be empty");
\r
432 Jedis jedis = null;
\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
444 .getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE, serviceName, delKey);
\r
445 Set<String> infoSet = jedis.keys(routekey);
\r
447 if (infoSet.isEmpty()) {
\r
448 throw new ExtendedNotFoundException("delete CustomRoute FAIL:serviceName-"
\r
449 + serviceName + " not fond ");
\r
453 String[] paths = new String[infoSet.size()];
\r
456 infoSet.toArray(paths);
\r
460 } catch (ExtendedNotFoundException e) {
\r
462 } catch (Exception e) {
\r
464 LOGGER.error("delete CustomRoute throw exception", e);
\r
465 throw new ExtendedInternalServerErrorException("delete CustomRoute throw exception:"
\r
469 JedisUtil.returnJedisInstance(jedis);
\r