Skip to content
This repository has been archived by the owner on Sep 26, 2018. It is now read-only.

subscriber client based changes: #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ of patent rights can be found in the PATENTS file in the same directory.
<!-- This is a free_sms so we don't need to pass a destination_number to VBTS_Get_Service_Tariff -->
<action inline="true" application="set" data='call_tariff=${python(VBTS_Get_Service_Tariff ${service_type}|sms|)}'/>

<!-- Get the IMSIs status -->
<action inline="true" application="set" data='vbts_account_status=${python(VBTS_Get_Account_Status ${from_imsi})}'/>
<action inline="true" application="set" data='vbts_dest_account_status=${python(VBTS_Get_Account_Status ${vbts_destid}|True)}'/>
<action inline="true" application="set" data='vbts_incoming_account_status=${python(VBTS_Get_Account_Status ${vbts_destid}|True)}'/>

<!-- Determine if the dest number is local -->
<action inline="true" application="python" data='endaga_camped ${vbts_canonical_tp_dest_address}'/>
<action inline="true" application="set" data="endaga_islocal=${_openbts_ret}"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
Copyright (c) 2016-present, Facebook, Inc.
All rights reserved.

This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
-->

<extension name="number_status">
<condition field='${vbts_account_status}' expression="False">
<action application="python" data="endaga_i18n Failed to deliver the message! Your account is blocked or expired, Please contact your provider."/>
<action application="python" data="VBTS_Send_SMS ${vbts_callerid}|103|${_localstr}"/>
<action application="set" data="service_type=error_sms"/>
<action application="system" data='curl --data "from_number=${vbts_callerid}&from_name=${from_imsi}&service_type=${service_type}&destination=${vbts_tp_dest_address}" http://127.0.0.1/smscdr'/>
</condition>
</extension>

<extension name="called_number_status">
<condition field='${vbts_dest_account_status}' expression="False">
<action application="python" data="endaga_i18n Failed to deliver the message! The receiver account has expired."/>
<action application="python" data="VBTS_Send_SMS ${vbts_callerid}|103|${_localstr}"/>
<action application="set" data="service_type=error_sms"/>
<action application="system" data='curl --data "from_number=${vbts_callerid}&from_name=${from_imsi}&service_type=${service_type}&destination=${vbts_tp_dest_address}" http://127.0.0.1/smscdr'/>
<action application="hangup"/>
</condition>
</extension>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ of patent rights can be found in the PATENTS file in the same directory.
<action inline="true" application="set" data='vbts_destid=${python(VBTS_Get_IMSI_From_Number ${canonical_destination_number})}'/>
<!-- Get the caller's account balance, 'from_imsi' is an IMSI -->
<action inline="true" application="set" data='vbts_account_balance=${python(VBTS_Get_Account_Balance ${from_imsi})}'/>
<!--Get IMSIs Status -->
<action inline="true" application="set" data='vbts_account_status=${python(VBTS_Get_Account_Status ${from_imsi})}'/>
<action inline="true" application="set" data='vbts_dest_account_status=${python(VBTS_Get_Account_Status ${vbts_destid}|True)}'/>
<action inline="true" application="set" data='endaga_uuid=${system /usr/local/bin/snowflake}'/>
<action inline="true" application="set" data='endaga_uuid=${endaga_uuid:0:36}'/>
<action inline="true" application="set" data='endaga_apikey=${python(endaga_config_get endaga_token)}'/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
Copyright (c) 2016-present, Facebook, Inc.
All rights reserved.

This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
-->

<extension name="number_status">
<condition field='${vbts_callerid}' expression="^\d+$"/>
<condition field='${vbts_account_status}' expression="False">
<action application="set" data='service_type=error_call'/>
<action application="export" data='service_type=error_call'/>
<action application="python" data="endaga_i18n Failed to connect! Your account is blocked or expired, Please contact your provider."/>
<action application="set" data='res=${python(VBTS_Send_SMS ${vbts_callerid}|103|${_localstr})}'/>
<action application="answer"/>
<action application="speak" data='flite|slt|${_localstr}'/>
<action application="hangup"/>
</condition>
</extension>

<extension name="called_number_status">
<condition field='${vbts_dest_account_status}' expression="False">
<action application="hangup"/>
</condition>
</extension>
1 change: 1 addition & 0 deletions client/core/checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def process_config(self, config_dict):
@delta.DeltaCapable(section_ctx['subscribers'], True)
def process_subscribers(self, data_dict):
subscriber.process_update(data_dict)
subscriber.status(update=data_dict)

def process_events(self, data_dict):
"""Process information about events.
Expand Down
5 changes: 3 additions & 2 deletions client/core/config_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ def set_defaults(force_replace=False):
'external_interface': 'tun0',
# The internal interface is the NIC used by the BSC/BTS to address this
# system
'internal_interface': 'lo'

'internal_interface': 'lo',
# Network Max Permissible Transaction
'network_mput': 3

}
config = ConfigDB()
Expand Down
2 changes: 1 addition & 1 deletion client/core/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def usage(num=100):
def kind_from_reason(reason_str):
types = ["local_call", "local_sms", "outside_call", "outside_sms",
"free_call", "free_sms", "incoming_sms", "error_sms",
"error_call", "transfer", "add_money", "deduct_money",
"error_call", "error_transfer", "transfer", "add_money", "deduct_money",
"set_balance", "unknown", "Provisioned", "local_recv_call",
"local_recv_sms", "incoming_call", "gprs"]
for t in types:
Expand Down
96 changes: 96 additions & 0 deletions client/scripts/freeswitch/VBTS_Get_Account_Status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""Get a subscriber's account balance via their IMSI.

Copyright (c) 2016-present, Facebook, Inc.
All rights reserved.

This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
"""

from freeswitch import consoleLog

from core.subscriber import subscriber
from core.subscriber.base import SubscriberNotFound


def chat(message, args):
"""Handle chat requests.

Args:
string of the form <imsi>|<True if for dest_imsi (default is False)>

Subscriber State can be:
active (unblocked), -active (blocked),first_expired (validity expired)
"""
args = args.split('|')
imsi = args[0]
dest_imsi = False

if len(args) > 1:
dest_imsi = True
if len(imsi) < 4: # Toll Free Numbers don't have imsis
subscriber_state = 'active'
else:
subscriber_state = str(
subscriber.status().get_account_status(imsi)).lower()
else:
subscriber_state = str(
subscriber.status().get_account_status(imsi)).lower()
try:
account_status = False
if not dest_imsi:
if 'active' == subscriber_state:
account_status = True
else:
# incoming number status
allowed_states = ['active', 'active*',
'first_expired', 'first_expired*']
if subscriber_state in allowed_states:
account_status = True

except SubscriberNotFound:
account_status = False
consoleLog('info', "Returned Chat:" + str(account_status) + "\n")
message.chat_execute('set', '_openbts_ret=%s' % account_status)


def fsapi(session, stream, env, args):
"""Handle FS API requests.

Args:
string of the form <imsi>|<True if for dest_imsi (default is False)>

Subscriber State can be:
active (unblocked), -active (blocked),first_expired (validity expired)
"""
args = args.split('|')
imsi = args[0]
dest_imsi = False

if len(args) > 1:
dest_imsi = True
if len(imsi) < 4: # Toll Free Numbers don't have imsis
subscriber_state = 'active'
else:
subscriber_state = str(
subscriber.status().get_account_status(imsi)).lower()
else:
subscriber_state = str(
subscriber.status().get_account_status(imsi)).lower()
try:
account_status = False
if not dest_imsi:
if 'active' == subscriber_state:
account_status = True
else:
# incoming number status
allowed_states = ['active', 'active*',
'first_expired', 'first_expired*']
if subscriber_state in allowed_states:
account_status = True

except SubscriberNotFound:
account_status = False
consoleLog('info', "Returned FSAPI: " + str(account_status) + "\n")
stream.write(str(account_status))
12 changes: 12 additions & 0 deletions cloud/endagaweb/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,17 @@
'task': 'endagaweb.tasks.usageevents_to_sftp',
# Run this at 15:00 UTC (10:00 PDT, 02:00 Papua time)
'schedule': crontab(minute=0, hour=17),
},'unblock-blocked-subscriber': {
'task': 'endagaweb.tasks.unblock_blocked_subscribers',
# Run this in every minute
'schedule': crontab(minute='*'),
},'validity-expiry-sms': {
'task': 'endagaweb.tasks.validity_expiry_sms',
# Run this at 14:00 UTC (09:00 PDT, 01:00 Papua time).
'schedule': crontab(minute=0, hour=15),
},'subscriber-validity-state': {
'task': 'endagaweb.tasks.subscriber_validity_state',
# Run this prior to vacuuming of subscribers.
'schedule': crontab(minute=58, hour=16),
}
})
38 changes: 35 additions & 3 deletions cloud/endagaweb/checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from endagaweb.models import TimeseriesStat
from endagaweb.models import UsageEvent
from endagaweb.util.parse_destination import parse_destination

import dateutil.parser as dateparser

class CheckinResponder(object):

Expand Down Expand Up @@ -56,6 +56,7 @@ def __init__(self, bts):
'system_utilization': self.timeseries_handler,
'subscribers': self.subscribers_handler,
'radio': self.radio_handler, # needs location_handler -kurtis
'subscriber_status': self.subscriber_status_handler,
# TODO: (kheimerl) T13270418 Add location update information
}

Expand Down Expand Up @@ -264,19 +265,49 @@ def subscribers_handler(self, subscribers):
(imsi, ))
continue

def subscriber_status_handler(self, subscriber_status):
"""
Update the subscribers' state and validity info based on
what the client submits.
"""
for imsi in subscriber_status:
sub_info = json.loads(subscriber_status[imsi]['state'])
validity_now = str(sub_info['validity'])
state = str(sub_info['state'])
try:
sub = Subscriber.objects.get(imsi=imsi)
if sub.valid_through.date() < dateparser.parse(validity_now).date():
sub.state = 'active'
sub.valid_through = validity_now
if state == 'active*':
sub.is_blocked = True
evt_gen = UsageEvent.objects.filter(
kind='error_transfer').order_by('-date')[0]
sub.last_blocked = evt_gen.date
sub.save()
except Subscriber.DoesNotExist:
logging.warn('[subscriber_status_handler] subscriber %s does not'
' exist.' % imsi)


def radio_handler(self, radio):
if 'band' in radio and 'c0' in radio:
self.bts.update_band_and_channel(radio['band'], radio['c0'])

def gen_subscribers(self):
"""
Returns a list of active subscribers for a network, along with
PN-counter for each sub containing last known balance.
PN-counter for each sub containing last known balance and state.
"""
res = {}
for s in Subscriber.objects.filter(network=self.bts.network):
bal = crdt.PNCounter.from_state(json.loads(s.crdt_balance))
data = {'numbers': s.numbers_as_list(), 'balance': bal.state}
state = str(s.state)
if s.is_blocked:
# append '*' if subscriber is blocked, even if in active state
state = state + '*'
data = {'numbers': s.numbers_as_list(), 'balance': bal.state,
'state': state, 'validity': str(s.valid_through.date())}
res[s.imsi] = data
return res

Expand Down Expand Up @@ -389,6 +420,7 @@ def gen_config(self):
# pylint: disable=no-member
result['endaga']['number_country'] = self.bts.network.number_country
result['endaga']['currency_code'] = self.bts.network.subscriber_currency
result['endaga']['network_mput'] = self.bts.network.max_failure_transaction
# Get the latest versions available on each channel.
latest_stable_version = ClientRelease.objects.filter(
channel='stable').order_by('-date')[0].version
Expand Down
Loading