Skip to content

Commit

Permalink
change dataTimeSeconds type to uint32 (vmware#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyiou authored Oct 30, 2020
1 parent de88d0d commit a947fbd
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/entities/ie.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var InfoElementLength = [...]uint16{
1,
6,
VariableLength,
8,
4,
8,
8,
8,
Expand Down
11 changes: 8 additions & 3 deletions pkg/entities/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,14 @@ func (d *dataRecord) AddInfoElement(element *InfoElement, val interface{}) (uint
} else {
bytesToAppend[0] = 2
}
case DateTimeSeconds, DateTimeMilliseconds:
// We expect time to be given in int64 as unix time type in go
v, ok := val.(int64)
case DateTimeSeconds:
v, ok := val.(uint32)
if !ok {
return 0, fmt.Errorf("val argument is not of type int64")
}
binary.BigEndian.PutUint32(bytesToAppend, uint32(v))
case DateTimeMilliseconds:
v, ok := val.(uint64)
if !ok {
return 0, fmt.Errorf("val argument is not of type int64")
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/entities/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"github.com/stretchr/testify/assert"
"net"
"testing"
"time"
)

var uniqueTemplateID uint16 = 256

func TestPrepareRecord(t *testing.T) {
Expand Down Expand Up @@ -57,6 +59,8 @@ func TestAddInfoElements(t *testing.T) {
NewInfoElement("sourceMacAddress", 56, 12, 0, 6), // mac address
NewInfoElement("sourceIPv4Address", 8, 18, 0, 4), // IP Address
NewInfoElement("interfaceDescription", 83, 13, 0, 65535), // String
NewInfoElement("flowStartSeconds", 150, 14, 0, 8), // dateTimeSeconds
NewInfoElement("flowStartMilliseconds", 152, 15, 0, 8), // dateTimeMilliseconds
}
macAddress, _ := net.ParseMAC("aa:bb:cc:dd:ee:ff")
valData := []interface{}{
Expand All @@ -70,6 +74,8 @@ func TestAddInfoElements(t *testing.T) {
macAddress, // mac address
net.ParseIP("1.2.3.4"), // IP Address
"My Interface in IPFIX lib", // String
uint32(time.Now().Unix()), // dateTimeSeconds
uint64(time.Now().Unix()), // dateTimeMilliseconds
}
addIETests := []struct {
record Record
Expand Down
9 changes: 8 additions & 1 deletion pkg/entities/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,14 @@ func (d DataSet) AddInfoElement(element *InfoElement, val *bytes.Buffer) error {
} else {
d[element.EnterpriseId][element.ElementId] = false
}
case DateTimeSeconds, DateTimeMilliseconds:
case DateTimeSeconds:
var v uint32
err := util.Decode(val, &v)
if err != nil {
return fmt.Errorf("Error in decoding val to uint32: %v", err)
}
d[element.EnterpriseId][element.ElementId] = v
case DateTimeMilliseconds:
var v uint64
err := util.Decode(val, &v)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/registry/registry_IANA.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func loadIANARegistry() {
registerInfoElement(*entities.NewInfoElement("wlanSSID", 147, 13, 0, 65535), 0)
registerInfoElement(*entities.NewInfoElement("flowId", 148, 4, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("observationDomainId", 149, 3, 0, 4), 0)
registerInfoElement(*entities.NewInfoElement("flowStartSeconds", 150, 14, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("flowEndSeconds", 151, 14, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("flowStartSeconds", 150, 14, 0, 4), 0)
registerInfoElement(*entities.NewInfoElement("flowEndSeconds", 151, 14, 0, 4), 0)
registerInfoElement(*entities.NewInfoElement("flowStartMilliseconds", 152, 15, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("flowEndMilliseconds", 153, 15, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("flowStartMicroseconds", 154, 16, 0, 8), 0)
Expand Down Expand Up @@ -254,12 +254,12 @@ func loadIANARegistry() {
registerInfoElement(*entities.NewInfoElement("postIpPrecedence", 257, 1, 0, 1), 0)
registerInfoElement(*entities.NewInfoElement("collectionTimeMilliseconds", 258, 15, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("exportSctpStreamId", 259, 2, 0, 2), 0)
registerInfoElement(*entities.NewInfoElement("maxExportSeconds", 260, 14, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("maxFlowEndSeconds", 261, 14, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("maxExportSeconds", 260, 14, 0, 4), 0)
registerInfoElement(*entities.NewInfoElement("maxFlowEndSeconds", 261, 14, 0, 4), 0)
registerInfoElement(*entities.NewInfoElement("messageMD5Checksum", 262, 0, 0, 65535), 0)
registerInfoElement(*entities.NewInfoElement("messageScope", 263, 1, 0, 1), 0)
registerInfoElement(*entities.NewInfoElement("minExportSeconds", 264, 14, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("minFlowStartSeconds", 265, 14, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("minExportSeconds", 264, 14, 0, 4), 0)
registerInfoElement(*entities.NewInfoElement("minFlowStartSeconds", 265, 14, 0, 4), 0)
registerInfoElement(*entities.NewInfoElement("opaqueOctets", 266, 0, 0, 65535), 0)
registerInfoElement(*entities.NewInfoElement("sessionScope", 267, 1, 0, 1), 0)
registerInfoElement(*entities.NewInfoElement("maxFlowEndMicroseconds", 268, 16, 0, 8), 0)
Expand Down Expand Up @@ -316,7 +316,7 @@ func loadIANARegistry() {
registerInfoElement(*entities.NewInfoElement("selectorIdTotalPktsSelected", 319, 4, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("absoluteError", 320, 10, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("relativeError", 321, 10, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("observationTimeSeconds", 322, 14, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("observationTimeSeconds", 322, 14, 0, 4), 0)
registerInfoElement(*entities.NewInfoElement("observationTimeMilliseconds", 323, 15, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("observationTimeMicroseconds", 324, 16, 0, 8), 0)
registerInfoElement(*entities.NewInfoElement("observationTimeNanoseconds", 325, 17, 0, 8), 0)
Expand Down
4 changes: 4 additions & 0 deletions pkg/registry/registry_antrea.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ func loadAntreaRegistry() {
registerInfoElement(*entities.NewInfoElement("destinationClusterIP", 106, 18, 55829, 4), 55829)
registerInfoElement(*entities.NewInfoElement("destinationServicePort", 107, 2, 55829, 2), 55829)
registerInfoElement(*entities.NewInfoElement("destinationServicePortName", 108, 13, 55829, 65535), 55829)
registerInfoElement(*entities.NewInfoElement("ingressNetworkPolicyName", 109, 13, 55829, 65535), 55829)
registerInfoElement(*entities.NewInfoElement("ingressNetworkPolicyNamespace", 110, 13, 55829, 65535), 55829)
registerInfoElement(*entities.NewInfoElement("egressNetworkPolicyName", 111, 13, 55829, 65535), 55829)
registerInfoElement(*entities.NewInfoElement("egressNetworkPolicyNamespace", 112, 13, 55829, 65535), 55829)
}
19 changes: 16 additions & 3 deletions pkg/test/exporter_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func testExporterToCollector(address net.Addr, t *testing.T) {
klog.Fatalf("Got error when connecting to %s", address.String())
}

// Create template record with 4 fields
// Create template record with 5 fields
templateID := export.NewTemplateID()
tempRec := entities.NewTemplateRecord(4, templateID)
tempRec := entities.NewTemplateRecord(5, templateID)
tempRec.PrepareRecord()
element, err := registry.GetInfoElement("sourceIPv4Address", registry.IANAEnterpriseID)
if err != nil {
Expand All @@ -85,6 +85,12 @@ func testExporterToCollector(address net.Addr, t *testing.T) {
}
tempRec.AddInfoElement(element, nil)

element, err = registry.GetInfoElement("flowStartSeconds", registry.IANAEnterpriseID)
if err != nil {
klog.Errorf("Did not find the element with name flowStartSeconds")
}
tempRec.AddInfoElement(element, nil)

// Send template record
_, err = export.AddRecordAndSendMsg(entities.Template, tempRec)
if err != nil {
Expand Down Expand Up @@ -117,6 +123,12 @@ func testExporterToCollector(address net.Addr, t *testing.T) {
}
dataRec.AddInfoElement(element, "pod1")

element, err = registry.GetInfoElement("flowStartSeconds", registry.IANAEnterpriseID)
if err != nil {
klog.Errorf("Did not find the element with name flowStartSeconds")
}
dataRec.AddInfoElement(element, uint32(1257894000))

// Send data record
_, err = export.AddRecordAndSendMsg(entities.Data, dataRec)
if err != nil {
Expand All @@ -140,7 +152,7 @@ func testExporterToCollector(address net.Addr, t *testing.T) {
if !ok {
t.Error("Template packet is not decoded correctly.")
}
assert.Equal(t, []uint16{8, 12}, templateSet[registry.IANAEnterpriseID], "TemplateSet does not store template elements (IANA) correctly.")
assert.Equal(t, []uint16{8, 12, 150}, templateSet[registry.IANAEnterpriseID], "TemplateSet does not store template elements (IANA) correctly.")
assert.Equal(t, []uint16{1}, templateSet[registry.IANAReversedEnterpriseID], "TemplateSet does not store template elements (reverse information element) correctly.")
assert.Equal(t, []uint16{101}, templateSet[registry.AntreaEnterpriseID], "TemplateSet does not store template elements (Antrea) correctly.")

Expand All @@ -149,6 +161,7 @@ func testExporterToCollector(address net.Addr, t *testing.T) {
t.Error("Data packet is not decoded correctly.")
}
assert.Equal(t, []byte{1, 2, 3, 4}, dataSet[registry.IANAEnterpriseID][8], "DataSet does not store elements (IANA) correctly.")
assert.Equal(t, uint32(1257894000), dataSet[registry.IANAEnterpriseID][150], "DataSet does not store elements (IANA) correctly.")
assert.Equal(t, uint64(12345678), dataSet[registry.IANAReversedEnterpriseID][1], "DataSet does not store reverse information elements (IANA) correctly.")
assert.Equal(t, "pod1", dataSet[registry.AntreaEnterpriseID][101], "DataSet does not store elements (Antrea) correctly.")
}

0 comments on commit a947fbd

Please sign in to comment.