diff --git a/README.md b/README.md index 2e1c0e5..a6b4f0c 100644 --- a/README.md +++ b/README.md @@ -18,14 +18,16 @@ import ( "github.com/dot-5g/pfcp/client" "github.com/dot-5g/pfcp/ie" + "github.com/dot-5g/pfcp/messages" ) func main() { pfcpClient := client.New("1.2.3.4:8805") recoveryTimeStamp := ie.NewRecoveryTimeStamp(time.Now()) sequenceNumber := uint32(21) + heartbeatRequestMsg := messages.NewHeartbeatRequest(recoveryTimeStamp) - _, err := pfcpClient.SendHeartbeatRequest(recoveryTimeStamp, sequenceNumber) + _, err := pfcpClient.SendHeartbeatRequest(heartbeatRequestMsg, sequenceNumber) if err != nil { log.Fatalf("SendHeartbeatRequest failed: %v", err) } @@ -48,7 +50,6 @@ import ( func main() { pfcpServer := server.New("localhost:8805") pfcpServer.HeartbeatRequest(HandleHeartbeatRequest) - pfcpServer.HeartbeatResponse(HandleHeartbeatResponse) pfcpServer.Run() } @@ -56,9 +57,6 @@ func HandleHeartbeatRequest(sequenceNumber uint32, msg messages.HeartbeatRequest fmt.Printf("Received Heartbeat Request - Recovery TimeStamp: %v", msg.RecoveryTimeStamp) } -func HandleHeartbeatResponse(sequenceNumber uint32, msg messages.HeartbeatResponse) { - fmt.Printf("Received Heartbeat Response - Recovery TimeStamp: %v", msg.RecoveryTimeStamp) -} ``` ## Procedures diff --git a/client/client.go b/client/client.go index 4691af6..1e4ff98 100644 --- a/client/client.go +++ b/client/client.go @@ -44,29 +44,29 @@ func serializeMessage(header headers.PFCPHeader, payload []byte) []byte { return append(headerBytes, payload...) } -func (pfcp *Pfcp) SendHeartbeatRequest(recoveryTimeStamp ie.RecoveryTimeStamp, sequenceNumber uint32) (ie.RecoveryTimeStamp, error) { +func (pfcp *Pfcp) SendHeartbeatRequest(msg messages.HeartbeatRequest, sequenceNumber uint32) (ie.RecoveryTimeStamp, error) { header := headers.NewPFCPHeader(messages.HeartbeatRequestMessageType, sequenceNumber) - payload := []ie.InformationElement{recoveryTimeStamp} + payload := []ie.InformationElement{msg.RecoveryTimeStamp} err := pfcp.sendPfcpMessage(header, payload) if err != nil { - return recoveryTimeStamp, fmt.Errorf("error sending PFCP Heartbeat Request: %w", err) + return msg.RecoveryTimeStamp, fmt.Errorf("error sending PFCP Heartbeat Request: %w", err) } - return recoveryTimeStamp, nil + return msg.RecoveryTimeStamp, nil } -func (pfcp *Pfcp) SendHeartbeatResponse(recoveryTimeStamp ie.RecoveryTimeStamp, sequenceNumber uint32) (ie.RecoveryTimeStamp, error) { +func (pfcp *Pfcp) SendHeartbeatResponse(msg messages.HeartbeatResponse, sequenceNumber uint32) (ie.RecoveryTimeStamp, error) { header := headers.NewPFCPHeader(messages.HeartbeatResponseMessageType, sequenceNumber) - payload := []ie.InformationElement{recoveryTimeStamp} + payload := []ie.InformationElement{msg.RecoveryTimeStamp} err := pfcp.sendPfcpMessage(header, payload) if err != nil { - return recoveryTimeStamp, fmt.Errorf("error sending PFCP Heartbeat Response: %w", err) + return msg.RecoveryTimeStamp, fmt.Errorf("error sending PFCP Heartbeat Response: %w", err) } - return recoveryTimeStamp, nil + return msg.RecoveryTimeStamp, nil } -func (pfcp *Pfcp) SendPFCPAssociationSetupRequest(nodeID ie.NodeID, recoveryTimeStamp ie.RecoveryTimeStamp, sequenceNumber uint32) error { +func (pfcp *Pfcp) SendPFCPAssociationSetupRequest(msg messages.PFCPAssociationSetupRequest, sequenceNumber uint32) error { header := headers.NewPFCPHeader(messages.PFCPAssociationSetupRequestMessageType, sequenceNumber) - payload := []ie.InformationElement{nodeID, recoveryTimeStamp} + payload := []ie.InformationElement{msg.NodeID, msg.RecoveryTimeStamp} err := pfcp.sendPfcpMessage(header, payload) if err != nil { return fmt.Errorf("error sending PFCP Association Setup Request: %w", err) @@ -74,9 +74,9 @@ func (pfcp *Pfcp) SendPFCPAssociationSetupRequest(nodeID ie.NodeID, recoveryTime return nil } -func (pfcp *Pfcp) SendPFCPAssociationSetupResponse(nodeID ie.NodeID, cause ie.Cause, recoveryTimeStamp ie.RecoveryTimeStamp, sequenceNumber uint32) error { +func (pfcp *Pfcp) SendPFCPAssociationSetupResponse(msg messages.PFCPAssociationSetupResponse, sequenceNumber uint32) error { header := headers.NewPFCPHeader(messages.PFCPAssociationSetupResponseMessageType, sequenceNumber) - payload := []ie.InformationElement{nodeID, cause, recoveryTimeStamp} + payload := []ie.InformationElement{msg.NodeID, msg.Cause, msg.RecoveryTimeStamp} err := pfcp.sendPfcpMessage(header, payload) if err != nil { return fmt.Errorf("error sending PFCP Association Setup Response: %w", err) @@ -84,9 +84,9 @@ func (pfcp *Pfcp) SendPFCPAssociationSetupResponse(nodeID ie.NodeID, cause ie.Ca return nil } -func (pfcp *Pfcp) SendPFCPAssociationUpdateRequest(nodeID ie.NodeID, sequenceNumber uint32) error { +func (pfcp *Pfcp) SendPFCPAssociationUpdateRequest(msg messages.PFCPAssociationUpdateRequest, sequenceNumber uint32) error { header := headers.NewPFCPHeader(messages.PFCPAssociationUpdateRequestMessageType, sequenceNumber) - payload := []ie.InformationElement{nodeID} + payload := []ie.InformationElement{msg.NodeID} err := pfcp.sendPfcpMessage(header, payload) if err != nil { return fmt.Errorf("error sending PFCP Association Update Request: %w", err) @@ -94,9 +94,9 @@ func (pfcp *Pfcp) SendPFCPAssociationUpdateRequest(nodeID ie.NodeID, sequenceNum return nil } -func (pfcp *Pfcp) SendPFCPAssociationUpdateResponse(nodeID ie.NodeID, cause ie.Cause, sequenceNumber uint32) error { +func (pfcp *Pfcp) SendPFCPAssociationUpdateResponse(msg messages.PFCPAssociationUpdateResponse, sequenceNumber uint32) error { header := headers.NewPFCPHeader(messages.PFCPAssociationUpdateResponseMessageType, sequenceNumber) - payload := []ie.InformationElement{nodeID, cause} + payload := []ie.InformationElement{msg.NodeID, msg.Cause} err := pfcp.sendPfcpMessage(header, payload) if err != nil { return fmt.Errorf("error sending PFCP Association Update Response: %w", err) @@ -104,9 +104,9 @@ func (pfcp *Pfcp) SendPFCPAssociationUpdateResponse(nodeID ie.NodeID, cause ie.C return nil } -func (pfcp *Pfcp) SendPFCPAssociationReleaseRequest(nodeID ie.NodeID, sequenceNumber uint32) error { +func (pfcp *Pfcp) SendPFCPAssociationReleaseRequest(msg messages.PFCPAssociationReleaseRequest, sequenceNumber uint32) error { header := headers.NewPFCPHeader(messages.PFCPAssociationReleaseRequestMessageType, sequenceNumber) - payload := []ie.InformationElement{nodeID} + payload := []ie.InformationElement{msg.NodeID} err := pfcp.sendPfcpMessage(header, payload) if err != nil { return fmt.Errorf("error sending PFCP Association Release Request: %w", err) @@ -114,9 +114,9 @@ func (pfcp *Pfcp) SendPFCPAssociationReleaseRequest(nodeID ie.NodeID, sequenceNu return nil } -func (pfcp *Pfcp) SendPFCPAssociationReleaseResponse(nodeID ie.NodeID, cause ie.Cause, sequenceNumber uint32) error { +func (pfcp *Pfcp) SendPFCPAssociationReleaseResponse(msg messages.PFCPAssociationReleaseResponse, sequenceNumber uint32) error { header := headers.NewPFCPHeader(messages.PFCPAssociationReleaseResponseMessageType, sequenceNumber) - payload := []ie.InformationElement{nodeID, cause} + payload := []ie.InformationElement{msg.NodeID, msg.Cause} err := pfcp.sendPfcpMessage(header, payload) if err != nil { return fmt.Errorf("error sending PFCP Association Release Response: %w", err) diff --git a/client/client_test.go b/client/client_test.go index b40ff83..d17eb13 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -6,6 +6,7 @@ import ( "github.com/dot-5g/pfcp/client" "github.com/dot-5g/pfcp/ie" + "github.com/dot-5g/pfcp/messages" ) type MockUdpSender struct { @@ -26,8 +27,9 @@ func TestGivenPfcpWhenSendHeartbeatRequestThenNoError(t *testing.T) { pfcpClient.Udp = mockSender recoveryTimeStamp := ie.NewRecoveryTimeStamp(time.Now()) sequenceNumber := uint32(21) + heartbeatRequestMsg := messages.NewHeartbeatRequest(recoveryTimeStamp) - _, err := pfcpClient.SendHeartbeatRequest(recoveryTimeStamp, sequenceNumber) + _, err := pfcpClient.SendHeartbeatRequest(heartbeatRequestMsg, sequenceNumber) if err != nil { t.Errorf("SendHeartbeatRequest failed: %v", err) diff --git a/main.go b/main.go index 7168893..472aa06 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,9 @@ func main() { pfcpClient := client.New("1.2.3.4:8805") recoveryTimeStamp := ie.NewRecoveryTimeStamp(time.Now()) sequenceNumber := uint32(21) + heartbeatRequestMsg := messages.NewHeartbeatRequest(recoveryTimeStamp) - _, err := pfcpClient.SendHeartbeatRequest(recoveryTimeStamp, sequenceNumber) + _, err := pfcpClient.SendHeartbeatRequest(heartbeatRequestMsg, sequenceNumber) if err != nil { log.Fatalf("SendHeartbeatRequest failed: %v", err) } diff --git a/messages/heartbeat.go b/messages/heartbeat.go index 76bc565..9a03c4c 100644 --- a/messages/heartbeat.go +++ b/messages/heartbeat.go @@ -11,11 +11,22 @@ type HeartbeatRequest struct { } type HeartbeatResponse struct { - MessageType MessageType SequenceNumber uint32 RecoveryTimeStamp ie.RecoveryTimeStamp } +func NewHeartbeatRequest(recoveryTimeStamp ie.RecoveryTimeStamp) HeartbeatRequest { + return HeartbeatRequest{ + RecoveryTimeStamp: recoveryTimeStamp, + } +} + +func NewHeartbeatResponse(recoveryTimeStamp ie.RecoveryTimeStamp) HeartbeatResponse { + return HeartbeatResponse{ + RecoveryTimeStamp: recoveryTimeStamp, + } +} + func ParseHeartbeatRequest(data []byte) (HeartbeatRequest, error) { ies, err := ie.ParseInformationElements(data) var recoveryTimeStamp ie.RecoveryTimeStamp diff --git a/messages/pfcp_association_release.go b/messages/pfcp_association_release.go index e8e61c9..e15f91b 100644 --- a/messages/pfcp_association_release.go +++ b/messages/pfcp_association_release.go @@ -13,6 +13,19 @@ type PFCPAssociationReleaseResponse struct { Cause ie.Cause } +func NewPFCPAssociationReleaseRequest(nodeID ie.NodeID) PFCPAssociationReleaseRequest { + return PFCPAssociationReleaseRequest{ + NodeID: nodeID, + } +} + +func NewPFCPAssociationReleaseResponse(nodeID ie.NodeID, cause ie.Cause) PFCPAssociationReleaseResponse { + return PFCPAssociationReleaseResponse{ + NodeID: nodeID, + Cause: cause, + } +} + func ParsePFCPAssociationReleaseRequest(data []byte) (PFCPAssociationReleaseRequest, error) { ies, err := ie.ParseInformationElements(data) var nodeID ie.NodeID diff --git a/messages/pfcp_association_setup.go b/messages/pfcp_association_setup.go index 7c3eea6..4e11136 100644 --- a/messages/pfcp_association_setup.go +++ b/messages/pfcp_association_setup.go @@ -3,20 +3,31 @@ package messages import "github.com/dot-5g/pfcp/ie" type PFCPAssociationSetupRequest struct { - MessageType MessageType - SequenceNumber uint32 NodeID ie.NodeID RecoveryTimeStamp ie.RecoveryTimeStamp } type PFCPAssociationSetupResponse struct { - MessageType MessageType - SequenceNumber uint32 NodeID ie.NodeID Cause ie.Cause RecoveryTimeStamp ie.RecoveryTimeStamp } +func NewPFCPAssociationSetupRequest(nodeID ie.NodeID, recoveryTimeStamp ie.RecoveryTimeStamp) PFCPAssociationSetupRequest { + return PFCPAssociationSetupRequest{ + NodeID: nodeID, + RecoveryTimeStamp: recoveryTimeStamp, + } +} + +func NewPFCPAssociationSetupResponse(nodeID ie.NodeID, cause ie.Cause, recoveryTimeStamp ie.RecoveryTimeStamp) PFCPAssociationSetupResponse { + return PFCPAssociationSetupResponse{ + NodeID: nodeID, + Cause: cause, + RecoveryTimeStamp: recoveryTimeStamp, + } +} + func ParsePFCPAssociationSetupRequest(data []byte) (PFCPAssociationSetupRequest, error) { ies, err := ie.ParseInformationElements(data) var nodeID ie.NodeID diff --git a/messages/pfcp_association_update.go b/messages/pfcp_association_update.go index 6e71987..b5c5102 100644 --- a/messages/pfcp_association_update.go +++ b/messages/pfcp_association_update.go @@ -13,6 +13,19 @@ type PFCPAssociationUpdateResponse struct { Cause ie.Cause } +func NewPFCPAssociationUpdateRequest(nodeID ie.NodeID) PFCPAssociationUpdateRequest { + return PFCPAssociationUpdateRequest{ + NodeID: nodeID, + } +} + +func NewPFCPAssociationUpdateResponse(nodeID ie.NodeID, cause ie.Cause) PFCPAssociationUpdateResponse { + return PFCPAssociationUpdateResponse{ + NodeID: nodeID, + Cause: cause, + } +} + func ParsePFCPAssociationUpdateRequest(data []byte) (PFCPAssociationUpdateRequest, error) { ies, err := ie.ParseInformationElements(data) var nodeID ie.NodeID diff --git a/tests/heartbeat_test.go b/tests/heartbeat_test.go index 7453cc8..eeccfa1 100644 --- a/tests/heartbeat_test.go +++ b/tests/heartbeat_test.go @@ -51,6 +51,7 @@ func HeartbeatRequest(t *testing.T) { pfcpServer.HeartbeatRequest(HandleHeartbeatRequest) sentSequenceNumber := uint32(32) recoveryTimeStamp := ie.NewRecoveryTimeStamp(time.Now()) + heartbeatRequestMsg := messages.NewHeartbeatRequest(recoveryTimeStamp) pfcpServer.Run() @@ -59,7 +60,7 @@ func HeartbeatRequest(t *testing.T) { time.Sleep(time.Second) pfcpClient := client.New("127.0.0.1:8805") - sentRecoveryTimeStamp, err := pfcpClient.SendHeartbeatRequest(recoveryTimeStamp, sentSequenceNumber) + sentRecoveryTimeStamp, err := pfcpClient.SendHeartbeatRequest(heartbeatRequestMsg, sentSequenceNumber) if err != nil { t.Fatalf("Failed to send Heartbeat request: %v", err) } @@ -84,6 +85,7 @@ func HeartbeatResponse(t *testing.T) { pfcpServer.HeartbeatResponse(HandleHeartbeatResponse) sentSequenceNumber := uint32(971) recoveryTimeStamp := ie.NewRecoveryTimeStamp(time.Now()) + heartbeatResponseMsg := messages.NewHeartbeatResponse(recoveryTimeStamp) pfcpServer.Run() @@ -92,7 +94,7 @@ func HeartbeatResponse(t *testing.T) { time.Sleep(time.Second) pfcpClient := client.New("127.0.0.1:8805") - sentRecoveryTimeStamp, err := pfcpClient.SendHeartbeatResponse(recoveryTimeStamp, sentSequenceNumber) + sentRecoveryTimeStamp, err := pfcpClient.SendHeartbeatResponse(heartbeatResponseMsg, sentSequenceNumber) if err != nil { t.Fatalf("Failed to send Heartbeat response: %v", err) } diff --git a/tests/pfcp_association_release_test.go b/tests/pfcp_association_release_test.go index cd8c916..0086761 100644 --- a/tests/pfcp_association_release_test.go +++ b/tests/pfcp_association_release_test.go @@ -60,7 +60,9 @@ func PFCPAssociationReleaseRequest(t *testing.T) { pfcpClient := client.New("127.0.0.1:8805") nodeID := ie.NewNodeID(ie.IPv4, "12.23.34.45") sequenceNumber := uint32(32) - pfcpClient.SendPFCPAssociationReleaseRequest(nodeID, sequenceNumber) + PFCPAssociationReleaseRequestMsg := messages.NewPFCPAssociationReleaseRequest(nodeID) + + pfcpClient.SendPFCPAssociationReleaseRequest(PFCPAssociationReleaseRequestMsg, sequenceNumber) time.Sleep(time.Second) @@ -108,7 +110,9 @@ func PFCPAssociationReleaseResponse(t *testing.T) { sequenceNumber := uint32(32) cause := ie.NewCause(2) - pfcpClient.SendPFCPAssociationReleaseResponse(nodeID, cause, sequenceNumber) + PFCPAssociationReleaseResponseMsg := messages.NewPFCPAssociationReleaseResponse(nodeID, cause) + + pfcpClient.SendPFCPAssociationReleaseResponse(PFCPAssociationReleaseResponseMsg, sequenceNumber) time.Sleep(time.Second) diff --git a/tests/pfcp_association_setup_test.go b/tests/pfcp_association_setup_test.go index 1099b5c..6f9007b 100644 --- a/tests/pfcp_association_setup_test.go +++ b/tests/pfcp_association_setup_test.go @@ -65,7 +65,9 @@ func PFCPAssociationSetupRequest(t *testing.T) { nodeID := ie.NewNodeID(ie.IPv4, "12.23.34.45") recoveryTimeStamp := ie.NewRecoveryTimeStamp(time.Now()) sequenceNumber := uint32(32) - pfcpClient.SendPFCPAssociationSetupRequest(nodeID, recoveryTimeStamp, sequenceNumber) + PFCPAssociationSetupRequestMsg := messages.NewPFCPAssociationSetupRequest(nodeID, recoveryTimeStamp) + + pfcpClient.SendPFCPAssociationSetupRequest(PFCPAssociationSetupRequestMsg, sequenceNumber) time.Sleep(time.Second) @@ -118,7 +120,9 @@ func PFCPAssociationSetupResponse(t *testing.T) { cause := ie.NewCause(2) recoveryTimeStamp := ie.NewRecoveryTimeStamp(time.Now()) sequenceNumber := uint32(32) - pfcpClient.SendPFCPAssociationSetupResponse(nodeID, cause, recoveryTimeStamp, sequenceNumber) + PFCPAssociationSetupResponseMsg := messages.NewPFCPAssociationSetupResponse(nodeID, cause, recoveryTimeStamp) + + pfcpClient.SendPFCPAssociationSetupResponse(PFCPAssociationSetupResponseMsg, sequenceNumber) time.Sleep(time.Second) diff --git a/tests/pfcp_association_update_test.go b/tests/pfcp_association_update_test.go index d86198e..8ddc764 100644 --- a/tests/pfcp_association_update_test.go +++ b/tests/pfcp_association_update_test.go @@ -60,7 +60,9 @@ func PFCPAssociationUpdateRequest(t *testing.T) { pfcpClient := client.New("127.0.0.1:8805") nodeID := ie.NewNodeID(ie.IPv4, "12.23.34.45") sequenceNumber := uint32(32) - pfcpClient.SendPFCPAssociationUpdateRequest(nodeID, sequenceNumber) + PFCPAssociationUpdateRequestMsg := messages.NewPFCPAssociationUpdateRequest(nodeID) + + pfcpClient.SendPFCPAssociationUpdateRequest(PFCPAssociationUpdateRequestMsg, sequenceNumber) time.Sleep(time.Second) @@ -105,10 +107,11 @@ func PFCPAssociationUpdateResponse(t *testing.T) { time.Sleep(time.Second) pfcpClient := client.New("127.0.0.1:8805") nodeID := ie.NewNodeID(ie.IPv4, "3.4.5.6") - sequenceNumber := uint32(32) cause := ie.NewCause(2) - pfcpClient.SendPFCPAssociationUpdateResponse(nodeID, cause, sequenceNumber) + PFCPAssociationUpdateResponseMsg := messages.NewPFCPAssociationUpdateResponse(nodeID, cause) + + pfcpClient.SendPFCPAssociationUpdateResponse(PFCPAssociationUpdateResponseMsg, sequenceNumber) time.Sleep(time.Second)