Fix: update the correct pre-built script to cmake-sonar.sh
[ric-app/mc.git] / mc-core / mc / data_gen / dc_gen.py
1 # -------------------------------------------------------------------------------
2 #    Copyright (c) 2018-2019 AT&T Intellectual Property.
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 # -------------------------------------------------------------------------------
16
17 import sys
18 import random
19 import time
20 import os
21 import datetime
22
23 import sgnb_reconfiguration_complete_pb2
24 import ue_context_release_pb2
25 import x2ap_streaming_pb2
26
27
28 # --------------------------------
29 #       Operating params
30 n_ue = 50
31 events_per_fl = 5
32 epoch_len = 1
33 ue_id_range = 100000
34
35 conn_flnm = "MT_000010280"
36 dis_flnm = "MT_000010050"
37
38 fifo_dir = "/tmp/mcl/fifos"
39
40 # -------------------------------
41 #       Create records
42
43 def process_conn_record(ofl,gnb_id, ue_id, ts):
44 #       print "Connected ue_iud="+str(ue_id)+", gnb_id="+str(gnb_id)
45         X2APStreaming = x2ap_streaming_pb2.X2APStreaming() # header message
46         X2APStreaming.header.gNbID.value = "Bar"
47         sec = ts / 1000
48         ms = ts % 1000
49         utc_time = datetime.datetime.utcfromtimestamp(sec)
50         diff = utc_time - datetime.datetime(1900, 1, 1, 0, 0, 0)
51         ntp_hi = diff.days*24*60*60+diff.seconds
52         ntp_lo = (ms << 32) / 1000
53         X2APStreaming.header.timestamp = ntp_hi << 32 | ntp_lo
54
55 #       root = sgnb_reconfiguration_complete_pb2.SgNBReconfigurationComplete()
56         root = X2APStreaming.sgNBReconfigurationComplete
57
58         root.id_MeNB_UE_X2AP_ID = ue_id
59         root.id_SgNB_UE_X2AP_ID = gnb_id
60         child = root.id_ResponseInformationSgNBReconfComp
61         child.success_SgNBReconfComp.meNBtoSgNBContainer.value = "foo"
62
63         v = X2APStreaming.SerializeToString()
64         vlen = len(v)
65         ofl.write("@MCL")
66         ofl.write("{0:7}".format(vlen))
67         ofl.write('\0')
68         ofl.write(str(ts))
69         ofl.write('\0')
70         ofl.write('\0')
71         ofl.write('\0')
72         ofl.write(v)
73
74         
75
76 def process_dis_record(ofl, gnb_id, old_enb_id, new_enb_id, ts):
77 #       print "Disconnected old_ue_iud="+str(old_enb_id)+", gnb_id="+str(gnb_id)+", new_ue_id="+str(new_enb_id)
78         X2APStreaming = x2ap_streaming_pb2.X2APStreaming() # header message
79         X2APStreaming.header.gNbID.value = "Bar"
80         sec = ts / 1000
81         ms = ts % 1000
82         utc_time = datetime.datetime.utcfromtimestamp(sec)
83         diff = utc_time - datetime.datetime(1900, 1, 1, 0, 0, 0)
84         ntp_hi = diff.days*24*60*60+diff.seconds
85         ntp_lo = (ms << 32) / 1000
86         X2APStreaming.header.timestamp = ntp_hi << 32 | ntp_lo
87
88 #       root = ue_context_release_pb2.UEContextRelease()
89         root = X2APStreaming.ueContextRelease
90
91         root.id_Old_eNB_UE_X2AP_ID = old_enb_id
92         root.id_New_eNB_UE_X2AP_ID = new_enb_id
93         root.id_SgNB_UE_X2AP_ID.value = gnb_id
94
95         v = X2APStreaming.SerializeToString()
96         vlen = len(v)
97         ofl.write("@MCL")
98         ofl.write("{0:7}".format(vlen))
99         ofl.write('\0')
100         ofl.write(str(ts))
101         ofl.write('\0')
102         ofl.write('\0')
103         ofl.write('\0')
104         ofl.write(v)
105
106 # -------------------------------
107 #       main body of data generator
108
109 enb_to_ue = {}  # avoid duplicate enb_ue_ids
110 gnb_to_ue = {}  # avoid duplicate gnb_ue_ids
111 ue_list = []
112
113 # initialize
114 for i in xrange(0,n_ue):
115         enb_id = random.randint(1,ue_id_range)
116         while enb_id in enb_to_ue:
117                 enb_id = random.randint(1,ue_id_range)
118         ue_list.append({"state": "dis", "enb_id": enb_id})
119         enb_to_ue[enb_id] = i
120
121 if not os.path.exists(fifo_dir + "/" + conn_flnm):
122         os.mkfifo(fifo_dir + "/" + conn_flnm)
123
124 if not os.path.exists(fifo_dir + "/" + dis_flnm):
125         os.mkfifo(fifo_dir + "/" + dis_flnm)
126
127 cfl = open(fifo_dir + "/" + conn_flnm,"w", 0)
128 dfl = open(fifo_dir + "/" + dis_flnm, "w", 0)
129
130
131 # main loop
132 while(1):
133
134         curr_time = int(time.time())
135
136         processed = []
137         for i in xrange(0, events_per_fl):
138                 ueix = random.randint(0,n_ue-1)
139                 if ueix in processed:
140                         ueix = random.randint(0,n_ue-1)
141                 processed.append(ueix)
142
143                 curr_ue = ue_list[ueix]
144                 if curr_ue["state"] == "dis":
145                         gnb_id = random.randint(1,ue_id_range)
146                         while gnb_id in gnb_to_ue:
147                                 gnb_id = random.randint(1,ue_id_range)
148                         gnb_to_ue[gnb_id] = ueix
149                         curr_ue["gnb_id"] = gnb_id
150                         curr_ue["state"] = "conn"
151                         process_conn_record(cfl,curr_ue["gnb_id"], curr_ue["enb_id"],1000*curr_time +50*i)
152                 else:
153                         new_enb_id = random.randint(1,ue_id_range)
154                         while new_enb_id in enb_to_ue:
155                                 new_enb_id = random.randint(1,ue_id_range)
156                         old_enb_id = curr_ue["enb_id"]
157                         gnb_id = curr_ue["gnb_id"]
158                         del enb_to_ue[old_enb_id]
159                         del gnb_to_ue[gnb_id]
160                         enb_to_ue[new_enb_id] = ueix
161
162                         curr_ue["enb_id"] = new_enb_id
163                         curr_ue["state"] = "dis"
164                         process_dis_record(dfl,gnb_id, old_enb_id, new_enb_id,1000*curr_time +50*i)
165
166         time.sleep(epoch_len)
167                         
168                         
169