Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
BirknerAlex committed Oct 18, 2023
1 parent dab1349 commit afed9c3
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/info/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
}
}()

driver.Logger().SetLevel(log.DebugLevel)
driver.Logger().SetLevel(log.InfoLevel)
driver.Logger().Info("+++++++++++++++++++")
driver.Logger().Info("System Information:")
driver.Logger().Info("+++++++++++++++++++")
Expand Down
11 changes: 10 additions & 1 deletion pkg/vendors/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/charmbracelet/log"
"github.com/g-portal/switchmgr-go/pkg/config"
"github.com/g-portal/switchmgr-go/pkg/models"
"github.com/g-portal/switchmgr-go/pkg/vendors/fsos_n5"
"github.com/g-portal/switchmgr-go/pkg/vendors/fsos_s3"
"github.com/g-portal/switchmgr-go/pkg/vendors/fsos_s5"
"github.com/g-portal/switchmgr-go/pkg/vendors/juniper"
Expand Down Expand Up @@ -33,7 +34,7 @@ const (
// Valid checks if this lib supports the given vendor.
func (v Vendor) Valid() bool {
switch v {
case VendorFSOSS3, VendorFSOSS5, VendorJuniper, VendorJuniperELS:
case VendorFSOSS3, VendorFSOSS5, VendorFSOSN5, VendorJuniper, VendorJuniperELS:
return true
default:
return false
Expand Down Expand Up @@ -107,6 +108,14 @@ func New(vendor Vendor) (Driver, error) {
},
},
}, nil
case VendorFSOSN5:
return &fsos_n5.FSComN5{
FSComS3: fsos_s3.FSComS3{
LoginCommands: []string{
"terminal length 0",
},
},
}, nil
default:
// Should never be reached because of the Valid() check above.
return &unimplemented.Unimplemented{}, nil
Expand Down
20 changes: 16 additions & 4 deletions pkg/vendors/fsos_n5/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/g-portal/switchmgr-go/pkg/iosconfig"
"github.com/g-portal/switchmgr-go/pkg/models"
"github.com/g-portal/switchmgr-go/pkg/utils"
"strconv"
"strings"
)

type Configuration iosconfig.Config
Expand All @@ -30,17 +32,27 @@ func (cfg Configuration) ListInterfaces() ([]*models.Interface, error) {

interfaceMode := models.InterfaceModeAccess

defaultVLAN := int32(1)
if strings.HasPrefix(nic, "VLAN") {
var err error
defaultVLANInt, err := strconv.Atoi(strings.TrimSpace(strings.TrimPrefix(nic, "VLAN")))
// check if err is nil and defaultVLANInt can be converted to int32
if err == nil && int32(defaultVLANInt) > 0 {
defaultVLAN = int32(defaultVLANInt)
}
}

switch mode {
case InterfaceModeAccess:
accessVlanID := config.GetInt32Value("switchport access vlan", 1)
accessVlanID := config.GetInt32Value("switchport access vlan", defaultVLAN)
untaggedVLAN = &accessVlanID
case InterfaceModeTrunk:
interfaceMode = models.InterfaceModeTrunk
accessVlanID := int32(1)
accessVlanID := defaultVLAN
if config.Exists("switchport trunk vlan-untagged", false) {
accessVlanID = config.GetInt32Value("switchport trunk vlan-untagged", 1)
accessVlanID = config.GetInt32Value("switchport trunk vlan-untagged", defaultVLAN)
} else if config.Exists("switchport trunk native vlan", false) {
accessVlanID = config.GetInt32Value("switchport trunk native vlan", 1)
accessVlanID = config.GetInt32Value("switchport trunk native vlan", defaultVLAN)
}

untaggedVLAN = &accessVlanID
Expand Down
8 changes: 4 additions & 4 deletions pkg/vendors/fsos_n5/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ type fscomInterface struct {
}

var interfaceMacAddressRegex = regexp.MustCompile(`address is ([0-9a-fA-F.]+)`)
var interfaceBandwithRegex = regexp.MustCompile(`Bandwidth (\d+) kbits`)
var interfaceMTURegex = regexp.MustCompile(`The maximum transmit unit \(MTU\) is (\d+) bytes`)
var interfaceBandwithRegex = regexp.MustCompile(`,\sBW\s([0-9]+)\sKbit`)
var interfaceMTURegex = regexp.MustCompile(`TMTU\s([0-9]+)\sbytes`)

func ParseInterfaces(output string) (map[string]fscomInterface, error) {
interfaces := map[string]*fscomInterface{}
Expand All @@ -154,9 +154,9 @@ func ParseInterfaces(output string) (map[string]fscomInterface, error) {
var currentInterfaceConfig *fscomInterface
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "Interface ") {
if strings.HasPrefix(line, "==========================") {
// New interface definition reached, saving old one
currentInterface = strings.TrimSpace(strings.TrimPrefix(line, "Interface "))
currentInterface = strings.TrimSpace(strings.ReplaceAll(line, "=", " "))
}
if _, ok := interfaces[currentInterface]; !ok {
currentInterfaceConfig = &fscomInterface{
Expand Down
84 changes: 62 additions & 22 deletions pkg/vendors/fsos_n5/interfaces_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fsos_n5_test

import (
"github.com/g-portal/switchmgr-go/pkg/models"
"github.com/g-portal/switchmgr-go/pkg/vendors/fsos_n5"
"github.com/g-portal/switchmgr-go/pkg/vendors/fsos_n5/utils"
"testing"
Expand Down Expand Up @@ -34,57 +35,96 @@ func TestListInterfaces(t *testing.T) {
t.Fatalf("Expected interface %s to be enabled", nic.Name)
}

if nic.Name != "eth-0-5" {
t.Fatalf("Expected interface %s name to be eth-0-5", nic.Name)
if nic.Name != "TenGigabitEthernet 0/34" {
t.Fatalf("Expected interface %s name to be TenGigabitEthernet 0/34", nic.Name)
}

if nic.UntaggedVLAN == nil {
t.Fatalf("Expected untagged VLAN to be set on %s", nic.Name)
}

nic, err = cfg.GetInterface("eth-0-9")
untaggedVLAN := int32(4)
if *nic.UntaggedVLAN != untaggedVLAN {
t.Fatalf("Expected untagged VLAN to be %d, got %d", untaggedVLAN, *nic.UntaggedVLAN)
}

nic, err = cfg.GetInterface("AggregatePort 4")
if err != nil {
t.Fatal(err)
}

if nic.Enabled {
if !nic.Enabled {
t.Fatalf("Expected interface %s to be disabled", nic.Name)
}

if nic.Mode != models.InterfaceModeTrunk {
t.Fatalf("Expected interface %s to be in trunk mode", nic.Name)
}

if nic.UntaggedVLAN == nil {
t.Fatalf("Expected untagged VLAN to be set on %s", nic.Name)
}

untaggedVLAN = int32(1900)
if *nic.UntaggedVLAN != untaggedVLAN {
t.Fatalf("Expected untagged VLAN to be %d, got %d", untaggedVLAN, *nic.UntaggedVLAN)
}

nic, err = cfg.GetInterface("VLAN 4")
if err != nil {
t.Fatal(err)
}

if !nic.Enabled {
t.Fatalf("Expected interface %s to be disabled", nic.Name)
}

if nic.Mode != models.InterfaceModeAccess {
t.Fatalf("Expected interface %s to be in access mode", nic.Name)
}

if nic.UntaggedVLAN == nil {
t.Fatalf("Expected untagged VLAN to be set on %s", nic.Name)
}

untaggedVLAN = int32(4)
if *nic.UntaggedVLAN != untaggedVLAN {
t.Fatalf("Expected untagged VLAN to be %d, got %d", untaggedVLAN, *nic.UntaggedVLAN)
}

}

func TestParseInterfaces(t *testing.T) {
nics, err := fsos_n5.ParseInterfaces(utils.ReadTestData("show interface", nil))
nics, err := fsos_n5.ParseInterfaces(utils.ReadTestData("show interfaces", nil))
if err != nil {
t.Fatal(err)
}

if len(nics) != 72 {
t.Fatalf("expected 72 nics, got %d", len(nics))
if len(nics) != 71 {
t.Fatalf("expected 71 nics, got %d", len(nics))
}

if info, ok := nics["vlan20"]; !ok {
t.Fatalf("interface vlan20 not found")
if info, ok := nics["VLAN 4"]; !ok {
t.Fatalf("interface VLAN 4 not found")
} else {

if info.MacAddress.String() != "64:9d:99:06:ff:33" {
t.Fatalf("expected vlan20 mac 64:9d:99:06:ff:33, got %s", info.MacAddress.String())
if info.MacAddress.String() != "64:9d:99:d2:50:2e" {
t.Fatalf("expected vlan20 mac 64:9d:99:d2:50:2e, got %s", info.MacAddress.String())
}

if info.MTU != 1300 {
t.Fatalf("expected vlan20 MTU 1300, got %d", info.MTU)
if info.MTU != 1500 {
t.Fatalf("expected VLAN 4 MTU 1500, got %d", info.MTU)
}

if info.Speed != 10000000 {
t.Fatalf("expected vlan20 Speed 10000000, got %d", info.Speed)
if info.Speed != 1000000 {
t.Fatalf("expected VLAN 4 Speed 1000000, got %d", info.Speed)
}
}

if info, ok := nics["eth-0-51"]; !ok {
t.Fatalf("interface eth-0-51 not found")
if info, ok := nics["TenGigabitEthernet 0/1"]; !ok {
t.Fatalf("interface TenGigabitEthernet 0/1 not found")
} else {
if info.MacAddress.String() != "64:9d:99:06:ff:66" {
t.Fatalf("expected eth-0-51 mac 64:9d:99:06:ff:66, got %s", info.MacAddress.String())
if info.MacAddress.String() != "64:9d:99:d2:50:2d" {
t.Fatalf("expected eth-0-51 mac 64:9d:99:d2:50:2d, got %s", info.MacAddress.String())
}

if info.MTU != 1500 {
Expand All @@ -96,8 +136,8 @@ func TestParseInterfaces(t *testing.T) {
}
}

if _, ok := nics["TGigaEthernet0/123"]; ok {
t.Fatalf("interface TGigaEthernet0/123 found, but should not exist")
if _, ok := nics["TenGigabitEthernet 0/100"]; ok {
t.Fatalf("interface TenGigabitEthernet 0/100 found, but should not exist")
}

}
20 changes: 16 additions & 4 deletions pkg/vendors/fsos_s3/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/g-portal/switchmgr-go/pkg/iosconfig"
"github.com/g-portal/switchmgr-go/pkg/models"
"github.com/g-portal/switchmgr-go/pkg/utils"
"strconv"
"strings"
)

type Configuration iosconfig.Config
Expand All @@ -30,17 +32,27 @@ func (cfg Configuration) ListInterfaces() ([]*models.Interface, error) {

interfaceMode := models.InterfaceModeAccess

defaultVLAN := int32(1)
if strings.HasPrefix(strings.TrimSpace(nic), "VLAN") {
var err error
defaultVLANInt, err := strconv.Atoi(strings.TrimSpace(strings.TrimPrefix(nic, "VLAN")))
// check if err is nil and defaultVLANInt can be converted to int32
if err == nil && int32(defaultVLANInt) > 0 {
defaultVLAN = int32(defaultVLANInt)
}
}

switch mode {
case InterfaceModeAccess:
accessVlanID := config.GetInt32Value("switchport access vlan", 1)
accessVlanID := config.GetInt32Value("switchport access vlan", defaultVLAN)
untaggedVLAN = &accessVlanID
case InterfaceModeTrunk:
interfaceMode = models.InterfaceModeTrunk
accessVlanID := int32(1)
accessVlanID := defaultVLAN
if config.Exists("switchport trunk vlan-untagged", false) {
accessVlanID = config.GetInt32Value("switchport trunk vlan-untagged", 1)
accessVlanID = config.GetInt32Value("switchport trunk vlan-untagged", defaultVLAN)
} else if config.Exists("switchport pvid", false) {
accessVlanID = config.GetInt32Value("switchport pvid", 1)
accessVlanID = config.GetInt32Value("switchport pvid", defaultVLAN)
}

untaggedVLAN = &accessVlanID
Expand Down
4 changes: 4 additions & 0 deletions pkg/vendors/fsos_s3/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func TestListInterfaces(t *testing.T) {
t.Fatalf("Expected management to be true, got %t", nic.Management)
}

if nic.UntaggedVLAN == nil || *nic.UntaggedVLAN != int32(4) {
t.Fatal("Expected untagged VLAN to be 4")
}

}

func TestParseInterfaces(t *testing.T) {
Expand Down
20 changes: 16 additions & 4 deletions pkg/vendors/fsos_s5/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/g-portal/switchmgr-go/pkg/iosconfig"
"github.com/g-portal/switchmgr-go/pkg/models"
"github.com/g-portal/switchmgr-go/pkg/utils"
"strconv"
"strings"
)

type Configuration iosconfig.Config
Expand All @@ -30,17 +32,27 @@ func (cfg Configuration) ListInterfaces() ([]*models.Interface, error) {

interfaceMode := models.InterfaceModeAccess

defaultVLAN := int32(1)
if strings.HasPrefix(strings.TrimSpace(nic), "vlan") {
var err error
defaultVLANInt, err := strconv.Atoi(strings.TrimSpace(strings.TrimPrefix(nic, "vlan")))
// check if err is nil and defaultVLANInt can be converted to int32
if err == nil && int32(defaultVLANInt) > 0 {
defaultVLAN = int32(defaultVLANInt)
}
}

switch mode {
case InterfaceModeAccess:
accessVlanID := config.GetInt32Value("switchport access vlan", 1)
accessVlanID := config.GetInt32Value("switchport access vlan", defaultVLAN)
untaggedVLAN = &accessVlanID
case InterfaceModeTrunk:
interfaceMode = models.InterfaceModeTrunk
accessVlanID := int32(1)
accessVlanID := defaultVLAN
if config.Exists("switchport trunk vlan-untagged", false) {
accessVlanID = config.GetInt32Value("switchport trunk vlan-untagged", 1)
accessVlanID = config.GetInt32Value("switchport trunk vlan-untagged", defaultVLAN)
} else if config.Exists("switchport trunk native vlan", false) {
accessVlanID = config.GetInt32Value("switchport trunk native vlan", 1)
accessVlanID = config.GetInt32Value("switchport trunk native vlan", defaultVLAN)
}

untaggedVLAN = &accessVlanID
Expand Down
24 changes: 23 additions & 1 deletion pkg/vendors/fsos_s5/interfaces_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fsos_s5_test

import (
"github.com/g-portal/switchmgr-go/pkg/models"
"github.com/g-portal/switchmgr-go/pkg/vendors/fsos_s5"
"github.com/g-portal/switchmgr-go/pkg/vendors/fsos_s5/utils"
"testing"
Expand Down Expand Up @@ -51,6 +52,28 @@ func TestListInterfaces(t *testing.T) {
t.Fatalf("Expected interface %s to be disabled", nic.Name)
}

nic, err = cfg.GetInterface("vlan20")
if err != nil {
t.Fatal(err)
}

if !nic.Enabled {
t.Fatalf("Expected interface %s to be enabled", nic.Name)
}

if nic.Mode != models.InterfaceModeAccess {
t.Fatalf("Expected interface %s to be in access mode", nic.Name)
}

if nic.UntaggedVLAN == nil {
t.Fatalf("Expected untagged VLAN to be set on %s", nic.Name)
}

untaggedVLAN := int32(20)
if *nic.UntaggedVLAN != untaggedVLAN {
t.Fatalf("Expected untagged VLAN to be %d, got %d", untaggedVLAN, *nic.UntaggedVLAN)
}

}

func TestParseInterfaces(t *testing.T) {
Expand All @@ -66,7 +89,6 @@ func TestParseInterfaces(t *testing.T) {
if info, ok := nics["vlan20"]; !ok {
t.Fatalf("interface vlan20 not found")
} else {

if info.MacAddress.String() != "64:9d:99:06:ff:33" {
t.Fatalf("expected vlan20 mac 64:9d:99:06:ff:33, got %s", info.MacAddress.String())
}
Expand Down

0 comments on commit afed9c3

Please sign in to comment.