c010506a30b1dd385546840a814995d8d2365291
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / resources / iui-route / js / routeUtil.js
1 /*
2  * Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
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_openo_msb_route_property_ttl')+title,
22                 message: message+"        "
23                         },{
24                                 type: type
25                         });
26 }
27
28
29 routeUtil.refreshRoute=function(){
30      $(".stats_box .routeDiv div[data-name='route_click_zone']").on({
31         click: function(){
32         $(".stats_box .routeDiv").removeClass("active");
33         $(this).parent().addClass("active");
34         }
35       });
36
37      $(".form-title a").click(function(){
38          $(this).parent().parent().next().collapse('toggle');
39         });
40
41         $('.collapseContent').on('show.bs.collapse', function () {
42           var icon = $(this).prev().find('i:first');
43          icon.removeClass('fa-chevron-down').addClass("fa-chevron-up");
44         });
45
46         $('.collapseContent').on('hidden.bs.collapse', function () {
47           var icon = $(this).prev().find('i:first');
48           icon.removeClass('fa-chevron-up').addClass("fa-chevron-down");
49         });
50
51
52 }
53
54 //Sorting grouping custom service
55 routeUtil.groupRoute=function(resp){
56     var routeArray=new Array();
57     var routeGroupArray=[[]];
58
59      for(var i=0;i<resp.length;i++){ 
60
61           var fullServiceName=resp[i].serviceName;
62           var groupServiceName;
63
64           var reg_match1=/(\/.*?)\/.*$/
65            var reg_match2=/(\/.*?)$/
66
67            //Canonical decomposition grouping service name
68           if(reg_match1.test(fullServiceName)){  
69             
70               groupServiceName = fullServiceName.match(reg_match1)[1];
71           }
72           else if(reg_match2.test(fullServiceName)){  
73             
74               groupServiceName = fullServiceName.match(reg_match2)[1];
75           }
76           else{
77             groupServiceName=$.i18n.prop('org_openo_msb_route_property_root');
78           }
79
80
81             //Loop all packet classification
82             if(routeArray[groupServiceName] == undefined){  
83                 var list = [];  
84                 list.push(resp[i]);  
85                 routeArray[groupServiceName] = list;  
86             }else{  
87                 routeArray[groupServiceName].push(resp[i]);  
88             }  
89
90         }
91
92            
93
94             //Sorting through all quantity > 1
95       
96            
97               for(var groupServiceName in routeArray){  
98                  var routeGroup = routeArray[groupServiceName]; 
99
100                  if(routeGroup.length>1){
101                    
102                     routeGroupArray.push(routeGroup);
103                     
104                 }
105                 else{
106                    routeGroupArray[0].push(routeGroup[0]); 
107                 }
108
109           }
110
111
112           //Sort + place other grouping in the final
113           var defaultGroupRoute=routeGroupArray[0];
114              defaultGroupRoute.sort(function(a,b){return a.serviceName>b.serviceName?1:-1}); 
115           for(var i=0;i<routeGroupArray.length-1;i++){
116             routeGroupArray[i]=routeGroupArray[i+1];
117             routeGroupArray[i].sort(function(a,b){return a.serviceName>b.serviceName?1:-1}); 
118           }
119
120          
121              routeGroupArray[routeGroupArray.length-1]=defaultGroupRoute;
122
123           
124
125
126         vm.customGroupRouteArray=routeGroupArray;
127
128
129  
130 }
131
132 routeUtil.showGroupName=function(index,serviceArray){
133
134   
135     var maxGroupSN=vm.customGroupRouteArray.length-1;
136     if(index==maxGroupSN){
137         return $.i18n.prop('org_openo_msb_route_property_other_group');
138     }
139     else{
140         var serviceName=serviceArray[0].serviceName;
141         var reg_match1=/(\/.*?)\/.*$/
142         var reg_match2=/(\/.*?)$/
143
144            //Canonical decomposition grouping service name
145           if(reg_match1.test(serviceName)){  
146             
147               return serviceName.match(reg_match1)[1];
148           }
149           else if(reg_match2.test(serviceName)){  
150             
151               return serviceName.match(reg_match2)[1];
152           }
153     }
154
155 }
156
157 routeUtil.currentTime=function()
158     { 
159         var now = new Date();
160        
161         var year = now.getFullYear();       //year
162         var month = now.getMonth() + 1;     //month
163         var day = now.getDate();            //date
164        
165         var hh = now.getHours();            //hour
166         var mm = now.getMinutes();          //minu
167        
168         var clock = year + "-";
169        
170         if(month < 10)
171             clock += "0";
172        
173         clock += month + "-";
174        
175         if(day < 10)
176             clock += "0";
177            
178         clock += day + " ";
179        
180         if(hh < 10)
181             clock += "0";
182            
183         clock += hh + ":";
184         if (mm < 10) clock += '0'; 
185         clock += mm; 
186         return clock; 
187     } 
188
189
190
191 routeUtil.showStatus=function(status){
192    if(status === '1'){
193       return " <span class='label label-success'>"+$.i18n.prop('org_openo_msb_route_property_normal')+"</span>";
194    }
195    else if(status === '0'){
196       return " <span class='label label-danger'>"+$.i18n.prop('org_openo_msb_route_property_disable')+"</span>";
197    }
198    else {
199       return " <span class='label label-info'>"+$.i18n.prop('org_openo_msb_route_property_unknown')+"</span>";
200    }
201
202
203 }
204
205 routeUtil.showVisualRange=function(visualRange){
206
207     var rangArray=visualRange.split("|");
208
209     var visualRangeText="";
210
211     for(var i=0;i<rangArray.length;i++){
212         if(rangArray[i] === '0'){
213               visualRangeText+= $.i18n.prop('org_openo_msb_route_form_intersystem')+"   ";
214            }
215          else if(rangArray[i] === '1'){
216               visualRangeText+= $.i18n.prop('org_openo_msb_route_form_insystem')+"   ";
217            }
218
219     }
220
221 return visualRangeText;
222
223 }
224
225
226
227
228 routeUtil.formatDetail=function(nodes){
229
230  var tableDetail='<table class="table table-striped hostTable">'
231                 +'<thead><tr><th>IP</th><th>PORT</th><th>TTL</th><th></th></tr></thead>'
232                 +' <tbody>';
233
234  for(var i=0;i<nodes.length;i++){ 
235         var node=nodes[i].split(":");
236         tableDetail+='<tr><td>'+node[0]+'</td><td>'+node[1]+'</td><td>'+node[2]+'</td><td></td>';
237  }                                     
238                                      
239                                    
240
241
242 tableDetail+=' </tbody></table>';
243 return tableDetail;
244
245 }
246
247 routeUtil.ifAPIUrl=function(url){
248   if(url=="" || url ==null) return false;
249     
250     var reg_api_match =new RegExp("^(\/"+apiRootPath+"\/.*?)$","im"); // re为/^\d+bl$/gim   
251    // var reg_api_match=/^(\/api\/.*?)$/;
252    return reg_api_match.test(url);
253       
254 }
255
256
257 routeUtil.changeTargetServiceUrl=function(){
258 var serviceName=vm.msbRouteInfo.serviceName==""?"serviceName":vm.msbRouteInfo.serviceName;
259
260   if(vm.msbRouteInfo.protocol=='UI'){
261     vm.targetFullServiceUrl=vm.targetServiceUrl+"/"+iuiRootPath+"/"+serviceName;
262   }
263   else if(vm.msbRouteInfo.protocol=='REST'){
264     if(routeUtil.ifAPIUrl(vm.msbRouteInfo.url)){
265       var version=vm.msbRouteInfo.version==""?"":"/"+vm.msbRouteInfo.version
266        vm.targetFullServiceUrl=vm.targetServiceUrl+"/"+apiRootPath+"/"+serviceName+version;
267     }
268     else{
269        var reg_customName_match=/^(\/.*?)$/;
270         if(!reg_customName_match.test(serviceName)) serviceName="/"+serviceName;
271        vm.targetFullServiceUrl=vm.targetServiceUrl+serviceName;
272
273     }
274   }
275 }
276
277
278
279
280