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() {
49 public IuiRouteInfo[] getAllIuiRouteInstances() {
53 IuiRouteInfo[] iuiRouteList = null;
55 jedis = JedisUtil.borrowJedisInstance();
57 throw new ExtendedInternalServerErrorException(
58 "fetch from jedis pool failed,null object!");
63 .getPrefixedKey("", RouteUtil.IUIROUTE, "*", RouteUtil.ROUTE_PATH_INFO);
64 Set<String> routeSet = jedis.keys(routekey);
65 iuiRouteList = new IuiRouteInfo[routeSet.size()];
68 for (String routePath : routeSet) {
69 String[] routePathArray = routePath.split(":");
70 IuiRouteInfo iuiRoute = getIuiRouteInstance(routePathArray[3], jedis);
71 iuiRouteList[i] = iuiRoute;
76 } catch (Exception e) {
77 LOGGER.error("call redis throw exception", e);
78 throw new ExtendedInternalServerErrorException("call redis throw exception:"
81 JedisUtil.returnJedisInstance(jedis);
88 public IuiRouteInfo getIuiRouteInstance(String serviceName) {
90 if (StringUtils.isBlank(serviceName)) {
91 throw new ExtendedNotSupportedException("serviceName can't be empty");
94 IuiRouteInfo iuiRouteInfo = null;
98 jedis = JedisUtil.borrowJedisInstance();
100 throw new ExtendedInternalServerErrorException(
101 "fetch from jedis pool failed,null object!");
104 iuiRouteInfo = getIuiRouteInstance(serviceName, jedis);
107 } catch (Exception e) {
108 LOGGER.error("call redis throw exception", e);
109 throw new ExtendedInternalServerErrorException("call redis throw exception:"
112 JedisUtil.returnJedisInstance(jedis);
115 if (null == iuiRouteInfo) {
116 String errInfo = "iuiRouteInfo not found: serviceName-" + serviceName;
117 LOGGER.warn(errInfo);
118 throw new ExtendedNotFoundException(errInfo);
126 public IuiRouteInfo getIuiRouteInstance(String serviceName, Jedis jedis) throws Exception {
129 IuiRouteInfo iuiRouteInfo = null;
133 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName,
134 RouteUtil.ROUTE_PATH_INFO);
135 Map<String, String> infomap = jedis.hgetAll(routekey);
136 if (!infomap.isEmpty()) {
137 iuiRouteInfo = new IuiRouteInfo();
138 iuiRouteInfo.setServiceName(serviceName);
139 iuiRouteInfo.setUrl(infomap.get("url"));
140 iuiRouteInfo.setControl(infomap.get("control"));
141 iuiRouteInfo.setStatus(infomap.get("status"));
142 iuiRouteInfo.setVisualRange(infomap.get("visualRange"));
143 iuiRouteInfo.setUseOwnUpstream(infomap.get("useOwnUpstream"));
146 String serviceLBkey =
147 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName,
148 RouteUtil.ROUTE_PATH_LOADBALANCE);
149 Set<String> serviceLBset = jedis.keys(serviceLBkey + ":*");
150 int serverNum = serviceLBset.size();
151 RouteServer[] iuiRouteServerList = new RouteServer[serverNum];
153 for (String serviceInfo : serviceLBset) {
154 Map<String, String> serviceLBmap = jedis.hgetAll(serviceInfo);
155 RouteServer server = new RouteServer();
156 server.setIp(serviceLBmap.get("ip"));
157 server.setPort(serviceLBmap.get("port"));
158 server.setWeight(Integer.parseInt(serviceLBmap.get("weight")));
159 iuiRouteServerList[i] = server;
163 iuiRouteInfo.setServers(iuiRouteServerList);
170 public synchronized IuiRouteInfo updateIuiRouteInstance(String serviceName,
171 IuiRouteInfo iuiRouteInfo) {
173 if (StringUtils.isBlank(serviceName)) {
174 throw new ExtendedNotSupportedException("serviceName can't be empty");
178 if (serviceName.equals(iuiRouteInfo.getServiceName())) {
179 deleteIuiRoute(serviceName, RouteUtil.ROUTE_PATH_LOADBALANCE + "*");
182 deleteIuiRoute(serviceName, "*");
184 saveIuiRouteInstance(iuiRouteInfo);
186 } catch (ExtendedNotSupportedException e) {
188 } catch (Exception e) {
189 LOGGER.error("updateIuiRoute throw exception", e);
190 throw new ExtendedInternalServerErrorException("update IuiRouteInfo throw exception"
198 public synchronized IuiRouteInfo updateIuiRouteStatus(String serviceName, String status) {
201 if (StringUtils.isBlank(serviceName)) {
202 throw new ExtendedNotSupportedException("serviceName can't be empty");
205 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, status)) {
206 throw new ExtendedNotSupportedException(
207 "save IuiRouteInfo Status FAIL:status is wrong,value range:("
208 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
211 IuiRouteInfo new_iuiRouteInfo = getIuiRouteInstance(serviceName);
213 String serviceInfokey =
214 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName,
215 RouteUtil.ROUTE_PATH_INFO);
216 Map<String, String> serviceInfoMap = new HashMap<String, String>();
217 serviceInfoMap.put("status", status);
222 jedis = JedisUtil.borrowJedisInstance();
224 throw new ExtendedInternalServerErrorException(
225 "fetch from jedis pool failed,null object!");
227 jedis.hmset(serviceInfokey, serviceInfoMap);
228 new_iuiRouteInfo.setStatus(status);
230 } catch (Exception e) {
231 LOGGER.error("update IuiRoute status throw exception", e);
232 throw new ExtendedInternalServerErrorException(
233 "update IuiRouteInfo status throw exception" + e.getMessage());
236 JedisUtil.returnJedisInstance(jedis);
239 return new_iuiRouteInfo;
242 public synchronized IuiRouteInfo saveIuiRouteInstance(IuiRouteInfo iuiRouteInfo) {
244 if (StringUtils.isBlank(iuiRouteInfo.getServiceName())
245 || iuiRouteInfo.getServers().length == 0) {
246 throw new ExtendedNotSupportedException(
247 "save iuiRouteInfo FAIL: Some required fields are empty");
250 if (StringUtils.isNotBlank(iuiRouteInfo.getUrl())){
251 if (!RegExpTestUtil.urlRegExpTest(iuiRouteInfo.getUrl())) {
252 throw new ExtendedNotSupportedException(
253 "save iuiRouteInfo FAIL:url is not a valid format(url must be begin with /)");
258 if (!RouteUtil.contain(RouteUtil.visualRangeRange, iuiRouteInfo.getVisualRange())) {
259 throw new ExtendedNotSupportedException(
260 "save iuiRouteInfo FAIL:VisualRange is wrong,value range:("
261 + RouteUtil.show(RouteUtil.visualRangeMatches) + ")");
264 if (!RouteUtil.contain(RouteUtil.controlRangeMatches, iuiRouteInfo.getControl())) {
265 throw new ExtendedNotSupportedException(
266 "save iuiRouteInfo FAIL:control is wrong,value range:("
267 + RouteUtil.show(RouteUtil.controlRangeMatches) + ")");
270 if (!RouteUtil.contain(RouteUtil.statusRangeMatches, iuiRouteInfo.getStatus())) {
271 throw new ExtendedNotSupportedException(
272 "save iuiRouteInfo FAIL:status is wrong,value range:("
273 + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");
276 if (!RouteUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, iuiRouteInfo.getUseOwnUpstream())) {
277 throw new ExtendedNotSupportedException(
278 "save apiRouteInfo FAIL:useOwnUpstream is wrong,value range:("
279 + RouteUtil.show(RouteUtil.useOwnUpstreamRangeMatches) + ")");
282 RouteServer[] serverList = iuiRouteInfo.getServers();
283 for (int i = 0; i < serverList.length; i++) {
284 RouteServer server = serverList[i];
285 if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {
286 throw new ExtendedNotSupportedException("save iuiRouteInfo FAIL:IP("
287 + server.getIp() + ")is not a valid ip address");
290 if (!RegExpTestUtil.portRegExpTest(server.getPort())) {
291 throw new ExtendedNotSupportedException("save iuiRouteInfo FAIL:Port("
292 + server.getPort() + ")is not a valid Port address");
297 String serviceInfokey =
298 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, iuiRouteInfo.getServiceName().trim(),
299 RouteUtil.ROUTE_PATH_INFO);
300 Map<String, String> serviceInfoMap = new HashMap<String, String>();
301 serviceInfoMap.put("url", "/".equals(iuiRouteInfo.getUrl().trim()) ? "" : iuiRouteInfo
303 serviceInfoMap.put("control", iuiRouteInfo.getControl());
304 serviceInfoMap.put("status", iuiRouteInfo.getStatus());
305 serviceInfoMap.put("visualRange", iuiRouteInfo.getVisualRange());
306 serviceInfoMap.put("useOwnUpstream", iuiRouteInfo.getUseOwnUpstream());
309 String serviceLBkey =
310 RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, iuiRouteInfo.getServiceName(),
311 RouteUtil.ROUTE_PATH_LOADBALANCE);
316 jedis = JedisUtil.borrowJedisInstance();
318 throw new ExtendedInternalServerErrorException(
319 "fetch from jedis pool failed,null object!");
321 jedis.hmset(serviceInfokey, serviceInfoMap);
323 for (int i = 0; i < serverList.length; i++) {
324 Map<String, String> servermap = new HashMap<String, String>();
325 RouteServer server = serverList[i];
327 servermap.put("ip", server.getIp());
328 servermap.put("port", server.getPort());
329 servermap.put("weight", Integer.toString(server.getWeight()));
331 jedis.hmset(serviceLBkey + ":server" + (i + 1), servermap);
335 } catch (Exception e) {
336 LOGGER.error("call redis throw exception", e);
337 throw new ExtendedInternalServerErrorException("call redis throw exception:"
340 JedisUtil.returnJedisInstance(jedis);;
348 public synchronized void deleteIuiRoute(String serviceName, String delKey) {
350 if (StringUtils.isBlank(serviceName)) {
351 throw new ExtendedNotSupportedException("serviceName can't be empty");
356 jedis = JedisUtil.borrowJedisInstance();
358 throw new ExtendedInternalServerErrorException(
359 "fetch from jedis pool failed,null object!");
362 String routekey = RouteUtil.getPrefixedKey("", RouteUtil.IUIROUTE, serviceName, delKey);
363 Set<String> infoSet = jedis.keys(routekey);
365 if (infoSet.isEmpty()) {
366 LOGGER.warn("delete IuiRoute FAIL:serviceName-"
367 + serviceName + " not fond ");
371 String[] paths = new String[infoSet.size()];
372 infoSet.toArray(paths);
377 } catch (ExtendedNotFoundException e) {
379 } catch (Exception e) {
380 LOGGER.error("delete IuiRoute throw exception", e);
381 throw new ExtendedInternalServerErrorException("delete IuiRoute throw exception:"
384 JedisUtil.returnJedisInstance(jedis);