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