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