1. Adjust the directory hierarchy
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / CustomRouteServiceWrapper.java
diff --git a/apiroute/apiroute-service/src/main/java/org/openo/msb/wrapper/CustomRouteServiceWrapper.java b/apiroute/apiroute-service/src/main/java/org/openo/msb/wrapper/CustomRouteServiceWrapper.java
deleted file mode 100644 (file)
index 85d4d4e..0000000
+++ /dev/null
@@ -1,474 +0,0 @@
-/**\r
-* Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
-*\r
-* Licensed under the Apache License, Version 2.0 (the "License");\r
-* you may not use this file except in compliance with the License.\r
-* You may obtain a copy of the License at\r
-*\r
-* http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*/\r
-package org.openo.msb.wrapper;\r
-\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.apache.commons.lang3.StringUtils;\r
-import org.openo.msb.api.CustomRouteInfo;\r
-import org.openo.msb.api.RouteServer;\r
-import org.openo.msb.api.exception.ExtendedInternalServerErrorException;\r
-import org.openo.msb.api.exception.ExtendedNotFoundException;\r
-import org.openo.msb.api.exception.ExtendedNotSupportedException;\r
-import org.openo.msb.wrapper.util.JedisUtil;\r
-import org.openo.msb.wrapper.util.RegExpTestUtil;\r
-import org.openo.msb.wrapper.util.RouteUtil;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-import redis.clients.jedis.Jedis;\r
-\r
-public class CustomRouteServiceWrapper {\r
-\r
-\r
-    private static final Logger LOGGER = LoggerFactory.getLogger(CustomRouteServiceWrapper.class);\r
-\r
-    private static CustomRouteServiceWrapper instance = new CustomRouteServiceWrapper();\r
-\r
-    private CustomRouteServiceWrapper() {}\r
-\r
-    public static CustomRouteServiceWrapper getInstance() {\r
-        return instance;\r
-    }\r
-\r
-\r
-    /**\r
-     * @Title: getAllCustomRouteService\r
-     * @Description: TODO(获取全部内容服务列表)\r
-     * @param: @return\r
-     * @return: CustomRouteInfo[]\r
-     */\r
-    public CustomRouteInfo[] getAllCustomRouteInstances() {\r
-\r
-\r
-        Jedis jedis = null;\r
-        CustomRouteInfo[] customRouteList = null;\r
-        try {\r
-            jedis = JedisUtil.borrowJedisInstance();\r
-            if (jedis == null) {\r
-                throw new ExtendedInternalServerErrorException(\r
-                        "fetch from jedis pool failed,null object!");\r
-            }\r
-\r
-            // 获取全部服务列表\r
-            String routekey =\r
-                    RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, "*",\r
-                            RouteUtil.ROUTE_PATH_INFO);\r
-            Set<String> routeSet = jedis.keys(routekey);\r
-            customRouteList = new CustomRouteInfo[routeSet.size()];\r
-\r
-            int i = 0;\r
-            for (String routePath : routeSet) {\r
-                String[] routePathArray = routePath.split(":");\r
-                CustomRouteInfo customRoute = getCustomRouteInstance(routePathArray[3], jedis);\r
-                customRouteList[i] = customRoute;\r
-                i++;\r
-            }\r
-\r
-\r
-        } catch (Exception e) {\r
-            LOGGER.error("call redis throw exception", e);\r
-            throw new ExtendedInternalServerErrorException("call redis throw exception:"\r
-                    + e.getMessage());\r
-\r
-        } finally {\r
-            JedisUtil.returnJedisInstance(jedis);\r
-        }\r
-\r
-        return customRouteList;\r
-    }\r
-\r
-\r
-\r
-    /**\r
-     * @Title: getCustomRouteInstance\r
-     * @Description: TODO(通过服务名获取单个内容服务对象信息)\r
-     * @param: @param serviceName\r
-     * @param: @return\r
-     * @return: CustomRouteInfo\r
-     */\r
-    public CustomRouteInfo getCustomRouteInstance(String serviceName) {\r
-\r
-        if (StringUtils.isBlank(serviceName)) {\r
-            throw new ExtendedNotSupportedException("serviceName  can't be empty");\r
-        }\r
-\r
-        CustomRouteInfo customRouteInfo;\r
-\r
-        Jedis jedis = null;\r
-        try {\r
-            jedis = JedisUtil.borrowJedisInstance();\r
-            if (jedis == null) {\r
-                throw new ExtendedInternalServerErrorException(\r
-                        "fetch from jedis pool failed,null object!");\r
-            }\r
-\r
-            customRouteInfo = getCustomRouteInstance(serviceName, jedis);\r
-\r
-\r
-        } catch (Exception e) {\r
-            LOGGER.error("call redis throw exception", e);\r
-            throw new ExtendedInternalServerErrorException("call redis throw exception:"\r
-                    + e.getMessage());\r
-        } finally {\r
-            JedisUtil.returnJedisInstance(jedis);\r
-        }\r
-\r
-        if (null == customRouteInfo) {\r
-            String errInfo = "customRouteInfo not found: serviceName-" + serviceName;\r
-            LOGGER.warn(errInfo);\r
-            throw new ExtendedNotFoundException(errInfo);\r
-\r
-        }\r
-\r
-        return customRouteInfo;\r
-\r
-    }\r
-\r
-    public CustomRouteInfo getCustomRouteInstance(String serviceName, Jedis jedis) throws Exception {\r
-\r
-\r
-        CustomRouteInfo customRouteInfo = null;\r
-\r
-\r
-        // 获取info信息\r
-        String routekey =\r
-                RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,\r
-                        RouteUtil.ROUTE_PATH_INFO);\r
-        Map<String, String> infomap = jedis.hgetAll(routekey);\r
-        if (!infomap.isEmpty()) {\r
-            customRouteInfo = new CustomRouteInfo();\r
-            customRouteInfo.setServiceName(serviceName);\r
-            customRouteInfo.setUrl(infomap.get("url"));\r
-            customRouteInfo.setControl(infomap.get("control"));\r
-            customRouteInfo.setStatus(infomap.get("status"));\r
-            customRouteInfo.setVisualRange(infomap.get("visualRange"));\r
-            customRouteInfo.setUseOwnUpstream(infomap.get("useOwnUpstream"));\r
-\r
-\r
-            // 获取负载均衡信息\r
-            String serviceLBkey =\r
-                    RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,\r
-                            RouteUtil.ROUTE_PATH_LOADBALANCE);\r
-            Set<String> serviceLBset = jedis.keys(serviceLBkey + ":*");\r
-            int serverNum = serviceLBset.size();\r
-            RouteServer[] CustomRouteServerList = new RouteServer[serverNum];\r
-            int i = 0;\r
-            for (String serviceInfo : serviceLBset) {\r
-                Map<String, String> serviceLBmap = jedis.hgetAll(serviceInfo);\r
-                RouteServer server = new RouteServer();\r
-                server.setIp(serviceLBmap.get("ip"));\r
-                server.setPort(serviceLBmap.get("port"));\r
-                server.setWeight(Integer.parseInt(serviceLBmap.get("weight")));\r
-                CustomRouteServerList[i] = server;\r
-                i++;\r
-            }\r
-\r
-            customRouteInfo.setServers(CustomRouteServerList);\r
-        }\r
-\r
-\r
-        return customRouteInfo;\r
-    }\r
-\r
-    /**\r
-     * @Title: updateCustomRouteInstance\r
-     * @Description: TODO(更新单个服务信息)\r
-     * @param: @param serviceName\r
-     * @param: @param CustomRouteInfo\r
-     * @param: @return\r
-     * @return: CustomRouteInfo\r
-     */\r
-    public synchronized CustomRouteInfo updateCustomRouteInstance(String serviceName,\r
-            CustomRouteInfo customRouteInfo, String serverPort) {\r
-        if (StringUtils.isBlank(serviceName)) {\r
-            throw new ExtendedNotSupportedException("serviceName  can't be empty");\r
-        }\r
-\r
-        try {\r
-\r
-            if (serviceName.equals(customRouteInfo.getServiceName())) {\r
-                // 删除已存在负载均衡服务器信息\r
-                deleteCustomRoute(serviceName, RouteUtil.ROUTE_PATH_LOADBALANCE + "*", serverPort);\r
-            } else {\r
-                // 如果已修改服务名,先删除此服务全部已有信息\r
-                deleteCustomRoute(serviceName, "*", serverPort);\r
-            }\r
-\r
-\r
-            saveCustomRouteInstance(customRouteInfo, serverPort);\r
-\r
-\r
-\r
-        } catch (ExtendedNotSupportedException e) {\r
-            throw e;\r
-        } catch (Exception e) {\r
-            LOGGER.error("updateCustomRoute throw exception", e);\r
-            throw new ExtendedInternalServerErrorException("update CustomRoute throw exception"\r
-                    + e.getMessage());\r
-\r
-        }\r
-\r
-        return customRouteInfo;\r
-\r
-    }\r
-\r
-    /**\r
-     * @Title updateCustomRouteStatus\r
-     * @Description TODO(更新单个服务状态)\r
-     * @param serviceName\r
-     * @param status\r
-     * @return\r
-     * @return RouteResult\r
-     */\r
-    public synchronized CustomRouteInfo updateCustomRouteStatus(String serviceName, String status) {\r
-\r
-        if (StringUtils.isBlank(serviceName)) {\r
-            throw new ExtendedNotSupportedException("serviceName  can't be empty");\r
-        }\r
-\r
-        if (!RouteUtil.contain(RouteUtil.statusRangeMatches, status)) {\r
-            throw new ExtendedNotSupportedException(\r
-                    "save CustomRouteInfo Status FAIL:status is wrong,value range:("\r
-                            + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");\r
-        }\r
-\r
-        CustomRouteInfo new_customRouteInfo = getCustomRouteInstance(serviceName);\r
-\r
-\r
-\r
-        // 准备info信息\r
-        String serviceInfokey =\r
-                RouteUtil.getPrefixedKey("", RouteUtil.CUSTOMROUTE, serviceName,\r
-                        RouteUtil.ROUTE_PATH_INFO);\r
-        Map<String, String> serviceInfoMap = new HashMap<String, String>();\r
-        serviceInfoMap.put("status", status);\r
-\r
-\r
-        Jedis jedis = null;\r
-        try {\r
-            jedis = JedisUtil.borrowJedisInstance();\r
-            if (jedis == null) {\r
-                throw new ExtendedInternalServerErrorException(\r
-                        "fetch from jedis pool failed,null object!");\r
-            }\r
-            // 保存info信息\r
-            jedis.hmset(serviceInfokey, serviceInfoMap);\r
-            new_customRouteInfo.setStatus(status);\r
-\r
-        } catch (Exception e) {\r
-\r
-            LOGGER.error("update CustomRoute status throw exception", e);\r
-            throw new ExtendedInternalServerErrorException(\r
-                    "update CustomRoute status throw exception" + e.getMessage());\r
-\r
-        } finally {\r
-            JedisUtil.returnJedisInstance(jedis);\r
-        }\r
-\r
-        return new_customRouteInfo;\r
-    }\r
-\r
-    /**\r
-     * @Title: saveCustomRouteInstance\r
-     * @Description: TODO(存储单个服务信息)\r
-     * @param: @param CustomRouteInfo\r
-     * @param: @return\r
-     * @return: CustomRouteInfo\r
-     */\r
-    public synchronized CustomRouteInfo saveCustomRouteInstance(CustomRouteInfo customRouteInfo,\r
-            String serverPort) {\r
-\r
-        if (StringUtils.isBlank(customRouteInfo.getServiceName())\r
-                || customRouteInfo.getServers().length == 0) {\r
-            throw new ExtendedNotSupportedException(\r
-                    "save CustomRouteInfo FAIL: Some required fields are empty");\r
-        }\r
-\r
-       \r
-            if (!RegExpTestUtil.urlRegExpTest(customRouteInfo.getServiceName())) {\r
-                throw new ExtendedNotSupportedException(\r
-                        "save CustomRouteInfo FAIL: ServiceName is not a valid format(ServiceName must be begin with /)");\r
-    \r
-            }\r
-        \r
-            if (StringUtils.isNotBlank(customRouteInfo.getUrl())){\r
-                if (!RegExpTestUtil.urlRegExpTest(customRouteInfo.getUrl())) {\r
-                    throw new ExtendedNotSupportedException(\r
-                            "save CustomRouteInfo FAIL:url is not a valid format(url must be begin with /)");\r
-        \r
-                }\r
-            }\r
-\r
-        if (!RouteUtil.contain(RouteUtil.visualRangeRange, customRouteInfo.getVisualRange())) {\r
-            throw new ExtendedNotSupportedException(\r
-                    "save CustomRouteInfo FAIL:VisualRange is wrong,value range:("\r
-                            + RouteUtil.show(RouteUtil.visualRangeMatches) + ")");\r
-        }\r
-\r
-        if (!RouteUtil.contain(RouteUtil.controlRangeMatches, customRouteInfo.getControl())) {\r
-            throw new ExtendedNotSupportedException(\r
-                    "save CustomRouteInfo FAIL:control is wrong,value range:("\r
-                            + RouteUtil.show(RouteUtil.controlRangeMatches) + ")");\r
-        }\r
-\r
-        if (!RouteUtil.contain(RouteUtil.statusRangeMatches, customRouteInfo.getStatus())) {\r
-            throw new ExtendedNotSupportedException(\r
-                    "save CustomRouteInfo FAIL:status is wrong,value range:("\r
-                            + RouteUtil.show(RouteUtil.statusRangeMatches) + ")");\r
-        }\r
-\r
-        if (!RouteUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, customRouteInfo.getUseOwnUpstream())) {\r
-            throw new ExtendedNotSupportedException(\r
-                    "save apiRouteInfo FAIL:useOwnUpstream is wrong,value range:("\r
-                            + RouteUtil.show(RouteUtil.useOwnUpstreamRangeMatches) + ")");\r
-        }\r
-\r
-        // 检查服务实例格式\r
-        RouteServer[] serverList = customRouteInfo.getServers();\r
-        for (int i = 0; i < serverList.length; i++) {\r
-            RouteServer server = serverList[i];\r
-            if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {\r
-                throw new ExtendedNotSupportedException("save CustomRouteInfo FAIL:IP("\r
-                        + server.getIp() + ")is not a valid ip address");\r
-            }\r
-\r
-            if (!RegExpTestUtil.portRegExpTest(server.getPort())) {\r
-                throw new ExtendedNotSupportedException("save CustomRouteInfo FAIL:Port("\r
-                        + server.getPort() + ")is not a valid Port address");\r
-            }\r
-        }\r
-\r
-\r
-        // 准备info信息\r
-        String serviceInfokey =\r
-                RouteUtil.getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE,\r
-                        customRouteInfo.getServiceName().trim(), RouteUtil.ROUTE_PATH_INFO);\r
-        Map<String, String> serviceInfoMap = new HashMap<String, String>();\r
-        serviceInfoMap.put("url", "/".equals(customRouteInfo.getUrl().trim())\r
-                ? ""\r
-                : customRouteInfo.getUrl().trim());\r
-        serviceInfoMap.put("control", customRouteInfo.getControl());\r
-        serviceInfoMap.put("status", customRouteInfo.getStatus());\r
-        serviceInfoMap.put("visualRange", customRouteInfo.getVisualRange());\r
-        serviceInfoMap.put("useOwnUpstream", customRouteInfo.getUseOwnUpstream());\r
-\r
-\r
-\r
-        // 准备负载均衡信息\r
-        String serviceLBkey =\r
-                RouteUtil.getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE,\r
-                        customRouteInfo.getServiceName(), RouteUtil.ROUTE_PATH_LOADBALANCE);\r
-\r
-\r
-        Jedis jedis = null;\r
-        try {\r
-            jedis = JedisUtil.borrowJedisInstance();\r
-            if (jedis == null) {\r
-                throw new ExtendedInternalServerErrorException(\r
-                        "fetch from jedis pool failed,null object!");\r
-            }\r
-            // 保存info信息\r
-            jedis.hmset(serviceInfokey, serviceInfoMap);\r
-\r
-            // 保存负载均衡信息\r
-\r
-            for (int i = 0; i < serverList.length; i++) {\r
-                Map<String, String> servermap = new HashMap<String, String>();\r
-                RouteServer server = serverList[i];\r
-\r
-                servermap.put("ip", server.getIp());\r
-                servermap.put("port", server.getPort());\r
-                servermap.put("weight", Integer.toString(server.getWeight()));\r
-\r
-                jedis.hmset(serviceLBkey + ":server" + (i + 1), servermap);\r
-            }\r
-\r
-\r
-        } catch (Exception e) {\r
-            LOGGER.error("call redis throw exception", e);\r
-            throw new ExtendedInternalServerErrorException("call redis throw exception:"\r
-                    + e.getMessage());\r
-\r
-        } finally {\r
-            JedisUtil.returnJedisInstance(jedis);;\r
-        }\r
-\r
-        return customRouteInfo;\r
-    }\r
-\r
-\r
-\r
-    /**\r
-     * @Title: deleteCustomRoute\r
-     * @Description: TODO(删除单个服务信息)\r
-     * @param: @param type\r
-     * @param: @param serviceName\r
-     * @param: @param delKey\r
-     * @param: @return\r
-     * @return: void\r
-     */\r
-    public synchronized void deleteCustomRoute(String serviceName, String delKey, String serverPort) {\r
-\r
-        if (StringUtils.isBlank(serviceName)) {\r
-            throw new ExtendedNotSupportedException("serviceName  can't be empty");\r
-        }\r
-\r
-        Jedis jedis = null;\r
-\r
-        try {\r
-            jedis = JedisUtil.borrowJedisInstance();\r
-            if (jedis == null) {\r
-                throw new ExtendedInternalServerErrorException(\r
-                        "fetch from jedis pool failed,null object!");\r
-            }\r
-\r
-            // 获取info信息\r
-            String routekey =\r
-                    RouteUtil\r
-                            .getPrefixedKey(serverPort, RouteUtil.CUSTOMROUTE, serviceName, delKey);\r
-            Set<String> infoSet = jedis.keys(routekey);\r
-\r
-            if (infoSet.isEmpty()) {\r
-                throw new ExtendedNotFoundException("delete CustomRoute FAIL:serviceName-"\r
-                        + serviceName + " not fond ");\r
-            }\r
-\r
-\r
-            String[] paths = new String[infoSet.size()];\r
-\r
-            // Set-->数组\r
-            infoSet.toArray(paths);\r
-\r
-            jedis.del(paths);\r
-\r
-        } catch (ExtendedNotFoundException e) {\r
-            throw e;\r
-        } catch (Exception e) {\r
-\r
-            LOGGER.error("delete CustomRoute throw exception", e);\r
-            throw new ExtendedInternalServerErrorException("delete CustomRoute throw exception:"\r
-                    + e.getMessage());\r
-\r
-        } finally {\r
-            JedisUtil.returnJedisInstance(jedis);\r
-        }\r
-\r
-\r
-    }\r
-}\r