msc_gen.py 14.3 KB
Newer Older
1 2 3
#!/usr/bin/python
# -*- coding: utf-8 -*-

4
# The aim of this script is to collect some traces from oai stack and generate a sequence diagram image (png or jpeg).
5 6 7
#
# It is supposed that a protocol name (MSC_NEW_STR) starts with...its name (RRC, MAC, NAS, S1AP, etc) then is followed by an underscore and whatever (RRC_UE,  RRC_eNB)
#   Like this it is possible to distinguish between PDUS, SDUs or other messages only by reading source ans destination
8 9 10 11 12 13 14

import sys
import subprocess
import re
import socket
import datetime
from datetime import date
15
import os, errno
gauthier's avatar
gauthier committed
16 17 18 19 20 21 22 23
import argparse




parser = argparse.ArgumentParser()
parser.add_argument("--diag_rlc_um", "-u", type=str,help="Try to find RLC protocol diagnostics", default="no")
parser.add_argument("--dir", "-d", type=str,help="Directory where msc logs can be found", default="/tmp")
24
parser.add_argument("--profile", "-p", type=str,help="E_UTRAN, EPC", default="EPC")
25 26 27
parser.add_argument("--no_message", "-M", type=str,help="Trace PDUs, not messages, SDUs", default="no")
parser.add_argument("--no_pdu", "-P", type=str,help="Trace messages, SDUs, not PDUs", default="no")
parser.add_argument("--no_event", "-E", type=str,help="Do not trace events", default="no")
gauthier's avatar
gauthier committed
28 29
args = parser.parse_args()

30 31 32
MSCGEN_OUTPUT_TYPE       = "png"
MAX_MESSAGES_PER_PAGE    = 36

33 34 35 36 37
MSC_NEW_STR              = '[PROTO]'
MSC_MSG_STR              = '[MESSAGE]'
MSC_BOX_STR              = '[EVENT]'


38
# This list is filled as follow : g_proto_names[module_id_int] = (proto_name), example RRC_UE
39
g_proto_names = []
40 41
# This list is filled as follow : g_proto_names[module_id_int] = (basename proto_name) example: RRC
g_proto_types = []
42 43 44 45 46 47



# list of messages
g_messages         = {}

48 49

# Display color of messages of sending entities
gauthier's avatar
gauthier committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
g_display_color  = ['\"teal\"',   # To check in msc.h: MSC_NAS_UE
                    '\"red\"',    # To check in msc.h: MSC_RRC_UE
                    '\"red\"',    # To check in msc.h: MSC_NAS_UE
                    '\"red\"',    # To check in msc.h: MSC_PDCP_UE
                    '\"red\"',    # To check in msc.h: MSC_RLC_UE
                    '\"red\"',    # To check in msc.h: MSC_MAC_UE
                    '\"red\"',    # To check in msc.h: MSC_PHY_UE
                    '\"indigo\"', # To check in msc.h: MSC_PHY_ENB
                    '\"indigo\"', # To check in msc.h: MSC_MAC_ENB
                    '\"indigo\"', # To check in msc.h: MSC_RLC_ENB
                    '\"indigo\"', # To check in msc.h: MSC_PDCP_ENB
                    '\"orange\"', # To check in msc.h: MSC_RRC_ENB
                    '\"black\"',  # To check in msc.h: MSC_IP_ENB
                    '\"black\"',  # To check in msc.h: MSC_S1AP_ENB
                    '\"black\"',  # To check in msc.h: MSC_GTPU_ENB
                    '\"black\"',  # To check in msc.h: MSC_GTPU_SGW
                    '\"black\"',  # To check in msc.h: MSC_S1AP_MME
                    '\"black\"',  # To check in msc.h: MSC_MMEAPP_MME
                    '\"black\"',  # To check in msc.h: MSC_NAS_MME
                    '\"black\"',  # To check in msc.h: MSC_NAS_EMM_MME
                    '\"black\"',  # To check in msc.h: MSC_NAS_ESM_MME
gauthier's avatar
gauthier committed
71 72 73 74 75 76 77 78 79 80 81 82
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"',  
                    '\"black\"'] 
83 84


gauthier's avatar
gauthier committed
85 86
# helper for diagnostic of RLC
g_diag_rlc_sn = {}
87 88 89 90

g_sequence_generator = 0


91 92
g_filenames = []
if "E_UTRAN" == args.profile.strip():
93 94 95
    g_filenames = [    
        args.dir+'/openair.msc.0.log',
        args.dir+'/openair.msc.1.log']
96 97
elif "EPC" == args.profile.strip():
    g_filenames = [        
98 99 100
        args.dir+'/openair.msc.2.log',
        args.dir+'/openair.msc.3.log',
        args.dir+'/openair.msc.4.log']
gauthier's avatar
gauthier committed
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
def sequence_number_generator():
    global g_sequence_generator
    l_seq = g_sequence_generator
    g_sequence_generator = g_sequence_generator + 1
    return l_seq

def file_is_empty(fpath):  
    return False if os.path.isfile(fpath) and os.path.getsize(fpath) > 0 else True

def parse_oai_log_files():
    global g_entities_dic
    global g_entities
    global g_messages
    global g_final_display_order_list
    #open TXT file that contain OAI filtered traces for mscgen

gauthier's avatar
gauthier committed
118 119
    # we may insert diagnostic events
    event_id_offset = 0
120
    for filename in g_filenames:
gauthier's avatar
gauthier committed
121 122
        if file_is_empty(filename):
            continue
123 124 125 126 127 128 129 130
        try:
            fhandle  = open(filename, 'r')
            fcontent = fhandle.read()
            fhandle.close()

            # split file content in lines
            lines = fcontent.splitlines()
            for line in lines:
131
                print ("INPUT LINE:  %s " % line)
gauthier's avatar
gauthier committed
132
                if line.strip() != ""  and not line.strip().startswith('#'):
133
                    partition = line.split(' ',3)
gauthier's avatar
gauthier committed
134
                    event_id = int(partition[0]) + event_id_offset
135 136 137 138 139 140 141
                    event_type = partition[1]
                    entity_id = int(partition[2])
                    if MSC_NEW_STR == event_type:
                        entity_name = partition[3]
                        if len(g_proto_names) <= entity_id:
                            for i in range(len(g_proto_names),(entity_id +1)):
                                g_proto_names.append("NotDeclared")
142
                                g_proto_types.append("NotDeclared")
143
                        g_proto_names[entity_id] = entity_name
144 145 146 147 148
                        partition_name1 = entity_name.split('_',1);
                        partition_name2 = partition_name1[0].split('-',1);
                        partition_name3 = partition_name2[0].split();
                        g_proto_types[entity_id] = partition_name3
                        print ("entity name %s , entity type %s" % (g_proto_names[entity_id],g_proto_types[entity_id]) )
149 150
                    # if line is a trace of a message between 2 protocol entities or layers
                    elif MSC_MSG_STR == event_type:
gauthier's avatar
gauthier committed
151
                        #print ("partition[3]:%s" % partition[3])
152 153 154 155 156 157 158 159 160 161 162 163 164
                        sub_partition = partition[3].split(' ',4)
                        arrow   = sub_partition[0]
                        entity2_id = int(sub_partition[1])
                        mac     = int(sub_partition[2])
                        time    = sub_partition[3]
                        message = sub_partition[4]
                        Message = {}
                        Message['mac'] = mac
                        Message['time'] = time
                        Message['message'] = message
                        Message['line_color'] = g_display_color[entity_id]
                        Message['text_color'] = g_display_color[entity_id]
                        if arrow == '<-':
gauthier's avatar
gauthier committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
                            if "yes" == args.diag_rlc_um.strip() and "DATA SN " in message and "RB UM " in message:
                                rlc_key = re.match(r"[^[]*\[([^]]*)\]", message).groups()[0]
                                sn_info = message.partition("SN")[2]
                                if sn_info.strip() != "":
                                    sn_str = sn_info.strip().partition(" ")[0]                                
                                    sn = int(sn_str)
                                    if rlc_key in g_diag_rlc_sn:
                                        previous_sn = g_diag_rlc_sn[rlc_key]
                                        if (previous_sn + 1) % 1024 != sn:
                                            print ("DIAG missing SN:  %s " % line)  
                                            MessageDiag = {}
                                            MessageDiag['type'] = "box"
                                            MessageDiag['tx'] = entity_id
                                            MessageDiag['rx'] = entity_id
                                            MessageDiag['discarded'] = False
                                            MessageDiag['time'] = time
                                            MessageDiag['message'] = "Missing SN "+ sn_str + " detected"
                                            MessageDiag['line_color'] = '\"red\"'
                                            MessageDiag['text_color'] = '\"red\"'
                                            g_messages[event_id + event_id_offset] = MessageDiag
                                            event_id_offset = event_id_offset + 1                       
                                    g_diag_rlc_sn[rlc_key] = int(sn)
187 188 189 190
                            Message['type'] = "rx"
                            Message['tx'] = entity2_id
                            Message['rx'] = entity_id
                            Message['discarded'] = False
gauthier's avatar
gauthier committed
191 192
                            g_messages[event_id + event_id_offset] = Message

193 194 195 196 197
                        elif arrow == '->':
                            Message['type'] = "tx"
                            Message['tx'] = entity_id
                            Message['rx'] = entity2_id
                            Message['discarded'] = False
gauthier's avatar
gauthier committed
198
                            g_messages[event_id + event_id_offset] = Message
199 200 201 202 203
                        elif arrow == 'x-':
                            Message['type'] = "rx"
                            Message['tx'] = entity2_id
                            Message['rx'] = entity_id
                            Message['discarded'] = True
gauthier's avatar
gauthier committed
204
                            g_messages[event_id + event_id_offset] = Message
205 206 207 208 209
                        elif arrow == '-x':
                            Message['type'] = "tx"
                            Message['tx'] = entity_id
                            Message['rx'] = entity2_id
                            Message['discarded'] = True
gauthier's avatar
gauthier committed
210
                            g_messages[event_id + event_id_offset] = Message
211 212 213 214 215 216 217 218 219 220 221 222 223 224

                    elif MSC_BOX_STR == event_type:
                        sub_partition = partition[3].split(' ',1)
                        time    = sub_partition[0]
                        message = sub_partition[1]
                        Message = {}
                        Message['type'] = "box"
                        Message['tx'] = entity_id
                        Message['rx'] = entity_id
                        Message['discarded'] = False
                        Message['time'] = time
                        Message['message'] = message
                        Message['line_color'] = g_display_color[entity_id]
                        Message['text_color'] = g_display_color[entity_id]
gauthier's avatar
gauthier committed
225
                        g_messages[event_id + event_id_offset] = Message
226 227

        except IOError, e:  
gauthier's avatar
gauthier committed
228
            print ("File %s INPUT LINE:  %s " % (filename, line))
gauthier's avatar
gauthier committed
229
            print 'err message'
230

231
    #print("------------------------------------")
232 233 234
    #print ("  %s " % ( g_messages ) )

#    for event_id_int in sorted(g_messages.iterkeys()):
235 236 237 238 239 240 241 242 243



def msc_chart_write_header(fileP):
    global g_final_display_order_list
    fileP.write("msc {\n")
    fileP.write("width = \"2048\";\n")

    entity_line_list_str = ''
gauthier's avatar
gauthier committed
244
    #print ("  %s " % ( g_proto_names ) )
245
    for entity in g_proto_names:
gauthier's avatar
gauthier committed
246 247
        if entity != 'NotDeclared':
            entity_line_list_str = entity_line_list_str + ' ' + entity + ','
248 249

    entity_line_list_str = entity_line_list_str.rstrip().strip(',')
gauthier's avatar
gauthier committed
250
    fileP.write("  %s;\n" % (entity_line_list_str))
251 252 253


def msc_chart_write_footer(fileP):
254
    fileP.write("\n}\n")
255 256 257 258 259 260 261 262 263

def msc_chart_generate(file_nameP):
    global  MSCGEN_OUTPUT_TYPE
    print "Generating sequence diagram for ",file_nameP
    command_line = "mscgen -T " + MSCGEN_OUTPUT_TYPE + " -i " + file_nameP
    fi,fo,fe=os.popen3(command_line)
    for i in fe.readlines():
        print "error:",i

264
def get_new_file_descriptor():
265 266 267 268
    global g_base_file_name
    global g_page_index
    l_file_name = g_base_file_name + str(g_page_index)+'.txt'
    l_file = open(l_file_name, "wb")
269
    print "Generating mscgen input file ",l_file_name;
270 271 272 273
    return l_file


###### MAIN STAR HERE #################
274
parse_oai_log_files()
275 276 277 278

g_page_index    = 0
g_message_index = 0
g_now = datetime.datetime.now()
knopp's avatar
knopp committed
279
g_now_formated = 'mscgen_' + args.profile.strip() + '_'+ g_now.strftime("%Y-%m-%d_%H.%M.%S")
gauthier's avatar
gauthier committed
280 281
#g_currentdir = os.curdir
g_resultdir = os.path.join(args.dir, g_now_formated)
282 283 284
os.mkdir(g_resultdir)
os.chdir(g_resultdir)

knopp's avatar
knopp committed
285
g_base_file_name = args.profile.strip() + '_mscgen_page_'
286

287
g_file = get_new_file_descriptor()
288 289
msc_chart_write_header(g_file)

290 291 292
for event_id_int in sorted(g_messages.iterkeys()):
    message = g_messages[event_id_int]
    if 'tx' in message['type']:
293 294 295 296 297 298
        if "yes" == args.no_message.strip():
            if g_proto_types[message['tx']] != g_proto_types[message['rx']]:
                continue
        if "yes" == args.no_pdu.strip():
            if g_proto_types[message['tx']] == g_proto_types[message['rx']]:
                continue   
299
        g_file.write("  %s=>%s [ label = \"(%d|%s) %s\", linecolour=%s , textcolour=%s ] ;\n" % (g_proto_names[message['tx']], g_proto_names[message['rx']], event_id_int, message['time'], message['message'], message['line_color'], message['text_color']))
300

301
    elif 'rx' in message['type']:
302 303 304 305 306 307
        if "yes" == args.no_message.strip():
            if g_proto_types[message['tx']] != g_proto_types[message['rx']]:
                continue
        if "yes" == args.no_pdu.strip():
            if g_proto_types[message['tx']] == g_proto_types[message['rx']]:
                continue   
308
        g_file.write("  %s<=%s [ label = \"(%d|%s) %s\", linecolour=%s , textcolour=%s ] ;\n" % (g_proto_names[message['rx']], g_proto_names[message['tx']], event_id_int, message['time'], message['message'], message['line_color'], message['text_color']))
309

310
    elif 'box' in message['type']:
311 312
        if "yes" == args.no_event.strip():
            continue  
313
        g_file.write("  %s note %s [ label = \"%s\", textcolour=%s ] ;\n" % (g_proto_names[message['tx']], g_proto_names[message['rx']], message['message'], message['text_color']))
314 315 316 317 318 319 320 321 322

    g_message_index = g_message_index + 1

    if ((g_message_index % MAX_MESSAGES_PER_PAGE) == 0):
        msc_chart_write_footer(g_file)
        g_file.close()
        msc_chart_generate(g_file.name)
        g_page_index = g_page_index + 1

323
        g_file = get_new_file_descriptor()
324 325 326 327 328 329
        msc_chart_write_header(g_file)


msc_chart_write_footer(g_file)
g_file.close()
msc_chart_generate(g_file.name)