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

Commit

Permalink
Adds create far
Browse files Browse the repository at this point in the history
  • Loading branch information
gruyaume committed Jan 6, 2024
1 parent c8688fb commit 34ee467
Show file tree
Hide file tree
Showing 13 changed files with 543 additions and 31 deletions.
130 changes: 130 additions & 0 deletions ie/apply_action.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,136 @@
package ie

import (
"bytes"
"encoding/binary"
"fmt"
)

type ApplyAction struct {
IEType uint16
Length uint16
DFRT bool
IPMD bool
IPMA bool
DUPL bool
NOCP bool
BUFF bool
FORW bool
DROP bool
DDPN bool
BDPN bool
EDRT bool
}

func NewApplyAction(dfrt, ipmd, ipma, dupl, nocp, buff, forw, drop, ddpn, bdpn, edrt bool) ApplyAction {
return ApplyAction{
IEType: uint16(ApplyActionIEType),
Length: 2,
DFRT: dfrt,
IPMD: ipmd,
IPMA: ipma,
DUPL: dupl,
NOCP: nocp,
BUFF: buff,
FORW: forw,
DROP: drop,
DDPN: ddpn,
BDPN: bdpn,
EDRT: edrt,
}
}

func (applyaction ApplyAction) Serialize() []byte {
buf := new(bytes.Buffer)

// Octets 1 to 2: Type
binary.Write(buf, binary.BigEndian, uint16(applyaction.IEType))

// Octets 3 to 4: Length
binary.Write(buf, binary.BigEndian, uint16(applyaction.Length))

// Octet 5: DFRT (bit 8), IPMD (bit 7), IPMA (bit 6), DUPL (bit 5), NOCP (bit 4), BUFF (bit 3), FORW (bit 2), DROP (bit 1)
var byte5 byte
if applyaction.DFRT {
byte5 |= 1 << 7
}
if applyaction.IPMD {
byte5 |= 1 << 6
}
if applyaction.IPMA {
byte5 |= 1 << 5
}
if applyaction.DUPL {
byte5 |= 1 << 4
}
if applyaction.NOCP {
byte5 |= 1 << 3
}
if applyaction.BUFF {
byte5 |= 1 << 2
}
if applyaction.FORW {
byte5 |= 1 << 1
}
if applyaction.DROP {
byte5 |= 1
}
buf.WriteByte(byte5)

// Octet 6: Spare (bits 8 to 4), DDPN (bit 3), BDPN (bit 2), EDRT (bit 1)
var byte6 byte
if applyaction.DDPN {
byte6 |= 1 << 2
}
if applyaction.BDPN {
byte6 |= 1 << 1
}
if applyaction.EDRT {
byte6 |= 1
}
buf.WriteByte(byte6)

return buf.Bytes()
}

func (applyaction ApplyAction) IsZeroValue() bool {
return applyaction.Length == 0
}

func DeserializeApplyAction(ieType uint16, ieLength uint16, ieValue []byte) (ApplyAction, error) {
var applyaction ApplyAction

if ieType != uint16(ApplyActionIEType) {
return applyaction, fmt.Errorf("invalid IE type: expected %d, got %d", ApplyActionIEType, ieType)
}

if ieLength != 2 {
return applyaction, fmt.Errorf("invalid length field for ApplyAction: expected 2, got %d", ieLength)
}

if len(ieValue) != 2 {
return applyaction, fmt.Errorf("invalid length for ApplyAction: got %d bytes, want 2", len(ieValue))
}

applyaction.IEType = ieType
applyaction.Length = ieLength

// Deserialize the first byte (Octet 5)
byte5 := ieValue[0]
applyaction.DFRT = byte5&(1<<7) != 0
applyaction.IPMD = byte5&(1<<6) != 0
applyaction.IPMA = byte5&(1<<5) != 0
applyaction.DUPL = byte5&(1<<4) != 0
applyaction.NOCP = byte5&(1<<3) != 0
applyaction.BUFF = byte5&(1<<2) != 0
applyaction.FORW = byte5&(1<<1) != 0
applyaction.DROP = byte5&1 != 0

// Deserialize the second byte (Octet 6)
byte6 := ieValue[1]
applyaction.DDPN = byte6&(1<<2) != 0
applyaction.BDPN = byte6&(1<<1) != 0
applyaction.EDRT = byte6&1 != 0

return applyaction, nil
}
153 changes: 153 additions & 0 deletions ie/apply_action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package ie_test

import (
"testing"

"github.com/dot-5g/pfcp/ie"
)

func TestGivenCorrectValuesWhenNewApplyActionThenFieldsSetCorrectly(t *testing.T) {
dfrt := true
ipmd := false
ipma := true
dupl := false
nocp := false
buff := true
forw := false
drop := true
ddpn := true
bdpn := true
edrt := false

applyAction := ie.NewApplyAction(dfrt, ipmd, ipma, dupl, nocp, buff, forw, drop, ddpn, bdpn, edrt)

if applyAction.IEType != 44 {
t.Errorf("Expected IEType 44, got %d", applyAction.IEType)
}

if applyAction.Length != 2 {
t.Errorf("Expected Length 2, got %d", applyAction.Length)
}

if applyAction.DFRT != dfrt {
t.Errorf("Expected DFRT %v, got %v", dfrt, applyAction.DFRT)
}

if applyAction.IPMD != ipmd {
t.Errorf("Expected IPMD %v, got %v", ipmd, applyAction.IPMD)
}

if applyAction.IPMA != ipma {
t.Errorf("Expected IPMA %v, got %v", ipma, applyAction.IPMA)
}

if applyAction.DUPL != dupl {
t.Errorf("Expected DUPL %v, got %v", dupl, applyAction.DUPL)
}

if applyAction.NOCP != nocp {
t.Errorf("Expected NOCP %v, got %v", nocp, applyAction.NOCP)
}

if applyAction.BUFF != buff {
t.Errorf("Expected BUFF %v, got %v", buff, applyAction.BUFF)
}

if applyAction.FORW != forw {
t.Errorf("Expected FORW %v, got %v", forw, applyAction.FORW)
}

if applyAction.DROP != drop {
t.Errorf("Expected DROP %v, got %v", drop, applyAction.DROP)
}

if applyAction.DDPN != ddpn {
t.Errorf("Expected DDPN %v, got %v", ddpn, applyAction.DDPN)
}

if applyAction.BDPN != bdpn {
t.Errorf("Expected BDPN %v, got %v", bdpn, applyAction.BDPN)
}

if applyAction.EDRT != edrt {
t.Errorf("Expected EDRT %v, got %v", edrt, applyAction.EDRT)
}

}

func TestGivenApplyActionSerializedWhenDeserializeThenFieldsSetCorrectly(t *testing.T) {
dfrt := true
ipmd := false
ipma := true
dupl := false
nocp := false
buff := true
forw := false
drop := true
ddpn := true
bdpn := true
edrt := false

applyAction := ie.NewApplyAction(dfrt, ipmd, ipma, dupl, nocp, buff, forw, drop, ddpn, bdpn, edrt)

serialized := applyAction.Serialize()

deserialized, err := ie.DeserializeApplyAction(44, 2, serialized[4:])

if err != nil {
t.Fatalf("Error deserializing ApplyAction: %v", err)
}

if deserialized.IEType != 44 {
t.Errorf("Expected IEType 44, got %d", deserialized.IEType)
}

if deserialized.Length != 2 {
t.Errorf("Expected Length 2, got %d", deserialized.Length)
}

if deserialized.DFRT != dfrt {
t.Errorf("Expected DFRT %v, got %v", dfrt, deserialized.DFRT)
}

if deserialized.IPMD != ipmd {
t.Errorf("Expected IPMD %v, got %v", ipmd, deserialized.IPMD)
}

if deserialized.IPMA != ipma {
t.Errorf("Expected IPMA %v, got %v", ipma, deserialized.IPMA)
}

if deserialized.DUPL != dupl {
t.Errorf("Expected DUPL %v, got %v", dupl, deserialized.DUPL)
}

if deserialized.NOCP != nocp {
t.Errorf("Expected NOCP %v, got %v", nocp, deserialized.NOCP)
}

if deserialized.BUFF != buff {
t.Errorf("Expected BUFF %v, got %v", buff, deserialized.BUFF)
}

if deserialized.FORW != forw {
t.Errorf("Expected FORW %v, got %v", forw, deserialized.FORW)
}

if deserialized.DROP != drop {
t.Errorf("Expected DROP %v, got %v", drop, deserialized.DROP)
}

if deserialized.DDPN != ddpn {
t.Errorf("Expected DDPN %v, got %v", ddpn, deserialized.DDPN)
}

if deserialized.BDPN != bdpn {
t.Errorf("Expected BDPN %v, got %v", bdpn, deserialized.BDPN)
}

if deserialized.EDRT != edrt {
t.Errorf("Expected EDRT %v, got %v", edrt, deserialized.EDRT)
}

}
8 changes: 4 additions & 4 deletions ie/cause.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
)

type Cause struct {
IEtype uint16
IEType uint16
Length uint16
Value uint8
}

func NewCause(value int) Cause {
return Cause{
IEtype: uint16(CauseIEType),
IEType: uint16(CauseIEType),
Length: 1,
Value: uint8(value),
}
Expand All @@ -24,7 +24,7 @@ func (cause Cause) Serialize() []byte {
buf := new(bytes.Buffer)

// Octets 1 to 2: Type
binary.Write(buf, binary.BigEndian, uint16(cause.IEtype))
binary.Write(buf, binary.BigEndian, uint16(cause.IEType))

// Octets 3 to 4: Length
binary.Write(buf, binary.BigEndian, uint16(cause.Length))
Expand Down Expand Up @@ -55,7 +55,7 @@ func DeserializeCause(ieType uint16, ieLength uint16, ieValue []byte) (Cause, er
}

return Cause{
IEtype: ieType,
IEType: ieType,
Length: ieLength,
Value: ieValue[0],
}, nil
Expand Down
Loading

0 comments on commit 34ee467

Please sign in to comment.