Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

chore: Client requires a message instead of individual IE's #20

Merged
merged 2 commits into from
Dec 27, 2023
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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -48,17 +50,13 @@ import (
func main() {
pfcpServer := server.New("localhost:8805")
pfcpServer.HeartbeatRequest(HandleHeartbeatRequest)
pfcpServer.HeartbeatResponse(HandleHeartbeatResponse)
pfcpServer.Run()
}

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
Expand Down
40 changes: 20 additions & 20 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,79 +44,79 @@ 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)
}
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)
}
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)
}
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)
}
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)
}
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)
Expand Down
4 changes: 3 additions & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 12 additions & 1 deletion messages/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions messages/pfcp_association_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions messages/pfcp_association_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions messages/pfcp_association_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
}
Expand All @@ -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()

Expand All @@ -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)
}
Expand Down
8 changes: 6 additions & 2 deletions tests/pfcp_association_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
8 changes: 6 additions & 2 deletions tests/pfcp_association_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
9 changes: 6 additions & 3 deletions tests/pfcp_association_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
Loading