From 1c719f84f18b0ff48ba60cfab9156a08704f4ce7 Mon Sep 17 00:00:00 2001 From: davidkneipp Date: Fri, 23 Aug 2024 15:26:54 +1000 Subject: [PATCH 1/2] incoming and outgoing communication barring support in UDA --- default_sh_user_data.xml | 3 +++ lib/diameter.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/default_sh_user_data.xml b/default_sh_user_data.xml index 928acd4c..062d48c6 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 18927039..2e450745 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 From 63b01957f4498e82b4050c9c48c60307d43b2ff1 Mon Sep 17 00:00:00 2001 From: davidkneipp Date: Fri, 23 Aug 2024 15:45:47 +1000 Subject: [PATCH 2/2] Remove type declaration --- lib/diameter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/diameter.py b/lib/diameter.py index 2e450745..cde6767a 100755 --- a/lib/diameter.py +++ b/lib/diameter.py @@ -569,7 +569,7 @@ def getPeerType(self, originHost: str) -> str: except Exception as e: return '' - def getConnectedPeersByType(self, peerType: str) -> list[Peer]: + def getConnectedPeersByType(self, peerType: str) -> list: try: requestedPeerType = peerType.lower() peerTypes = ['mme', 'pgw', 'pcscf', 'icscf', 'scscf', 'hss', 'ocs', 'dra']