-
Notifications
You must be signed in to change notification settings - Fork 0
/
running_config.go
117 lines (107 loc) · 3.21 KB
/
running_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package juniper
import (
"encoding/xml"
"errors"
"fmt"
"github.com/Juniper/go-netconf/netconf"
"github.com/g-portal/switchmgr-go/pkg/models"
"github.com/g-portal/switchmgr-go/pkg/utils"
"log"
"regexp"
"strconv"
"strings"
)
func (j *Juniper) GetRunningConfig() (*junosConfiguration, error) {
reply, err := j.session.Exec(netconf.RawMethod("<get-config><source><running/></source></get-config>"))
if err != nil {
return nil, err
}
var cfg junosConfiguration
if err := xml.Unmarshal([]byte(reply.RawReply), &cfg); err != nil {
return nil, err
}
return &cfg, err
}
// GetVlanMap returns the int32 representation of a specified vlan name
func (j *junosConfiguration) GetVlanIDByName(name string) (int32, error) {
if name == "" {
return 0, errors.New("no vlan defined")
}
for _, vlan := range j.VLANs {
if vlan.Name == name {
return vlan.ID, nil
}
}
intval, err := strconv.Atoi(strings.TrimSpace(name))
if err == nil {
return int32(intval), nil
}
return 0, fmt.Errorf("GetVlanIDByName: no vlan found: %s", name)
}
// GetInterfaceMode returns the mode of a specified interface
func (j *junosConfiguration) GetInterfaceMode(name string) (models.InterfaceMode, error) {
for _, nic := range j.Interfaces {
if nic.Name != name {
continue
}
switch strings.TrimSpace(nic.PortMode) {
case "trunk":
return models.InterfaceModeTrunk, nil
case "access":
return models.InterfaceModeAccess, nil
default: // "access" is default
return models.InterfaceModeAccess, nil
}
}
return "", fmt.Errorf("GetInterfaceMode: no interface found: %s", name)
}
// GetVlansByInterface returns the VLANs of a specified interface
func (j *junosConfiguration) GetVlansByInterface(name string) (*junosVlanMapEntry, error) {
rgSingleVLAN := regexp.MustCompile("^[0-9]+$")
rgVLANRange := regexp.MustCompile(`^[0-9]+-[0-9]+$`)
for _, nic := range j.Interfaces {
if nic.Name != name {
continue
}
junosInterface := junosVlanMapEntry{}
if rgSingleVLAN.MatchString(nic.UntaggedVLAN) {
intId, err := strconv.Atoi(nic.UntaggedVLAN)
if err != nil {
log.Println(err.Error())
continue
}
junosInterface.UntaggedVLAN = int32(intId)
} else {
vlanID, err := j.GetVlanIDByName(nic.UntaggedVLAN)
if err == nil {
junosInterface.UntaggedVLAN = vlanID
}
}
for _, vlan := range nic.TaggedVLANs {
vlan = strings.TrimSpace(vlan)
// Numeric -> to int32
if rgSingleVLAN.MatchString(vlan) {
intId, err := strconv.Atoi(vlan)
if err != nil {
log.Println(err.Error())
continue
}
if int32(intId) == junosInterface.UntaggedVLAN {
continue
}
junosInterface.TaggedVLANs = append(junosInterface.TaggedVLANs, int32(intId))
} else if rgVLANRange.MatchString(vlan) {
convertedVlans := utils.ConvertVlans(vlan, ",")
convertedVlans = utils.DeleteVlanFromIDs(convertedVlans, junosInterface.UntaggedVLAN)
junosInterface.TaggedVLANs = append(junosInterface.TaggedVLANs, convertedVlans...)
} else {
vlanID, err := j.GetVlanIDByName(nic.UntaggedVLAN)
if err == nil {
junosInterface.TaggedVLANs = append(junosInterface.TaggedVLANs, vlanID)
}
}
}
return &junosInterface, nil
}
return nil, fmt.Errorf("GetVlansByInterface: no interface found: %s", name)
}