From 294191eda3a10b754604ddc929f28afa53a9faf5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joanne=20Hug=C3=A9?= <joanne.huge@nexedi.com>
Date: Wed, 4 Dec 2024 16:30:45 +0000
Subject: [PATCH] simpleran: generate unique IDs for each ORS

---
 software/simpleran/buildout.hash.cfg          |  4 +--
 .../simpleran/instance-ors-enb.jinja2.cfg     | 27 +++++++++-----
 software/simpleran/instance.cfg               | 36 +++++++++++++++++++
 3 files changed, 57 insertions(+), 10 deletions(-)

diff --git a/software/simpleran/buildout.hash.cfg b/software/simpleran/buildout.hash.cfg
index ce3cf38c8..625ea70a7 100644
--- a/software/simpleran/buildout.hash.cfg
+++ b/software/simpleran/buildout.hash.cfg
@@ -16,7 +16,7 @@
 
 [template]
 filename = instance.cfg
-md5sum = 75057fd3fc8b1722b13163f020b196c0
+md5sum = 176b84bde85d95a115a0cf2068912461
 
 [template-ors]
 filename = instance-ors.cfg
@@ -64,7 +64,7 @@ md5sum = e89ee67dcee89923aace67759b3a7867
 
 [template-ors-enb]
 _update_hash_filename_ = instance-ors-enb.jinja2.cfg
-md5sum = 8ed4dcc068950078daadfa7dd63b67fe
+md5sum = bdc8ca95cad8374f24af7ab6a276b61c
 
 [template-ors-ue]
 _update_hash_filename_ = instance-ors-ue.jinja2.cfg
diff --git a/software/simpleran/instance-ors-enb.jinja2.cfg b/software/simpleran/instance-ors-enb.jinja2.cfg
index 9d1c32ede..d103d8a60 100644
--- a/software/simpleran/instance-ors-enb.jinja2.cfg
+++ b/software/simpleran/instance-ors-enb.jinja2.cfg
@@ -12,11 +12,11 @@
   "n_antenna_ul": 2,
   "rf_mode": "tdd",
   "tdd_ul_dl_config": "[Configuration 2] 5ms 2UL 6DL (default)",
-  "pci": 1,
-  "cell_id": "0x01",
+  "enb_id":  ors_id['enb_id'],
+  "cell_id": ors_id['enb_cell_id'],
+  "pci":     ors_id['enb_pci'],
+  "root_sequence_index":     ors_id['root_sequence_index'],
   "tac": "0x0001",
-  "root_sequence_index": 204,
-  "enb_id": "0x1A2D0",
   'mme_list': {
       'Local Core Network': {
           'mme_addr': '127.0.1.100'
@@ -40,9 +40,10 @@
   "rf_mode": "tdd",
   "tdd_ul_dl_config": "5ms 2UL 7DL 4/6 (default)",
   "ssb_pos_bitmap": "10000000",
-  "pci": 500,
-  "cell_id": "0x01",
-  "gnb_id": "0x12345",
+  "gnb_id":  ors_id['gnb_id'],
+  "cell_id": ors_id['gnb_cell_id'],
+  "pci":     ors_id['gnb_pci'],
+  "root_sequence_index":     ors_id['root_sequence_index'],
   "gnb_id_bits": 28,
   'amf_list': {
       'Local Core Network': {
@@ -113,7 +114,7 @@
       'nr_band':      ors_version['current-nr-band'],
       'bandwidth':    slapparameter_dict.nr_bandwidth,
       'ssb_pos_bitmap': slapparameter_dict.ssb_pos_bitmap,
-      'root_sequence_index':  1,
+      'root_sequence_index':  slapparameter_dict.root_sequence_index,
     }
 %}
 {%- endif %}
@@ -219,6 +220,16 @@ frequency-range-rating = {{ ors_version['range'] }}
 current-tx-power-estimate = {{ ors_version['power-estimate'] }}
 current-tx-gain = {{ ors_version['current-tx-gain'] }}
 current-rx-gain = {{ ors_version['current-rx-gain'] }}
+cell-id = {{ slapparameter_dict['cell_id'] }}
+physical-cell-id = {{ slapparameter_dict['pci'] }}
+root-sequence-index = {{ slapparameter_dict['root_sequence_index'] }}
+{%- if enb_mode == "enb" %}
+enb-id = {{ slapparameter_dict['enb_id'] }}
+eutra-cell-id = {{ ors_id['eutra_cell_id'] }}
+{%- else %}
+gnb-id = {{ slapparameter_dict['gnb_id'] }}
+nr-cell-id = {{ ors_id['nr_cell_id'] }}
+{%- endif %}
 
 {%- if enb_mode == 'enb'  %}
 current-frequency  = {{ xearfcn_module.frequency(ors_version['current-earfcn']) }} MHz
diff --git a/software/simpleran/instance.cfg b/software/simpleran/instance.cfg
index f22f46899..b8d5862de 100644
--- a/software/simpleran/instance.cfg
+++ b/software/simpleran/instance.cfg
@@ -164,6 +164,41 @@ init =
               except:
                   options['mac'] = "00:00:00:00:00:00"
 
+[ors-id]
+recipe = slapos.recipe.build
+configuration = $${slap-configuration:configuration}
+init =
+  import socket
+  try:
+    sn = int(socket.gethostname().split('ors')[1])
+  except (IndexError, ValueError):
+    sn = 0
+  options['serial_number'] = sn
+  options['enb_id'] = "0x{:05X}".format(sn % 2**20)
+  options['gnb_id'] = "0x{:05X}".format((sn + 2**19) % 2**20)
+  options['enb_pci'] = sn % 504
+  options['gnb_pci'] = sn % 1008
+  options['enb_cell_id'] = "0x{:02X}".format(sn % 2**8)
+  options['gnb_cell_id'] = "0x{:02X}".format((sn + 2**7) % 2**8)
+  options['root_sequence_index'] = sn % 834
+
+  def to_int(x):
+    try:
+      if '0x' in enb_id:
+        return int(x, 16)
+      return int(x)
+    except ValueError:
+      return 0
+  enb_id = options['configuration'].get('enb_id', options['enb_id'])
+  gnb_id = options['configuration'].get('enb_id', options['enb_id'])
+  cell_id = options['configuration'].get('cell_id', options['enb_cell_id'])
+  gnb_id_bits = options['configuration'].get('gnb_id_bits', 28)
+  
+  options['eutra_cell_id'] = \
+    "0x{:07X}".format(to_int(enb_id) * 2**8                  + to_int(cell_id))
+  options['nr_cell_id']    = \
+    "0x{:07X}".format(to_int(gnb_id) * 2**(36 - gnb_id_bits) + to_int(cell_id))
+
 [comp-id]
 recipe = slapos.recipe.build
 computer = $${slap-connection:computer-id}
@@ -191,6 +226,7 @@ extra-context =
     section comp_id comp-id
     section slap_configuration slap-configuration
     section amarisoft amarisoft
+    section ors_id ors-id
     section sdr sdr
     raw enb_template   ${enb.jinja2.cfg:target}
     raw slaplte_template ${slaplte.jinja2:target}
-- 
2.30.9