Initial commit of mc-core part of mc xApp codebase
[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
22 import sgnb_reconfiguration_complete_pb2
23 import ue_context_release_pb2
24 import x2ap_streaming_pb2
25
26
27 # --------------------------------
28 #       Operating params
29 n_ue = 50
30 events_per_fl = 5
31 epoch_len = 1
32 ue_id_range = 100000
33
34 conn_flnm = "MT_000010280"
35 dis_flnm = "MT_000010050"
36
37 fifo_dir = "/tmp/mcl/fifos"
38
39 # -------------------------------
40 #       Create records
41
42 def process_conn_record(ofl,gnb_id, ue_id, ts):
43 #       print "Connected ue_iud="+str(ue_id)+", gnb_id="+str(gnb_id)
44         X2APStreaming = x2ap_streaming_pb2.X2APStreaming() # header message
45         X2APStreaming.header.gNbID.value = "Bar"
46
47 #       root = sgnb_reconfiguration_complete_pb2.SgNBReconfigurationComplete()
48         root = X2APStreaming.sgNBReconfigurationComplete
49
50         root.id_MeNB_UE_X2AP_ID = ue_id
51         root.id_SgNB_UE_X2AP_ID = gnb_id
52         child = root.id_ResponseInformationSgNBReconfComp
53         child.success_SgNBReconfComp.meNBtoSgNBContainer.value = "foo"
54
55         v = X2APStreaming.SerializeToString()
56         vlen = len(v)
57         ofl.write("@MCL")
58         ofl.write("{0:7}".format(vlen))
59         ofl.write('\0')
60         ofl.write(str(ts))
61         ofl.write('\0')
62         ofl.write('\0')
63         ofl.write('\0')
64         ofl.write(v)
65
66         
67
68 def process_dis_record(ofl, gnb_id, old_enb_id, new_enb_id, ts):
69 #       print "Disconnected old_ue_iud="+str(old_enb_id)+", gnb_id="+str(gnb_id)+", new_ue_id="+str(new_enb_id)
70         X2APStreaming = x2ap_streaming_pb2.X2APStreaming() # header message
71         X2APStreaming.header.gNbID.value = "Bar"
72
73 #       root = ue_context_release_pb2.UEContextRelease()
74         root = X2APStreaming.ueContextRelease
75
76         root.id_Old_eNB_UE_X2AP_ID = old_enb_id
77         root.id_New_eNB_UE_X2AP_ID = new_enb_id
78         root.id_SgNB_UE_X2AP_ID.value = gnb_id
79
80         v = X2APStreaming.SerializeToString()
81         vlen = len(v)
82         ofl.write("@MCL")
83         ofl.write("{0:7}".format(vlen))
84         ofl.write('\0')
85         ofl.write(str(ts))
86         ofl.write('\0')
87         ofl.write('\0')
88         ofl.write('\0')
89         ofl.write(v)
90
91 # -------------------------------
92 #       main body of data generator
93
94 enb_to_ue = {}  # avoid duplicate enb_ue_ids
95 gnb_to_ue = {}  # avoid duplicate gnb_ue_ids
96 ue_list = []
97
98 # initialize
99 for i in xrange(0,n_ue):
100         enb_id = random.randint(1,ue_id_range)
101         while enb_id in enb_to_ue:
102                 enb_id = random.randint(1,ue_id_range)
103         ue_list.append({"state": "dis", "enb_id": enb_id})
104         enb_to_ue[enb_id] = i
105
106 if not os.path.exists(fifo_dir + "/" + conn_flnm):
107         os.mkfifo(fifo_dir + "/" + conn_flnm)
108
109 if not os.path.exists(fifo_dir + "/" + dis_flnm):
110         os.mkfifo(fifo_dir + "/" + dis_flnm)
111
112 cfl = open(fifo_dir + "/" + conn_flnm,"w", 0)
113 dfl = open(fifo_dir + "/" + dis_flnm, "w", 0)
114
115
116 # main loop
117 while(1):
118
119         curr_time = int(time.time())
120
121         processed = []
122         for i in xrange(0, events_per_fl):
123                 ueix = random.randint(0,n_ue-1)
124                 if ueix in processed:
125                         ueix = random.randint(0,n_ue-1)
126                 processed.append(ueix)
127
128                 curr_ue = ue_list[ueix]
129                 if curr_ue["state"] == "dis":
130                         gnb_id = random.randint(1,ue_id_range)
131                         while gnb_id in gnb_to_ue:
132                                 gnb_id = random.randint(1,ue_id_range)
133                         gnb_to_ue[gnb_id] = ueix
134                         curr_ue["gnb_id"] = gnb_id
135                         curr_ue["state"] = "conn"
136                         process_conn_record(cfl,curr_ue["gnb_id"], curr_ue["enb_id"],1000*curr_time +50*i)
137                 else:
138                         new_enb_id = random.randint(1,ue_id_range)
139                         while new_enb_id in enb_to_ue:
140                                 new_enb_id = random.randint(1,ue_id_range)
141                         old_enb_id = curr_ue["enb_id"]
142                         gnb_id = curr_ue["gnb_id"]
143                         del enb_to_ue[old_enb_id]
144                         del gnb_to_ue[gnb_id]
145                         enb_to_ue[new_enb_id] = ueix
146
147                         curr_ue["enb_id"] = new_enb_id
148                         curr_ue["state"] = "dis"
149                         process_dis_record(dfl,gnb_id, old_enb_id, new_enb_id,1000*curr_time +50*i)
150
151         time.sleep(epoch_len)
152                         
153                         
154