-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUE_feature_collection_loop.py
More file actions
222 lines (191 loc) · 6.76 KB
/
Copy pathUE_feature_collection_loop.py
File metadata and controls
222 lines (191 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from websocket import create_connection
import json
import time
import itertools
def imsi_id(j_res):
try:
imsi_list = []
ue_list=j_res['ue_list']
for ue in ue_list:
imsi=str(ue['imsi'])
imsi_list.append({'imsi':imsi})
return imsi_list
except Exception as ex:
print(ex)
def ue_bitrate(j_res, ran_id):
samples = {}
for i in range (0,len(j_res['ue_list'])):
ue = j_res['ue_list'][i]
imsi = ue['imsi']
values = [ue['ul_bitrate'],ue['dl_bitrate']]
samples[imsi] = values
return samples
def ue_stats(j_res, ran_id):
samples = {}
for i in range (0,len(j_res['ue_list'])):
ue = j_res['ue_list'][i]
imsi = ue['imsi']
values = []
values.append(ue['dl_rx_count'])
values.append(ue['dl_retx_count'])
values.append(ue['dl_err_count'])
values.append(ue['ul_tx_count'])
values.append(ue['ul_retx_count'])
samples[imsi] = values
return samples
def ue_pdn(j_res, ran_id):
samples = {}
for i in range (0,len(j_res['ue_list'])):
ue = j_res['ue_list'][i]
imsi = ue['imsi']
internet_count = 0
ims_count = 0
slice3_count = 0
slice4_count = 0
slice5_count = 0
slice6_count = 0
if 'pdn_list' in ue:
for pdn_session in ue['pdn_list']:
if pdn_session['apn'] == 'internet':
internet_count += 1
elif pdn_session['apn'] == 'ims':
ims_count += 1
elif pdn_session['apn'] == 'slice3':
slice3_count += 1
elif pdn_session['apn'] == 'slice4':
slice4_count += 1
elif pdn_session['apn'] == 'slice5':
slice5_count += 1
elif pdn_session['apn'] == 'slice6':
slice6_count += 1
else:
pass
slices = []
slices.append(internet_count)
slices.append(ims_count)
slices.append(slice3_count)
slices.append(slice4_count)
slices.append(slice5_count)
slices.append(slice6_count)
samples[imsi] = slices
return samples
def get_ran_id(j_res):
if j_res and j_res.get('global_enb_id'):
return str(j_res['global_enb_id']['enb_id'])
elif j_res and j_res.get('global_gnb_id'):
return str(j_res['global_gnb_id']['gnb_id'])
else:
return '-1'
def get_lte_cells(j_res):
cells = []
if j_res and j_res.get('cells'):
for key in j_res.get('cells'):
cells.append(key)
return cells
def get_nr_cells(j_res):
cells = []
if j_res and j_res.get('nr_cells'):
for key in j_res.get('nr_cells'):
cells.append(key)
return cells
def read_thread(enb_ip, ue_sample_stack):
ws = None
try:
ws = create_connection('ws://%s:9002' % enb_ip)
ws.recv()
ws.send('{"message":"config_get"}')
result = ws.recv()
j_res = json.loads(result)
ran_id = get_ran_id(j_res)
lte_cells = get_lte_cells(j_res)
nr_cells = get_nr_cells(j_res)
ws.send('{"message":"stats"}')
result = ws.recv()
j_res = json.loads(result)
ws.send('{"message":"ue_get" ,"stats":true}')
result = ws.recv()
j_res = json.loads(result)
timestamp = time.time()
# imsi_list = imsi_id(enb_ip)
# imsi_samples = {imsi:[] for imsi in imsi_list}
bit_rate_samples = ue_bitrate(j_res, ran_id)
stat_samples = ue_stats(j_res, ran_id)
pdn_samples = ue_pdn(j_res, ran_id)
list_of_samples = [pdn_samples, stat_samples, bit_rate_samples]
for k in set().union(*list_of_samples):
ue_sample_stack[k][timestamp] = list(itertools.chain(*[d.get(k) for d in list_of_samples]))
# imsi_samples = {
# k: {timestamp: list(itertools.chain(*[d.get(k) for d in list_of_samples]))}
# for k in set().union(*list_of_samples)
# }
# ue_sample_stack[timestamp] = imsi_samples
return ue_sample_stack
except Exception as e:
print('error here')
print(e)
print('enb @ %s is not connected !' % enb_ip)
if ws:
ws.shutdown
def read(enb_ip,ue_sample_stack):
ue_sample_stack = read_thread(enb_ip,ue_sample_stack)
# global enb_list
# for ip_i in enb_list:
# threading.Thread(target=read_thread,kwargs=dict(enb_ip=ip_i)).start()
return ue_sample_stack
def stack_ue_data(ue_samples, frame_size):
imsi = list(ue_samples.keys())[0]
start_timestamp = list(ue_samples[imsi].keys())[0]
stacked_features = {}
for ue in ue_samples.keys():
stacked_features[ue] = ([list(feature) for feature in list(ue_samples[ue].values())[0:frame_size]])
return start_timestamp, stacked_features
def data_collection_loop():
# Global var def (sort of) for enb ip
enb_ip = '10.10.4.188'
# Make an initial connection to establish the list of IMSIs
ws = None
try:
ws = create_connection('ws://%s:9002' % enb_ip)
ws.recv()
ws.send('{"message":"config_get"}')
result = ws.recv()
j_res = json.loads(result)
ran_id = get_ran_id(j_res)
lte_cells = get_lte_cells(j_res)
nr_cells = get_nr_cells(j_res)
ws.send('{"message":"stats"}')
result = ws.recv()
j_res = json.loads(result)
ws.send('{"message":"ue_get" ,"stats":true}')
result = ws.recv()
j_res = json.loads(result)
imsi_list = imsi_id(j_res)
except Exception as e:
print(e)
print('enb @ %s is not connected !' % enb_ip)
if ws:
ws.shutdown
ws.shutdown
# Empty dict for running UE samples
ue_sample_stack = {list(imsi.values())[0]:{} for imsi in imsi_list}
# Timing information
start_time = time.time()
current_time = time.time()
scenario_duration = 1800
measurment_window = 5
frame_size = 24
# Run for the duration of the scenario (defualt of 30 min)
while (time.time() - start_time) < scenario_duration:
if time.time() - current_time > measurment_window:
# Update vars
current_time = time.time()
ue_sample_stack = read(enb_ip, ue_sample_stack)
print(list(ue_sample_stack.values())[0])
if len(list(ue_sample_stack.values())[0]) > frame_size:
start_timestamp, stacked_features = stack_ue_data(ue_sample_stack, frame_size)
print(stacked_features)
for imsi in ue_sample_stack.keys():
ue_sample_stack[imsi].pop(next(iter(ue_sample_stack[imsi])))
return
if __name__ == '__main__':
data_collection_loop()