Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / util / RouteUtil.java
1 /**
2  * Copyright 2016 ZTE, Inc. and others.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.msb.apiroute.wrapper.util;
18
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;
25
26
27 public class RouteUtil {
28
29     
30     public static final int consulDeafultPort=8500;
31   
32         public static final String ROUTE_PATH="msb:routing";   
33         
34         public static final String ROUTE_PORT_PATH="msb:";  
35         
36         public static final String ROUTE_PATH_HOST="msb:host";   
37         
38         public static final String APIROUTE="api";  
39         
40         public static final String IUIROUTE="iui";  
41         
42         public static final String CUSTOMROUTE="custom";  
43         
44         public static final String HTTPS_PROTOCOL="https"; 
45         
46         public static final String CUSTOM_PORTAL="portal"; 
47         
48         
49     public static final String PROTOCOL_LIST="REST,HTTP,UI,MQ,FTP,SNMP,TCP,UDP"; 
50     
51     public static final String MSB_ROUTE_URL = "/api/microservices/v1/services";
52     
53     public static final String MSB_CHECK_URL = "/api/catalog/v1/service/router-all";
54     
55     public static final String visualRangeRange="0,1";
56     
57     public static final String controlRangeMatches="0,1,2";
58     
59     public static final String statusRangeMatches="0,1";
60     
61     public static final String useOwnUpstreamRangeMatches="0,1";
62     
63     public static final String ROUTEWAY_IP="ip";
64     
65     public static final String ROUTEWAY_DOMAIN="domain";
66     
67     public static final String SPLIT_LINE="|";
68     
69     public static final String PROTOCOL_REST="REST";
70     
71     public static final String PROTOCOL_UI="UI";
72     
73     public static final String PROTOCOL_HTTP="HTTP";
74     
75     public static final String FILTER_PROTOCOLS="REST,UI,HTTP";
76     
77     public static final int SERVICE_DATA_QUEUE_NUM=5;
78     
79     public static final int SERVICE_QUEUE_CAPACITY=100;
80     
81     public static final int SERVICE_LIST_QUEUE_CAPACITY=5;
82     
83     public static final int WATCH_SECOND=120;
84     
85     public static final String HEALTH_CHECK_PASSING="passing";
86     
87
88     
89         
90         /** 
91         * @Title: getPrefixedKey  
92         * @Description: TODO(Add base path prefix radis assembly path) 
93         * @param: @param serviceName
94         * @param: @param version
95         * @param: @param type
96         * @param: @return      
97         * @return: String    
98         */
99         
100         public static String getPrefixedKey(String...paths){
101                 StringBuffer sb= new StringBuffer();
102                 
103                     if(paths[0].trim().equals("") || paths[0].equals(ConfigUtil.getInstance().getServerPort())){
104                     sb.append(ROUTE_PATH);
105                 }
106                 else{
107                    sb.append(ROUTE_PORT_PATH).append(paths[0]); 
108                 }
109         
110                 for (int i = 1; i < paths.length; i++) {
111                         sb.append(":");
112                         sb.append(paths[i]);
113                 }
114                 return sb.toString();
115         }
116         
117         public static String getPrefixedKey4Host(String...paths){
118       StringBuffer sb= new StringBuffer();
119       
120       sb.append(ROUTE_PATH_HOST);
121          
122   
123       for (int i = 0; i < paths.length; i++) {
124           sb.append(":");
125           sb.append(paths[i]);
126       }
127       return sb.toString();
128   }
129         
130         
131         
132
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);
137             }
138           }
139         
140         public static void checkServiceNameAndVersion(String serviceName,String version){
141           if (StringUtils.isBlank(serviceName)) {
142               throw new UnprocessableEntityException("serviceName  can't be empty");
143             }
144
145             if (StringUtils.isNotBlank(version)) {
146               if (!RegExpTestUtil.versionRegExpTest(version)) {
147                 throw new UnprocessableEntityException("version  is not a valid  format");
148               }
149             }
150     }
151
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 + ")");
157             }     
158     }
159         
160           
161           
162           public static void checkRouterInfoFormat(RouteInfo routeInfo) {
163             
164             if (StringUtils.isBlank(routeInfo.getServiceName()) || routeInfo.getServers().length == 0) {
165               throw new UnprocessableEntityException(
166                   "save RouteInfo FAIL: Some required fields are empty");
167             }
168
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 /)");
173
174               }
175             }
176
177             if (!CommonUtil.contain(RouteUtil.visualRangeRange, routeInfo.getVisualRange())) {
178               throw new UnprocessableEntityException(
179                   "save RouteInfo FAIL:VisualRange is wrong,value range:("
180                       + RouteUtil.visualRangeRange + ")");
181             }
182
183             if (!CommonUtil.contain(RouteUtil.controlRangeMatches, routeInfo.getControl())) {
184               throw new UnprocessableEntityException(
185                   "save RouteInfo FAIL:control is wrong,value range:("
186                       + RouteUtil.controlRangeMatches + ")");
187             }
188
189             if (!CommonUtil.contain(RouteUtil.statusRangeMatches, routeInfo.getStatus())) {
190               throw new UnprocessableEntityException(
191                   "save RouteInfo FAIL:status is wrong,value range:("
192                       + RouteUtil.statusRangeMatches + ")");
193             }
194
195             if (!CommonUtil.contain(RouteUtil.useOwnUpstreamRangeMatches, routeInfo.getUseOwnUpstream())) {
196               throw new UnprocessableEntityException(
197                   "save RouteInfo FAIL:useOwnUpstream is wrong,value range:("
198                       + RouteUtil.useOwnUpstreamRangeMatches + ")");
199             }
200
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");
208               }
209
210               if (!RegExpTestUtil.portRegExpTest(server.getPort())) {
211                 throw new UnprocessableEntityException("save RouteInfo FAIL:Port(" + server.getPort()
212                     + ")is not a valid Port address");
213               }
214             }
215           }
216           
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");
224             }
225
226             for (Node node : microServiceInfo.getNodes()) {
227
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");
233               }
234
235               if (!RegExpTestUtil.portRegExpTest(node.getPort())) {
236                 throw new UnprocessableEntityException("register MicroServiceInfo FAIL:Port("
237                     + node.getPort() + ")is not a valid Port address");
238               }
239             }
240
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");
245
246               }
247             }
248
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 /)");
253
254               }
255             }
256
257
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 + ")");
262             }
263
264           }
265           
266
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);
271             }
272             else{
273               redisPrefixedKey=RouteUtil.getPrefixedKey(publish_port, APIROUTE, routeName, version);
274             }
275             
276             return redisPrefixedKey;
277           }
278           
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);
283         }
284         else{
285           redisPrefixedKey=RouteUtil.getPrefixedKey(publish_port, routeType, routeName);
286         }
287         
288         return redisPrefixedKey;
289       }
290           
291           public static String getMutiRedisKey(String routeType,String routeWay){
292             String redisKey;
293             if(RouteUtil.ROUTEWAY_DOMAIN.equals(routeWay)){
294                  redisKey =
295                     RouteUtil.getPrefixedKey4Host("*", routeType, "*");
296                  
297               }
298               else{
299                 redisKey =
300                     RouteUtil.getPrefixedKey("[^h]*", routeType, "*");
301                
302               }
303             
304             return redisKey;
305           }
306         
307          /** 
308           * @Title getRouteNameByns 
309           * @Description TODO(根据服务名和命名空间拆分服务路由名) 
310           * @param serviceName
311           * @param namespace
312           * @return      
313           * @return String    
314           */
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);
320                 }  
321             }    
322             
323             return serviceName;
324           }
325         
326         public static String getVisualRangeByRouter(String visualRange){
327           String[] rangs = StringUtils.split(visualRange, "|");
328           if(rangs.length>1){
329             String visualRangeMatches=ConfigUtil.getInstance().getVisualRangeMatches();
330             if(StringUtils.split(visualRangeMatches, "|").length>1){
331               return "0";
332             }
333             else{
334               return visualRangeMatches;
335             }
336           }
337           else{
338             return visualRange;
339           }
340           
341         }
342
343  
344
345 }