diff --git a/default_sh_user_data.xml b/default_sh_user_data.xml index 928acd4..062d48c 100644 --- a/default_sh_user_data.xml +++ b/default_sh_user_data.xml @@ -24,6 +24,9 @@ {{ Sh_template_vars['scscf'] }} {{ Sh_template_vars['imsUserState'] }} + + {{ Sh_template_vars['inboundCommunicationBarred'] }} + {{ Sh_template_vars['outboundCommunicationBarred'] }} diff --git a/lib/diameter.py b/lib/diameter.py index 1892703..2e45074 100755 --- a/lib/diameter.py +++ b/lib/diameter.py @@ -18,6 +18,7 @@ import re from baseModels import Peer, OutboundData import pydantic_core +import xml.etree.ElementTree as ET class Diameter: @@ -338,7 +339,14 @@ def generate_diameter_packet(self, packet_version, packet_flags, packet_command_ except Exception as e: self.logTool.log(service='HSS', level='error', message=f"[diameter.py] [generate_diameter_packet] Exception: {e}", redisClient=self.redisMessaging) - + def get_sh_profile_rules(self, serviceName, xmlRoot, xmlNamespace): + service = xmlRoot.find(f'default:{serviceName}', xmlNamespace) + if service is not None: + active = service.get('active') + allow = service.find('.//default:allow', xmlNamespace) + allowValue = allow.text if allow is not None else None + return active, allowValue + return None, None def roundUpToMultiple(self, n, multiple): return ((n + multiple - 1) // multiple) * multiple @@ -2772,6 +2780,7 @@ def Answer_16777217_306(self, packet_vars, avps): msisdn = None imsi = None scscf = None + subscriberIsBarred = False username = None try: user_identity_avp = self.get_avp_data(avps, 700)[0] @@ -2853,9 +2862,36 @@ def Answer_16777217_306(self, packet_vars, avps): #These variables are passed to the template for use subscriber_details['mnc'] = self.MNC.zfill(3) subscriber_details['mcc'] = self.MCC.zfill(3) + subscriberShProfile = subscriber_details.get('sh_profile', '') + if not subscriberShProfile: + subscriberShProfile = subscriber_details.get('xcap_profile', '') + + subscriber_details['inboundCommunicationBarred'] = False + subscriber_details['outboundCommunicationBarred'] = False + + try: + subscriberShXml = ET.fromstring(subscriberShProfile) + namespaces = { + 'default': 'http://uri.etsi.org/ngn/params/xml/simservs/xcap', + 'cp': 'urn:ietf:params:xml:ns:common-policy' + } + incomingCommunicationBarringRuleActive, incomingCommunicationBarringAllowed = self.get_sh_profile_rules('incoming-communication-barring') + outgoingCommunicationBarringRuleActive, outgoingCommunicationBarringAllowed = self.get_sh_profile_rules('outgoing-communication-barring') + + if incomingCommunicationBarringRuleActive: + if not incomingCommunicationBarringAllowed: + subscriber_details['inboundCommunicationBarred'] = True + + if outgoingCommunicationBarringRuleActive: + if not outgoingCommunicationBarringAllowed: + subscriber_details['outboundCommunicationBarred'] = True + + except Exception as e: + self.logTool.log(service='HSS', level='debug', message="Unable to parse Sh Profile XML for subscriber: " + str(subscriber_details), redisClient=self.redisMessaging) self.logTool.log(service='HSS', level='debug', message="Rendering template with values: " + str(subscriber_details), redisClient=self.redisMessaging) xmlbody = template.render(Sh_template_vars=subscriber_details) + avp += self.generate_vendor_avp(702, "c0", 10415, str(binascii.hexlify(str.encode(xmlbody)),'ascii')) avp += self.generate_avp(268, 40, "000007d1") #DIAMETER_SUCCESS