update link to upper-constraints.txt
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / util / RouteUtil.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.util;
15
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;
22
23
24 public class RouteUtil {
25
26
27     public static final int consulDeafultPort = 8500;
28
29     public static final String ROUTE_PATH = "msb:routing";
30
31     public static final String ROUTE_PORT_PATH = "msb:";
32
33     public static final String ROUTE_PATH_HOST = "msb:host";
34
35     public static final String APIROUTE = "api";
36
37     public static final String IUIROUTE = "iui";
38
39     public static final String CUSTOMROUTE = "custom";
40
41     public static final String HTTPS_PROTOCOL = "https";
42
43     public static final String CUSTOM_PORTAL = "portal";
44
45
46     public static final String PROTOCOL_LIST = "REST,HTTP,UI,MQ,FTP,SNMP,TCP,UDP";
47
48     public static final String MSB_ROUTE_URL = "/api/microservices/v1/services";
49
50     public static final String MSB_CHECK_URL = "/api/catalog/v1/service/router-all";
51
52     public static final String visualRangeRange = "0,1";
53
54     public static final String controlRangeMatches = "0,1,2";
55
56     public static final String statusRangeMatches = "0,1";
57
58     public static final String useOwnUpstreamRangeMatches = "0,1";
59
60     public static final String ROUTEWAY_IP = "ip";
61
62     public static final String ROUTEWAY_DOMAIN = "domain";
63
64     public static final String SPLIT_LINE = "|";
65
66     public static final String PROTOCOL_REST = "REST";
67
68     public static final String PROTOCOL_UI = "UI";
69
70     public static final String PROTOCOL_HTTP = "HTTP";
71
72     public static final String FILTER_PROTOCOLS = "REST,UI,HTTP";
73
74     public static final int SERVICE_DATA_QUEUE_NUM = 5;
75
76     public static final int SERVICE_QUEUE_CAPACITY = 100;
77
78     public static final int SERVICE_LIST_QUEUE_CAPACITY = 5;
79
80     public static final int WATCH_SECOND = 120;
81
82     public static final String HEALTH_CHECK_PASSING = "passing";
83
84
85
86     /**
87      * @Title: getPrefixedKey
88      * @Description: TODO(Add base path prefix radis assembly path)
89      * @param: @param serviceName
90      * @param: @param version
91      * @param: @param type
92      * @param: @return
93      * @return: String
94      */
95
96     public static String getPrefixedKey(String... paths) {
97         StringBuffer sb = new StringBuffer();
98
99         if (paths[0].trim().equals("") || paths[0].equals(ConfigUtil.getInstance().getServerPort())) {
100             sb.append(ROUTE_PATH);
101         } else {
102             sb.append(ROUTE_PORT_PATH).append(paths[0]);
103         }
104
105         for (int i = 1; i < paths.length; i++) {
106             sb.append(":");
107             sb.append(paths[i]);
108         }
109         return sb.toString();
110     }
111
112     public static String getPrefixedKey4Host(String... paths) {
113         StringBuffer sb = new StringBuffer();
114
115         sb.append(ROUTE_PATH_HOST);
116
117
118         for (int i = 0; i < paths.length; i++) {
119             sb.append(":");
120             sb.append(paths[i]);
121         }
122         return sb.toString();
123     }
124
125
126
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);
131         }
132     }
133
134     public static void checkServiceNameAndVersion(String serviceName, String version) {
135         if (StringUtils.isBlank(serviceName)) {
136             throw new UnprocessableEntityException("serviceName  can't be empty");
137         }
138
139         if (StringUtils.isNotBlank(version)) {
140             if (!RegExpTestUtil.versionRegExpTest(version)) {
141                 throw new UnprocessableEntityException("version  is not a valid  format");
142             }
143         }
144     }
145
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 + ")");
150         }
151     }
152
153
154
155     public static void checkRouterInfoFormat(RouteInfo routeInfo) {
156
157         if (StringUtils.isBlank(routeInfo.getServiceName()) || routeInfo.getServers().length == 0) {
158             throw new UnprocessableEntityException("save RouteInfo FAIL: Some required fields are empty");
159         }
160
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 /)");
165
166             }
167         }
168
169         if (!CommonUtil.contain(RouteUtil.visualRangeRange, routeInfo.getVisualRange())) {
170             throw new UnprocessableEntityException("save RouteInfo FAIL:VisualRange is wrong,value range:("
171                             + RouteUtil.visualRangeRange + ")");
172         }
173
174         if (!CommonUtil.contain(RouteUtil.controlRangeMatches, routeInfo.getControl())) {
175             throw new UnprocessableEntityException(
176                             "save RouteInfo FAIL:control is wrong,value range:(" + RouteUtil.controlRangeMatches + ")");
177         }
178
179         if (!CommonUtil.contain(RouteUtil.statusRangeMatches, routeInfo.getStatus())) {
180             throw new UnprocessableEntityException(
181                             "save RouteInfo FAIL:status is wrong,value range:(" + RouteUtil.statusRangeMatches + ")");
182         }
183
184         if (!CommonUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, routeInfo.getUseOwnUpstream())) {
185             throw new UnprocessableEntityException("save RouteInfo FAIL:useOwnUpstream is wrong,value range:("
186                             + RouteUtil.useOwnUpstreamRangeMatches + ")");
187         }
188
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");
196             }
197
198             if (!RegExpTestUtil.portRegExpTest(server.getPort())) {
199                 throw new UnprocessableEntityException(
200                                 "save RouteInfo FAIL:Port(" + server.getPort() + ")is not a valid Port address");
201             }
202         }
203     }
204
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");
211         }
212
213         for (Node node : microServiceInfo.getNodes()) {
214
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");
220             }
221
222             if (!RegExpTestUtil.portRegExpTest(node.getPort())) {
223                 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Port(" + node.getPort()
224                                 + ")is not a valid Port address");
225             }
226         }
227
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");
231
232             }
233         }
234
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 /)");
239
240             }
241         }
242
243
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 + ")");
247         }
248
249     }
250
251
252     public static String getAPIRedisPrefixedKey(String routeName, String version, String host, String publish_port,
253                     String routeWay) {
254         String redisPrefixedKey;
255         if (ROUTEWAY_DOMAIN.equals(routeWay)) {
256             redisPrefixedKey = RouteUtil.getPrefixedKey4Host(host, APIROUTE, routeName, version);
257         } else {
258             redisPrefixedKey = RouteUtil.getPrefixedKey(publish_port, APIROUTE, routeName, version);
259         }
260
261         return redisPrefixedKey;
262     }
263
264     public static String getRedisPrefixedKey(String routeType, String routeName, String host, String publish_port,
265                     String routeWay) {
266         String redisPrefixedKey;
267         if (ROUTEWAY_DOMAIN.equals(routeWay)) {
268             redisPrefixedKey = RouteUtil.getPrefixedKey4Host(host, routeType, routeName);
269         } else {
270             redisPrefixedKey = RouteUtil.getPrefixedKey(publish_port, routeType, routeName);
271         }
272
273         return redisPrefixedKey;
274     }
275
276     public static String getMutiRedisKey(String routeType, String routeWay) {
277         String redisKey;
278         if (RouteUtil.ROUTEWAY_DOMAIN.equals(routeWay)) {
279             redisKey = RouteUtil.getPrefixedKey4Host("*", routeType, "*");
280
281         } else {
282             redisKey = RouteUtil.getPrefixedKey("[^h]*", routeType, "*");
283
284         }
285
286         return redisKey;
287     }
288
289     /**
290      * @Title getRouteNameByns
291      * @Description TODO(根据服务名和命名空间拆分服务路由名)
292      * @param serviceName
293      * @param namespace
294      * @return
295      * @return String
296      */
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);
305             }
306         } else if (StringUtils.isNotBlank(version)) {
307             if (consul_serviceName.endsWith("-" + version)) {
308                 serviceName = consul_serviceName.substring(0, consul_serviceName.length() - version.length() - 1);
309             }
310         } else if (StringUtils.isNotBlank(namespace)) {
311             if (consul_serviceName.endsWith("-" + namespace)) {
312                 serviceName = consul_serviceName.substring(0, consul_serviceName.length() - namespace.length() - 1);
313             }
314         }
315         return serviceName;
316     }
317
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) {
323                 return "0";
324             } else {
325                 return visualRangeMatches;
326             }
327         } else {
328             return visualRange;
329         }
330
331     }
332
333
334
335 }