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

Commit

Permalink
chore: Removes IE header from each ie
Browse files Browse the repository at this point in the history
  • Loading branch information
gruyaume committed Jan 22, 2024
1 parent c7aeec0 commit 68377dd
Show file tree
Hide file tree
Showing 46 changed files with 344 additions and 819 deletions.
63 changes: 24 additions & 39 deletions ie/apply_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import (
)

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

type ApplyActionFlag int
Expand Down Expand Up @@ -62,11 +61,6 @@ func NewApplyAction(flag ApplyActionFlag, extraFlags []ApplyActionExtraFlag) (Ap
var bdpn bool
var edrt bool

ieHeader := Header{
Type: IEType(ApplyActionIEType),
Length: 2,
}

if (contains(extraFlags, NOCP) || contains(extraFlags, BDPN) || contains(extraFlags, DDPN)) && flag != BUFF {
return ApplyAction{}, fmt.Errorf("the NOCP flag, BDPN and DDPN flag may only be set if the BUFF flag is set")
}
Expand Down Expand Up @@ -124,27 +118,23 @@ func NewApplyAction(flag ApplyActionFlag, extraFlags []ApplyActionExtraFlag) (Ap
}

return ApplyAction{
Header: ieHeader,
DFRT: dfrt,
IPMD: ipmd,
IPMA: ipma,
DUPL: dupl,
NOCP: nocp,
BUFF: buff,
FORW: forw,
DROP: drop,
DDPN: ddpn,
BDPN: bdpn,
EDRT: edrt,
DFRT: dfrt,
IPMD: ipmd,
IPMA: ipma,
DUPL: dupl,
NOCP: nocp,
BUFF: buff,
FORW: forw,
DROP: drop,
DDPN: ddpn,
BDPN: bdpn,
EDRT: edrt,
}, nil
}

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

// Octets 1 to 4: Header
buf.Write(applyaction.Header.Serialize())

// 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 {
Expand Down Expand Up @@ -189,13 +179,8 @@ func (applyaction ApplyAction) Serialize() []byte {
return buf.Bytes()
}

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

func (applyaction ApplyAction) SetHeader(header Header) InformationElement {
applyaction.Header = header
return applyaction
func (applyAction ApplyAction) GetType() IEType {
return ApplyActionIEType
}

func DeserializeApplyAction(ieValue []byte) (ApplyAction, error) {
Expand Down
10 changes: 1 addition & 9 deletions ie/apply_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ func TestGivenCorrectValuesWhenNewApplyActionThenFieldsSetCorrectly(t *testing.T
t.Fatalf("Error creating ApplyAction: %v", err)
}

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

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

if applyAction.FORW != true {
t.Errorf("Expected FORW %v, got %v", flag, applyAction.FORW)
}
Expand Down Expand Up @@ -114,7 +106,7 @@ func TestGivenApplyActionSerializedWhenDeserializeThenFieldsSetCorrectly(t *test

serialized := applyAction.Serialize()

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

if err != nil {
t.Fatalf("Error deserializing ApplyAction: %v", err)
Expand Down
23 changes: 4 additions & 19 deletions ie/cause.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

type Cause struct {
Header Header
Value CauseValue
Value CauseValue
}

type CauseValue uint8
Expand Down Expand Up @@ -38,36 +37,22 @@ func NewCause(value CauseValue) (Cause, error) {
return Cause{}, fmt.Errorf("invalid value for Cause: %d", value)
}

header := Header{
Type: CauseIEType,
Length: 1,
}

return Cause{
Header: header,
Value: value,
Value: value,
}, nil
}

func (cause Cause) Serialize() []byte {
buf := new(bytes.Buffer)

// Octets 1 to 4: Header
buf.Write(cause.Header.Serialize())

// Octet 5: Value (1 byte)
buf.WriteByte(uint8(cause.Value))

return buf.Bytes()
}

func (cause Cause) IsZeroValue() bool {
return cause.Value == 0
}

func (cause Cause) SetHeader(ieHeader Header) InformationElement {
cause.Header = ieHeader
return cause
func (cause Cause) GetType() IEType {
return CauseIEType
}

func DeserializeCause(ieValue []byte) (Cause, error) {
Expand Down
58 changes: 19 additions & 39 deletions ie/create_far.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ import (
)

type CreateFAR struct {
Header Header
FARID FARID
ApplyAction ApplyAction
}

func NewCreateFAR(farid FARID, applyaction ApplyAction) (CreateFAR, error) {
ieHeader := Header{
Type: IEType(CreateFARIEType),
Length: farid.Header.Length + applyaction.Header.Length + 8,
}

return CreateFAR{
Header: ieHeader,
FARID: farid,
ApplyAction: applyaction,
}, nil
Expand All @@ -28,28 +22,32 @@ func NewCreateFAR(farid FARID, applyaction ApplyAction) (CreateFAR, error) {
func (createfar CreateFAR) Serialize() []byte {
buf := new(bytes.Buffer)

// Octets 1 to 4: Header
buf.Write(createfar.Header.Serialize())

// Octets 5 to n: FAR ID
serializedFARID := createfar.FARID.Serialize()
farIDLength := uint16(len(serializedFARID))
farIDHeader := Header{
Type: createfar.FARID.GetType(),
Length: farIDLength,
}
buf.Write(farIDHeader.Serialize())
buf.Write(serializedFARID)

// Octets n+1 to m: Apply Action
serializedApplyAction := createfar.ApplyAction.Serialize()
applyActionLength := uint16(len(serializedApplyAction))
applyActionHeader := Header{
Type: createfar.ApplyAction.GetType(),
Length: applyActionLength,
}
buf.Write(applyActionHeader.Serialize())
buf.Write(serializedApplyAction)

return buf.Bytes()

}

func (createfar CreateFAR) IsZeroValue() bool {
return createfar.Header.Length == 0
}

func (createfar CreateFAR) SetHeader(header Header) InformationElement {
createfar.Header = header
return createfar
func (createfar CreateFAR) GetType() IEType {
return CreateFARIEType
}

func DeserializeCreateFAR(value []byte) (CreateFAR, error) {
Expand All @@ -65,7 +63,7 @@ func DeserializeCreateFAR(value []byte) (CreateFAR, error) {
if buffer.Len() < 2 {
return CreateFAR{}, fmt.Errorf("not enough data for FARID type")
}
faridIEType := binary.BigEndian.Uint16(buffer.Next(2))
buffer.Next(2)

if buffer.Len() < 2 {
return CreateFAR{}, fmt.Errorf("not enough data for FARID length")
Expand All @@ -74,26 +72,18 @@ func DeserializeCreateFAR(value []byte) (CreateFAR, error) {
faridIELength := binary.BigEndian.Uint16(buffer.Next(2))
faridIEValue := buffer.Next(int(faridIELength))

faridIEHEader := Header{
Type: IEType(faridIEType),
Length: faridIELength,
}

tempFarid, err := DeserializeFARID(faridIEValue)
farid, err := DeserializeFARID(faridIEValue)
if err != nil {
return CreateFAR{}, fmt.Errorf("failed to deserialize FARID: %v", err)
}
farid, ok := tempFarid.SetHeader(faridIEHEader).(FARID)
if !ok {
return CreateFAR{}, fmt.Errorf("type assertion to FarID failed")
}

createfar.FARID = farid

if buffer.Len() < 2 {
return CreateFAR{}, fmt.Errorf("not enough data for ApplyAction type")
}
applyactionIEType := binary.BigEndian.Uint16(buffer.Next(2))

buffer.Next(2)

if buffer.Len() < 2 {
return CreateFAR{}, fmt.Errorf("not enough data for ApplyAction length")
Expand All @@ -105,21 +95,11 @@ func DeserializeCreateFAR(value []byte) (CreateFAR, error) {
}
applyactionIEValue := buffer.Next(int(applyactionIELength))

applyActionHeader := Header{
Type: IEType(applyactionIEType),
Length: applyactionIELength,
}

tempApplyaction, err := DeserializeApplyAction(applyactionIEValue)
applyaction, err := DeserializeApplyAction(applyactionIEValue)
if err != nil {
return CreateFAR{}, fmt.Errorf("failed to deserialize ApplyAction: %v", err)
}

applyaction, ok := tempApplyaction.SetHeader(applyActionHeader).(ApplyAction)
if !ok {
return CreateFAR{}, fmt.Errorf("type assertion to ApplyAction failed")
}

createfar.ApplyAction = applyaction
return createfar, nil
}
11 changes: 1 addition & 10 deletions ie/create_far_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,10 @@ func TestGivenCorrectValuesWhenNewFarThenFieldsSetCorrectly(t *testing.T) {
t.Errorf("Expected BDPN false, got %v", createFar.ApplyAction.BDPN)
}

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

if createFar.ApplyAction.Header.Type != 44 {
t.Errorf("Expected IEType 44, got %d", createFar.ApplyAction.Header.Type)
}

}

func TestGivenSerializedWhenDeserializeCreateFarThenFieldsSetCorrectly(t *testing.T) {
farId, err := ie.NewFarID(1)

if err != nil {
t.Fatalf("Error creating FARID: %v", err)
}
Expand All @@ -105,7 +96,7 @@ func TestGivenSerializedWhenDeserializeCreateFarThenFieldsSetCorrectly(t *testin

serialized := createFar.Serialize()

deserialized, err := ie.DeserializeCreateFAR(serialized[4:])
deserialized, err := ie.DeserializeCreateFAR(serialized)

if err != nil {
t.Fatalf("Error deserializing CreateFAR: %v", err)
Expand Down
Loading

0 comments on commit 68377dd

Please sign in to comment.