Divide the MSB source codes into two repos
[msb/apigateway.git] / 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_onap_msb_route_property_ttl')+title,
22                 message: message+"        "
23                         },{
24                                 type: type
25                         });
26 }
27
28 routeUtil.cutString=function(str){
29   var newStr;
30   if(str.length>22){
31      newStr=str.substring(0,20)+"...";
32   }
33   else{
34     newStr=str;
35   }
36
37   return newStr;
38 }
39
40
41 routeUtil.refreshRoute=function(){
42      $(".stats_box .routeDiv div[data-name='route_click_zone']").on({
43         click: function(){
44         $(".stats_box .routeDiv").removeClass("active");
45         $(this).parent().addClass("active");
46         }
47       });
48
49      $(".form-title a").click(function(){
50          $(this).parent().parent().next().collapse('toggle');
51         });
52
53         $('.collapseContent').on('show.bs.collapse', function () {
54           var icon = $(this).prev().find('i:first');
55          icon.removeClass('fa-chevron-down').addClass("fa-chevron-up");
56         });
57
58         $('.collapseContent').on('hidden.bs.collapse', function () {
59           var icon = $(this).prev().find('i:first');
60           icon.removeClass('fa-chevron-up').addClass("fa-chevron-down");
61         });
62
63
64 }
65
66 routeUtil.groupRouteByPort=function(resp){
67    var routeArray=new Array();
68    var routeGroupArray=[[]];
69     for(var i=0;i<resp.length;i++){ 
70
71           var publish_port=resp[i].publish_port;
72           
73
74             //Loop all packet classification
75             if(routeArray[publish_port] == undefined){  
76                 var list = [];  
77                 list.push(resp[i]);  
78                 routeArray[publish_port] = list;  
79             }else{  
80                 routeArray[publish_port].push(resp[i]);  
81             }  
82
83         }
84
85         //Sorting through all quantity > 1
86       
87            
88             for(var groupServiceName in routeArray){  
89             
90               if(groupServiceName==""){
91                 routeGroupArray[0]=routeArray[groupServiceName];
92               }
93               else{
94                  routeGroupArray.push(routeArray[groupServiceName]);
95               }
96                
97             }
98
99
100              for(var i=0;i<routeGroupArray.length;i++){
101           
102               routeGroupArray[i].sort(function(a,b){return a.serviceName>b.serviceName?1:-1}); 
103           }
104
105        
106
107         return routeGroupArray;
108
109 }
110
111 routeUtil.showGroupPort=function(index,serviceArray){
112    
113    var defaultPort=vm.route.routePort+" <span class='protocal'>http</span> / 443 <span class='protocal'>https</span>";
114
115    if(serviceArray[0]==null) {return defaultPort;}
116
117   var publish_port=serviceArray[0].publish_port
118
119    if(publish_port==""){ return defaultPort;}
120    else { return publish_port}
121
122 }
123
124 routeUtil.showPotocol=function(publish_port,publishProtocol){
125    
126    if(publish_port=="") return "";
127
128    return "<div class=\"protocal\" style=\"float:right\">"+publishProtocol+"</div>";
129
130 }
131
132
133
134 //Sorting grouping custom service
135 routeUtil.groupRoute=function(resp){
136     var routeArray=new Array();
137     var routeGroupArray=[[]];
138
139      for(var i=0;i<resp.length;i++){ 
140
141           var fullServiceName=resp[i].serviceName;
142           var groupServiceName;
143
144           var reg_match1=/(\/.*?)\/.*$/
145            var reg_match2=/(\/.*?)$/
146
147            //Canonical decomposition grouping service name
148           if(reg_match1.test(fullServiceName)){  
149             
150               groupServiceName = fullServiceName.match(reg_match1)[1];
151           }
152           else if(reg_match2.test(fullServiceName)){  
153             
154               groupServiceName = fullServiceName.match(reg_match2)[1];
155           }
156           else{
157             groupServiceName=$.i18n.prop('org_onap_msb_route_property_root');
158           }
159
160
161             //Loop all packet classification
162             if(routeArray[groupServiceName] == undefined){  
163                 var list = [];  
164                 list.push(resp[i]);  
165                 routeArray[groupServiceName] = list;  
166             }else{  
167                 routeArray[groupServiceName].push(resp[i]);  
168             }  
169
170         }
171
172            
173
174             //Sorting through all quantity > 1
175       
176            
177               for(var groupServiceName in routeArray){  
178                  var routeGroup = routeArray[groupServiceName]; 
179
180                  if(routeGroup.length>1){
181                    
182                     routeGroupArray.push(routeGroup);
183                     
184                 }
185                 else{
186                    routeGroupArray[0].push(routeGroup[0]); 
187                 }
188
189           }
190
191
192           //Sort + place other grouping in the final
193           var defaultGroupRoute=routeGroupArray[0];
194              defaultGroupRoute.sort(function(a,b){return a.serviceName>b.serviceName?1:-1}); 
195           for(var i=0;i<routeGroupArray.length-1;i++){
196             routeGroupArray[i]=routeGroupArray[i+1];
197             routeGroupArray[i].sort(function(a,b){return a.serviceName>b.serviceName?1:-1}); 
198           }
199
200          
201              routeGroupArray[routeGroupArray.length-1]=defaultGroupRoute;
202
203           
204
205
206         vm.customGroupRouteArray=routeGroupArray;
207
208
209  
210 }
211
212 routeUtil.showGroupName=function(index,serviceArray){
213
214   
215     var maxGroupSN=vm.customGroupRouteArray.length-1;
216     if(index==maxGroupSN){
217         return $.i18n.prop('org_onap_msb_route_property_other_group');
218     }
219     else{
220         var serviceName=serviceArray[0].serviceName;
221         var reg_match1=/(\/.*?)\/.*$/
222         var reg_match2=/(\/.*?)$/
223
224            //Canonical decomposition grouping service name
225           if(reg_match1.test(serviceName)){  
226             
227               return serviceName.match(reg_match1)[1];
228           }
229           else if(reg_match2.test(serviceName)){  
230             
231               return serviceName.match(reg_match2)[1];
232           }
233     }
234
235 }
236
237 routeUtil.currentTime=function()
238     { 
239         var now = new Date();
240        
241         var year = now.getFullYear();       //year
242         var month = now.getMonth() + 1;     //month
243         var day = now.getDate();            //date
244        
245         var hh = now.getHours();            //hour
246         var mm = now.getMinutes();          //minu
247        
248         var clock = year + "-";
249        
250         if(month < 10)
251             clock += "0";
252        
253         clock += month + "-";
254        
255         if(day < 10)
256             clock += "0";
257            
258         clock += day + " ";
259        
260         if(hh < 10)
261             clock += "0";
262            
263         clock += hh + ":";
264         if (mm < 10) clock += '0'; 
265         clock += mm; 
266         return clock; 
267     } 
268
269
270
271 routeUtil.showStatus=function(status){
272    if(status === '1'){
273       return " <span class='label label-success'>"+$.i18n.prop('org_onap_msb_route_property_normal')+"</span>";
274    }
275    else if(status === '0'){
276       return " <span class='label label-danger'>"+$.i18n.prop('org_onap_msb_route_property_disable')+"</span>";
277    }
278    else {
279       return " <span class='label label-info'>"+$.i18n.prop('org_onap_msb_route_property_unknown')+"</span>";
280    }
281
282
283 }
284
285 routeUtil.showVisualRange=function(visualRange){
286
287     var rangArray=visualRange.split("|");
288
289     var visualRangeText="";
290
291     for(var i=0;i<rangArray.length;i++){
292         if(rangArray[i] === '0'){
293               visualRangeText+= $.i18n.prop('org_onap_msb_route_form_intersystem')+"   ";
294            }
295          else if(rangArray[i] === '1'){
296               visualRangeText+= $.i18n.prop('org_onap_msb_route_form_insystem')+"   ";
297            }
298
299     }
300
301 return visualRangeText;
302
303 }
304
305
306
307
308 routeUtil.formatDetail=function(nodes){
309
310  var tableDetail='<table class="table table-striped hostTable">'
311                 +'<thead><tr><th>IP</th><th>PORT</th><th>TTL</th><th></th></tr></thead>'
312                 +' <tbody>';
313
314  for(var i=0;i<nodes.length;i++){ 
315         var node=nodes[i].split(":");
316         tableDetail+='<tr><td>'+node[0]+'</td><td>'+node[1]+'</td><td>'+node[2]+'</td><td></td>';
317  }                                     
318                                      
319                                    
320
321
322 tableDetail+=' </tbody></table>';
323 return tableDetail;
324
325 }
326
327 routeUtil.ifAPIUrl=function(url){
328   if(url=="" || url ==null) return false;
329     
330     var reg_api_match =new RegExp("^(\/"+apiRootPath+"\/.*?)$","im"); // re为/^\d+bl$/gim   
331    // var reg_api_match=/^(\/api\/.*?)$/;
332    return reg_api_match.test(url);
333       
334 }
335
336
337 routeUtil.changeTargetServiceUrl=function(){
338 var serviceName=vm.msbRouteInfo.serviceName==""?"serviceName":vm.msbRouteInfo.serviceName;
339
340   if(vm.msbRouteInfo.protocol=='UI'){
341     vm.targetFullServiceUrl=vm.targetServiceUrl+"/"+iuiRootPath+"/"+serviceName;
342   }
343   else if(vm.msbRouteInfo.protocol=='REST'){
344      var version=vm.msbRouteInfo.version==""?"":"/"+vm.msbRouteInfo.version
345        vm.targetFullServiceUrl=vm.targetServiceUrl+"/"+apiRootPath+"/"+serviceName+version;
346   }
347    else if(vm.msbRouteInfo.protocol=='HTTP'){
348      var reg_customName_match=/^(\/.*?)$/;
349         if(!reg_customName_match.test(serviceName)) serviceName="/"+serviceName;
350        vm.targetFullServiceUrl=vm.targetServiceUrl+serviceName;
351    }
352    
353   
354 }
355
356
357
358
359