github.com/onosproject/onos-api/go@v0.10.32/onos/ransim/types/types.go (about) 1 // SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package types 6 7 import ( 8 "errors" 9 "fmt" 10 "strconv" 11 ) 12 13 // PlmnID is a globally unique network identifier (Public Land Mobile Network) 14 type PlmnID uint32 15 16 // GnbID is a 5G gNodeB Identifier 17 type GnbID uint64 18 19 // EnbID is an eNodeB Identifier 20 type EnbID uint32 21 22 // CellID is a node-local cell identifier; 4 bits for 4G; 14 bits fo 5G 23 type CellID uint16 24 25 // NodeID is a general abstraction of a global E2 node identifier. 26 // It holds appropriately bit-shifted concatenation of either: 27 // - [PLMNID + GnbID] or 28 // - [PLMNID + EnbID] 29 // To extract the corresponding components, application must use the 30 // appropriate 4G or 5G method provided below. 31 type NodeID uint64 32 33 // NCI is a NR Cell Identifier; a 36-bit value (gNBID + CID) 34 type NCI uint64 35 36 // NCGI is NR Cell Global Identity (MCC+MNC+NCI) 37 type NCGI uint64 38 39 // ECI is a E-UTRAN Cell Identifier (gNBID + CID) 40 type ECI uint32 41 42 // ECGI is E-UTRAN Cell Global Identifier (MCC+MNC+ECI) 43 type ECGI uint64 44 45 // CRNTI is a cell-specific UE identifier 46 type CRNTI uint32 47 48 // MSIN is Mobile Subscriber Identification Number 49 type MSIN uint32 50 51 // IMSI is International Mobile Subscriber Identity 52 type IMSI uint64 53 54 // AmfUENgapID is AMF UE NGAP ID 55 type AmfUENgapID uint64 56 57 const ( 58 mask36 = 0xfffffffff 59 mask28 = 0xfffffff 60 mask20 = 0xfffff00 61 ) 62 63 // EncodePlmnID encodes MCC and MNC strings into a PLMNID hex string 64 func EncodePlmnID(mcc string, mnc string) string { 65 return mcc + mnc 66 } 67 68 // DecodePlmnID decodes MCC and MNC strings from PLMNID hex string 69 func DecodePlmnID(plmnID string) (mcc string, mnc string) { 70 return plmnID[0:3], plmnID[3:] 71 } 72 73 // ToPlmnID encodes the specified MCC and MNC strings into a numeric PLMNID 74 func ToPlmnID(mcc string, mnc string) PlmnID { 75 s := EncodePlmnID(mcc, mnc) 76 n, err := strconv.ParseUint(s, 16, 32) 77 if err != nil { 78 return 0 79 } 80 return PlmnID(n) 81 } 82 83 // PlmnIDFromHexString converts string form of PLMNID in its hex form into a numeric one suitable for APIs 84 func PlmnIDFromHexString(plmnID string) PlmnID { 85 n, err := strconv.ParseUint(plmnID, 16, 32) 86 if err != nil { 87 return 0 88 } 89 return PlmnID(n) 90 } 91 92 // PlmnIDFromString converts string form of PLMNID given as a simple MCC-MCN catenation into a numeric one suitable for APIs 93 func PlmnIDFromString(plmnID string) PlmnID { 94 return ToPlmnID(plmnID[0:3], plmnID[3:]) 95 } 96 97 // PlmnIDToString generates the MCC-MCN catenation format from the specified numeric PLMNID 98 func PlmnIDToString(plmnID PlmnID) string { 99 hexString := fmt.Sprintf("%x", plmnID) 100 mcc, mnc := DecodePlmnID(hexString) 101 return mcc + mnc 102 } 103 104 // 5G Identifiers 105 var ( 106 gnbBits uint8 = 22 107 cidBits uint8 = 14 108 gnbMask uint64 = 0b111111111111111111111100000000000000 109 cidMask uint64 = 0b000000000000000000000011111111111111 110 ) 111 112 // SetNCIBitSplit sets how the NCI bits are split between gNBID and CID 113 func SetNCIBitSplit(gnb uint8, cid uint8) error { 114 if (gnb+cid) == 36 && 4 <= cid && cid <= 14 { 115 cidBits = cid 116 gnbMask = 0 117 cidMask = 0 118 for i := 0; i < 64; i++ { 119 b := uint8(i) 120 if b < cid { 121 cidMask |= 1 << i 122 } 123 if cid <= b && b < (cid+gnb) { 124 gnbMask |= 1 << i 125 } 126 } 127 return nil 128 } 129 return errors.New("invalid bit split") 130 } 131 132 // ToNCI produces NCI from the specified components 133 func ToNCI(gnbID GnbID, cid CellID) NCI { 134 return NCI(uint(gnbID)<<cidBits | uint(cid)) 135 } 136 137 // ToNCGI produces NCGI from the specified components 138 func ToNCGI(plmnID PlmnID, nci NCI) NCGI { 139 return NCGI(uint(plmnID)<<36 | (uint(nci) & mask36)) 140 } 141 142 // ToNodeID produces a 5G global node ID as a catenation of PLMNID + GnbID 143 func ToNodeID(plmnID PlmnID, gnbID GnbID) NodeID { 144 return NodeID(uint(plmnID)<<gnbBits | uint(gnbID)) 145 } 146 147 // To5GNodeID produces a 5G global node ID as a catenation of PLMNID + GnbID 148 func To5GNodeID(plmnID PlmnID, gnbID GnbID) NodeID { 149 return ToNodeID(plmnID, gnbID) 150 } 151 152 // GetPlmnID extracts PLMNID from the specified NCGI 153 func GetPlmnID(ncgi uint64) PlmnID { 154 return PlmnID(ncgi >> 36) 155 } 156 157 // Get5GPlmnID extracts PLMNID from the specified NCGI 158 func Get5GPlmnID(ncgi uint64) PlmnID { 159 return GetPlmnID(ncgi) 160 } 161 162 // GetNCI extracts NCI from the specified NCGI 163 func GetNCI(ncgi NCGI) NCI { 164 return NCI(ncgi & mask36) 165 } 166 167 // GetGnbID extracts gNodeB ID from the specified NCGI or NCI 168 func GetGnbID(id uint64) GnbID { 169 return GnbID((id & gnbMask) >> cidBits) 170 } 171 172 // GetCellID extracts Cell ID from the specified NCGI or NCI 173 func GetCellID(id uint64) CellID { 174 return CellID(id & cidMask) 175 } 176 177 // Get5GCellID extracts Cell ID from the specified NCGI or NCI 178 func Get5GCellID(id uint64) CellID { 179 return GetCellID(id) 180 } 181 182 // 4G Identifiers 183 184 // ToECI produces ECI from the specified components 185 func ToECI(enbID EnbID, cid CellID) ECI { 186 return ECI(uint(enbID)<<8 | uint(cid)) 187 } 188 189 // ToECGI produces ECGI from the specified components 190 func ToECGI(plmnID PlmnID, eci ECI) ECGI { 191 return ECGI(uint(plmnID)<<28 | (uint(eci) & mask28)) 192 } 193 194 // To4GNodeID produces a 54 global node ID as a catenation of PLMNID + EnbID 195 func To4GNodeID(plmnID PlmnID, enbID EnbID) NodeID { 196 return NodeID(uint(plmnID)<<28 | uint(enbID)) 197 } 198 199 // Get4GPlmnID extracts PLMNID from the specified ECGI or IMSI 200 func Get4GPlmnID(id uint64) PlmnID { 201 return PlmnID(id >> 28) 202 } 203 204 // Get4GCellID extracts Cell ID from the specified ECGI 205 func Get4GCellID(id uint64) CellID { 206 return CellID(id & 0xff) 207 } 208 209 // GetEnbID extracts Enb ID from the specified ECGI 210 func GetEnbID(id uint64) EnbID { 211 return EnbID((id & mask20) >> 8) 212 } 213 214 // GetECI extracts ECI from the specified ECGI 215 func GetECI(id uint64) ECI { 216 return ECI(id & mask28) 217 }