Dimitrios Mavrommatis | c53923a | 2017-12-03 19:48:48 -0800 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | # coding: utf8 |
| 3 | #Copyright © 2016 - 2017 Copyright (c) Sprint, Inc. and others. All rights reserved. |
| 4 | # |
| 5 | #This program and the accompanying materials are made available under the |
| 6 | #terms of the Eclipse Public License v1.0 which accompanies this distribution, |
| 7 | #and is available at http://www.eclipse.org/legal/epl-v10.html |
| 8 | |
| 9 | import signal |
| 10 | import sys |
| 11 | import zmq |
| 12 | import struct |
| 13 | import socket as socketlib |
| 14 | import datetime |
| 15 | import time |
| 16 | import random |
| 17 | import thread |
| 18 | from multiprocessing.pool import ThreadPool |
| 19 | |
| 20 | pool = ThreadPool(processes=1) |
| 21 | |
| 22 | conflict = False |
| 23 | topicId = None |
| 24 | #nodeId = "node3" |
| 25 | #networkId = "network4" |
| 26 | nodeId = "node"+sys.argv[1] |
| 27 | networkId = "network"+sys.argv[2] |
| 28 | toSend = sys.argv[3] |
| 29 | source = random.randrange(0,65535) |
| 30 | topicId = random.randrange(4,255) |
| 31 | |
| 32 | def signal_handler(signal, frame): |
| 33 | print "\nExiting... Sending DPN Status Indication message with Status = GOODBYE" |
| 34 | pub_socket.send("%s" % (struct.pack("!BBBBIB",2,12,topicId,2,source,len(nodeId)) + nodeId + struct.pack('!B',len(networkId)) + networkId)) |
| 35 | count = 0 |
| 36 | while True: |
| 37 | time.sleep(1) |
| 38 | sys.stdout.write("\r"+str(5-count)+" ") |
| 39 | sys.stdout.flush() |
| 40 | count += 1 |
| 41 | if(count > 5): |
| 42 | print "\n" |
| 43 | sys.exit(0) |
| 44 | |
| 45 | signal.signal(signal.SIGINT, signal_handler) |
| 46 | |
| 47 | def sendAssignId(pub_socket): |
| 48 | global conflict |
| 49 | global topicId |
| 50 | time.sleep(1) |
| 51 | pub_socket.send("%s" % (struct.pack('!BBBIB',1,10,topicId,source,len(nodeId)) + nodeId + struct.pack('!B',len(networkId)) + networkId)) |
| 52 | count = 0 |
| 53 | while True: |
| 54 | time.sleep(1) |
| 55 | sys.stdout.write("\r"+str(9-count)+" ") |
| 56 | sys.stdout.flush() |
| 57 | count += 1 |
| 58 | if conflict: |
| 59 | conflict = False |
| 60 | sendAssignId(pub_socket) |
| 61 | return |
| 62 | if count > 9: |
| 63 | break |
| 64 | print "\nDPN Topic = ", topicId |
| 65 | print "Node Id = ", nodeId |
| 66 | print "Network Id = ", networkId |
| 67 | print "Source Id = ", source |
| 68 | print "Sending Hello Message . . ." |
| 69 | pub_socket.send("%s" % (struct.pack("!BBBBIB",2,12,topicId,1,source,len(nodeId)) + nodeId + struct.pack('!B',len(networkId)) + networkId)) |
| 70 | |
| 71 | print "Ready to receive messages. Press Ctrl+C when ready to exit." |
| 72 | |
| 73 | rec_port = "5560" |
| 74 | send_port = "5559" |
| 75 | # Socket to talk to server |
| 76 | context = zmq.Context() |
| 77 | socket = context.socket(zmq.SUB) |
| 78 | pub_socket = context.socket(zmq.PUB) |
| 79 | socket.connect ("tcp://localhost:%s" % rec_port) |
| 80 | pub_socket.connect("tcp://localhost:%s" % send_port) |
| 81 | topicfilter = "" |
| 82 | controller_topic= 252 |
| 83 | socket.setsockopt(zmq.SUBSCRIBE, topicfilter) |
| 84 | print "Listening to port ", rec_port |
| 85 | print "DPN Lifecycle start up . . . Please wait." |
| 86 | |
| 87 | async_result = pool.apply_async(sendAssignId,(pub_socket,)) |
| 88 | |
| 89 | count = 0 |
| 90 | msgnum1count = 0 |
| 91 | msgnum2count = 0 |
| 92 | msgnum3count = 0 |
| 93 | msgnum4count = 0 |
| 94 | msgnum5count = 0 |
| 95 | msgnum6count = 0 |
| 96 | for update_nbr in range(900000): |
| 97 | string = socket.recv() |
| 98 | ts = time.time() |
| 99 | st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') |
| 100 | |
| 101 | topic,msgnum = struct.unpack('!BB',string[:2]) |
| 102 | |
| 103 | if topic == 1 and msgnum == 10: #Assign_Id |
| 104 | top,msg,topId,src,nodeIdLen = struct.unpack('!BBBIB',string[:8]) |
| 105 | top,msg,topId,src,nodeIdLen,nodeId1,networkIdLen = struct.unpack('!BBBIB'+str(nodeIdLen)+'sB',string[:8+nodeIdLen+1]) |
| 106 | top,msg,topId,src,nodeIdLen,nodeId1,networkIdLen,networkId1 = struct.unpack('!BBBIB'+str(nodeIdLen)+'sB'+str(networkIdLen)+'s',string[:8+nodeIdLen+1+networkIdLen]) |
| 107 | #print nodeId1, networkId1 |
| 108 | if src != source and (topId == topicId or nodeId1 == nodeId): |
| 109 | pub_socket.send("%s" % struct.pack('!BBBIBsBs',1,11,topicId,source,len(nodeId),nodeId,len(networkId),networkId)) |
| 110 | continue |
| 111 | |
| 112 | elif topic == 1 and msgnum == 11: #Assign_Conflict |
| 113 | top,msg,topId,src,nodeIdLen = struct.unpack('!BBBIB',string[:8]) |
| 114 | top,msg,topId,src,nodeIdLen,nodeId1,networkIdLen = struct.unpack('!BBBIB'+str(nodeIdLen)+'sB',string[:8+nodeIdLen+1]) |
| 115 | top,msg,topId,src,nodeIdLen,nodeId1,networkIdLen,networkId1 = struct.unpack('!BBBIB'+str(nodeIdLen)+'sB'+str(networkIdLen)+'s',string[:8+nodeIdLen+1+networkIdLen]) |
| 116 | |
| 117 | if src != source: |
| 118 | if(nodeId == nodeId1): |
| 119 | print "Received assign conflict for node id. Change the node id and restart this script." |
| 120 | exit(0) |
| 121 | if(networkId == networkId1): |
| 122 | print "Received assign conflict for network id. Change the network id and restart this script." |
| 123 | exit(0) |
| 124 | if(top == topicId): |
| 125 | print "Received assign conflict for topic id. Generating new topic id and resending Assign Topic Id Message." |
| 126 | topicId = random.randrange(0,255) |
| 127 | conflict = True |
| 128 | continue |
| 129 | |
| 130 | elif topic == topicId and msgnum == 13: |
| 131 | top, msg, controller_topic, controller_status = struct.unpack('!BBBB',string[:4]) |
| 132 | print "DPN Status ACK received. Controller Topic = ",controller_topic |
| 133 | continue |
| 134 | |
| 135 | elif topic == 3 and msgnum == 14: |
| 136 | top, msg, controller_topic, controller_status = struct.unpack('!BBBB',string[:4]) |
| 137 | if controller_status == 1: |
| 138 | print "Received controller Hello. Controller Topic = ",controller_topic |
| 139 | print "Sending Hello To Controller that has a topic id of ", controller_topic |
| 140 | pub_socket.send("%s" % (struct.pack("!BBBBIB",controller_topic,12,topicId,1,source,len(nodeId)) + nodeId + struct.pack('!B',len(networkId)) + networkId)) |
| 141 | |
| 142 | elif controller_status == 2: |
| 143 | print "Received controller Goodbye. Controller Topic = ",controller_topic |
| 144 | |
| 145 | if topic != topicId: |
| 146 | continue |
| 147 | print 'Receiving message', count, ' at ', st, ' ..... ' |
| 148 | count += 1 |
| 149 | print 'length of message = ', len(string) |
| 150 | print 'topic =', topic |
| 151 | print 'msgnum =', msgnum |
| 152 | |
| 153 | if msgnum == 1: |
| 154 | msgnum1count += 1 |
| 155 | topic,msgnum, imsi, default_ebi, ue_ip, s1u_sgw_gtpu_teid, s1u_sgw_gtpu_ipv4, sessionid, ctopic, cid, opid = struct.unpack('!cBQBLLLQBLL',string[:40]) |
| 156 | print 'imsi = ', imsi |
| 157 | ipa = socketlib.inet_ntoa(struct.pack('!L',ue_ip)) |
| 158 | print 'ue_ip = ', ipa |
| 159 | print 'default_ebi = ', default_ebi |
| 160 | s1u_sgw_gtpu_ipv4a = socketlib.inet_ntoa(struct.pack('!L',s1u_sgw_gtpu_ipv4)) |
| 161 | print 's1u_sgw_gtpu_ipv4 = ', s1u_sgw_gtpu_ipv4a |
| 162 | print 's1u_sgw_gtpu_teid = ', s1u_sgw_gtpu_teid |
| 163 | print 'sessionid = ', sessionid |
| 164 | print 'controller topic = ', ctopic |
| 165 | print 'cid = ', cid |
| 166 | print 'opid = ', opid |
| 167 | responsedata = struct.pack('!BBBLL',controller_topic,4, 16, cid, opid) |
| 168 | if toSend == "true": |
| 169 | pub_socket.send("%s" % (responsedata)) |
| 170 | #uncomment the following lines to send a DDN for every create session message |
| 171 | #time.sleep(5) |
| 172 | #pub_socket.send("%s" % (struct.pack('!BBQLLB'+str(len(nodeId))+'sB'+str(len(networkId))+'s',controller_topic,5,sessionid,cid,opid,len(nodeId),nodeId,len(networkId),networkId))) |
| 173 | |
| 174 | elif msgnum == 2: |
| 175 | msgnum2count += 1 |
| 176 | topic, msgnum, s1u_enb_gtpu_ipv4, s1u_enb_gtpu_teid, s1u_sgw_gtpu_ipv4, sessionid, ctopic, cid, opid = struct.unpack("!cBLLLQBLL",string[:31]) |
| 177 | s1u_enb_gtpu_ipv4a = socketlib.inet_ntoa(struct.pack('!L',s1u_enb_gtpu_ipv4)) |
| 178 | print 's1u_enb_gtpu_ipv4 = ', s1u_enb_gtpu_ipv4a |
| 179 | print 'dl s1u_enb_gtpu_teid = ', s1u_enb_gtpu_teid |
| 180 | print 'dl s1u_sgw_gtpu_ipv4 = ', socketlib.inet_ntoa(struct.pack('!L',s1u_sgw_gtpu_ipv4)) |
| 181 | print 'sessionid = ', sessionid |
| 182 | print 'controller topic = ', ctopic |
| 183 | print 'cid = ', cid |
| 184 | print 'opid = ', opid |
| 185 | responsedata = struct.pack('!BBBLL',controller_topic,4, 16, cid, opid) |
| 186 | if toSend == "true": |
| 187 | pub_socket.send("%s" % (responsedata)) |
| 188 | |
| 189 | elif msgnum == 3: |
| 190 | msgnum3count += 1 |
| 191 | topic, msgnum, sessionid, ctopic, cid, opid = struct.unpack("!cBQBLL",string[:19]) |
| 192 | print 'sessionid = ', sessionid |
| 193 | print 'controller topic = ', ctopic |
| 194 | print 'cid = ', cid |
| 195 | print 'opid = ', opid |
| 196 | responsedata = struct.pack('!BBBLL',controller_topic,4, 0, cid, opid) |
| 197 | if toSend == "true": |
| 198 | pub_socket.send("%s" % (responsedata)) |
| 199 | |
| 200 | elif msgnum == 6: |
| 201 | if(len(string)==14): |
| 202 | #topic,msgnum,bufduration,bufcount,controller_topic,cid,opid = struct.unpack('!BBBHBLL',string[:14]) |
| 203 | topic,msgnum,controller_topic,cid,opid = struct.unpack('!BBBLL',string[:11]) |
| 204 | #print "dl-buffering-duration",bufduration |
| 205 | #print "dl-buffering-suggested-count",bufcount |
| 206 | print "Controller Topic = ",controller_topic |
| 207 | print "Client id = ", cid |
| 208 | print "Op Id = ", opid |
| 209 | |
| 210 | elif msgnum == 17: |
| 211 | print "-------------------------------------------------------------" |
| 212 | print "ADC Rule received. Details:" |
| 213 | topic,msgnum,selector_type = struct.unpack('!BBB',string[:3]) |
| 214 | |
| 215 | #Domain |
| 216 | if(selector_type == 0): |
| 217 | domain_name_length, = struct.unpack('!B',string[3:4]) |
| 218 | domain_name, = struct.unpack('!'+str(domain_name_length)+'s',string[4:4+int(domain_name_length)]) |
| 219 | next_index = 4+int(domain_name_length) |
| 220 | print "Domain Name = ",domain_name |
| 221 | |
| 222 | #IP Address |
| 223 | if(selector_type == 1 or selector_type == 2): |
| 224 | ip_address, = struct.unpack('!L',string[3:7]) |
| 225 | ip_addressa = socketlib.inet_ntoa(struct.pack('!L',ip_address)) |
| 226 | next_index = 7 |
| 227 | print "IP Address = ",ip_addressa |
| 228 | |
| 229 | #IP Prefix |
| 230 | if selector_type == 2: |
| 231 | ip_prefix, = struct.unpack('!H',string[7:9]) |
| 232 | next_index += 2 |
| 233 | print "IP Prefix = ",ip_prefix |
| 234 | |
| 235 | #rule_id, = struct.unpack('!L',string[rule_id_index:rule_id_index+4]) |
| 236 | #print "Rule Id = ", rule_id |
| 237 | |
| 238 | #rating_group,service_id,sponsor_id_length = struct.unpack('!LLB', string[rule_id_index+4:rule_id_index+4+9]) |
| 239 | drop,rating_group,service_id,sponsor_id_length = struct.unpack('!BLLB', string[next_index:next_index+10]) |
| 240 | print "Drop = ", drop |
| 241 | print "Rating Group = ", rating_group |
| 242 | print "Service Id = ", service_id |
| 243 | #print "Sponsor Length = ", sponsor_id_length |
| 244 | #sponsor_id, = struct.unpack('!'+str(sponsor_id_length)+'s',string[rule_id_index+4+9:rule_id_index+4+9+int(sponsor_id_length)]) |
| 245 | sponsor_id, = struct.unpack('!'+str(sponsor_id_length)+'s',string[next_index+10:next_index+10+int(sponsor_id_length)]) |
| 246 | print "Sponsor = ", sponsor_id |
| 247 | print "-------------------------------------------------------------" |
| 248 | #precedence, = struct.unpack('!L',string[rule_id_index+4+9+int(sponsor_id_length):rule_id_index+4+9+int(sponsor_id_length)+4]) |
| 249 | #print "precedence = ", precedence |
| 250 | |
| 251 | print '================' |
| 252 | print 'Total = ', count, 'msgnum1 count', msgnum1count, 'msgnum2 count', msgnum2count, 'msgnum3 count', msgnum3count, 'msgnum4 count', msgnum4count,'msgnum5 count', msgnum5count, 'msgnum6 count', msgnum6count |
| 253 | socket.close() |