Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Nnrf_NFDiscovery/api_search_nf_instances_request_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ func (r ApiSearchNFInstancesRequest) GetAmfSetId() *string {
func (r ApiSearchNFInstancesRequest) GetSupi() *string {
return r.supi
}

func (r ApiSearchNFInstancesRequest) GetTai() *models.Tai {
return r.tai
}
118 changes: 71 additions & 47 deletions nrfcache/match_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ var matchFilters = MatchFilters{
}

func MatchSmfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.ApiSearchNFInstancesRequest) (bool, error) {
if profile == nil {
return false, fmt.Errorf("profile cannot be nil")
}
serviceNames := opts.GetServiceNames()
if serviceNames != nil && len(*serviceNames) > 0 {
found := false
for _, requiredService := range *serviceNames {
for _, nfService := range profile.NfServices {
if nfService.ServiceName == requiredService {
for _, nfService := range profile.GetNfServices() {
if nfService.GetServiceName() == requiredService {
found = true
break
}
Expand All @@ -55,25 +58,27 @@ func MatchSmfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.A
}

if !found {
logger.NrfcacheLog.Debugf("smf match failed: no service match for %s", profile.NfInstanceId)
logger.NrfcacheLog.Debugf("smf match failed: no service match for %s", profile.GetNfInstanceId())
return false, nil
}
}

snssais := opts.GetSnssais()
if snssais != nil {
matchCount := 0
smfInfo, hasSmfInfo := profile.GetSmfInfoOk()
for _, reqSnssai := range *snssais {
// Snssai in the smfInfo has priority
if profile.SmfInfo != nil && profile.SmfInfo.SNssaiSmfInfoList != nil {
for _, s := range profile.SmfInfo.SNssaiSmfInfoList {
if (s.SNssai.GetSst() == reqSnssai.GetSst()) && (s.SNssai.GetSd() == reqSnssai.GetSd()) {
if hasSmfInfo && len(smfInfo.GetSNssaiSmfInfoList()) > 0 {
for _, s := range smfInfo.GetSNssaiSmfInfoList() {
snssai := s.GetSNssai()
if snssai.GetSst() == reqSnssai.GetSst() && snssai.GetSd() == reqSnssai.GetSd() {
matchCount++
}
}
} else if profile.AllowedNssais != nil {
for _, s := range profile.AllowedNssais {
if (s.GetSst() == reqSnssai.GetSst()) && (s.GetSd() == reqSnssai.GetSd()) {
} else {
for _, s := range profile.GetAllowedNssais() {
if s.GetSst() == reqSnssai.GetSst() && s.GetSd() == reqSnssai.GetSd() {
matchCount++
}
}
Expand All @@ -93,15 +98,14 @@ func MatchSmfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.A
// or wild card match
dnnMatched := false

if profile.SmfInfo != nil && profile.SmfInfo.SNssaiSmfInfoList != nil {
smfInfo, hasSmfInfo := profile.GetSmfInfoOk()
if hasSmfInfo {
matchDnnLoop:
for _, s := range profile.SmfInfo.SNssaiSmfInfoList {
if s.DnnSmfInfoList != nil {
for _, d := range s.DnnSmfInfoList {
if d.GetDnn() == *dnn || d.GetDnn() == "*" {
dnnMatched = true
break matchDnnLoop
}
for _, s := range smfInfo.GetSNssaiSmfInfoList() {
for _, d := range s.GetDnnSmfInfoList() {
if d.GetDnn() == *dnn || d.GetDnn() == "*" {
dnnMatched = true
break matchDnnLoop
}
}
}
Expand All @@ -111,7 +115,7 @@ func MatchSmfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.A
return false, nil
}
}
logger.NrfcacheLog.Infof("smf match found, nfInstance Id %v", profile.NfInstanceId)
logger.NrfcacheLog.Infof("smf match found, nfInstance Id %v", profile.GetNfInstanceId())
return true, nil
}

Expand Down Expand Up @@ -186,28 +190,35 @@ func extractSupiNumber(supi string) string {
}

func MatchAusfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.ApiSearchNFInstancesRequest) (bool, error) {
if profile == nil {
return false, fmt.Errorf("profile cannot be nil")
}
supi := opts.GetSupi()
if supi != nil {
// Unrestricted when no SUPI ranges are declared; see MatchPcfProfile.
if profile.AusfInfo == nil || len(profile.AusfInfo.SupiRanges) == 0 {
logger.NrfcacheLog.Debugf("ausf match successful (unrestricted: no SUPI ranges) for %s", profile.NfInstanceId)
ausfInfo, ok := profile.GetAusfInfoOk()
if !ok || len(ausfInfo.GetSupiRanges()) == 0 {
logger.NrfcacheLog.Debugf("ausf match successful (unrestricted: no SUPI ranges) for %s", profile.GetNfInstanceId())
return true, nil
}

matchFound := matchSupiRange(*supi, profile.AusfInfo.SupiRanges)
matchFound := matchSupiRange(*supi, ausfInfo.GetSupiRanges())
if matchFound {
logger.NrfcacheLog.Debugf("ausf match successful for %s", profile.NfInstanceId)
logger.NrfcacheLog.Debugf("ausf match successful for %s", profile.GetNfInstanceId())
} else {
logger.NrfcacheLog.Debugf("ausf match failed: SUPI range mismatch for %s", profile.NfInstanceId)
logger.NrfcacheLog.Debugf("ausf match failed: SUPI range mismatch for %s", profile.GetNfInstanceId())
}
return matchFound, nil
}

logger.NrfcacheLog.Debugf("ausf match successful (no SUPI filter) for %s", profile.NfInstanceId)
logger.NrfcacheLog.Debugf("ausf match successful (no SUPI filter) for %s", profile.GetNfInstanceId())
return true, nil
}

func MatchNssfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.ApiSearchNFInstancesRequest) (bool, error) {
if profile == nil {
return false, fmt.Errorf("profile cannot be nil")
}
logger.NrfcacheLog.Infoln("nssf match found")
return true, nil
}
Expand All @@ -217,14 +228,14 @@ func MatchAmfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.A
return false, fmt.Errorf("profile cannot be nil")
}

if profile.NfType != models.NFTYPE_AMF {
return false, fmt.Errorf("profile is not AMF type: %v", profile.NfType)
if profile.GetNfType() != models.NFTYPE_AMF {
return false, fmt.Errorf("profile is not AMF type: %v", profile.GetNfType())
}
targetPlmnList := opts.GetTargetPlmnList()
if targetPlmnList != nil && len(*targetPlmnList) > 0 {
profilePlmnList := profile.GetPlmnList()
if len(profilePlmnList) == 0 {
logger.NrfcacheLog.Debugf("amf match failed: no profile PLMNs for %s", profile.NfInstanceId)
logger.NrfcacheLog.Debugf("amf match failed: no profile PLMNs for %s", profile.GetNfInstanceId())
return false, nil
}

Expand All @@ -237,48 +248,52 @@ func MatchAmfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.A
}

if !found {
logger.NrfcacheLog.Debugf("amf match failed: no PLMN match for %s", profile.NfInstanceId)
logger.NrfcacheLog.Debugf("amf match failed: no PLMN match for %s", profile.GetNfInstanceId())
return false, nil
}
}

targetNfInstanceId := opts.GetTargetNfInstanceId()
if targetNfInstanceId != nil && profile.GetNfInstanceId() != *targetNfInstanceId {
logger.NrfcacheLog.Debugf("amf match failed: NF instance ID mismatch for %s", profile.NfInstanceId)
logger.NrfcacheLog.Debugf("amf match failed: NF instance ID mismatch for %s", profile.GetNfInstanceId())
return false, nil
}

if profile.AmfInfo != nil {
amfInfo, hasAmfInfo := profile.GetAmfInfoOk()
if hasAmfInfo {
guamiOpt := opts.GetGuami()
if guamiOpt != nil && (profile.AmfInfo.GuamiList == nil || !slices.Contains(profile.AmfInfo.GuamiList, *guamiOpt)) {
logger.NrfcacheLog.Debugf("amf match failed: GUAMI mismatch for %s", profile.NfInstanceId)
if guamiOpt != nil && !slices.Contains(amfInfo.GetGuamiList(), *guamiOpt) {
logger.NrfcacheLog.Debugf("amf match failed: GUAMI mismatch for %s", profile.GetNfInstanceId())
return false, nil
}

amfRegionId := opts.GetAmfRegionId()
if amfRegionId != nil && profile.AmfInfo.GetAmfRegionId() != *amfRegionId {
logger.NrfcacheLog.Debugf("amf match failed: AMF region ID mismatch for %s", profile.NfInstanceId)
if amfRegionId != nil && amfInfo.GetAmfRegionId() != *amfRegionId {
logger.NrfcacheLog.Debugf("amf match failed: AMF region ID mismatch for %s", profile.GetNfInstanceId())
return false, nil
}

amfSetId := opts.GetAmfSetId()
if amfSetId != nil && profile.AmfInfo.GetAmfSetId() != *amfSetId {
logger.NrfcacheLog.Debugf("amf match failed: AMF set ID mismatch for %s", profile.NfInstanceId)
if amfSetId != nil && amfInfo.GetAmfSetId() != *amfSetId {
logger.NrfcacheLog.Debugf("amf match failed: AMF set ID mismatch for %s", profile.GetNfInstanceId())
return false, nil
}
} else {
// Handle case where AMF-specific filters are provided but AmfInfo is nil
if opts.GetGuami() != nil || opts.GetAmfRegionId() != nil || opts.GetAmfSetId() != nil {
logger.NrfcacheLog.Debugf("amf match failed: AMF filters provided but no AmfInfo for %s", profile.NfInstanceId)
logger.NrfcacheLog.Debugf("amf match failed: AMF filters provided but no AmfInfo for %s", profile.GetNfInstanceId())
return false, nil
}
}

logger.NrfcacheLog.Infof("amf match found = %v", profile.NfInstanceId)
logger.NrfcacheLog.Infof("amf match found = %v", profile.GetNfInstanceId())
return true, nil
}

func MatchPcfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.ApiSearchNFInstancesRequest) (bool, error) {
if profile == nil {
return false, fmt.Errorf("profile cannot be nil")
}
supi := opts.GetSupi()
if supi != nil {
// A profile declaring no SUPI ranges is unrestricted and serves every
Expand All @@ -287,12 +302,13 @@ func MatchPcfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.A
// absent" (nrf producer/nf_discovery.go, [Query-18] supi). Rationale:
// the cache must select the same profiles as the NRF it caches,
// otherwise a cached lookup and a live discovery disagree.
if profile.PcfInfo == nil || len(profile.PcfInfo.SupiRanges) == 0 {
pcfInfo, ok := profile.GetPcfInfoOk()
if !ok || len(pcfInfo.GetSupiRanges()) == 0 {
logger.NrfcacheLog.Debugf("pcf match found = true (unrestricted: no SUPI ranges)")
return true, nil
}

matchFound := matchSupiRange(*supi, profile.PcfInfo.SupiRanges)
matchFound := matchSupiRange(*supi, pcfInfo.GetSupiRanges())
logger.NrfcacheLog.Infof("pcf match found = %v", matchFound)
return matchFound, nil
}
Expand All @@ -303,15 +319,19 @@ func MatchPcfProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.A
}

func MatchUdmProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.ApiSearchNFInstancesRequest) (bool, error) {
if profile == nil {
return false, fmt.Errorf("profile cannot be nil")
}
supi := opts.GetSupi()
if supi != nil {
// Unrestricted when no SUPI ranges are declared; see MatchPcfProfile.
if profile.UdmInfo == nil || len(profile.UdmInfo.GetSupiRanges()) == 0 {
udmInfo, ok := profile.GetUdmInfoOk()
if !ok || len(udmInfo.GetSupiRanges()) == 0 {
logger.NrfcacheLog.Debugf("udm match found = true (unrestricted: no SUPI ranges)")
return true, nil
}

matchFound := matchSupiRange(*supi, profile.UdmInfo.GetSupiRanges())
matchFound := matchSupiRange(*supi, udmInfo.GetSupiRanges())
logger.NrfcacheLog.Infof("udm match found = %v", matchFound)
return matchFound, nil
}
Expand All @@ -328,19 +348,23 @@ func MatchUdmProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.A
// through to a live NRF query. Rationale: UDR is resolved on every subscriber
// data access, which makes it the most frequently discovered NF in the core.
func MatchUdrProfile(profile *models.NFProfileDiscovery, opts Nnrf_NFDiscovery.ApiSearchNFInstancesRequest) (bool, error) {
if profile == nil {
return false, fmt.Errorf("profile cannot be nil")
}
supi := opts.GetSupi()
if supi == nil {
logger.NrfcacheLog.Debugf("udr match successful (no SUPI filter) for %s", profile.NfInstanceId)
logger.NrfcacheLog.Debugf("udr match successful (no SUPI filter) for %s", profile.GetNfInstanceId())
return true, nil
}

// Unrestricted when no SUPI ranges are declared; see MatchPcfProfile.
if profile.UdrInfo == nil || len(profile.UdrInfo.GetSupiRanges()) == 0 {
logger.NrfcacheLog.Debugf("udr match successful (unrestricted: no SUPI ranges) for %s", profile.NfInstanceId)
udrInfo, ok := profile.GetUdrInfoOk()
if !ok || len(udrInfo.GetSupiRanges()) == 0 {
logger.NrfcacheLog.Debugf("udr match successful (unrestricted: no SUPI ranges) for %s", profile.GetNfInstanceId())
return true, nil
}

matchFound := matchSupiRange(*supi, profile.UdrInfo.GetSupiRanges())
logger.NrfcacheLog.Debugf("udr match found = %v for %s", matchFound, profile.NfInstanceId)
matchFound := matchSupiRange(*supi, udrInfo.GetSupiRanges())
logger.NrfcacheLog.Debugf("udr match found = %v for %s", matchFound, profile.GetNfInstanceId())
return matchFound, nil
}
30 changes: 17 additions & 13 deletions nrfcache/nrfcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func (c *NrfCache) handleLookup(ctx context.Context, nrfUri string, targetNfType
c.mutex.RUnlock()

if len(nfInstances) > 0 {
return models.SearchResult{NfInstances: nfInstances}, nil
var result models.SearchResult
result.SetNfInstances(nfInstances)
return result, nil
}

// discoveryMutex serializes NRF round-trips without blocking concurrent cache hits.
Expand All @@ -165,7 +167,9 @@ func (c *NrfCache) handleLookup(ctx context.Context, nrfUri string, targetNfType
c.mutex.RUnlock()

if len(nfInstances) > 0 {
return models.SearchResult{NfInstances: nfInstances}, nil
var result models.SearchResult
result.SetNfInstances(nfInstances)
return result, nil
}

logger.NrfcacheLog.Warnf("cache miss for nftype %s", targetNfType)
Expand All @@ -178,11 +182,11 @@ func (c *NrfCache) handleLookup(ctx context.Context, nrfUri string, targetNfType
return models.SearchResult{}, fmt.Errorf("NRF discovery returned nil result")
}

ttl := time.Duration(searchResult.ValidityPeriod) * time.Second

ttl := time.Duration(searchResult.GetValidityPeriod()) * time.Second
c.mutex.Lock()
for i := range searchResult.NfInstances {
c.set(&searchResult.NfInstances[i], ttl)
nfInstances = searchResult.GetNfInstances()
for i := range nfInstances {
c.set(&nfInstances[i], ttl)
}
c.mutex.Unlock()

Expand All @@ -200,13 +204,13 @@ func (c *NrfCache) set(nfProfile *models.NFProfileDiscovery, ttl time.Duration)
ttl = ttl * time.Second
}

item, exists := c.cache[nfProfile.NfInstanceId]
item, exists := c.cache[nfProfile.GetNfInstanceId()]
if exists {
// if item.isExpired()
c.priorityQ.update(item, nfProfile, ttl)
} else {
newItem := newNfProfileItem(nfProfile, ttl)
c.cache[nfProfile.NfInstanceId] = newItem
c.cache[nfProfile.GetNfInstanceId()] = newItem
c.priorityQ.push(newItem)
}
}
Expand Down Expand Up @@ -240,9 +244,9 @@ func (c *NrfCache) get(opts Nnrf_NFDiscovery.ApiSearchNFInstancesRequest) []mode
continue
}

if cb, ok := matchFilters[element.nfProfile.NfType]; ok {
if cb, ok := matchFilters[element.nfProfile.GetNfType()]; ok {
if matchFound, err := cb(element.nfProfile, opts); err != nil {
logger.NrfcacheLog.Errorf("match filter error for %s: %v", element.nfProfile.NfInstanceId, err)
logger.NrfcacheLog.Errorf("match filter error for %s: %v", element.nfProfile.GetNfInstanceId(), err)
} else if matchFound {
nfProfiles = append(nfProfiles, *element.nfProfile)
}
Expand All @@ -267,7 +271,7 @@ func (c *NrfCache) removeByNfInstanceId(nfInstanceId string) bool {
// remove -
func (c *NrfCache) remove(item *NfProfileItem) {
c.priorityQ.remove(item)
delete(c.cache, item.nfProfile.NfInstanceId)
delete(c.cache, item.nfProfile.GetNfInstanceId())
}

// cleanupExpiredItems - removes the profiles with expired TTLs
Expand All @@ -279,7 +283,7 @@ func (c *NrfCache) cleanupExpiredItems() {
break
}

logger.NrfcacheLog.Debugf("evicted nf instance %s", item.nfProfile.NfInstanceId)
logger.NrfcacheLog.Debugf("evicted nf instance %s", item.nfProfile.GetNfInstanceId())
c.remove(item)
}
}
Expand Down Expand Up @@ -460,7 +464,7 @@ func SearchNFInstances(ctx context.Context, nrfUri string, targetNfType, request
logger.NrfcacheLog.With("nfType", targetNfType, "param", param).Errorln("handleLookup failed:", err)
return nil, fmt.Errorf("handleLookup for nfType %v failed: %w", targetNfType, err)
}
for _, np := range searchResult.NfInstances {
for _, np := range searchResult.GetNfInstances() {
logger.NrfcacheLog.Infof("%+v", np)
}
return &searchResult, err
Expand Down
Loading
Loading