From 8c76c175ee8c3e2de4de54c461abccc04c2b7167 Mon Sep 17 00:00:00 2001 From: Pablo Mazzini Date: Sat, 22 Dec 2018 11:34:37 +0000 Subject: [ztpv4] make it consistent with ztpv6 --- dhcpv4/ztp/ztp.go | 80 ------------------------------------------------ dhcpv4/ztp/ztp_test.go | 75 --------------------------------------------- dhcpv4/ztpv4/ztp.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++ dhcpv4/ztpv4/ztp_test.go | 75 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 155 deletions(-) delete mode 100644 dhcpv4/ztp/ztp.go delete mode 100644 dhcpv4/ztp/ztp_test.go create mode 100644 dhcpv4/ztpv4/ztp.go create mode 100644 dhcpv4/ztpv4/ztp_test.go (limited to 'dhcpv4') diff --git a/dhcpv4/ztp/ztp.go b/dhcpv4/ztp/ztp.go deleted file mode 100644 index b899425..0000000 --- a/dhcpv4/ztp/ztp.go +++ /dev/null @@ -1,80 +0,0 @@ -package ztpv4 - -import ( - "errors" - "strings" - - "github.com/insomniacslk/dhcp/dhcpv4" -) - -// VendorData is optional data a particular vendor may or may not include -// in the Vendor Class options. -type VendorData struct { - VendorName string - Model string - Serial string -} - -var errVendorOptionMalformed = errors.New("malformed vendor option") - -// ParseVendorData will try to parse dhcp4 options looking for more -// specific vendor data (like model, serial number, etc). -func ParseVendorData(packet *dhcpv4.DHCPv4) (*VendorData, error) { - opt := packet.GetOneOption(dhcpv4.OptionClassIdentifier) - if opt == nil { - return nil, nil - } - vc := opt.(*dhcpv4.OptClassIdentifier).Identifier - vd := &VendorData{} - - switch { - // Arista;DCS-7050S-64;01.23;JPE12221671 - case strings.HasPrefix(vc, "Arista;"): - p := strings.Split(vc, ";") - if len(p) < 4 { - return nil, errVendorOptionMalformed - } - - vd.VendorName = p[0] - vd.Model = p[1] - vd.Serial = p[3] - return vd, nil - - // ZPESystems:NSC:002251623 - case strings.HasPrefix(vc, "ZPESystems:"): - p := strings.Split(vc, ":") - if len(p) < 3 { - return nil, errVendorOptionMalformed - } - - vd.VendorName = p[0] - vd.Model = p[1] - vd.Serial = p[2] - return vd, nil - - // Juniper option 60 parsing is a bit more nuanced. The following are all - // "valid" identifying stings for Juniper: - // Juniper-ptx1000-DD576 --- (serial in hostname option) - // Juniper-qfx10002-361-DN817 -- (model has a dash in it!) - case strings.HasPrefix(vc, "Juniper-"): - p := strings.Split(vc, "-") - if len(p) < 3 { - vd.Model = p[1] - if opt := packet.GetOneOption(dhcpv4.OptionHostName); opt != nil { - vd.Serial = opt.(*dhcpv4.OptHostName).HostName - } else { - return nil, errors.New("host name option is missing") - } - } else { - vd.Model = strings.Join(p[1:len(p)-1], "-") - vd.Serial = p[len(p)-1] - } - - vd.VendorName = p[0] - return vd, nil - } - - // We didn't match anything. - return nil, nil -} diff --git a/dhcpv4/ztp/ztp_test.go b/dhcpv4/ztp/ztp_test.go deleted file mode 100644 index 15a9d26..0000000 --- a/dhcpv4/ztp/ztp_test.go +++ /dev/null @@ -1,75 +0,0 @@ -package ztpv4 - -import ( - "testing" - - "github.com/insomniacslk/dhcp/dhcpv4" - "github.com/stretchr/testify/require" -) - -func TestParseV4VendorClass(t *testing.T) { - tt := []struct { - name string - vc, hostname string - want *VendorData - fail bool - }{ - {name: "empty"}, - {name: "unknownVendor", vc: "VendorX;BFR10K;XX12345"}, - {name: "truncatedVendor", vc: "Arista;1234", fail: true}, - { - name: "arista", - vc: "Arista;DCS-7050S-64;01.23;JPE12345678", - want: &VendorData{VendorName: "Arista", Model: "DCS-7050S-64", Serial: "JPE12345678"}, - }, - { - name: "juniper", - vc: "Juniper-ptx1000-DD123", - want: &VendorData{VendorName: "Juniper", Model: "ptx1000", Serial: "DD123"}, - }, - { - name: "juniperModelDash", - vc: "Juniper-qfx10002-36q-DN817", - want: &VendorData{VendorName: "Juniper", Model: "qfx10002-36q", Serial: "DN817"}, - }, - { - name: "juniperHostnameSerial", - vc: "Juniper-qfx10008", - hostname: "DE123", - want: &VendorData{VendorName: "Juniper", Model: "qfx10008", Serial: "DE123"}, - }, - {name: "juniperNoSerial", vc: "Juniper-qfx10008", fail: true}, - { - name: "zpe", - vc: "ZPESystems:NSC:001234567", - want: &VendorData{VendorName: "ZPESystems", Model: "NSC", Serial: "001234567"}, - }, - } - - for _, tc := range tt { - t.Run(tc.name, func(t *testing.T) { - packet, err := dhcpv4.New() - if err != nil { - t.Fatalf("failed to creat dhcpv4 packet object: %v", err) - } - - packet.AddOption(&dhcpv4.OptClassIdentifier{ - Identifier: tc.vc, - }) - - if tc.hostname != "" { - packet.AddOption(&dhcpv4.OptHostName{ - HostName: tc.hostname, - }) - } - - vd, err := ParseVendorData(packet) - if tc.fail { - require.Error(t, err) - } else { - require.NoError(t, err) - require.Equal(t, tc.want, vd) - } - }) - } -} diff --git a/dhcpv4/ztpv4/ztp.go b/dhcpv4/ztpv4/ztp.go new file mode 100644 index 0000000..d8c507a --- /dev/null +++ b/dhcpv4/ztpv4/ztp.go @@ -0,0 +1,78 @@ +package ztpv4 + +import ( + "errors" + "strings" + + "github.com/insomniacslk/dhcp/dhcpv4" +) + +// VendorData is optional data a particular vendor may or may not include +// in the Vendor Class options. +type VendorData struct { + VendorName, Model, Serial string +} + +var errVendorOptionMalformed = errors.New("malformed vendor option") + +// ParseVendorData will try to parse dhcp4 options looking for more +// specific vendor data (like model, serial number, etc). +func ParseVendorData(packet *dhcpv4.DHCPv4) (*VendorData, error) { + opt := packet.GetOneOption(dhcpv4.OptionClassIdentifier) + if opt == nil { + return nil, errors.New("vendor options not found") + } + vc := opt.(*dhcpv4.OptClassIdentifier).Identifier + vd := &VendorData{} + + switch { + // Arista;DCS-7050S-64;01.23;JPE12221671 + case strings.HasPrefix(vc, "Arista;"): + p := strings.Split(vc, ";") + if len(p) < 4 { + return nil, errVendorOptionMalformed + } + + vd.VendorName = p[0] + vd.Model = p[1] + vd.Serial = p[3] + return vd, nil + + // ZPESystems:NSC:002251623 + case strings.HasPrefix(vc, "ZPESystems:"): + p := strings.Split(vc, ":") + if len(p) < 3 { + return nil, errVendorOptionMalformed + } + + vd.VendorName = p[0] + vd.Model = p[1] + vd.Serial = p[2] + return vd, nil + + // Juniper option 60 parsing is a bit more nuanced. The following are all + // "valid" identifying stings for Juniper: + // Juniper-ptx1000-DD576 --- (serial in hostname option) + // Juniper-qfx10002-361-DN817 -- (model has a dash in it!) + case strings.HasPrefix(vc, "Juniper-"): + p := strings.Split(vc, "-") + if len(p) < 3 { + vd.Model = p[1] + if opt := packet.GetOneOption(dhcpv4.OptionHostName); opt != nil { + vd.Serial = opt.(*dhcpv4.OptHostName).HostName + } else { + return nil, errors.New("host name option is missing") + } + } else { + vd.Model = strings.Join(p[1:len(p)-1], "-") + vd.Serial = p[len(p)-1] + } + + vd.VendorName = p[0] + return vd, nil + } + + // We didn't match anything. + return nil, errors.New("failed to parse vendor option data") +} diff --git a/dhcpv4/ztpv4/ztp_test.go b/dhcpv4/ztpv4/ztp_test.go new file mode 100644 index 0000000..c9c8d86 --- /dev/null +++ b/dhcpv4/ztpv4/ztp_test.go @@ -0,0 +1,75 @@ +package ztpv4 + +import ( + "testing" + + "github.com/insomniacslk/dhcp/dhcpv4" + "github.com/stretchr/testify/require" +) + +func TestParseV4VendorClass(t *testing.T) { + tt := []struct { + name string + vc, hostname string + want *VendorData + fail bool + }{ + {name: "empty", fail: true}, + {name: "unknownVendor", vc: "VendorX;BFR10K;XX12345", fail: true}, + {name: "truncatedVendor", vc: "Arista;1234", fail: true}, + { + name: "arista", + vc: "Arista;DCS-7050S-64;01.23;JPE12345678", + want: &VendorData{VendorName: "Arista", Model: "DCS-7050S-64", Serial: "JPE12345678"}, + }, + { + name: "juniper", + vc: "Juniper-ptx1000-DD123", + want: &VendorData{VendorName: "Juniper", Model: "ptx1000", Serial: "DD123"}, + }, + { + name: "juniperModelDash", + vc: "Juniper-qfx10002-36q-DN817", + want: &VendorData{VendorName: "Juniper", Model: "qfx10002-36q", Serial: "DN817"}, + }, + { + name: "juniperHostnameSerial", + vc: "Juniper-qfx10008", + hostname: "DE123", + want: &VendorData{VendorName: "Juniper", Model: "qfx10008", Serial: "DE123"}, + }, + {name: "juniperNoSerial", vc: "Juniper-qfx10008", fail: true}, + { + name: "zpe", + vc: "ZPESystems:NSC:001234567", + want: &VendorData{VendorName: "ZPESystems", Model: "NSC", Serial: "001234567"}, + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + packet, err := dhcpv4.New() + if err != nil { + t.Fatalf("failed to creat dhcpv4 packet object: %v", err) + } + + packet.AddOption(&dhcpv4.OptClassIdentifier{ + Identifier: tc.vc, + }) + + if tc.hostname != "" { + packet.AddOption(&dhcpv4.OptHostName{ + HostName: tc.hostname, + }) + } + + vd, err := ParseVendorData(packet) + if tc.fail { + require.Error(t, err) + } else { + require.NoError(t, err) + require.Equal(t, tc.want, vd) + } + }) + } +} -- cgit v1.2.3