-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.go
230 lines (198 loc) · 5.96 KB
/
interfaces.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package juniper_els
import (
"encoding/xml"
"fmt"
"github.com/Juniper/go-netconf/netconf"
"github.com/g-portal/switchmgr-go/pkg/models"
"github.com/g-portal/switchmgr-go/pkg/utils"
"strconv"
"strings"
)
func (j *JuniperELS) ListInterfaces() ([]*models.Interface, error) {
cfg, err := j.GetRunningConfig()
if err != nil {
return nil, err
}
reply, err := j.session.Exec(netconf.RawMethod("<get-interface-information><level>extensive</level></get-interface-information>"))
if err != nil {
return nil, err
}
var interfaces *junosInterfaces
if err := xml.Unmarshal([]byte(reply.RawReply), &interfaces); err != nil {
return nil, err
}
resp := make([]*models.Interface, 0)
for _, physicalInterface := range interfaces.Interfaces {
// Name
physicalInterfaceName := strings.TrimSpace(physicalInterface.Name)
// IP addresses
ips := make([]string, 0)
for _, logicalInterface := range physicalInterface.LogicalInterfaces {
for _, address := range logicalInterface.AddressFamily.Addresses {
ips = append(ips, strings.TrimSpace(address.IP))
}
}
// Mode
mode := models.InterfaceModeAccess
if detectedMode, err := cfg.GetInterfaceMode(physicalInterfaceName); err == nil {
mode = detectedMode
}
// MTU
mtu := 0
if m, err := strconv.Atoi(strings.TrimSpace(physicalInterface.MTU)); err == nil {
mtu = m
}
// Type
t := models.InterfaceType1GE
if strings.HasPrefix(physicalInterfaceName, "xe-") {
t = models.InterfaceType10GSFPPlus
}
if strings.HasPrefix(physicalInterfaceName, "et-") {
t = models.InterfaceType40QGSFPPlus
}
// LAG ports
lagPorts := make([]string, 0)
if strings.HasPrefix(physicalInterfaceName, "ae") {
t = models.InterfaceTypeLAG
// If it is a LAG, the mode is trunk
for _, logicalInterface := range physicalInterface.LogicalInterfaces {
if strings.TrimSpace(logicalInterface.Name) == fmt.Sprintf("%s.0", physicalInterfaceName) {
for _, lagLink := range logicalInterface.LagTrafficStatistics.Links {
lagPorts = append(lagPorts, strings.TrimSuffix(strings.TrimSpace(lagLink.Name), ".0"))
}
break
}
}
}
nic := &models.Interface{
Name: physicalInterfaceName,
Mode: mode,
Type: t,
MTU: uint32(mtu),
Speed: physicalInterface.GetSpeed(),
Description: strings.TrimSpace(physicalInterface.Description),
Enabled: strings.TrimSpace(physicalInterface.AdminStatus) == "up",
LagInterfaces: lagPorts,
IPAddresses: ips,
}
// Is it a logical interface?
for _, logicalInterface := range physicalInterface.LogicalInterfaces {
if len(logicalInterface.AddressFamily.Addresses) > 0 {
nic.Management = true
break
}
}
// If there is a MAC address, parse and add it
mac := models.MacAddress(strings.TrimSpace(physicalInterface.MacAddress))
if mac.Valid() {
nic.MacAddress = mac
}
resp = append(resp, nic)
}
if err != nil {
return nil, err
}
// Add VLANs to interfaces
for i := range resp {
if vlanCfg, err := cfg.GetVlansByInterface(resp[i].Name); err == nil {
resp[i].TaggedVLANs = vlanCfg.TaggedVLANs
if vlanCfg.UntaggedVLAN > 0 {
resp[i].UntaggedVLAN = &vlanCfg.UntaggedVLAN
}
}
}
return resp, nil
}
func (s *JuniperELS) isUplink(inter string) bool {
inter = strings.TrimSpace(inter)
// Skip et, xe, ae interfaces for now
if strings.HasPrefix(inter, "xe-") ||
strings.HasPrefix(inter, "te-") ||
strings.HasPrefix(inter, "ae") {
return true
}
return false
}
const EditPortConfigurationTemplate = `<edit-config>
<target>
<candidate/>
</target>
<default-operation>merge</default-operation>
<test-option>test-then-set</test-option>
<config>
<configuration>
<interfaces>
<interface operation="replace">
<name>{{ .Name }}</name>
{{if .Description}}<description>{{ .Description }}</description>{{end}}
{{if .Disabled}}<disable/>{{end}}
{{if .UntaggedVLAN}}<native-vlan-id>{{ .UntaggedVLAN }}</native-vlan-id>{{end}}
<unit>
<name>0</name>
<family>
<ethernet-switching>
<interface-mode>trunk</interface-mode>
{{if gt (len .TaggedVLANs) 0}}<vlan>
{{range .TaggedVLANs }}<members>{{ . }}</members>{{end}}
</vlan>{{end}}
</ethernet-switching>
</family>
</unit>
</interface>
</interfaces>
</configuration>
</config>
</edit-config>
`
// ConfigureInterface configures a single interface. It returns true if the
// configuration has changed. If the interface is an uplink, it will return
// false and an error.
func (j *JuniperELS) ConfigureInterface(update *models.UpdateInterface) (bool, error) {
configMutex.Lock(j.identifier)
defer configMutex.Unlock(j.identifier)
if j.isUplink(update.Name) {
return false, fmt.Errorf("never configure the uplink port %s", update.Name)
}
swport, err := j.GetInterface(update.Name)
if err != nil {
return false, err
}
update.Fill(swport)
if !swport.Differs(update) {
return false, nil
}
// Add the untagged VLAN id to tagged VLANs, otherwise it won't work
if *update.UntaggedVLAN != 0 {
update.TaggedVLANs = append(update.TaggedVLANs, *update.UntaggedVLAN)
}
// Unique tagged VLAN ids
update.TaggedVLANs = utils.UniqueVlanIDs(update.TaggedVLANs)
if len(update.TaggedVLANs) == 0 && update.UntaggedVLAN != nil && *update.UntaggedVLAN == 0 {
return false, fmt.Errorf("switch port has no vlans to configure")
}
tpl, err := update.Template(EditPortConfigurationTemplate)
if err != nil {
return false, err
}
_, err = j.session.Exec(netconf.RawMethod(tpl.String()))
if err != nil {
return false, err
}
_, err = j.session.Exec(netconf.RawMethod("<commit/>"))
if err != nil {
return false, err
}
return true, nil
}
func (j *JuniperELS) GetInterface(name string) (*models.Interface, error) {
nics, err := j.ListInterfaces()
if err != nil {
return nil, err
}
for _, nic := range nics {
if nic.Name == name {
return nic, nil
}
}
return nil, fmt.Errorf("interface %s not found", name)
}