inherit from oparent
[msb/discovery.git] / discovery-ui / src / main / resources / iui / microservices / js / routeUtil.js
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 var routeUtil = {};
17
18 routeUtil.growl=function(title,message,type){
19       $.growl({
20     icon: "fa fa-envelope-o fa-lg",
21     title: "  "+$.i18n.prop('org_onap_msb_route_property_ttl')+title,
22     message: message+"        "
23       },{
24         type: type
25       });
26 }
27
28
29 routeUtil.showApiGateWayAddress=function(){
30
31
32
33   if($("input[name='chkVisualRange']:checked").length==0) return false;
34
35   if(vm.msbRouteInfo.protocol=='REST' || vm.msbRouteInfo.protocol=='HTTP' || vm.msbRouteInfo.protocol=='UI' || vm.msbRouteInfo.protocol=='PORTAL') return true;
36
37    if(vm.msbRouteInfo.protocol=='UDP' || vm.msbRouteInfo.protocol=='TCP') {
38     if(vm.if_publish_port.length==1) return true;
39    }
40
41
42     return false;
43
44
45 }
46
47
48 routeUtil.showHealthStatus=function(nodes){
49
50     for(var i=0;i<nodes.length;i++){
51
52         if(nodes[i].status!="passing"){
53            return " <span class='label label-warning'>"+$.i18n.prop('org_onap_msb_discover_property_abnormal')+"</span>"; 
54         }
55     }
56
57     return " <span class='label label-success'>"+$.i18n.prop('org_onap_msb_discover_property_normal')+"</span>";
58
59
60 }
61
62 routeUtil.showVisualRange=function(visualRange){
63
64     var rangArray=visualRange.split("|");
65
66     var visualRangeText="";
67
68 if(rangArray.length>1) rangArray.sort(function(a,b){return a>b?1:-1}); 
69
70     for(var i=0;i<rangArray.length;i++){
71         if(rangArray[i] === '0'){
72               visualRangeText+= "<span class='label-visualRange label-sysOut'>"+$.i18n.prop('org_onap_msb_route_form_intersystem')+"</span>";
73            }
74          else if(rangArray[i] === '1'){
75               visualRangeText+= "<span class='label-visualRange label-sysIn'>"+$.i18n.prop('org_onap_msb_route_form_insystem')+"</span>";
76            }
77
78     }
79
80 return visualRangeText;
81
82 }
83
84
85 routeUtil.showCheckInterval=function(node){
86   if(node.checkType=="TTL"){
87     return node.ttl==0?"":node.ttl;
88   }
89   else if(node.checkType=="TCP" || node.checkType=="HTTP"){
90     return node.checkInterval==0?"":node.checkInterval;
91   }
92   else{
93     return "";
94   }
95 }
96
97
98
99
100 routeUtil.ifAPIUrl=function(url){
101   if(url=="" || url ==null) return false;
102        
103     var reg_api_match=/^(\/api\/.*?)$/;
104    return reg_api_match.test(url);
105       
106 }
107
108
109 routeUtil.checkLables=function(apiGateway_labels_array,service_labels){
110  if(apiGateway_labels_array==null || apiGateway_labels_array.length==0) return true;
111  if(service_labels=="") return false;
112  var service_labels_array=service_labels.split(",");
113   for(var i=0;i<service_labels_array.length;i++){
114    for(var j=0;j<apiGateway_labels_array.length;j++){
115      if(service_labels_array[i].trim()==apiGateway_labels_array[j].trim()){
116       return true;
117      }
118   }
119  }
120  return false;
121 }
122
123 routeUtil.checkNetwork_plane_type=function(apiGateway_networkPlaneType,service_networkPlaneType){
124   if(apiGateway_networkPlaneType==null || apiGateway_networkPlaneType=="") return true;
125   var apiGateway_networkPlaneType_array=apiGateway_networkPlaneType.split("|");
126   var service_networkPlaneType_array=service_networkPlaneType.split("|")
127
128  for(var i=0;i<service_networkPlaneType_array.length;i++){
129    for(var j=0;j<apiGateway_networkPlaneType_array.length;j++){
130      if(service_networkPlaneType_array[i].trim()==apiGateway_networkPlaneType_array[j].trim()){
131       return true;
132      }
133   }
134  }
135  return false;
136
137 }
138
139
140 routeUtil.checkTargetServiceUrl=function(service){
141
142   if(service==null) return false;
143
144    return routeUtil.checkNetwork_plane_type(service.network_plane_type,$("input[name='networkPlaneType']").val()) &&
145           routeUtil.checkLables(service.labels,$("#labels").val());
146
147 }
148
149
150 Array.prototype.unique = function(){
151  var res = [];
152  var json = {};
153  for(var i = 0; i < this.length; i++){
154   if(!json[this[i]]){
155    res.push(this[i]);
156    json[this[i]] = 1;
157   }
158  }
159  return res;
160 }
161
162
163 routeUtil.buildTargetServiceUrl=function(service,namespace){
164
165 namespace=namespace==""?"default":namespace;
166 var serviceName=vm.msbRouteInfo.serviceName==""?"serviceName":vm.msbRouteInfo.serviceName;
167 var targetServiceUrlArray=[]; 
168 //routeWay
169  var routeWay="ip",routeSubdomain="openpalette.zte.com.cn";
170  var metadataArray=service.metadata;
171  if(metadataArray!=null){
172    for(var i=0;i<metadataArray.length;i++){
173       if(metadataArray[i].key=="routeWay"){
174         routeWay=metadataArray[i].value;
175       }
176       if(metadataArray[i].key=="routeSubdomain"){
177         routeSubdomain=metadataArray[i].value;
178       }
179    }
180 }
181
182
183 //get path
184 var path="";
185  var protocol=$("select[name='protocol']").find("option:selected").text();
186  if(vm.msbRouteInfo.path.trim()!="" && vm.msbRouteInfo.path.trim()!="/"){
187    path=vm.msbRouteInfo.path;
188  }
189  else{
190   var version=vm.msbRouteInfo.version==""?"":"/"+vm.msbRouteInfo.version
191    if(protocol=='UI'){
192
193    path="/iui/"+serviceName;
194    
195   }
196   else if(protocol=='REST'){
197      
198       path="/api/"+serviceName+version;
199       
200
201   }
202    else if(protocol=='HTTP' || (protocol=="" && vm.msbRouteInfo.protocol=="PORTAL")){
203       var reg_customName_match=/^(\/.*?)$/;
204         if(!reg_customName_match.test(serviceName)) {
205      
206            path="/"+serviceName+version;
207         }
208         else{
209           path=serviceName+version;
210         }
211       
212
213    } 
214  
215
216  }
217
218  //get host
219  var  host;
220   if(vm.msbRouteInfo.host!=""){
221    host=vm.msbRouteInfo.host;
222  }
223  else{
224      if(vm.msbRouteInfo.namespace!="" && vm.msbRouteInfo.namespace!="default")   host=serviceName+"-"+vm.msbRouteInfo.namespace;
225      else host=serviceName;
226  }
227
228 //get publish port
229 var publish_port,publish_protocol;
230
231
232
233 //get publish URL
234
235   var routeWays=routeWay.split("|");
236   for(var i=0;i<routeWays.length;i++){
237     if(routeWays[i]=="ip"){
238
239       if(service.visualRange=="1"){
240         
241
242           if(protocol=='TCP'||protocol=='UDP'){
243               publish_protocol=protocol.toLowerCase();
244                publish_port=vm.msbRouteInfo.publish_port;       
245             }
246             else{
247               publish_protocol="http";
248                publish_port=10080; // service.nodes[0].port;         
249             }  
250
251          targetServiceUrlArray.push("<span> ns:"+namespace+" </span> "+publish_protocol+"://"+service.nodes[0].ip+":"+publish_port+path);
252       }
253       else{
254         var publishPorts=vm.msbRouteInfo.publish_port.split("|");
255         if(publishPorts.length==2){
256           // multiPublishPort: https|http
257           
258            targetServiceUrlArray.push("<span> ns:"+namespace+" </span> "+"https://"+service.nodes[0].ip+":"+publishPorts[0]+path);
259            targetServiceUrlArray.push("<span> ns:"+namespace+" </span> "+"http://"+service.nodes[0].ip+":"+publishPorts[1]+path);
260
261         }
262         else{
263            // single Port
264             
265
266             if(protocol=='TCP'||protocol=='UDP'){
267               publish_protocol=protocol.toLowerCase();
268               publish_port=vm.msbRouteInfo.publish_port;
269             }
270             else{
271                if(vm.msbRouteInfo.publish_port!=""){
272                 publish_port=vm.msbRouteInfo.publish_port;
273                 publish_protocol="https";
274              }
275              else{
276                 publish_port=80;  //service.nodes[0].port;
277                 publish_protocol="http";
278
279                 //https default:443
280                 var https_default_publish_port=443;
281                  targetServiceUrlArray.push("<span> ns:"+namespace+" </span> https://"+service.nodes[0].ip+path);
282               }
283             }
284
285
286           var portInfo= publish_port==80?"":":"+ publish_port;
287           targetServiceUrlArray.push("<span> ns:"+namespace+" </span> "+publish_protocol+"://"+service.nodes[0].ip+portInfo+path);
288         }
289
290       }
291
292  
293     }
294     else if(routeWays[i]=="domain"){
295       var domain=host+"."+routeSubdomain;
296
297       var publish_url;
298         if(vm.msbRouteInfo.path.trim()!="" && vm.msbRouteInfo.path.trim()!="/" ){
299           publish_url=vm.msbRouteInfo.path;
300         }
301         else{
302            if(vm.msbRouteInfo.url.trim()==path || vm.msbRouteInfo.url.trim()=="/"){
303              publish_url="/";
304            }
305            else{
306             publish_url=path;
307            }
308
309         }
310
311      if(protocol=='TCP'||protocol=='UDP'){       
312      
313         publish_port=vm.msbRouteInfo.publish_port;
314         publish_protocol=protocol.toLowerCase();
315       } 
316       else
317       {
318        
319         publish_protocol="http";
320         publish_port=80;//service.nodes[0].port;     
321         targetServiceUrlArray.push("<span> ns:"+namespace+" </span> https://"+domain+publish_url);
322    
323       }
324
325        
326
327         var portInfo= publish_port==80?"":":"+ publish_port ;        
328         targetServiceUrlArray.push("<span> ns:"+namespace+" </span> "+publish_protocol+"://"+domain+portInfo+publish_url);
329     }
330
331   }
332
333
334    return targetServiceUrlArray;
335
336    
337 }
338
339 routeUtil.changeTargetServiceUrl=function(){
340
341 vm.targetServiceUrl4SysOut="";
342 vm.targetServiceUrl4SysIn="";
343 vm.publishUrl.ifShowPublishUrl4SysOut=false;
344 vm.publishUrl.ifShowPublishUrl4SysIn=false;
345 if(routeUtil.showApiGateWayAddress()==false) return;
346
347
348
349
350 $("input[name='chkVisualRange']:checked").each(function(){
351      
352
353    if($(this).val()==0) 
354    {
355    // targetAllServiceUrlArray.push("["+$.i18n.prop('org_onap_msb_route_form_intersystem')+"]");
356        var targetAllServiceUrlArray=[];
357        vm.publishUrl.ifShowPublishUrl4SysOut=true;
358     
359        for(var i=0;i<vm.routeService.length;i++){
360          if(routeUtil.checkTargetServiceUrl(vm.routeService[i])){
361             var targetServiceUrlArray = routeUtil.buildTargetServiceUrl(vm.routeService[i],vm.msbRouteInfo.namespace);
362
363             if(targetServiceUrlArray.length>0){
364                routeUtil.concatArray(targetAllServiceUrlArray,targetServiceUrlArray);
365             }
366          }
367        }
368      
369      
370        //vm.targetServiceUrl+="(ns:"+vm.msbRouteInfo.namespace+") 未匹配到对应apigateway<br>"; 
371     
372
373    for(var i=0;i<vm.routeService_all.length;i++){
374       if(routeUtil.checkTargetServiceUrl(vm.routeService_all[i])){
375         var targetServiceUrlArray = routeUtil.buildTargetServiceUrl(vm.routeService_all[i],"all");
376
377          if(targetServiceUrlArray.length>0){
378                routeUtil.concatArray(targetAllServiceUrlArray,targetServiceUrlArray);
379             }
380      }
381    }
382
383     vm.publishUrl.publishUrl4SysOut=targetAllServiceUrlArray.unique().join("<br>");
384      
385
386    }
387    else if($(this).val()==1){
388      var targetAllServiceUrlArray=[];
389      vm.publishUrl.ifShowPublishUrl4SysIn=true;
390     //targetAllServiceUrlArray.push("["+$.i18n.prop('org_onap_msb_route_form_insystem')+"]");
391    
392        for(var i=0;i<vm.apiGatewayService.length;i++){
393          if(routeUtil.checkTargetServiceUrl(vm.apiGatewayService[i])){
394             var targetServiceUrlArray = routeUtil.buildTargetServiceUrl(vm.apiGatewayService[i],vm.msbRouteInfo.namespace);
395             if(targetServiceUrlArray.length>0){
396                routeUtil.concatArray(targetAllServiceUrlArray,targetServiceUrlArray);
397             }
398          }
399        }
400      
401
402   for(var i=0;i<vm.apiGatewayService_all.length;i++){
403     if(routeUtil.checkTargetServiceUrl(vm.apiGatewayService_all[i])){
404         var targetServiceUrlArray =routeUtil.buildTargetServiceUrl(vm.apiGatewayService_all[i],"all");
405         if(targetServiceUrlArray.length>0){
406                routeUtil.concatArray(targetAllServiceUrlArray,targetServiceUrlArray);
407         }
408      }
409    }
410
411      vm.publishUrl.publishUrl4SysIn=targetAllServiceUrlArray.unique().join("<br>");
412      
413    }
414
415
416  
417
418
419 });
420
421
422   
423 }
424
425 routeUtil.getQueryString=function(url,name){
426  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
427     var r = url.match(reg); 
428     if (r != null) return unescape(r[2]); 
429     return null; 
430       
431 }
432
433
434 routeUtil.concatArray=function(parentArray,childArray){
435  
436  for (var i=0; i < childArray.length; i++) {
437     parentArray.push(childArray[i]);
438   }
439
440 }