OpenSER and Asterisk Question?

We have setup up a multi tier installation of openSER and asterisk.The first tier consist of openSER and asterisk installed on the same box and the second tier is a bank of three asterisk servers. The objective is in the first tier using openSER and asterisk to determine to which asterisk server on the second tier the calls will finally terminate.

Current functioning of the system is as follows, the call reach open ser and it’s transferred to asterisk residing on the same box, asterisk using IVR determine where the final destination of the call and transfer it to the asterisk box in tier two however the asterisk in tier one continues to stays in the path of the call.

i,e the incoming calls from openser is routed to Asterisk PBX on the same box, say a SIP call with 10 digit URI is routed to Asterisk PBX on Tier 1 for IVR

openser.cfg – snippet 1

            if (uri=~"^sip:[0-9]{10}@.*") {
                  rewritehostport("127.0.0.1:5065");#Asterisk Box 1in Tier1
                  t_relay();
                  exit;
            }

extensions.conf of Tier 1 Asterisk Box

[default]

exten => _X.,1,Ringing()
exten => _X.,1,Background(Play message)

exten => XXX,1,AGI(Script to decide where the call has to be routed) ;
3 digit extension entered by callee
exten => XXX,route,Dial(SIP/openser/*XXX) ; this will route to Asterisk Box 2
exten => XXX,hang,Hangup

Here callee enters 3 digit extension based on this extension we decide where the call has to be routed to Asterisk PBX’s in Tier 2. Say the call should be routed to Asterisk Box 2 in Tier 2. What I do is Dial the OpenSER
with extension *XXX which redirects to Asterisk Box 2 as shown in
extension.conf snippet above.

this is the code used in openser.cfg to route call to Asterisk Box 2

    if (uri=~"^sip:\*[0-9]{3}@") {
         strip(1);
         rewritehostport("192.168.1.8:5060"); #Asterisk Box 2 in ier 2
         t_relay();
         exit;
    }

The problem when I do this is the New channel is routed to Asterisk PBX
2 (A call is bridged from Asterisk Box 1 to Asterisk Box 2). By this the traffic is passed through Asterisk Box 1.

We would like to know if we can configure the current setup to be able
to hand off the call and for the asterisk on tier one to stay out of the loop from that point onwards?

This is what my observation on working with asterisk and OpenSER.
OpenSER is smart enough to route calls to Asterisk Box using rewritehostport. But when I want to route a channel from Asterisk to
OpenSER I have Dial OpenSER URI, by this we are creating a channel in
Box 1 which need to eliminated (i,e I want to transfer channel .from Asterisk Box 1 to Asterisk Box 2)

Thanks and looking for your reply and suggestions.