1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.util;
16 import org.apache.commons.lang3.StringUtils;
17 import org.onap.msb.apiroute.api.MicroServiceFullInfo;
18 import org.onap.msb.apiroute.api.Node;
19 import org.onap.msb.apiroute.api.RouteInfo;
20 import org.onap.msb.apiroute.api.RouteServer;
21 import org.onap.msb.apiroute.api.exception.UnprocessableEntityException;
24 public class RouteUtil {
27 public static final int consulDeafultPort = 8500;
29 public static final String ROUTE_PATH = "msb:routing";
31 public static final String ROUTE_PORT_PATH = "msb:";
33 public static final String ROUTE_PATH_HOST = "msb:host";
35 public static final String APIROUTE = "api";
37 public static final String IUIROUTE = "iui";
39 public static final String CUSTOMROUTE = "custom";
41 public static final String HTTPS_PROTOCOL = "https";
43 public static final String CUSTOM_PORTAL = "portal";
46 public static final String PROTOCOL_LIST = "REST,HTTP,UI,MQ,FTP,SNMP,TCP,UDP";
48 public static final String MSB_ROUTE_URL = "/api/microservices/v1/services";
50 public static final String MSB_CHECK_URL = "/api/catalog/v1/service/router-all";
52 public static final String visualRangeRange = "0,1";
54 public static final String controlRangeMatches = "0,1,2";
56 public static final String statusRangeMatches = "0,1";
58 public static final String useOwnUpstreamRangeMatches = "0,1";
60 public static final String ROUTEWAY_IP = "ip";
62 public static final String ROUTEWAY_DOMAIN = "domain";
64 public static final String SPLIT_LINE = "|";
66 public static final String PROTOCOL_REST = "REST";
68 public static final String PROTOCOL_UI = "UI";
70 public static final String PROTOCOL_HTTP = "HTTP";
72 public static final String FILTER_PROTOCOLS = "REST,UI,HTTP";
74 public static final int SERVICE_DATA_QUEUE_NUM = 5;
76 public static final int SERVICE_QUEUE_CAPACITY = 100;
78 public static final int SERVICE_LIST_QUEUE_CAPACITY = 5;
80 public static final int WATCH_SECOND = 120;
82 public static final String HEALTH_CHECK_PASSING = "passing";
87 * @Title: getPrefixedKey
88 * @Description: TODO(Add base path prefix radis assembly path)
89 * @param: @param serviceName
90 * @param: @param version
96 public static String getPrefixedKey(String... paths) {
97 StringBuffer sb = new StringBuffer();
99 if (paths[0].trim().equals("") || paths[0].equals(ConfigUtil.getInstance().getServerPort())) {
100 sb.append(ROUTE_PATH);
102 sb.append(ROUTE_PORT_PATH).append(paths[0]);
105 for (int i = 1; i < paths.length; i++) {
109 return sb.toString();
112 public static String getPrefixedKey4Host(String... paths) {
113 StringBuffer sb = new StringBuffer();
115 sb.append(ROUTE_PATH_HOST);
118 for (int i = 0; i < paths.length; i++) {
122 return sb.toString();
127 public static void checkRouteWay(String routeWay) {
128 if (!CommonUtil.contain(ConfigUtil.getInstance().getRouteWay(), routeWay)) {
129 String errInfo = "routeWay does not support,must be ip or domain";
130 throw new UnprocessableEntityException(errInfo);
134 public static void checkServiceNameAndVersion(String serviceName, String version) {
135 if (StringUtils.isBlank(serviceName)) {
136 throw new UnprocessableEntityException("serviceName can't be empty");
139 if (StringUtils.isNotBlank(version)) {
140 if (!RegExpTestUtil.versionRegExpTest(version)) {
141 throw new UnprocessableEntityException("version is not a valid format");
146 public static void checkServiceStatus(String status) {
147 if (!CommonUtil.contain(statusRangeMatches, status)) {
148 throw new UnprocessableEntityException("save RouteInfo Status FAIL:status is wrong,value range:("
149 + RouteUtil.statusRangeMatches + ")");
155 public static void checkRouterInfoFormat(RouteInfo routeInfo) {
157 if (StringUtils.isBlank(routeInfo.getServiceName()) || routeInfo.getServers().length == 0) {
158 throw new UnprocessableEntityException("save RouteInfo FAIL: Some required fields are empty");
161 if (StringUtils.isNotBlank(routeInfo.getUrl())) {
162 if (!RegExpTestUtil.urlRegExpTest(routeInfo.getUrl())) {
163 throw new UnprocessableEntityException(
164 "save RouteInfo FAIL:url is not a valid format(url must be begin with /)");
169 if (!CommonUtil.contain(RouteUtil.visualRangeRange, routeInfo.getVisualRange())) {
170 throw new UnprocessableEntityException("save RouteInfo FAIL:VisualRange is wrong,value range:("
171 + RouteUtil.visualRangeRange + ")");
174 if (!CommonUtil.contain(RouteUtil.controlRangeMatches, routeInfo.getControl())) {
175 throw new UnprocessableEntityException(
176 "save RouteInfo FAIL:control is wrong,value range:(" + RouteUtil.controlRangeMatches + ")");
179 if (!CommonUtil.contain(RouteUtil.statusRangeMatches, routeInfo.getStatus())) {
180 throw new UnprocessableEntityException(
181 "save RouteInfo FAIL:status is wrong,value range:(" + RouteUtil.statusRangeMatches + ")");
184 if (!CommonUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, routeInfo.getUseOwnUpstream())) {
185 throw new UnprocessableEntityException("save RouteInfo FAIL:useOwnUpstream is wrong,value range:("
186 + RouteUtil.useOwnUpstreamRangeMatches + ")");
189 // Check the service instance format
190 RouteServer[] serverList = routeInfo.getServers();
191 for (int i = 0; i < serverList.length; i++) {
192 RouteServer server = serverList[i];
193 if (!RegExpTestUtil.ipRegExpTest(server.getIp())) {
194 throw new UnprocessableEntityException(
195 "save RouteInfo FAIL:IP(" + server.getIp() + ")is not a valid ip address");
198 if (!RegExpTestUtil.portRegExpTest(server.getPort())) {
199 throw new UnprocessableEntityException(
200 "save RouteInfo FAIL:Port(" + server.getPort() + ")is not a valid Port address");
205 public static void checkMicroServiceInfoFormat(MicroServiceFullInfo microServiceInfo, String requestIP) {
206 // Check the service instance format
207 if (StringUtils.isBlank(microServiceInfo.getServiceName())
208 || StringUtils.isBlank(microServiceInfo.getProtocol())
209 || microServiceInfo.getNodes().size() == 0) {
210 throw new UnprocessableEntityException("register MicroServiceInfo FAIL: Some required fields are empty");
213 for (Node node : microServiceInfo.getNodes()) {
215 if (node.getIp() == null || node.getIp().isEmpty()) {
216 node.setIp(requestIP);
217 } else if (!RegExpTestUtil.ipRegExpTest(node.getIp())) {
218 throw new UnprocessableEntityException(
219 "register MicroServiceInfo FAIL:IP(" + node.getIp() + ")is not a valid ip address");
222 if (!RegExpTestUtil.portRegExpTest(node.getPort())) {
223 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Port(" + node.getPort()
224 + ")is not a valid Port address");
228 if (StringUtils.isNotBlank(microServiceInfo.getVersion())) {
229 if (!RegExpTestUtil.versionRegExpTest(microServiceInfo.getVersion())) {
230 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:version is not a valid format");
235 if (StringUtils.isNotBlank(microServiceInfo.getUrl().trim())) {
236 if (!RegExpTestUtil.urlRegExpTest(microServiceInfo.getUrl())) {
237 throw new UnprocessableEntityException(
238 "register MicroServiceInfo FAIL:url is not a valid format(url must be begin with /)");
244 if (RouteUtil.PROTOCOL_LIST.indexOf(microServiceInfo.getProtocol().trim()) == -1) {
245 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Protocol is wrong,value range:("
246 + RouteUtil.PROTOCOL_LIST + ")");
252 public static String getAPIRedisPrefixedKey(String routeName, String version, String host, String publish_port,
254 String redisPrefixedKey;
255 if (ROUTEWAY_DOMAIN.equals(routeWay)) {
256 redisPrefixedKey = RouteUtil.getPrefixedKey4Host(host, APIROUTE, routeName, version);
258 redisPrefixedKey = RouteUtil.getPrefixedKey(publish_port, APIROUTE, routeName, version);
261 return redisPrefixedKey;
264 public static String getRedisPrefixedKey(String routeType, String routeName, String host, String publish_port,
266 String redisPrefixedKey;
267 if (ROUTEWAY_DOMAIN.equals(routeWay)) {
268 redisPrefixedKey = RouteUtil.getPrefixedKey4Host(host, routeType, routeName);
270 redisPrefixedKey = RouteUtil.getPrefixedKey(publish_port, routeType, routeName);
273 return redisPrefixedKey;
276 public static String getMutiRedisKey(String routeType, String routeWay) {
278 if (RouteUtil.ROUTEWAY_DOMAIN.equals(routeWay)) {
279 redisKey = RouteUtil.getPrefixedKey4Host("*", routeType, "*");
282 redisKey = RouteUtil.getPrefixedKey("[^h]*", routeType, "*");
290 * @Title getRouteNameByns
291 * @Description TODO(根据服务名和命名空间拆分服务路由名)
297 public static String getRouteNameByns(String consul_serviceName, String version, String namespace) {
298 String serviceName = consul_serviceName;
299 // Remove version and namespace from consul service name
300 // Consul_serviceName Format: serviceName-version-namespace
301 if (StringUtils.isNotBlank(version) && StringUtils.isNotBlank(namespace)) {
302 if (consul_serviceName.endsWith("-" + version + "-" + namespace)) {
303 serviceName = consul_serviceName.substring(0,
304 consul_serviceName.length() - version.length() - namespace.length() - 2);
306 } else if (StringUtils.isNotBlank(version)) {
307 if (consul_serviceName.endsWith("-" + version)) {
308 serviceName = consul_serviceName.substring(0, consul_serviceName.length() - version.length() - 1);
310 } else if (StringUtils.isNotBlank(namespace)) {
311 if (consul_serviceName.endsWith("-" + namespace)) {
312 serviceName = consul_serviceName.substring(0, consul_serviceName.length() - namespace.length() - 1);
318 public static String getVisualRangeByRouter(String visualRange) {
319 String[] rangs = StringUtils.split(visualRange, "|");
320 if (rangs.length > 1) {
321 String visualRangeMatches = ConfigUtil.getInstance().getVisualRangeMatches();
322 if (StringUtils.split(visualRangeMatches, "|").length > 1) {
325 return visualRangeMatches;