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.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 public CustomRouteInfo[] getAllCustomRouteInstances() {
55 CustomRouteInfo[] customRouteList = null;
57 jedis = JedisUtil.borrowJedisInstance();
59 throw new ExtendedInternalServerErrorException(
60 "fetch from jedis pool failed,null object!");
64 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, "*",
65 RouteUtil.ROUTE_PATH_INFO);
66 Set<String> routeSet = jedis.keys(routekey);
67 customRouteList = new CustomRouteInfo[routeSet.size()];
70 for (String routePath : routeSet) {
71 String[] routePathArray = routePath.split(":");
72 CustomRouteInfo customRoute = getCustomRouteInstance(routePathArray[3], jedis);
73 customRouteList[i] = customRoute;
78 } catch (Exception e) {
79 LOGGER.error("call redis throw exception", e);
80 throw new ExtendedInternalServerErrorException("call redis throw exception:"
84 JedisUtil.returnJedisInstance(jedis);
87 return customRouteList;
92 public CustomRouteInfo getCustomRouteInstance(String serviceName) {
94 if (StringUtils.isBlank(serviceName)) {
95 throw new ExtendedNotSupportedException("serviceName can't be empty");
98 CustomRouteInfo customRouteInfo;
102 jedis = JedisUtil.borrowJedisInstance();
104 throw new ExtendedInternalServerErrorException(
105 "fetch from jedis pool failed,null object!");
108 customRouteInfo = getCustomRouteInstance(serviceName, jedis);
111 } catch (Exception e) {
112 LOGGER.error("call redis throw exception", e);
113 throw new ExtendedInternalServerErrorException("call redis throw exception:"
116 JedisUtil.returnJedisInstance(jedis);
119 if (null == customRouteInfo) {
120 String errInfo = "customRouteInfo not found: serviceName-" + serviceName;
121 LOGGER.warn(errInfo);
122 throw new ExtendedNotFoundException(errInfo);
126 return customRouteInfo;
130 public CustomRouteInfo getCustomRouteInstance(String serviceName, Jedis jedis) throws Exception {
133 CustomRouteInfo customRouteInfo = null;
137 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,
138 RouteUtil.ROUTE_PATH_INFO);
139 Map<String, String> infomap = jedis.hgetAll(routekey);
140 if (!infomap.isEmpty()) {
141 customRouteInfo = new CustomRouteInfo();
142 customRouteInfo.setServiceName(serviceName);
143 customRouteInfo.setUrl(infomap.get("url"));
144 customRouteInfo.setControl(infomap.get("control"));
145 customRouteInfo.setStatus(infomap.get("status"));
146 customRouteInfo.setVisualRange(infomap.get("visualRange"));
147 customRouteInfo.setUseOwnUpstream(infomap.get("useOwnUpstream"));
150 String serviceLBkey =
151 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,
152 RouteUtil.ROUTE_PATH_LOADBALANCE);
153 Set<String> serviceLBset = jedis.keys(serviceLBkey + ":*");
154 int serverNum = serviceLBset.size();
155 RouteServer[] CustomRouteServerList = new RouteServer[serverNum];
157 for (String serviceInfo : serviceLBset) {
158 Map<String, String> serviceLBmap = jedis.hgetAll(serviceInfo);
159 RouteServer server = new RouteServer();
160 server.setIp(serviceLBmap.get("ip"));
161 server.setPort(serviceLBmap.get("port"));
162 server.setWeight(Integer.parseInt(serviceLBmap.get("weight")));
163 CustomRouteServerList[i] = server;
167 customRouteInfo.setServers(CustomRouteServerList);
171 return customRouteInfo;
174 public synchronized CustomRouteInfo updateCustomRouteInstance(String serviceName,
175 CustomRouteInfo customRouteInfo, String serverPort) {
176 if (StringUtils.isBlank(serviceName)) {
177 throw new ExtendedNotSupportedException("serviceName can't be empty");
182 if (serviceName.equals(customRouteInfo.getServiceName())) {
183 deleteCustomRoute(serviceName, RouteUtil.ROUTE_PATH_LOADBALANCE + "*", serverPort);
185 deleteCustomRoute(serviceName, "*", serverPort);
189 saveCustomRouteInstance(customRouteInfo, serverPort);
193 } catch (ExtendedNotSupportedException e) {
195 } catch (Exception e) {
196 LOGGER.error("updateCustomRoute throw exception", e);
197 throw new ExtendedInternalServerErrorException("update CustomRoute throw exception"
202 return customRouteInfo;
206 public synchronized CustomRouteInfo updateCustomRouteStatus(String serviceName, String status) {
208 if (StringUtils.isBlank(serviceName)) {
209 throw new ExtendedNotSupportedException("serviceName can't be empty");
212 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, status)) {
213 throw new ExtendedNotSupportedException(
214 "save CustomRouteInfo Status FAIL:status is wrong,value range:("
215 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
218 CustomRouteInfo new_customRouteInfo = getCustomRouteInstance(serviceName);
222 String serviceInfokey =
223 RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,
224 RouteUtil.ROUTE_PATH_INFO);
225 Map<String, String> serviceInfoMap = new HashMap<String, String>();
226 serviceInfoMap.put("status", status);
231 jedis = JedisUtil.borrowJedisInstance();
233 throw new ExtendedInternalServerErrorException(
234 "fetch from jedis pool failed,null object!");
236 jedis.hmset(serviceInfokey, serviceInfoMap);
237 new_customRouteInfo.setStatus(status);
239 } catch (Exception e) {
241 LOGGER.error("update CustomRoute status throw exception", e);
242 throw new ExtendedInternalServerErrorException(
243 "update CustomRoute status throw exception" + e.getMessage());
246 JedisUtil.returnJedisInstance(jedis);
249 return new_customRouteInfo;
252 public synchronized CustomRouteInfo saveCustomRouteInstance(CustomRouteInfo customRouteInfo,
255 if (StringUtils.isBlank(customRouteInfo.getServiceName())
256 || customRouteInfo.getServers().length == 0) {
257 throw new ExtendedNotSupportedException(
258 "save CustomRouteInfo FAIL: Some required fields are empty");
262 if (!RegExpTestUtil.urlRegExpTest(customRouteInfo.getServiceName())) {
263 throw new ExtendedNotSupportedException(
264 "save CustomRouteInfo FAIL: ServiceName is not a valid format(ServiceName must be begin with /)");
268 if (StringUtils.isNotBlank(customRouteInfo.getUrl())){
269 if (!RegExpTestUtil.urlRegExpTest(customRouteInfo.getUrl())) {
270 throw new ExtendedNotSupportedException(
271 "save CustomRouteInfo FAIL:url is not a valid format(url must be begin with /)");
276 if (!RouteUtil.contain(RouteUtil.visualRangeRange, customRouteInfo.getVisualRange())) {
277 throw new ExtendedNotSupportedException(
278 "save CustomRouteInfo FAIL:VisualRange is wrong,value range:("
279 + RouteUtil.show(RouteUtil.visualRangeMatches) + ")");
282 if (!RouteUtil.contain(RouteUtil.controlRangeMatches, customRouteInfo.getControl())) {
283 throw new ExtendedNotSupportedException(
284 "save CustomRouteInfo FAIL:control is wrong,value range:("
285 + RouteUtil.show(RouteUtil.controlRangeMatches) + ")");
288 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, customRouteInfo.getStatus())) {
289 throw new ExtendedNotSupportedException(
290 "save CustomRouteInfo FAIL:status is wrong,value range:("
291 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
294 if (!RouteUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, customRouteInfo.getUseOwnUpstream())) {
295 throw new ExtendedNotSupportedException(
296 "save apiRouteInfo FAIL:useOwnUpstream is wrong,value range:("
297 + RouteUtil.show(RouteUtil.useOwnUpstreamRangeMatches) + ")");
300 RouteServer[] serverList = customRouteInfo.getServers();
301 for (int i = 0; i < serverList.length; i++) {
302 RouteServer server = serverList[i];
303 if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {
304 throw new ExtendedNotSupportedException("save CustomRouteInfo FAIL:IP("
305 + server.getIp() + ")is not a valid ip address");
308 if (!RegExpTestUtil.portRegExpTest(server.getPort())) {
309 throw new ExtendedNotSupportedException("save CustomRouteInfo FAIL:Port("
310 + server.getPort() + ")is not a valid Port address");
315 String serviceInfokey =
316 RouteUtil.getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE,
317 customRouteInfo.getServiceName().trim(), RouteUtil.ROUTE_PATH_INFO);
318 Map<String, String> serviceInfoMap = new HashMap<String, String>();
319 serviceInfoMap.put("url", "/".equals(customRouteInfo.getUrl().trim())
321 : customRouteInfo.getUrl().trim());
322 serviceInfoMap.put("control", customRouteInfo.getControl());
323 serviceInfoMap.put("status", customRouteInfo.getStatus());
324 serviceInfoMap.put("visualRange", customRouteInfo.getVisualRange());
325 serviceInfoMap.put("useOwnUpstream", customRouteInfo.getUseOwnUpstream());
329 String serviceLBkey =
330 RouteUtil.getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE,
331 customRouteInfo.getServiceName(), RouteUtil.ROUTE_PATH_LOADBALANCE);
336 jedis = JedisUtil.borrowJedisInstance();
338 throw new ExtendedInternalServerErrorException(
339 "fetch from jedis pool failed,null object!");
341 jedis.hmset(serviceInfokey, serviceInfoMap);
344 for (int i = 0; i < serverList.length; i++) {
345 Map<String, String> servermap = new HashMap<String, String>();
346 RouteServer server = serverList[i];
348 servermap.put("ip", server.getIp());
349 servermap.put("port", server.getPort());
350 servermap.put("weight", Integer.toString(server.getWeight()));
352 jedis.hmset(serviceLBkey + ":server" + (i + 1), servermap);
356 } catch (Exception e) {
357 LOGGER.error("call redis throw exception", e);
358 throw new ExtendedInternalServerErrorException("call redis throw exception:"
362 JedisUtil.returnJedisInstance(jedis);;
365 return customRouteInfo;
370 public synchronized void deleteCustomRoute(String serviceName, String delKey, String serverPort) {
372 if (StringUtils.isBlank(serviceName)) {
373 throw new ExtendedNotSupportedException("serviceName can't be empty");
379 jedis = JedisUtil.borrowJedisInstance();
381 throw new ExtendedInternalServerErrorException(
382 "fetch from jedis pool failed,null object!");
387 .getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE, serviceName, delKey);
388 Set<String> infoSet = jedis.keys(routekey);
390 if (infoSet.isEmpty()) {
391 LOGGER.warn("delete CustomRoute FAIL:serviceName-"
392 + serviceName + " not fond ");
396 String[] paths = new String[infoSet.size()];
397 infoSet.toArray(paths);
401 } catch (ExtendedNotFoundException e) {
403 } catch (Exception e) {
405 LOGGER.error("delete CustomRoute throw exception", e);
406 throw new ExtendedInternalServerErrorException("delete CustomRoute throw exception:"
410 JedisUtil.returnJedisInstance(jedis);