Skip to content

Commit 7b8fe7a

Browse files
committed
Breaking change: rework method prototypes to match golang conventions
1 parent 985b47d commit 7b8fe7a

File tree

11 files changed

+62
-59
lines changed

11 files changed

+62
-59
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ctx := context.TODO()
2727
cpNode := NewPFCPEntityCP(SMF_NODE_ID, SMF_IP_ADDR) // node id can be an IP Address or a FQDN
2828
go cpNode.ListenAndServeContext(ctx)
2929
cpNode.WaitReady(ctx)
30-
association, _ := cpNode.NewEstablishedPFCPAssociation(ie.NewNodeIDHeuristic(UPFADDR))
30+
association, _ := cpNode.NewEstablishedPFCPAssociation(ctx, ie.NewNodeIDHeuristic(UPFADDR))
3131
session, _ := a.CreateSession(pdrs, fars)
3232

3333
```

pfcp/api/association_interface.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
package api
77

88
import (
9+
"context"
10+
911
"github.com/wmnsk/go-pfcp/ie"
1012
)
1113

1214
type SEID = uint64
1315
type PFCPAssociationInterface interface {
1416
PFCPPeerInterface
15-
SetupInitiatedByCP() error
17+
SetupInitiatedByCP(ctx context.Context) error
1618
GetNextSEID() SEID
17-
CreateSession(remoteFseid *ie.IE, pdrs PDRMapInterface, fars FARMapInterface) (session PFCPSessionInterface, err error)
19+
CreateSession(ctx context.Context, remoteFseid *ie.IE, pdrs PDRMapInterface, fars FARMapInterface) (session PFCPSessionInterface, err error)
1820
}

pfcp/api/entity_interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type PFCPEntityInterface interface {
1818
IsControlPlane() bool
1919
NodeID() *ie.IE
2020
RecoveryTimeStamp() *ie.IE
21-
NewEstablishedPFCPAssociation(nodeID *ie.IE) (association PFCPAssociationInterface, err error)
21+
NewEstablishedPFCPAssociation(ctx context.Context, nodeID *ie.IE) (association PFCPAssociationInterface, err error)
2222
RemovePFCPAssociation(association PFCPAssociationInterface) error
2323
GetPFCPAssociation(nid string) (association PFCPAssociationInterface, err error)
2424
GetPFCPSessions() []PFCPSessionInterface

pfcp/api/peer_interface.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package api
77

88
import (
9+
"context"
10+
911
"github.com/wmnsk/go-pfcp/ie"
1012
"github.com/wmnsk/go-pfcp/message"
1113
)
@@ -19,5 +21,5 @@ type PFCPPeerInterface interface {
1921
IsUserPlane() bool
2022
IsControlPlane() bool
2123
LocalEntity() PFCPEntityInterface
22-
NewEstablishedPFCPAssociation() (PFCPAssociationInterface, error)
24+
NewEstablishedPFCPAssociation(ctx context.Context) (PFCPAssociationInterface, error)
2325
}

pfcp/association.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package pfcp_networking
77

88
import (
9+
"context"
910
"fmt"
1011
"net"
1112
"time"
@@ -23,14 +24,13 @@ type PFCPAssociation struct {
2324
}
2425

2526
// Create a new PFCPAssociation, this association is already set-up
26-
func newEstablishedPFCPAssociation(peer api.PFCPPeerInterface) (api.PFCPAssociationInterface, error) {
27+
func newEstablishedPFCPAssociation(ctx context.Context, peer api.PFCPPeerInterface) (api.PFCPAssociationInterface, error) {
2728
association := PFCPAssociation{
2829
PFCPPeerInterface: peer,
2930
isSetup: false,
3031
sessionIDPool: NewSessionIDPool(),
3132
}
32-
err := association.SetupInitiatedByCP()
33-
if err != nil {
33+
if err := association.SetupInitiatedByCP(ctx); err != nil {
3434
return nil, err
3535
}
3636
return &association, nil
@@ -57,14 +57,14 @@ func (association *PFCPAssociation) GetNextSEID() api.SEID {
5757
// clause 6.2.6.3).
5858
// The CP function and the UP function shall support the PFCP Association Setup initiated by the CP function. The CP
5959
// function and the UP function may additionally support the PFCP Association Setup initiated by the UP function.
60-
func (association *PFCPAssociation) SetupInitiatedByCP() error {
60+
func (association *PFCPAssociation) SetupInitiatedByCP(ctx context.Context) error {
6161
if association.isSetup {
6262
return fmt.Errorf("association is already set up")
6363
}
6464
switch {
6565
case association.LocalEntity().IsUserPlane():
6666
association.isSetup = true
67-
go association.heartMonitoring()
67+
go association.heartMonitoring(ctx)
6868
return nil
6969
case association.LocalEntity().IsControlPlane():
7070
sar := message.NewAssociationSetupRequest(0, association.LocalEntity().NodeID(), association.LocalEntity().RecoveryTimeStamp())
@@ -83,7 +83,7 @@ func (association *PFCPAssociation) SetupInitiatedByCP() error {
8383
}
8484
if cause == ie.CauseRequestAccepted {
8585
association.isSetup = true
86-
go association.heartMonitoring()
86+
go association.heartMonitoring(ctx)
8787
return nil
8888
}
8989
return fmt.Errorf("association setup request rejected")
@@ -93,7 +93,7 @@ func (association *PFCPAssociation) SetupInitiatedByCP() error {
9393
}
9494

9595
// Start monitoring heart of a PFCP Association
96-
func (association *PFCPAssociation) heartMonitoring() error {
96+
func (association *PFCPAssociation) heartMonitoring(ctx context.Context) error {
9797
defer association.Close()
9898
checkInterval := 30 * time.Second
9999
for {
@@ -106,6 +106,8 @@ func (association *PFCPAssociation) heartMonitoring() error {
106106
if err != nil {
107107
return err
108108
}
109+
case <-ctx.Done():
110+
return ctx.Err()
109111
}
110112
}
111113
}
@@ -158,7 +160,7 @@ func (association *PFCPAssociation) getFSEID(seid api.SEID) (*ie.IE, error) {
158160
}
159161

160162
// remoteFseid can be nil if caller is at CP function side
161-
func (association *PFCPAssociation) CreateSession(remoteFseid *ie.IE, pdrs api.PDRMapInterface, fars api.FARMapInterface) (session api.PFCPSessionInterface, err error) {
163+
func (association *PFCPAssociation) CreateSession(ctx context.Context, remoteFseid *ie.IE, pdrs api.PDRMapInterface, fars api.FARMapInterface) (session api.PFCPSessionInterface, err error) {
162164
// Generation of the F-SEID
163165
localSEID := association.GetNextSEID()
164166
localFseid, err := association.getFSEID(localSEID)

pfcp/entity.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (e *PFCPEntity) GetPFCPAssociation(nid string) (association api.PFCPAssocia
184184
return e.associationsMap.Get(nid)
185185
}
186186

187-
func (e *PFCPEntity) NewEstablishedPFCPAssociation(nodeID *ie.IE) (association api.PFCPAssociationInterface, err error) {
187+
func (e *PFCPEntity) NewEstablishedPFCPAssociation(ctx context.Context, nodeID *ie.IE) (association api.PFCPAssociationInterface, err error) {
188188
peer, err := newPFCPPeerUP(e, nodeID)
189189
if err != nil {
190190
return nil, err
@@ -199,7 +199,7 @@ func (e *PFCPEntity) NewEstablishedPFCPAssociation(nodeID *ie.IE) (association a
199199
if !e.associationsMap.CheckNonExist(nid) {
200200
return nil, fmt.Errorf("association already exists")
201201
}
202-
a, err := peer.NewEstablishedPFCPAssociation()
202+
a, err := peer.NewEstablishedPFCPAssociation(ctx)
203203
if err != nil {
204204
return nil, err
205205
}

pfcp/far_map.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (m *FARMap) NewCreateFARs() []*ie.IE {
129129
return f
130130
}
131131

132-
func NewFARMap(fars []*ie.IE) (farmap *FARMap, err error, cause uint8, offendingIE uint16) {
132+
func NewFARMap(fars []*ie.IE) (farmap *FARMap, cause uint8, offendingIE uint16, err error) {
133133
f := FARMap{
134134
farmap: make(farmapInternal),
135135
mu: sync.RWMutex{},
@@ -139,22 +139,22 @@ func NewFARMap(fars []*ie.IE) (farmap *FARMap, err error, cause uint8, offending
139139
if err != nil {
140140
switch err {
141141
case io.ErrUnexpectedEOF:
142-
return nil, err, ie.CauseInvalidLength, ie.FARID
142+
return nil, ie.CauseInvalidLength, ie.FARID, err
143143
case ie.ErrIENotFound:
144-
return nil, err, ie.CauseMandatoryIEMissing, ie.FARID
144+
return nil, ie.CauseMandatoryIEMissing, ie.FARID, err
145145
default:
146-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreateFAR
146+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreateFAR, err
147147
}
148148
}
149149
aa, err := far.ApplyAction()
150150
if err != nil {
151151
switch err {
152152
case io.ErrUnexpectedEOF:
153-
return nil, err, ie.CauseInvalidLength, ie.ApplyAction
153+
return nil, ie.CauseInvalidLength, ie.ApplyAction, err
154154
case ie.ErrIENotFound:
155-
return nil, err, ie.CauseMandatoryIEMissing, ie.ApplyAction
155+
return nil, ie.CauseMandatoryIEMissing, ie.ApplyAction, err
156156
default:
157-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreateFAR
157+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreateFAR, err
158158
}
159159
}
160160

@@ -170,7 +170,7 @@ func NewFARMap(fars []*ie.IE) (farmap *FARMap, err error, cause uint8, offending
170170
hasFP = true
171171
}
172172
if mustHaveFP && !hasFP {
173-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreateFAR
173+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreateFAR, err
174174
}
175175

176176
if !hasFP {
@@ -179,10 +179,10 @@ func NewFARMap(fars []*ie.IE) (farmap *FARMap, err error, cause uint8, offending
179179
err = f.Add(NewFAR(ie.NewFARID(id), ie.NewApplyAction(aa...), ie.NewForwardingParameters(fp...)))
180180
}
181181
if err != nil {
182-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreateFAR
182+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreateFAR, err
183183
}
184184
}
185-
return &f, nil, 0, 0
185+
return &f, 0, 0, nil
186186

187187
}
188188

pfcp/far_map_update.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type FARMapUpdate struct {
2222
mu sync.RWMutex
2323
}
2424

25-
func NewFARMapUpdate(fars []*ie.IE) (*FARMapUpdate, error, uint8, uint16) {
25+
func NewFARMapUpdate(fars []*ie.IE) (*FARMapUpdate, uint8, uint16, error) {
2626
f := FARMapUpdate{
2727
farmap: make(farmapUpdateInternal),
2828
mu: sync.RWMutex{},
@@ -32,11 +32,11 @@ func NewFARMapUpdate(fars []*ie.IE) (*FARMapUpdate, error, uint8, uint16) {
3232
if err != nil {
3333
switch err {
3434
case io.ErrUnexpectedEOF:
35-
return nil, err, ie.CauseInvalidLength, ie.FARID
35+
return nil, ie.CauseInvalidLength, ie.FARID, err
3636
case ie.ErrIENotFound:
37-
return nil, err, ie.CauseMandatoryIEMissing, ie.FARID
37+
return nil, ie.CauseMandatoryIEMissing, ie.FARID, err
3838
default:
39-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreateFAR
39+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreateFAR, err
4040
}
4141
}
4242
var ieaa *ie.IE = nil
@@ -51,7 +51,7 @@ func NewFARMapUpdate(fars []*ie.IE) (*FARMapUpdate, error, uint8, uint16) {
5151
}
5252
f.Add(NewFARUpdate(ie.NewFARID(id), ieaa, iefp))
5353
}
54-
return &f, nil, 0, 0
54+
return &f, 0, 0, nil
5555

5656
}
5757

pfcp/handlers.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func DefaultAssociationSetupRequestHandler(ctx context.Context, msg ReceivedMess
4242
return nil, fmt.Errorf("entity.RecoveryTimeStamp() is nil")
4343
}
4444

45-
if _, err := msg.Entity.NewEstablishedPFCPAssociation(m.NodeID); err != nil {
45+
if _, err := msg.Entity.NewEstablishedPFCPAssociation(ctx, m.NodeID); err != nil {
4646
logrus.WithError(err).Debug("Rejected Association")
4747
res := message.NewAssociationSetupResponse(msg.Sequence(), msg.Entity.NodeID(), ie.NewCause(ie.CauseRequestRejected), msg.Entity.RecoveryTimeStamp())
4848
return msg.NewResponse(res)
@@ -127,21 +127,21 @@ func DefaultSessionEstablishmentRequestHandler(ctx context.Context, msg Received
127127
}
128128

129129
// create PDRs
130-
pdrs, err, cause, offendingie := NewPDRMap(m.CreatePDR)
130+
pdrs, cause, offendingie, err := NewPDRMap(m.CreatePDR)
131131
if err != nil {
132132
res := message.NewSessionEstablishmentResponse(0, 0, rseid, msg.Sequence(), 0, msg.Entity.NodeID(), ie.NewCause(cause), ie.NewOffendingIE(offendingie))
133133
return msg.NewResponse(res)
134134
}
135135

136136
// create FARs
137-
fars, err, cause, offendingie := NewFARMap(m.CreateFAR)
137+
fars, cause, offendingie, err := NewFARMap(m.CreateFAR)
138138
if err != nil {
139139
res := message.NewSessionEstablishmentResponse(0, 0, rseid, msg.Sequence(), 0, msg.Entity.NodeID(), ie.NewCause(cause), ie.NewOffendingIE(offendingie))
140140
return msg.NewResponse(res)
141141
}
142142

143143
// create session with PDRs and FARs
144-
session, err := association.CreateSession(m.CPFSEID, pdrs, fars)
144+
session, err := association.CreateSession(ctx, m.CPFSEID, pdrs, fars)
145145
if err != nil {
146146
// Send cause(Rule creation/modification failure)
147147
res := message.NewSessionEstablishmentResponse(0, 0, rseid, msg.Sequence(), 0, msg.Entity.NodeID(), ie.NewCause(ie.CauseRuleCreationModificationFailure))
@@ -219,28 +219,28 @@ func DefaultSessionModificationRequestHandler(ctx context.Context, msg ReceivedM
219219
//XXX: CP F-SEID is ignored for the moment
220220

221221
// create PDRs
222-
createpdrs, err, cause, offendingie := NewPDRMap(m.CreatePDR)
222+
createpdrs, cause, offendingie, err := NewPDRMap(m.CreatePDR)
223223
if err != nil {
224224
res := message.NewSessionEstablishmentResponse(0, 0, rseid, msg.Sequence(), 0, msg.Entity.NodeID(), ie.NewCause(cause), ie.NewOffendingIE(offendingie))
225225
return msg.NewResponse(res)
226226
}
227227

228228
// create FARs
229-
createfars, err, cause, offendingie := NewFARMap(m.CreateFAR)
229+
createfars, cause, offendingie, err := NewFARMap(m.CreateFAR)
230230
if err != nil {
231231
res := message.NewSessionEstablishmentResponse(0, 0, rseid, msg.Sequence(), 0, msg.Entity.NodeID(), ie.NewCause(cause), ie.NewOffendingIE(offendingie))
232232
return msg.NewResponse(res)
233233
}
234234

235235
// update PDRs
236-
updatepdrs, err, cause, offendingie := NewPDRMap(m.UpdatePDR)
236+
updatepdrs, cause, offendingie, err := NewPDRMap(m.UpdatePDR)
237237
if err != nil {
238238
res := message.NewSessionEstablishmentResponse(0, 0, rseid, msg.Sequence(), 0, msg.Entity.NodeID(), ie.NewCause(cause), ie.NewOffendingIE(offendingie))
239239
return msg.NewResponse(res)
240240
}
241241

242242
// update FARs
243-
updatefars, err, cause, offendingie := NewFARMapUpdate(m.UpdateFAR)
243+
updatefars, cause, offendingie, err := NewFARMapUpdate(m.UpdateFAR)
244244
if err != nil {
245245
res := message.NewSessionEstablishmentResponse(0, 0, rseid, msg.Sequence(), 0, msg.Entity.NodeID(), ie.NewCause(cause), ie.NewOffendingIE(offendingie))
246246
return msg.NewResponse(res)

pfcp/pdr_map.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (m *PDRMap) SimulateRemove(key api.PDRID) error {
215215
return nil
216216
}
217217

218-
func NewPDRMap(pdrs []*ie.IE) (pdrmap *PDRMap, err error, cause uint8, offendingIE uint16) {
218+
func NewPDRMap(pdrs []*ie.IE) (pdrmap *PDRMap, cause uint8, offendingIE uint16, err error) {
219219
p := PDRMap{
220220
pdrmap: make(pdrmapInternal),
221221
mu: sync.RWMutex{},
@@ -229,44 +229,44 @@ func NewPDRMap(pdrs []*ie.IE) (pdrmap *PDRMap, err error, cause uint8, offending
229229
if err != nil {
230230
switch err {
231231
case io.ErrUnexpectedEOF:
232-
return nil, err, ie.CauseInvalidLength, ie.PDRID
232+
return nil, ie.CauseInvalidLength, ie.PDRID, err
233233
case ie.ErrIENotFound:
234-
return nil, err, ie.CauseMandatoryIEMissing, ie.PDRID
234+
return nil, ie.CauseMandatoryIEMissing, ie.PDRID, err
235235
default:
236-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreatePDR
236+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreatePDR, err
237237
}
238238
}
239239
pdi, err := pdr.PDI()
240240
if err != nil {
241241
switch err {
242242
case io.ErrUnexpectedEOF:
243-
return nil, err, ie.CauseInvalidLength, ie.PDI
243+
return nil, ie.CauseInvalidLength, ie.PDI, err
244244
case ie.ErrIENotFound:
245-
return nil, err, ie.CauseMandatoryIEMissing, ie.PDI
245+
return nil, ie.CauseMandatoryIEMissing, ie.PDI, err
246246
default:
247-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreatePDR
247+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreatePDR, err
248248
}
249249
}
250250
precedence, err := pdr.Precedence()
251251
if err != nil {
252252
switch err {
253253
case io.ErrUnexpectedEOF:
254-
return nil, err, ie.CauseInvalidLength, ie.Precedence
254+
return nil, ie.CauseInvalidLength, ie.Precedence, err
255255
case ie.ErrIENotFound:
256-
return nil, err, ie.CauseMandatoryIEMissing, ie.Precedence
256+
return nil, ie.CauseMandatoryIEMissing, ie.Precedence, err
257257
default:
258-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreatePDR
258+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreatePDR, err
259259
}
260260
}
261261
farid, err := pdr.FARID()
262262
if err != nil {
263263
switch err {
264264
case io.ErrUnexpectedEOF:
265-
return nil, err, ie.CauseInvalidLength, ie.FARID
265+
return nil, ie.CauseInvalidLength, ie.FARID, err
266266
case ie.ErrIENotFound:
267-
return nil, err, ie.CauseMandatoryIEMissing, ie.FARID
267+
return nil, ie.CauseMandatoryIEMissing, ie.FARID, err
268268
default:
269-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreatePDR
269+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreatePDR, err
270270
}
271271
}
272272

@@ -280,7 +280,7 @@ func NewPDRMap(pdrs []*ie.IE) (pdrmap *PDRMap, err error, cause uint8, offending
280280
}
281281
ohrIE = ie.NewOuterHeaderRemoval(ohr[0], ohr[1])
282282
} else if err == io.ErrUnexpectedEOF {
283-
return nil, err, ie.CauseInvalidLength, ie.OuterHeaderRemoval
283+
return nil, ie.CauseInvalidLength, ie.OuterHeaderRemoval, err
284284
}
285285

286286
err = p.Add(NewPDR(
@@ -291,10 +291,10 @@ func NewPDRMap(pdrs []*ie.IE) (pdrmap *PDRMap, err error, cause uint8, offending
291291
ohrIE,
292292
))
293293
if err != nil {
294-
return nil, err, ie.CauseMandatoryIEIncorrect, ie.CreatePDR
294+
return nil, ie.CauseMandatoryIEIncorrect, ie.CreatePDR, err
295295
}
296296
}
297-
return &p, nil, 0, 0
297+
return &p, 0, 0, nil
298298
}
299299

300300
func (m *PDRMap) IntoCreatePDR() []*ie.IE {

0 commit comments

Comments
 (0)