2 * Copyright 2016 ZTE Corporation.
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.IuiRouteInfo;
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 IuiRouteServiceWrapper {
39 private static final Logger LOGGER = LoggerFactory.getLogger(IuiRouteServiceWrapper.class);
41 private static IuiRouteServiceWrapper instance = new IuiRouteServiceWrapper();
43 private IuiRouteServiceWrapper() {}
45 public static IuiRouteServiceWrapper getInstance() {
51 * @Title: getAllIuiRouteService
52 * @Description: TODO(获取全部内容服务列表)
54 * @return: IuiRouteInfo[]
56 public IuiRouteInfo[] getAllIuiRouteInstances() {
60 IuiRouteInfo[] iuiRouteList = null;
62 jedis = JedisUtil.borrowJedisInstance();
64 throw new ExtendedInternalServerErrorException(
65 "fetch from jedis pool failed,null object!");
71 .getPrefixedKey("", RouteUtil.IUIROUTE, "*", RouteUtil.ROUTE_PATH_INFO);
72 Set<String> routeSet = jedis.keys(routekey);
73 iuiRouteList = new IuiRouteInfo[routeSet.size()];
76 for (String routePath : routeSet) {
77 String[] routePathArray = routePath.split(":");
78 IuiRouteInfo iuiRoute = getIuiRouteInstance(routePathArray[3], jedis);
79 iuiRouteList[i] = iuiRoute;
84 } catch (Exception e) {
85 LOGGER.error("call redis throw exception", e);
86 throw new ExtendedInternalServerErrorException("call redis throw exception:"
89 JedisUtil.returnJedisInstance(jedis);
98 * @Title: getIuiRouteInstance
99 * @Description: TODO(通过服务名获取单个内容服务对象信息)
100 * @param: @param serviceName
102 * @return: IuiRouteInfo
104 public IuiRouteInfo getIuiRouteInstance(String serviceName) {
106 if (StringUtils.isBlank(serviceName)) {
107 throw new ExtendedNotSupportedException("serviceName can't be empty");
110 IuiRouteInfo iuiRouteInfo = null;
114 jedis = JedisUtil.borrowJedisInstance();
116 throw new ExtendedInternalServerErrorException(
117 "fetch from jedis pool failed,null object!");
120 iuiRouteInfo = getIuiRouteInstance(serviceName, jedis);
123 } catch (Exception e) {
124 LOGGER.error("call redis throw exception", e);
125 throw new ExtendedInternalServerErrorException("call redis throw exception:"
128 JedisUtil.returnJedisInstance(jedis);
131 if (null == iuiRouteInfo) {
132 String errInfo = "iuiRouteInfo not found: serviceName-" + serviceName;
133 LOGGER.warn(errInfo);
134 throw new ExtendedNotFoundException(errInfo);
142 public IuiRouteInfo getIuiRouteInstance(String serviceName, Jedis jedis) throws Exception {
145 IuiRouteInfo iuiRouteInfo = null;
150 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName,
151 RouteUtil.ROUTE_PATH_INFO);
152 Map<String, String> infomap = jedis.hgetAll(routekey);
153 if (!infomap.isEmpty()) {
154 iuiRouteInfo = new IuiRouteInfo();
155 iuiRouteInfo.setServiceName(serviceName);
156 iuiRouteInfo.setUrl(infomap.get("url"));
157 iuiRouteInfo.setControl(infomap.get("control"));
158 iuiRouteInfo.setStatus(infomap.get("status"));
159 iuiRouteInfo.setVisualRange(infomap.get("visualRange"));
160 iuiRouteInfo.setUseOwnUpstream(infomap.get("useOwnUpstream"));
164 String serviceLBkey =
165 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName,
166 RouteUtil.ROUTE_PATH_LOADBALANCE);
167 Set<String> serviceLBset = jedis.keys(serviceLBkey + ":*");
168 int serverNum = serviceLBset.size();
169 RouteServer[] iuiRouteServerList = new RouteServer[serverNum];
171 for (String serviceInfo : serviceLBset) {
172 Map<String, String> serviceLBmap = jedis.hgetAll(serviceInfo);
173 RouteServer server = new RouteServer();
174 server.setIp(serviceLBmap.get("ip"));
175 server.setPort(serviceLBmap.get("port"));
176 server.setWeight(Integer.parseInt(serviceLBmap.get("weight")));
177 iuiRouteServerList[i] = server;
181 iuiRouteInfo.setServers(iuiRouteServerList);
189 * @Title: updateIuiRouteInstance
190 * @Description: TODO(更新单个服务信息)
191 * @param: @param serviceName
192 * @param: @param IuiRouteInfo
194 * @return: IuiRouteInfo
196 public synchronized IuiRouteInfo updateIuiRouteInstance(String serviceName,
197 IuiRouteInfo iuiRouteInfo) {
199 if (StringUtils.isBlank(serviceName)) {
200 throw new ExtendedNotSupportedException("serviceName can't be empty");
204 if (serviceName.equals(iuiRouteInfo.getServiceName())) {
206 deleteIuiRoute(serviceName, RouteUtil.ROUTE_PATH_LOADBALANCE + "*");
209 // 如果已修改服务名,先删除此服务全部已有信息
210 deleteIuiRoute(serviceName, "*");
212 saveIuiRouteInstance(iuiRouteInfo);
214 } catch (ExtendedNotSupportedException e) {
216 } catch (Exception e) {
217 LOGGER.error("updateIuiRoute throw exception", e);
218 throw new ExtendedInternalServerErrorException("update IuiRouteInfo throw exception"
227 * @Title updateIuiRouteStatus
228 * @Description TODO(更新单个服务状态)
232 * @return RouteResult
234 public synchronized IuiRouteInfo updateIuiRouteStatus(String serviceName, String status) {
237 if (StringUtils.isBlank(serviceName)) {
238 throw new ExtendedNotSupportedException("serviceName can't be empty");
241 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, status)) {
242 throw new ExtendedNotSupportedException(
243 "save IuiRouteInfo Status FAIL:status is wrong,value range:("
244 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
247 IuiRouteInfo new_iuiRouteInfo = getIuiRouteInstance(serviceName);
250 String serviceInfokey =
251 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName,
252 RouteUtil.ROUTE_PATH_INFO);
253 Map<String, String> serviceInfoMap = new HashMap<String, String>();
254 serviceInfoMap.put("status", status);
259 jedis = JedisUtil.borrowJedisInstance();
261 throw new ExtendedInternalServerErrorException(
262 "fetch from jedis pool failed,null object!");
265 jedis.hmset(serviceInfokey, serviceInfoMap);
266 new_iuiRouteInfo.setStatus(status);
268 } catch (Exception e) {
269 LOGGER.error("update IuiRoute status throw exception", e);
270 throw new ExtendedInternalServerErrorException(
271 "update IuiRouteInfo status throw exception" + e.getMessage());
274 JedisUtil.returnJedisInstance(jedis);
277 return new_iuiRouteInfo;
281 * @Title: saveIuiRouteInstance
282 * @Description: TODO(存储单个服务信息)
283 * @param: @param IuiRouteInfo
285 * @return: IuiRouteInfo
287 public synchronized IuiRouteInfo saveIuiRouteInstance(IuiRouteInfo iuiRouteInfo) {
289 if (StringUtils.isBlank(iuiRouteInfo.getServiceName())
290 || iuiRouteInfo.getServers().length == 0) {
291 throw new ExtendedNotSupportedException(
292 "save iuiRouteInfo FAIL: Some required fields are empty");
295 if (StringUtils.isNotBlank(iuiRouteInfo.getUrl())){
296 if (!RegExpTestUtil.urlRegExpTest(iuiRouteInfo.getUrl())) {
297 throw new ExtendedNotSupportedException(
298 "save iuiRouteInfo FAIL:url is not a valid format(url must be begin with /)");
303 if (!RouteUtil.contain(RouteUtil.visualRangeRange, iuiRouteInfo.getVisualRange())) {
304 throw new ExtendedNotSupportedException(
305 "save iuiRouteInfo FAIL:VisualRange is wrong,value range:("
306 + RouteUtil.show(RouteUtil.visualRangeMatches) + ")");
309 if (!RouteUtil.contain(RouteUtil.controlRangeMatches, iuiRouteInfo.getControl())) {
310 throw new ExtendedNotSupportedException(
311 "save iuiRouteInfo FAIL:control is wrong,value range:("
312 + RouteUtil.show(RouteUtil.controlRangeMatches) + ")");
315 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, iuiRouteInfo.getStatus())) {
316 throw new ExtendedNotSupportedException(
317 "save iuiRouteInfo FAIL:status is wrong,value range:("
318 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
321 if (!RouteUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, iuiRouteInfo.getUseOwnUpstream())) {
322 throw new ExtendedNotSupportedException(
323 "save apiRouteInfo FAIL:useOwnUpstream is wrong,value range:("
324 + RouteUtil.show(RouteUtil.useOwnUpstreamRangeMatches) + ")");
328 RouteServer[] serverList = iuiRouteInfo.getServers();
329 for (int i = 0; i < serverList.length; i++) {
330 RouteServer server = serverList[i];
331 if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {
332 throw new ExtendedNotSupportedException("save iuiRouteInfo FAIL:IP("
333 + server.getIp() + ")is not a valid ip address");
336 if (!RegExpTestUtil.portRegExpTest(server.getPort())) {
337 throw new ExtendedNotSupportedException("save iuiRouteInfo FAIL:Port("
338 + server.getPort() + ")is not a valid Port address");
344 String serviceInfokey =
345 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, iuiRouteInfo.getServiceName().trim(),
346 RouteUtil.ROUTE_PATH_INFO);
347 Map<String, String> serviceInfoMap = new HashMap<String, String>();
348 serviceInfoMap.put("url", "/".equals(iuiRouteInfo.getUrl().trim()) ? "" : iuiRouteInfo
350 serviceInfoMap.put("control", iuiRouteInfo.getControl());
351 serviceInfoMap.put("status", iuiRouteInfo.getStatus());
352 serviceInfoMap.put("visualRange", iuiRouteInfo.getVisualRange());
353 serviceInfoMap.put("useOwnUpstream", iuiRouteInfo.getUseOwnUpstream());
357 String serviceLBkey =
358 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, iuiRouteInfo.getServiceName(),
359 RouteUtil.ROUTE_PATH_LOADBALANCE);
364 jedis = JedisUtil.borrowJedisInstance();
366 throw new ExtendedInternalServerErrorException(
367 "fetch from jedis pool failed,null object!");
370 jedis.hmset(serviceInfokey, serviceInfoMap);
373 for (int i = 0; i < serverList.length; i++) {
374 Map<String, String> servermap = new HashMap<String, String>();
375 RouteServer server = serverList[i];
377 servermap.put("ip", server.getIp());
378 servermap.put("port", server.getPort());
379 servermap.put("weight", Integer.toString(server.getWeight()));
381 jedis.hmset(serviceLBkey + ":server" + (i + 1), servermap);
385 } catch (Exception e) {
386 LOGGER.error("call redis throw exception", e);
387 throw new ExtendedInternalServerErrorException("call redis throw exception:"
390 JedisUtil.returnJedisInstance(jedis);;
399 * @Title: deleteIuiRoute
400 * @Description: TODO(删除单个服务信息)
401 * @param: @param type
402 * @param: @param serviceName
403 * @param: @param delKey
407 public synchronized void deleteIuiRoute(String serviceName, String delKey) {
409 if (StringUtils.isBlank(serviceName)) {
410 throw new ExtendedNotSupportedException("serviceName can't be empty");
415 jedis = JedisUtil.borrowJedisInstance();
417 throw new ExtendedInternalServerErrorException(
418 "fetch from jedis pool failed,null object!");
422 String routekey = RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName, delKey);
423 Set<String> infoSet = jedis.keys(routekey);
425 if (infoSet.isEmpty()) {
426 throw new ExtendedNotFoundException("delete IuiRoute FAIL:serviceName-"
427 + serviceName + " not fond ");
430 String[] paths = new String[infoSet.size()];
433 infoSet.toArray(paths);
438 } catch (ExtendedNotFoundException e) {
440 } catch (Exception e) {
441 LOGGER.error("delete IuiRoute throw exception", e);
442 throw new ExtendedInternalServerErrorException("delete IuiRoute throw exception:"
445 JedisUtil.returnJedisInstance(jedis);