This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Nnrf.go
173 lines (144 loc) · 4.87 KB
/
Nnrf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package smf_consumer
import (
"context"
"fmt"
"free5gc/lib/Nnrf_NFDiscovery"
"free5gc/lib/Nudm_SubscriberDataManagement"
"free5gc/lib/openapi/models"
"free5gc/src/smf/logger"
"free5gc/src/smf/smf_context"
"net/http"
"strings"
"time"
"github.com/antihax/optional"
"github.com/mohae/deepcopy"
)
func SendNFRegistration() error {
//set nfProfile
profile := models.NfProfile{
NfInstanceId: smf_context.SMF_Self().NfInstanceID,
NfType: models.NfType_SMF,
NfStatus: models.NfStatus_REGISTERED,
Ipv4Addresses: []string{smf_context.SMF_Self().HTTPAddress},
NfServices: smf_context.NFServices,
SmfInfo: smf_context.SmfInfo,
}
var rep models.NfProfile
var res *http.Response
var err error
// Check data (Use RESTful PUT)
for {
rep, res, err = smf_context.SMF_Self().NFManagementClient.NFInstanceIDDocumentApi.RegisterNFInstance(context.TODO(), smf_context.SMF_Self().NfInstanceID, profile)
if err != nil || res == nil {
logger.AppLog.Infof("SMF register to NRF Error[%s]", err.Error())
time.Sleep(2 * time.Second)
continue
}
status := res.StatusCode
if status == http.StatusOK {
// NFUpdate
break
} else if status == http.StatusCreated {
// NFRegister
resourceUri := res.Header.Get("Location")
// resouceNrfUri := resourceUri[strings.LastIndex(resourceUri, "/"):]
smf_context.SMF_Self().NfInstanceID = resourceUri[strings.LastIndex(resourceUri, "/")+1:]
break
} else {
logger.AppLog.Infof("handler returned wrong status code %d", status)
// fmt.Errorf("NRF return wrong status code %d", status)
}
}
logger.InitLog.Infof("SMF Registration to NRF %v", rep)
return nil
}
func RetrySendNFRegistration(MaxRetry int) error {
retryCount := 0
for retryCount < MaxRetry {
err := SendNFRegistration()
if err == nil {
return nil
}
logger.AppLog.Warnf("Send NFRegistration Failed by %v", err)
retryCount++
}
return fmt.Errorf("[SMF] Retry NF Registration has meet maximum")
}
func SendNFDeregistration() {
// Check data (Use RESTful DELETE)
res, err := smf_context.SMF_Self().NFManagementClient.NFInstanceIDDocumentApi.DeregisterNFInstance(context.TODO(), smf_context.SMF_Self().NfInstanceID)
if err != nil {
logger.AppLog.Panic(err)
}
if res != nil {
if status := res.StatusCode; status != http.StatusNoContent {
logger.AppLog.Info("handler returned wrong status code", status)
}
}
}
func SendNFDiscoveryUDM() {
if smf_context.SMF_Self().SubscriberDataManagementClient != nil {
return
}
localVarOptionals := Nnrf_NFDiscovery.SearchNFInstancesParamOpts{}
// Check data
rep, res, err := smf_context.SMF_Self().NFDiscoveryClient.NFInstancesStoreApi.SearchNFInstances(context.TODO(), models.NfType_UDM, models.NfType_SMF, &localVarOptionals)
if err != nil {
return
}
if res != nil {
if status := res.StatusCode; status != http.StatusOK {
logger.AppLog.Info("handler returned wrong status code", status)
}
}
smf_context.SMF_Self().UDMProfile = rep.NfInstances[0]
for _, service := range *smf_context.SMF_Self().UDMProfile.NfServices {
if service.ServiceName == models.ServiceName_NUDM_SDM {
SDMConf := Nudm_SubscriberDataManagement.NewConfiguration()
SDMConf.SetBasePath(service.ApiPrefix)
smf_context.SMF_Self().SubscriberDataManagementClient = Nudm_SubscriberDataManagement.NewAPIClient(SDMConf)
}
}
if smf_context.SMF_Self().SubscriberDataManagementClient == nil {
logger.AppLog.Warnln("sdm client failed")
}
}
func SendNFDiscoveryPCF() {
// Set targetNfType
targetNfType := models.NfType_PCF
// Set requestNfType
requesterNfType := models.NfType_SMF
localVarOptionals := Nnrf_NFDiscovery.SearchNFInstancesParamOpts{}
// Check data
rep, res, err := smf_context.SMF_Self().NFDiscoveryClient.NFInstancesStoreApi.SearchNFInstances(context.TODO(), targetNfType, requesterNfType, &localVarOptionals)
if err != nil {
return
}
if res != nil {
if status := res.StatusCode; status != http.StatusOK {
logger.AppLog.Info("handler returned wrong status code", status)
}
}
logger.AppLog.Traceln(rep.NfInstances)
}
func SendNFDiscoveryServingAMF(smContext *smf_context.SMContext) {
targetNfType := models.NfType_AMF
requesterNfType := models.NfType_SMF
localVarOptionals := Nnrf_NFDiscovery.SearchNFInstancesParamOpts{}
localVarOptionals.TargetNfInstanceId = optional.NewInterface(smContext.ServingNfId)
// Check data
rep, res, err := smf_context.SMF_Self().NFDiscoveryClient.NFInstancesStoreApi.SearchNFInstances(context.TODO(), targetNfType, requesterNfType, &localVarOptionals)
if err != nil {
logger.AppLog.Panic(err)
return
}
if rep.NfInstances == nil {
if status := res.StatusCode; status != http.StatusOK {
logger.AppLog.Info("handler returned wrong status code", status)
}
logger.AppLog.Info("rep.NfInstances == nil")
return
}
logger.AppLog.Info("SendNFDiscoveryServingAMF ok")
smContext.AMFProfile = deepcopy.Copy(rep.NfInstances[0]).(models.NfProfile)
}