From d680276cfaabced3f9ce9d275696f3977a73974a Mon Sep 17 00:00:00 2001 From: Louis Royer <55180044+louisroyer@users.noreply.github.com> Date: Wed, 2 Feb 2022 14:58:07 +0100 Subject: [PATCH] Remove AddSrvHandler --- handlers.go | 30 ++++++++---------------------- pfcp_client_entity.go | 11 ++++++----- pfcp_entity.go | 34 +++++++++++++++++++++++++--------- pfcp_peer.go | 8 ++++---- pfcp_server_entity.go | 22 ++++------------------ 5 files changed, 47 insertions(+), 58 deletions(-) diff --git a/handlers.go b/handlers.go index 53eff75..111be1d 100644 --- a/handlers.go +++ b/handlers.go @@ -2,45 +2,31 @@ package pfcp_networking import ( "fmt" - "log" "net" "github.com/wmnsk/go-pfcp/ie" "github.com/wmnsk/go-pfcp/message" ) -func handleHeartbeatRequest(entity *PFCPEntity, senderAddr net.Addr, msg message.Message) error { - res := message.NewHeartbeatResponse(msg.Sequence(), entity.RecoveryTimeStamp) +func handleHeartbeatRequest(entity PFCPEntityInterface, senderAddr net.Addr, msg message.Message) error { + res := message.NewHeartbeatResponse(msg.Sequence(), entity.RecoveryTimeStamp()) return entity.ReplyTo(senderAddr, msg, res) } -func handleAssociationSetupRequest(srv *PFCPServerEntity, senderAddr net.Addr, msg message.Message) error { +func handleAssociationSetupRequest(entity PFCPEntityInterface, senderAddr net.Addr, msg message.Message) error { m, ok := msg.(*message.AssociationSetupRequest) if !ok { return fmt.Errorf("Issue with Association Setup Request") } - peer, err := NewPFCPPeer(&srv.PFCPEntity, m.NodeID) + peer, err := NewPFCPPeer(entity, m.NodeID) if err != nil { return err } - association := NewAssociation(peer, srv) - err = srv.CreatePFCPAssociation(&association) + association := NewAssociation(peer, entity) + err = entity.CreatePFCPAssociation(&association) if err != nil { return err } - switch { - case msg == nil: - log.Println("msg is nil") - fallthrough - case srv == nil: - log.Println("srv is nil") - fallthrough - case srv.NodeID == nil: - log.Println("srv.NodeID is nil") - fallthrough - case srv.RecoveryTimeStamp == nil: - log.Println("srv.RecoveryTimeStamp is nil") - } - res := message.NewAssociationSetupResponse(msg.Sequence(), srv.NodeID, ie.NewCause(ie.CauseRequestAccepted), srv.RecoveryTimeStamp) - return srv.ReplyTo(senderAddr, msg, res) + res := message.NewAssociationSetupResponse(msg.Sequence(), entity.NodeID(), ie.NewCause(ie.CauseRequestAccepted), entity.RecoveryTimeStamp()) + return entity.ReplyTo(senderAddr, msg, res) } diff --git a/pfcp_client_entity.go b/pfcp_client_entity.go index a9ddb7c..58828d1 100644 --- a/pfcp_client_entity.go +++ b/pfcp_client_entity.go @@ -20,11 +20,12 @@ func NewPFCPClientEntity(nodeID string) PFCPClientEntity { associations: make(map[string]*PFCPAssociation), muAssociations: sync.Mutex{}, } + e.iface = e return e } // Add an association to the association table -func (e *PFCPClientEntity) createPFCPAssociation(association *PFCPAssociation) error { +func (e PFCPClientEntity) CreatePFCPAssociation(association *PFCPAssociation) error { nid, err := association.NodeID.NodeID() if err != nil { return err @@ -36,7 +37,7 @@ func (e *PFCPClientEntity) createPFCPAssociation(association *PFCPAssociation) e } // Remove an association from the association table -func (e *PFCPClientEntity) RemovePFCPAssociation(association *PFCPAssociation) error { +func (e PFCPClientEntity) RemovePFCPAssociation(association *PFCPAssociation) error { nid, err := association.NodeID.NodeID() if err != nil { return err @@ -49,7 +50,7 @@ func (e *PFCPClientEntity) RemovePFCPAssociation(association *PFCPAssociation) e // Returns an existing PFCP Association func (e *PFCPClientEntity) GetPFCPAssociation(nodeID *ie.IE) (association *PFCPAssociation, err error) { - nid, err := e.NodeID.NodeID() + nid, err := e.NodeID().NodeID() if err != nil { return nil, err } @@ -71,7 +72,7 @@ func (e *PFCPClientEntity) NewPFCPAssociation(peer *PFCPPeer) (association *PFCP if _, exists := e.associations[nid]; exists { return nil, fmt.Errorf("Only one association shall be setup between given pair of CP and UP functions.") } - sar := message.NewAssociationSetupRequest(0, e.NodeID, e.RecoveryTimeStamp) + sar := message.NewAssociationSetupRequest(0, e.NodeID(), e.RecoveryTimeStamp()) resp, err := peer.Send(sar) if err != nil { return nil, err @@ -87,7 +88,7 @@ func (e *PFCPClientEntity) NewPFCPAssociation(peer *PFCPPeer) (association *PFCP } if cause == ie.CauseRequestAccepted { a := &PFCPAssociation{PFCPPeer: peer, localEntity: e} - e.createPFCPAssociation(a) + e.CreatePFCPAssociation(a) return a, nil } return nil, fmt.Errorf("Associaton setup request rejected") diff --git a/pfcp_entity.go b/pfcp_entity.go index a3b592f..6d310f8 100644 --- a/pfcp_entity.go +++ b/pfcp_entity.go @@ -13,10 +13,14 @@ import ( ) type PFCPEntityInterface interface { + NodeID() *ie.IE + RecoveryTimeStamp() *ie.IE + CreatePFCPAssociation(association *PFCPAssociation) error RemovePFCPAssociation(association *PFCPAssociation) error + ReplyTo(ipAddress net.Addr, requestMessage message.Message, responseMessage message.Message) error } -func (entity *PFCPEntity) ReplyTo(ipAddress net.Addr, requestMessage message.Message, responseMessage message.Message) error { +func (entity PFCPEntity) ReplyTo(ipAddress net.Addr, requestMessage message.Message, responseMessage message.Message) error { if !pfcputil.IsMessageTypeRequest(requestMessage.MessageType()) { return fmt.Errorf("requestMessage shall be a Request Message") } @@ -40,14 +44,22 @@ func (entity *PFCPEntity) ReplyTo(ipAddress net.Addr, requestMessage message.Mes return nil } -type handler = func(entity *PFCPEntity, senderAddr net.Addr, msg message.Message) error +type handler = func(entity PFCPEntityInterface, senderAddr net.Addr, msg message.Message) error type PFCPEntity struct { - NodeID *ie.IE - RecoveryTimeStamp *ie.IE + nodeID *ie.IE + recoveryTimeStamp *ie.IE handlers map[pfcputil.MessageType]handler conn *net.UDPConn mu sync.Mutex + iface PFCPEntityInterface +} + +func (e PFCPEntity) NodeID() *ie.IE { + return e.nodeID +} +func (e PFCPEntity) RecoveryTimeStamp() *ie.IE { + return e.recoveryTimeStamp } func newDefaultPFCPEntityHandlers() map[pfcputil.MessageType]handler { @@ -58,17 +70,21 @@ func newDefaultPFCPEntityHandlers() map[pfcputil.MessageType]handler { func NewPFCPEntity(nodeID string) PFCPEntity { return PFCPEntity{ - NodeID: pfcputil.CreateNodeID(nodeID), - RecoveryTimeStamp: nil, + nodeID: pfcputil.CreateNodeID(nodeID), + recoveryTimeStamp: nil, handlers: newDefaultPFCPEntityHandlers(), conn: nil, mu: sync.Mutex{}, + iface: nil, } } func (e *PFCPEntity) Start() error { - e.RecoveryTimeStamp = ie.NewRecoveryTimeStamp(time.Now()) - ipAddr, err := e.NodeID.NodeID() + if e.iface == nil { + return fmt.Errorf("PFCPEntity is incorrectly initialized") + } + e.recoveryTimeStamp = ie.NewRecoveryTimeStamp(time.Now()) + ipAddr, err := e.NodeID().NodeID() if err != nil { return err } @@ -95,7 +111,7 @@ func (e *PFCPEntity) Start() error { continue } if f, exists := e.handlers[msg.MessageType()]; exists { - err := f(e, addr, msg) + err := f(e.iface, addr, msg) if err != nil { log.Println(err) } diff --git a/pfcp_peer.go b/pfcp_peer.go index a8bd8d2..f7270a0 100644 --- a/pfcp_peer.go +++ b/pfcp_peer.go @@ -15,7 +15,7 @@ type messageChan chan []byte type PFCPPeer struct { NodeID *ie.IE - Srv *PFCPEntity + Srv PFCPEntityInterface conn *net.UDPConn udpAddr *net.UDPAddr seq uint32 @@ -24,7 +24,7 @@ type PFCPPeer struct { stop bool } -func NewPFCPPeer(srv *PFCPEntity, nodeID *ie.IE) (peer *PFCPPeer, err error) { +func NewPFCPPeer(srv PFCPEntityInterface, nodeID *ie.IE) (peer *PFCPPeer, err error) { ipAddr, err := nodeID.NodeID() if err != nil { return nil, err @@ -187,12 +187,12 @@ func (peer *PFCPPeer) Send(msg message.Message) (m message.Message, err error) { // Send an Heartbeat request, return true if the PFCP peer is alive. func (peer *PFCPPeer) IsAlive() (res bool, err error) { - if peer.Srv.RecoveryTimeStamp == nil { + if peer.Srv.RecoveryTimeStamp() == nil { return false, fmt.Errorf("SMF is not started.") } hreq := message.NewHeartbeatRequest( 0, - peer.Srv.RecoveryTimeStamp, + peer.Srv.RecoveryTimeStamp(), nil) _, err = peer.Send(hreq) diff --git a/pfcp_server_entity.go b/pfcp_server_entity.go index 7f76289..5c7b290 100644 --- a/pfcp_server_entity.go +++ b/pfcp_server_entity.go @@ -1,11 +1,9 @@ package pfcp_networking import ( - "fmt" "net" "sync" - "github.com/louisroyer/go-pfcp-networking/pfcputil" "github.com/wmnsk/go-pfcp/message" ) @@ -22,29 +20,17 @@ func NewPFCPServerEntity(nodeID string) PFCPServerEntity { associations: make(map[string]*PFCPAssociation), muAssociations: sync.Mutex{}, } + e.iface = e e.initDefaultHandlers() return e } func (e *PFCPServerEntity) initDefaultHandlers() error { - return e.AddServerHandler(message.MsgTypeAssociationSetupRequest, handleAssociationSetupRequest) -} - -func (e *PFCPServerEntity) AddServerHandler(t pfcputil.MessageType, h serverHandler) error { - f := func(entity *PFCPEntity, senderAddr net.Addr, msg message.Message) error { - if e == nil { - return fmt.Errorf("PFCPServerEntity is nil") - } - if e.RecoveryTimeStamp == nil { - return fmt.Errorf("RecoveryTimestamp is nil") - } - return h(e, senderAddr, msg) - } - return e.AddHandler(t, f) + return e.AddHandler(message.MsgTypeAssociationSetupRequest, handleAssociationSetupRequest) } // Add an association to the association table -func (e *PFCPServerEntity) CreatePFCPAssociation(association *PFCPAssociation) error { +func (e PFCPServerEntity) CreatePFCPAssociation(association *PFCPAssociation) error { nid, err := association.NodeID.NodeID() if err != nil { return err @@ -56,7 +42,7 @@ func (e *PFCPServerEntity) CreatePFCPAssociation(association *PFCPAssociation) e } // Remove an association from the association table -func (e *PFCPServerEntity) RemovePFCPAssociation(association *PFCPAssociation) error { +func (e PFCPServerEntity) RemovePFCPAssociation(association *PFCPAssociation) error { nid, err := association.NodeID.NodeID() if err != nil { return err