The following optimization rules need to be put in place:
For an optimization to an Extension between 2 subscribers of the same numbering plan, the skip defined on the number definition of the input number is applied to return the extension.
Any other optimization requires that both the input number and the output party's number are fully qualified numbers. There are 2 methods for optimization from here on:
For an optimization to a National or Subscriber number (public numbers), the local toll table of the number definition of the party that will receive the display number (the 'to' number) will be used if available. This will ensure that the public optimized number is presented according to the rules defined for the office code of the receiving subscriber or endpoint.
Any other optimization requires a number definition for at least one of the parties  so not only the 'to' party. The components of the number definition can then be used to find out whether country codes, area codes and even local office codes match.
Note:
If a direct optimization of input and output fails because both numbers have different NPIs (e.g. input is public and output is private), the number conversion table will be consulted to try a number optimization with the converted number.
Performing optimization of numbers given a 'from' and a 'to' number.
This is an internal method that performs optimization of numbers given a 'from' and a 'to' number.
If the request is to optimize to an extension between 2 subscribers of the same numbering plan, then the number definition of the 'from' party is sufficient to create the extension provided this number definition does provide a positive skip (a skip of 0 means that no extension can be created with this number definition). This is explained in the Optimize To Extension bullet below.
The remainder of the optimization logic works on fully qualified (private or public) numbers only. For this reason, the 'to' number is given to the GetFQN API. If no fully qualified number can be found for the 'to' number, then no optimization can be done.
In order to compare 'from' and 'to' for optimization, it must be guaranteed that both numbers are at the same type of number when the comparison starts. It may be necessary to find a matching number conversion entry to change the 'to' number from a private number to a public number or vice versa from a public number to a private number. If no number conversion entry could be found, the optimization of the 'from' number based on the 'to' number failed and the optimization logic needs to check whether an optimization using the 'from' number can be done. This is explained in the Optimize With Different NPIs bullet below.
Last but not least a number definition entry is required to be available, either for the 'from' party, the 'to' party or both the 'from' and the 'to' party. Based on which number definition(s) are found, the optimization shall be based on the 'from' and/or the 'to' number definition.
Figure 1. Get Dialable Number API
Optimize to Extension
Finding the number definition for the 'from' number and if there's a skip defined on this definition, the extension number is calculated with this skip and returned.
Optimize with Different NPIs
The following is done when converting the NPI of the 'to' number to the NPI of the 'from' number fails:
For a request for optimization to extension, the possibility exists that the 'from' number can be converted to get the 'from' and the 'to' on the same NPI. If this succeeds, it is checked whether the 'from' and the 'to' can be optimized all the way to an extension. To do this, the number definitions of both 'from' and 'to' number need to be obtained. If the 'from' number does not have a skip defined, then optimization to extension fails. If a skip is defined and code 2, code 1 and code 0 match exactly for both 'from' and 'to' definition, then the extension number can be calculated with the defined skip on the 'from' and returned.
If the 'from' number is public and the 'to' number is private, it is possible that the 'from' number can be optimized using a local toll table defined on the 'to' number's number definition. See also bullet below on Optimize Based on 'from' and Local Toll Table.
Else no optimization is possible at this point.
Optimize Based on 'from' Number Definition
If TON_OPT is Subscriber, L0 or Extension and the 'to' number starts with code2+code1 of the 'from' number, then the 'from' number is modified to the L0 or Subscriber number based on the NPI of the 'from' number.
If TON_OPT is National, Subscriber, L1 or L0 and the 'to' number starts with code2 of the 'from' number then the 'from' number is the L1 or National number based on the NPI of the 'from' number.
Else, no optimization is possible.
Optimize Based on 'to' Number Definition
If a local toll table is attached to the 'to' definition, then the local toll table is used to optimize the 'from' number (see Optimize Based on 'from' and Local Toll Table below). The 'from' number is then the result of this optimization.
If TON_OPT is Subscriber, L0 or Extension and the 'from' number starts with code2+code1 of the 'to' number, then code2+code1 are stripped from the 'from' number and the TON is set to L0 or Subscriber based on the NPI of the 'from' number.
If TON_OPT is National, Subscriber, L1, L0 or Extension and the 'from' number starts with code2 of the 'to' number, then code2 is stripped from the 'from' number and the TON is set to L1 or National based on the NPI of the 'from' number.Else, no optimization is possible.
Optimize Based on 'from' and 'to' number definition
If a local toll table is attached to the 'to' definition, then the local toll table is used to optimize the 'from' number (see Optimize Based on 'from' and Local Toll Table below). The 'from' number is then the result of this optimization.
If TON_OPT is Subscriber, L0 or Extension and code2 and code1 of the 'from' and the 'to' number match, then code2+code1 are stripped from the 'from' number and the TON is set to L0 or Subscriber based on the NPI of the 'from' number.
If TON_OPT is National, Subscriber, L1, L0 or Extension and code2 of the 'from' and 'to' number match, then code2 is stripped from the 'from' number and the TON is set to L1 or National based on the NPI of the 'from' number.
Else, no optimization is possible.
Optimize Based on 'from' and Local Toll Table
If the 'from' number is not a public number then optimization using the local toll table is not possible.
If TON_OPT is National and the 'from' number starts with the country code recorded in the local toll table entry then the country code is stripped from the 'from' number and the TON is set to National.
If TON_OPT is Subscriber and the 'from' number starts with the country code and one of the area codes recorded in the local toll table entry then a lookup is done whether any of the local office codes recorded for the matching are code is matching in the 'from' number.
If a match is found then the country code is removed from the 'from' number. The Area code is removed as well if the Include AC flag is not set. The TON of the 'from' number is set to Subscriber.
If no match is found then the country code is removed from the 'from' number and the TON is set to National.
Else, no optimization is possible.
DNM Modifications Optimization Rule/Optimized TON
Table 1. DNM Modifications Optimization Rule
Optimized  OAPN  Input TON  OUTPUT TON  Optimize TON 
False  Don't care  False  Don't care  NONE 
True  False  ANY  Don't care  EXT 

 ALL/INT  INT  NAT 
ALL/NAT  NAT  SUB 
ALL/SUB  SUB  EXT 
ALL/L2  L2  L1 
ALL/L1  L1  L0 
ALL/L0  L0  EXT 
True  True  Don't care  Don't care  EXT 
Maximum Optimization
The Maximum optimization level applies to a modification rule. This replaces the previous rules that just allowed optimization one level down or optimization all the way to an extension
Parent topic: Display Number Modifications Table