Routing script tutorial:route match Tutorial

From TBwiki
Jump to: navigation, search

Contents

route_match tutorial

'route_match' is a method implemented in the base_routing and is use to match a route to a call through parameters and/or status. This tutorial will explain how to use the parameter for the 'route_match' method.

route_match examples


Things to know about these examples:

  • A new field called 'priority' needs to be add to the routes.
  • A new field called 'asr_threshold' needs to be add to the routes.
  • A new field called 'usage_threshold' needs to be add to the routes.


route_match setup

Time to take a look how to match route to a call. List of routes use for the example:

 Calling      Called      NAP      Remapped NAP       Priority
 5550000     1110000     isdn       sip                4     
 5550000     2220000     sip        isdn               3     
 5550000     3330000     sip_p2     isdn_1             2     


List of naps:

 Name       Asr threshold       Usage threshold   
 isdn         40                 90     
 sip          52                 92     


Input used in the test script window:

 @call_params = {:calling => '5550000', :called => '5550002', :nap => 'isdn'}
 @nap_list = [
   {:name => 'sip', },
   {:name => 'isdn' },
   {:name => 'sip_p3' }
 ]

 :call_field_name and :route_field_name parameter


 :call_field_name and :route_field_name script example

Let's start with the :call_field_name and :route_field_name combo. The first example shows how to use the :call_field_name to match the calling number of the incoming call with the calling number of the route.

It will also try to find a route which has the same remapped nap parameter as the nap parameter of the incoming call, therefore ignoring the nap parameter of the route.

 require 'base_routing'
 
 class MatchTutorial < BaseRouting
   route_match :call_field_name => :calling
   route_match :call_field_name => :calling, :route_field_name => :calling
   route_match :call_field_name => :nap, :route_field_name => :remapped_nap
 
 end
 
 @@routing = MatchTutorial.new
 
 
 def init_routes( routes )
   @@routing.init routes
 end
 
 def route( call, nap_list )
   @@routing.route call, nap_list
 end


 :call_field_name and :route_field_name script output

If you look at the first 2 lines of the MatchTutorial class definition:

 route_match :call_field_name => :calling
 route_match :call_field_name => :calling, :route_field_name => :calling

Both lines do the exact same thing. The both try to match the calling number of the incoming call with the calling number of the routing call.

By default the if the :route_field_name is not explicitly declared, route_match will try to match the parameter from the :call_field_name to the same parameter in the route (in this case :calling). It is also important to notice that the :route_field_name parameter cannot be use without a :call_field_name parameter first.

It is also important to notice that the called number, the nap and the priority in the routes were ignored by our match rules.

The script will find a match. Here is the output:

 Call Params:
   Reason:        ok
   Called:        5550002
   Calling:       5550000
   Nap:           isdn
 Matched route:
   Remapped calling:    
   Called:        2220000
   Calling:       5550000
   Remapped called:    
   Priority:      3
   Remapped nap:  isdn
   Nap:           sip
 Output: OUT: Nap attribute must be remapped
 OUT: 


 :method and :proc parameter


Let's try the :method and :proc. The next example is a isdn-sip gateway.

It uses the one of the method contain in base_routing (:match_nap_availability), it will compare the :asr_statistics_struct_last_24h_asr_percent nap status with the :asr_threshold in the naps, and will also compare the :usage_percent nap status with the :usage_threshold.

The threshold and the status in this example were set so that a sip to isdn call will be dropped, but a isdn to sip call will be accepted.


 :method and :proc input

It is possible to set the nap status values in the test windows. Here is the input in the test window:

 @call_params = {:calling => '5550000', :called => '5550002', :nap => 'isdn'}
 @nap_list = [
   {:name => 'sip', :availability_percent => '100', :asr_statistics_struct_last_24h_asr_percent => '60', :usage_percent => '85'  },
   {:name => 'isdn', :availability_percent => '100', :asr_statistics_struct_last_24h_asr_percent => '40', :usage_percent => '100' },
   {:name => 'sip_p3', :availability_percent => '0', :asr_statistics_struct_last_24h_asr_percent => '10', :usage_percent => '90' }
 ]


 :method and :proc script example

 require 'base_routing'
 
 #
 #  This class requires that your add columns in the nap called asr_threshold and usage_threshold
 #
 class MatchProcMethodTutorial < BaseRouting
   route_match :call_field_name => :nap
   route_match :method => :match_nap_availability
   route_match :method => :match_nap_available_call
   route_match :proc => Proc.new { |route, call, nap_list|
     result = true
     #get the outgoing nap
     nap=nap_list[route[:remapped_nap].intern]
     puts "usage remapped nap #{nap[:name]}"
     if nap.nil?
       puts "Cannot found route with outgoing nap '#{route[:remapped_nap]}'"
       result = false
     end
 
     #get the last 24hrs asr
     asr_24hrs_status = nap[:asr_statistics_struct_last_24h_asr_percent].to_i
     asr_24hrs_threshold = nap[:asr_threshold].to_i
     puts "asr status #{asr_24hrs_status}, treshold#{asr_24hrs_threshold}"
     if asr_24hrs_status < asr_24hrs_threshold
       puts "Insufficient ASR in the last 24hrs for nap '#{route[:remapped_nap]}'"
        result = false
     end
     
     result
   }
 
   def match_nap_available_call(route, call, nap_list)
     #get the outgoing nap
     nap=nap_list[route[:remapped_nap].intern]
     puts "usage remapped nap #{nap[:name]}"
     if nap.nil?
       puts "Cannot found route with outgoing nap '#{route[:remapped_nap]}'"
       return false
     end
     
     # verify how many calls are available in the nap
     nap_usage_status = nap[:usage_percent].to_i
     nap_usage_threshold = nap[:usage_threshold].to_i
     puts "usage status #{nap_usage_status}, treshold#{nap_usage_threshold}"
     if nap_usage_status > nap_usage_threshold
       puts "Nap usage of '#{route[:remapped_nap]}' is higher then threshold '#{nap_usage_status}' "
       return false
     end
 
     true
 
   end
 
 end
 
 @@routing = MatchProcMethodTutorial.new
 
 def init_routes( routes )
   @@routing.init routes
 end
 
 def route( call, nap_list )
   @@routing.route call, nap_list
 end


 :method and :proc output

The gateway application queries every second the status and keep it available for routing script (due to the synchronous nature of the status query, it is preferable to have another thread polling the status than querying the status for each call).

The script uses 2 status available (:asr_statistics_struct_last_24h_asr_percent and :asr_threshold in the naps) in the naps, for a complete list of the status available go here in the NAP_STATS_FIELDS define.

If you check the NAP_STATS_FIELDS define, you will see that :asr_statistics_struct_last_24h_asr_percent is the combination of 2 field names asr_statistics_struct and last_24h_asr_percent.

You might have noticed that the block in the proc function did not use the return keyword, it is a rule from ruby where you cannot use the return keyword in a block.

Here is the output:

 Call Params:
   Reason:        ok
   Called:        5550002
   Calling:       5550000
   Nap:           isdn
 Matched route:
   Remapped calling:    
   Called:        1110000
   Calling:       5550000
   Remapped       called:    
   Priority:      4
   Remapped nap:  sip
   Nap:           isdn
 
 Output: OUT: usage remapped nap sip
 OUT:
 OUT: usage status 85, treshold92
 OUT:
 OUT: usage remapped nap sip
 OUT:
 OUT: asr status 60, treshold52
 OUT:
 OUT: Nap attribute must be remapped
 OUT:

Back to Routing Script Tutorial.

Personal tools