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;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
22 import java.util.HashMap;
26 import org.apache.commons.lang3.StringUtils;
27 import org.openo.msb.api.ApiRouteInfo;
28 import org.openo.msb.api.DiscoverInfo;
29 import org.openo.msb.api.RouteServer;
30 import org.openo.msb.api.exception.ExtendedInternalServerErrorException;
31 import org.openo.msb.api.exception.ExtendedNotFoundException;
32 import org.openo.msb.api.exception.ExtendedNotSupportedException;
33 import org.openo.msb.wrapper.util.JedisUtil;
34 import org.openo.msb.wrapper.util.RegExpTestUtil;
35 import org.openo.msb.wrapper.util.RouteUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import redis.clients.jedis.Jedis;
44 * @ClassName: ApiRouteServiceWrapper
45 * @Description: TODO(ApiRoute服务类)
46 * @author tanghua10186366
47 * @date 2015年9月25日 上午9:44:04
50 public class ApiRouteServiceWrapper {
52 private static final Logger LOGGER = LoggerFactory.getLogger(ApiRouteServiceWrapper.class);
55 private static ApiRouteServiceWrapper instance = new ApiRouteServiceWrapper();
57 private ApiRouteServiceWrapper() {}
59 public static ApiRouteServiceWrapper getInstance() {
64 * @Title: getAllApiRouteInstances
65 * @Description: TODO(获取全部服务列表)
67 * @return: ApiRouteInfoBean[]
69 public ApiRouteInfo[] getAllApiRouteInstances() {
73 ApiRouteInfo[] apiRouteList = null;
75 jedis = JedisUtil.borrowJedisInstance();
77 throw new ExtendedInternalServerErrorException(
78 "fetch from jedis pool failed,null object!");
84 .getPrefixedKey("", RouteUtil.APIROUTE, "*", RouteUtil.ROUTE_PATH_INFO);
85 Set<String> routeSet = jedis.keys(routekey);
86 apiRouteList = new ApiRouteInfo[routeSet.size()];
89 for (String routePath : routeSet) {
90 String[] routePathArray = routePath.split(":");
91 ApiRouteInfo apiRoute =
92 getApiRouteInstance(routePathArray[3], routePathArray[4], jedis);
93 apiRouteList[i] = apiRoute;
98 } catch (Exception e) {
99 LOGGER.error("call redis throw exception", e);
100 throw new ExtendedInternalServerErrorException("call redis throw exception:"
104 JedisUtil.returnJedisInstance(jedis);
112 public static boolean checkRedisConnect() {
116 jedis = JedisUtil.borrowJedisInstance();
120 } catch (Exception e) {
121 LOGGER.error("call redis throw exception", e);
123 JedisUtil.returnJedisInstance(jedis);
130 * @Title: getApiRouteInstance
131 * @Description: TODO(通过服务名+版本号获取单个服务对象信息)
132 * @param: @param serviceName
133 * @param: @param version
135 * @return: ApiRouteInfo
137 public ApiRouteInfo getApiRouteInstance(String serviceName, String version) {
139 if (StringUtils.isBlank(serviceName)) {
140 throw new ExtendedNotSupportedException("serviceName can't be empty");
143 if (StringUtils.isNotBlank(version)) {
144 if (!RegExpTestUtil.versionRegExpTest(version)) {
145 throw new ExtendedNotSupportedException("version (" + version
146 + ") is not a valid format");
151 ApiRouteInfo apiRouteInfo = null;
155 jedis = JedisUtil.borrowJedisInstance();
157 throw new ExtendedInternalServerErrorException(
158 "fetch from jedis pool failed,null object!");
161 apiRouteInfo = getApiRouteInstance(serviceName, version, jedis);
164 } catch (Exception e) {
165 LOGGER.error("call redis throw exception", e);
166 throw new ExtendedInternalServerErrorException("call redis throw exception:"
170 JedisUtil.returnJedisInstance(jedis);
173 if (null == apiRouteInfo) {
175 "ApiRouteInfo not found: serviceName-" + serviceName + " ,version-" + version;
176 LOGGER.warn(errInfo);
177 throw new ExtendedNotFoundException(errInfo);
185 public ApiRouteInfo getApiRouteInstance(String serviceName, String version, Jedis jedis)
187 if ("null".equals(version)) {
191 ApiRouteInfo apiRouteInfo = null;
196 RouteUtil.getPrefixedKey("", RouteUtil.APIROUTE, serviceName, version,
197 RouteUtil.ROUTE_PATH_INFO);
198 Map<String, String> infomap = jedis.hgetAll(routekey);
199 if (!infomap.isEmpty()) {
200 apiRouteInfo = new ApiRouteInfo();
201 apiRouteInfo.setServiceName(serviceName);
202 apiRouteInfo.setVersion(version);
203 apiRouteInfo.setUrl(infomap.get("url"));
204 apiRouteInfo.setMetricsUrl(infomap.get("metricsUrl"));
205 apiRouteInfo.setApiJson(infomap.get("apijson"));
206 apiRouteInfo.setApiJsonType(infomap.get("apiJsonType"));
207 apiRouteInfo.setControl(infomap.get("control"));
208 apiRouteInfo.setStatus(infomap.get("status"));
209 apiRouteInfo.setVisualRange(infomap.get("visualRange"));
210 apiRouteInfo.setUseOwnUpstream(infomap.get("useOwnUpstream"));
214 String serviceLBkey =
215 RouteUtil.getPrefixedKey("", RouteUtil.APIROUTE, serviceName, version,
216 RouteUtil.ROUTE_PATH_LOADBALANCE);
217 Set<String> serviceLBset = jedis.keys(serviceLBkey + ":*");
218 int serverNum = serviceLBset.size();
219 RouteServer[] apiRouteServerList = new RouteServer[serverNum];
221 for (String serviceInfo : serviceLBset) {
222 Map<String, String> serviceLBmap = jedis.hgetAll(serviceInfo);
223 RouteServer server = new RouteServer();
224 server.setIp(serviceLBmap.get("ip"));
225 server.setPort(serviceLBmap.get("port"));
226 server.setWeight(Integer.parseInt(serviceLBmap.get("weight")));
227 apiRouteServerList[i] = server;
231 apiRouteInfo.setServers(apiRouteServerList);
235 // ApiRouteLifeCycle lifeCycle = new ApiRouteLifeCycle();
236 // String serviceLifekey =
237 // RouteUtil.getPrefixedKey("", RouteUtil.APIROUTE, serviceName, version,
238 // RouteUtil.APIROUTE_PATH_LIFE);
239 // Map<String, String> serviceLifeMap = jedis.hgetAll(serviceLifekey);
241 // lifeCycle.setInstallPath(serviceLifeMap.get("path"));
242 // lifeCycle.setStartScript(serviceLifeMap.get("start"));
243 // lifeCycle.setStopScript(serviceLifeMap.get("stop"));
245 // apiRouteInfo.setLifeCycle(lifeCycle);
253 * @Title: updateApiRouteInstance
254 * @Description: TODO(更新单个服务信息)
255 * @param: @param serviceName
256 * @param: @param version
257 * @param: @param apiRouteInfo
259 * @return: ApiRouteInfo
261 public synchronized ApiRouteInfo updateApiRouteInstance(String serviceName, String version,
262 ApiRouteInfo apiRouteInfo, String serverPort) {
264 if ("null".equals(version)) {
268 if (StringUtils.isBlank(serviceName)) {
269 throw new ExtendedNotSupportedException("serviceName can't be empty");
272 if (StringUtils.isNotBlank(version)) {
273 if (!RegExpTestUtil.versionRegExpTest(version)) {
274 throw new ExtendedNotSupportedException("version (" + version
275 + ") is not a valid format");
285 if (serviceName.equals(apiRouteInfo.getServiceName())
286 && version.equals(apiRouteInfo.getVersion())) {
288 deleteApiRoute(serviceName, version, RouteUtil.ROUTE_PATH_LOADBALANCE + "*",
292 // 如果已修改服务名或者版本号,先删除此服务全部已有信息
293 deleteApiRoute(serviceName, version, "*", serverPort);
297 saveApiRouteInstance(apiRouteInfo, serverPort);
300 } catch (ExtendedNotSupportedException e) {
302 } catch (Exception e) {
303 LOGGER.error("update ApiRoute throw exception", e);
304 throw new ExtendedInternalServerErrorException("update apiRouteInfo throw exception"
314 * @Title updateApiRouteStatus
315 * @Description TODO(更新单个服务状态)
320 * @return RouteResult
322 public synchronized ApiRouteInfo updateApiRouteStatus(String serviceName, String version,
325 if ("null".equals(version)) {
329 if (StringUtils.isBlank(serviceName)) {
330 throw new ExtendedNotSupportedException("serviceName can't be empty");
333 if (StringUtils.isNotBlank(version)) {
334 if (!RegExpTestUtil.versionRegExpTest(version)) {
335 throw new ExtendedNotSupportedException("version (" + version
336 + ") is not a valid format");
340 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, status)) {
341 throw new ExtendedNotSupportedException(
342 "save ApiRouteInfo Status FAIL:status is wrong,value range:("
343 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
346 ApiRouteInfo new_apiRouteInfo = getApiRouteInstance(serviceName, version);
350 String serviceInfokey =
351 RouteUtil.getPrefixedKey("", RouteUtil.APIROUTE, serviceName, version,
352 RouteUtil.ROUTE_PATH_INFO);
353 Map<String, String> serviceInfoMap = new HashMap<String, String>();
354 serviceInfoMap.put("status", status);
359 jedis = JedisUtil.borrowJedisInstance();
361 throw new Exception("fetch from jedis pool failed,null object!");
364 jedis.hmset(serviceInfokey, serviceInfoMap);
365 new_apiRouteInfo.setStatus(status);
368 } catch (Exception e) {
369 LOGGER.error("update ApiRoute status throw exception", e);
370 throw new ExtendedInternalServerErrorException("update ApiRoute status throw exception"
374 JedisUtil.returnJedisInstance(jedis);
377 return new_apiRouteInfo;
382 * @Title: saveApiRouteInstance
383 * @Description: TODO(存储单个服务信息)
384 * @param: @param apiRouteInfo
386 * @return: ApiRouteInfo
388 public synchronized ApiRouteInfo saveApiRouteInstance(ApiRouteInfo apiRouteInfo,
393 if (StringUtils.isBlank(apiRouteInfo.getServiceName())
394 || apiRouteInfo.getServers().length == 0) {
395 throw new ExtendedNotSupportedException(
396 "save apiRouteInfo FAIL: Some required fields are empty");
399 if (StringUtils.isNotBlank(apiRouteInfo.getVersion())) {
400 if (!RegExpTestUtil.versionRegExpTest(apiRouteInfo.getVersion())) {
401 throw new ExtendedNotSupportedException("version (" + apiRouteInfo.getVersion()
402 + ") is not a valid format");
406 if (StringUtils.isNotBlank(apiRouteInfo.getUrl())) {
407 if (!RegExpTestUtil.urlRegExpTest(apiRouteInfo.getUrl())) {
408 throw new ExtendedNotSupportedException(
409 "save apiRouteInfo FAIL:url is not a valid format(url must be begin with /)");
414 if (!RouteUtil.contain(RouteUtil.visualRangeRange, apiRouteInfo.getVisualRange())) {
415 throw new ExtendedNotSupportedException(
416 "save apiRouteInfo FAIL:VisualRange is wrong,value range:("
417 + RouteUtil.show(RouteUtil.visualRangeMatches) + ")");
420 if (!RouteUtil.contain(RouteUtil.controlRangeMatches, apiRouteInfo.getControl())) {
421 throw new ExtendedNotSupportedException(
422 "save apiRouteInfo FAIL:control is wrong,value range:("
423 + RouteUtil.show(RouteUtil.controlRangeMatches) + ")");
426 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, apiRouteInfo.getStatus())) {
427 throw new ExtendedNotSupportedException(
428 "save apiRouteInfo FAIL:status is wrong,value range:("
429 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
432 if (!RouteUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, apiRouteInfo.getUseOwnUpstream())) {
433 throw new ExtendedNotSupportedException(
434 "save apiRouteInfo FAIL:useOwnUpstream is wrong,value range:("
435 + RouteUtil.show(RouteUtil.useOwnUpstreamRangeMatches) + ")");
439 RouteServer[] serverList = apiRouteInfo.getServers();
440 for (int i = 0; i < serverList.length; i++) {
441 RouteServer server = serverList[i];
442 if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {
443 throw new ExtendedNotSupportedException("save apiRouteInfo FAIL:IP("
444 + server.getIp() + ")is not a valid ip address");
447 if (!RegExpTestUtil.portRegExpTest(server.getPort())) {
448 throw new ExtendedNotSupportedException("save apiRouteInfo FAIL:Port("
449 + server.getPort() + ")is not a valid Port address");
454 String serviceInfokey =
455 RouteUtil.getPrefixedKey(serverPort, RouteUtil.APIROUTE,
456 apiRouteInfo.getServiceName().trim(), apiRouteInfo.getVersion().trim(),
457 RouteUtil.ROUTE_PATH_INFO);
458 Map<String, String> serviceInfoMap = new HashMap<String, String>();
459 serviceInfoMap.put("url", "/".equals(apiRouteInfo.getUrl().trim()) ? "" : apiRouteInfo
461 serviceInfoMap.put("apijson", apiRouteInfo.getApiJson());
462 serviceInfoMap.put("apiJsonType", apiRouteInfo.getApiJsonType());
463 serviceInfoMap.put("metricsUrl", apiRouteInfo.getMetricsUrl());
464 serviceInfoMap.put("control", apiRouteInfo.getControl());
465 serviceInfoMap.put("status", apiRouteInfo.getStatus());
466 serviceInfoMap.put("visualRange", apiRouteInfo.getVisualRange());
467 serviceInfoMap.put("useOwnUpstream", apiRouteInfo.getUseOwnUpstream());
470 String serviceLBkey =
471 RouteUtil.getPrefixedKey(serverPort, RouteUtil.APIROUTE,
472 apiRouteInfo.getServiceName(), apiRouteInfo.getVersion(),
473 RouteUtil.ROUTE_PATH_LOADBALANCE);
478 jedis = JedisUtil.borrowJedisInstance();
480 throw new ExtendedInternalServerErrorException(
481 "fetch from jedis pool failed,null object!");
484 jedis.hmset(serviceInfokey, serviceInfoMap);
487 for (int i = 0; i < serverList.length; i++) {
488 Map<String, String> servermap = new HashMap<String, String>();
489 RouteServer server = serverList[i];
491 servermap.put("ip", server.getIp());
492 servermap.put("port", server.getPort());
493 servermap.put("weight", Integer.toString(server.getWeight()));
495 jedis.hmset(serviceLBkey + ":server" + (i + 1), servermap);
499 // ApiRouteLifeCycle lifeCycle = apiRouteInfo.getLifeCycle();
500 // if (lifeCycle != null) {
501 // String serviceLifekey =
502 // RouteUtil.getPrefixedKey(serverPort, RouteUtil.APIROUTE,
503 // apiRouteInfo.getServiceName(), apiRouteInfo.getVersion(),
504 // RouteUtil.APIROUTE_PATH_LIFE);
505 // Map<String, String> serviceLifeMap = new HashMap<String, String>();
506 // serviceLifeMap.put("path", lifeCycle.getInstallPath());
507 // serviceLifeMap.put("start", lifeCycle.getStartScript());
508 // serviceLifeMap.put("stop", lifeCycle.getStopScript());
509 // jedis.hmset(serviceLifekey, serviceLifeMap);
514 } catch (Exception e) {
515 LOGGER.error("call redis throw exception", e);
516 throw new ExtendedInternalServerErrorException("call redis throw exception:"
520 JedisUtil.returnJedisInstance(jedis);
529 * @Title: deleteApiRoute
530 * @Description: TODO(删除单个服务信息)
531 * @param: @param type
532 * @param: @param serviceName
533 * @param: @param version
534 * @param: @param delKey
538 public synchronized void deleteApiRoute(String serviceName, String version, String delKey,
541 if ("null".equals(version)) {
545 if (StringUtils.isBlank(serviceName)) {
546 throw new ExtendedNotSupportedException("serviceName can't be empty");
549 if (StringUtils.isNotBlank(version)) {
550 if (!RegExpTestUtil.versionRegExpTest(version)) {
551 throw new ExtendedNotSupportedException("version (" + version
552 + ") is not a valid format");
559 jedis = JedisUtil.borrowJedisInstance();
561 throw new ExtendedInternalServerErrorException(
562 "fetch from jedis pool failed,null object!");
567 RouteUtil.getPrefixedKey(serverPort, RouteUtil.APIROUTE, serviceName, version,
569 Set<String> infoSet = jedis.keys(routekey);
571 if (infoSet.isEmpty()) {
572 throw new ExtendedNotFoundException("delete ApiRoute FAIL:serviceName-"
573 + serviceName + ",version:" + version + " not fond ");
576 String[] paths = new String[infoSet.size()];
579 infoSet.toArray(paths);
584 } catch (ExtendedNotFoundException e) {
586 } catch (Exception e) {
587 LOGGER.error("delete ApiRoute throw exception", e);
588 throw new ExtendedInternalServerErrorException("delete ApiRoute throw exception:"
591 JedisUtil.returnJedisInstance(jedis);
598 * @Title: getAllApiDocs
599 * @Description: TODO(获取本地ext\initSwaggerJson目录的全部json文件目录)
603 public String[] getAllApiDocs() {
604 URL apiDocsPath = ApiRouteServiceWrapper.class.getResource("/ext/initSwaggerJson");
605 if (apiDocsPath != null) {
606 String path = apiDocsPath.getPath();
609 return readfile(path);
610 } catch (FileNotFoundException e) {
611 // TODO Auto-generated catch block
612 LOGGER.error("read ApiDocs Files throw FileNotFoundException", e);
613 throw new ExtendedInternalServerErrorException("read ApiDocs Files throw FileNotFoundException:"
615 } catch (IOException e) {
616 // TODO Auto-generated catch block
617 LOGGER.error("read ApiDocs Files throw IOexception", e);
618 throw new ExtendedInternalServerErrorException("read ApiDocs Files throw IOexception:"
631 public String[] readfile(String filepath) throws FileNotFoundException, IOException {
632 File file = new File(filepath);
633 if (file.isDirectory()) {
634 String[] filelist = file.list();
640 public String getApiGatewayPort() {
641 // return JedisUtil.serverIp+":"+JedisUtil.serverPort;
642 return System.getenv("APIGATEWAY_EXPOSE_PORT") == null ? String
643 .valueOf(JedisUtil.serverPort) : System.getenv("APIGATEWAY_EXPOSE_PORT");
647 public DiscoverInfo getServiceDiscoverInfo() {
648 return RouteUtil.discoverInfo;