2 * Copyright 2016 ZTE, Inc. and others.
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.
17 package org.onap.msb.apiroute.wrapper.util;
19 import org.apache.commons.lang3.StringUtils;
20 import org.onap.msb.apiroute.api.MicroServiceFullInfo;
21 import org.onap.msb.apiroute.api.Node;
22 import org.onap.msb.apiroute.api.RouteInfo;
23 import org.onap.msb.apiroute.api.RouteServer;
24 import org.onap.msb.apiroute.api.exception.UnprocessableEntityException;
27 public class RouteUtil {
30 public static final int consulDeafultPort=8500;
32 public static final String ROUTE_PATH="msb:routing";
34 public static final String ROUTE_PORT_PATH="msb:";
36 public static final String ROUTE_PATH_HOST="msb:host";
38 public static final String APIROUTE="api";
40 public static final String IUIROUTE="iui";
42 public static final String CUSTOMROUTE="custom";
44 public static final String HTTPS_PROTOCOL="https";
46 public static final String CUSTOM_PORTAL="portal";
49 public static final String PROTOCOL_LIST="REST,HTTP,UI,MQ,FTP,SNMP,TCP,UDP";
51 public static final String MSB_ROUTE_URL = "/api/microservices/v1/services";
53 public static final String MSB_CHECK_URL = "/api/catalog/v1/service/router-all";
55 public static final String visualRangeRange="0,1";
57 public static final String controlRangeMatches="0,1,2";
59 public static final String statusRangeMatches="0,1";
61 public static final String useOwnUpstreamRangeMatches="0,1";
63 public static final String ROUTEWAY_IP="ip";
65 public static final String ROUTEWAY_DOMAIN="domain";
67 public static final String SPLIT_LINE="|";
69 public static final String PROTOCOL_REST="REST";
71 public static final String PROTOCOL_UI="UI";
73 public static final String PROTOCOL_HTTP="HTTP";
75 public static final String FILTER_PROTOCOLS="REST,UI,HTTP";
77 public static final int SERVICE_DATA_QUEUE_NUM=5;
79 public static final int SERVICE_QUEUE_CAPACITY=100;
81 public static final int SERVICE_LIST_QUEUE_CAPACITY=5;
83 public static final int WATCH_SECOND=120;
85 public static final String HEALTH_CHECK_PASSING="passing";
91 * @Title: getPrefixedKey
92 * @Description: TODO(Add base path prefix radis assembly path)
93 * @param: @param serviceName
94 * @param: @param version
100 public static String getPrefixedKey(String...paths){
101 StringBuffer sb= new StringBuffer();
103 if(paths[0].trim().equals("") || paths[0].equals(ConfigUtil.getInstance().getServerPort())){
104 sb.append(ROUTE_PATH);
107 sb.append(ROUTE_PORT_PATH).append(paths[0]);
110 for (int i = 1; i < paths.length; i++) {
114 return sb.toString();
117 public static String getPrefixedKey4Host(String...paths){
118 StringBuffer sb= new StringBuffer();
120 sb.append(ROUTE_PATH_HOST);
123 for (int i = 0; i < paths.length; i++) {
127 return sb.toString();
133 public static void checkRouteWay(String routeWay){
134 if(!CommonUtil.contain(ConfigUtil.getInstance().getRouteWay(),routeWay)){
135 String errInfo = "routeWay does not support,must be ip or domain";
136 throw new UnprocessableEntityException(errInfo);
140 public static void checkServiceNameAndVersion(String serviceName,String version){
141 if (StringUtils.isBlank(serviceName)) {
142 throw new UnprocessableEntityException("serviceName can't be empty");
145 if (StringUtils.isNotBlank(version)) {
146 if (!RegExpTestUtil.versionRegExpTest(version)) {
147 throw new UnprocessableEntityException("version is not a valid format");
152 public static void checkServiceStatus(String status){
153 if (!CommonUtil.contain(statusRangeMatches, status)) {
154 throw new UnprocessableEntityException(
155 "save RouteInfo Status FAIL:status is wrong,value range:("
156 + RouteUtil.statusRangeMatches + ")");
162 public static void checkRouterInfoFormat(RouteInfo routeInfo) {
164 if (StringUtils.isBlank(routeInfo.getServiceName()) || routeInfo.getServers().length == 0) {
165 throw new UnprocessableEntityException(
166 "save RouteInfo FAIL: Some required fields are empty");
169 if (StringUtils.isNotBlank(routeInfo.getUrl())) {
170 if (!RegExpTestUtil.urlRegExpTest(routeInfo.getUrl())) {
171 throw new UnprocessableEntityException(
172 "save RouteInfo FAIL:url is not a valid format(url must be begin with /)");
177 if (!CommonUtil.contain(RouteUtil.visualRangeRange, routeInfo.getVisualRange())) {
178 throw new UnprocessableEntityException(
179 "save RouteInfo FAIL:VisualRange is wrong,value range:("
180 + RouteUtil.visualRangeRange + ")");
183 if (!CommonUtil.contain(RouteUtil.controlRangeMatches, routeInfo.getControl())) {
184 throw new UnprocessableEntityException(
185 "save RouteInfo FAIL:control is wrong,value range:("
186 + RouteUtil.controlRangeMatches + ")");
189 if (!CommonUtil.contain(RouteUtil.statusRangeMatches, routeInfo.getStatus())) {
190 throw new UnprocessableEntityException(
191 "save RouteInfo FAIL:status is wrong,value range:("
192 + RouteUtil.statusRangeMatches + ")");
195 if (!CommonUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, routeInfo.getUseOwnUpstream())) {
196 throw new UnprocessableEntityException(
197 "save RouteInfo FAIL:useOwnUpstream is wrong,value range:("
198 + RouteUtil.useOwnUpstreamRangeMatches + ")");
201 // Check the service instance format
202 RouteServer[] serverList = routeInfo.getServers();
203 for (int i = 0; i < serverList.length; i++) {
204 RouteServer server = serverList[i];
205 if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {
206 throw new UnprocessableEntityException("save RouteInfo FAIL:IP(" + server.getIp()
207 + ")is not a valid ip address");
210 if (!RegExpTestUtil.portRegExpTest(server.getPort())) {
211 throw new UnprocessableEntityException("save RouteInfo FAIL:Port(" + server.getPort()
212 + ")is not a valid Port address");
217 public static void checkMicroServiceInfoFormat(MicroServiceFullInfo microServiceInfo,String requestIP){
218 // Check the service instance format
219 if (StringUtils.isBlank(microServiceInfo.getServiceName())
220 || StringUtils.isBlank(microServiceInfo.getProtocol())
221 || microServiceInfo.getNodes().size() == 0) {
222 throw new UnprocessableEntityException(
223 "register MicroServiceInfo FAIL: Some required fields are empty");
226 for (Node node : microServiceInfo.getNodes()) {
228 if (node.getIp() == null || node.getIp().isEmpty()) {
229 node.setIp(requestIP);
230 } else if (!RegExpTestUtil.ipRegExpTest(node.getIp())) {
231 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:IP(" + node.getIp()
232 + ")is not a valid ip address");
235 if (!RegExpTestUtil.portRegExpTest(node.getPort())) {
236 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Port("
237 + node.getPort() + ")is not a valid Port address");
241 if (StringUtils.isNotBlank(microServiceInfo.getVersion())) {
242 if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) {
243 throw new UnprocessableEntityException(
244 "register MicroServiceInfo FAIL:version is not a valid format");
249 if (StringUtils.isNotBlank(microServiceInfo.getUrl().trim())) {
250 if (!RegExpTestUtil.urlRegExpTest(microServiceInfo.getUrl())) {
251 throw new UnprocessableEntityException(
252 "register MicroServiceInfo FAIL:url is not a valid format(url must be begin with /)");
258 if (RouteUtil.PROTOCOL_LIST.indexOf(microServiceInfo.getProtocol().trim()) == -1) {
259 throw new UnprocessableEntityException(
260 "register MicroServiceInfo FAIL:Protocol is wrong,value range:("
261 + RouteUtil.PROTOCOL_LIST + ")");
267 public static String getAPIRedisPrefixedKey(String routeName, String version, String host,String publish_port,String routeWay){
268 String redisPrefixedKey;
269 if(ROUTEWAY_DOMAIN.equals(routeWay)){
270 redisPrefixedKey= RouteUtil.getPrefixedKey4Host(host, APIROUTE, routeName, version);
273 redisPrefixedKey=RouteUtil.getPrefixedKey(publish_port, APIROUTE, routeName, version);
276 return redisPrefixedKey;
279 public static String getRedisPrefixedKey(String routeType,String routeName, String host,String publish_port,String routeWay){
280 String redisPrefixedKey;
281 if(ROUTEWAY_DOMAIN.equals(routeWay)){
282 redisPrefixedKey= RouteUtil.getPrefixedKey4Host(host, routeType, routeName);
285 redisPrefixedKey=RouteUtil.getPrefixedKey(publish_port, routeType, routeName);
288 return redisPrefixedKey;
291 public static String getMutiRedisKey(String routeType,String routeWay){
293 if(RouteUtil.ROUTEWAY_DOMAIN.equals(routeWay)){
295 RouteUtil.getPrefixedKey4Host("*", routeType, "*");
300 RouteUtil.getPrefixedKey("[^h]*", routeType, "*");
308 * @Title getRouteNameByns
309 * @Description TODO(根据服务名和命名空间拆分服务路由名)
315 public static String getRouteNameByns(String consul_serviceName,String namespace){
316 String serviceName=consul_serviceName;
317 if(StringUtils.isNotBlank(namespace)){
318 if(consul_serviceName.endsWith("-"+namespace)){
319 serviceName=consul_serviceName.substring(0,consul_serviceName.length()-namespace.length()-1);
326 public static String getVisualRangeByRouter(String visualRange){
327 String[] rangs = StringUtils.split(visualRange, "|");
329 String visualRangeMatches=ConfigUtil.getInstance().getVisualRangeMatches();
330 if(StringUtils.split(visualRangeMatches, "|").length>1){
334 return visualRangeMatches;