-
Notifications
You must be signed in to change notification settings - Fork 39
/
import_csv.go
329 lines (265 loc) · 7.46 KB
/
import_csv.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"encoding/csv"
"fmt"
"io"
"math/rand"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/lxc/lxd/shared"
)
func parseCSV(path string) ([][]string, error) {
records := [][]string{}
file, err := os.Open(path)
if err != nil {
return nil, err
}
r := csv.NewReader(file)
header := true
for {
record, err := r.Read()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
if header {
header = false
continue
}
records = append(records, record)
}
return records, nil
}
func importFromCSV(path string) (Routers, error) {
routers := Routers{}
if !shared.PathExists(path) {
return nil, fmt.Errorf("Path doesn't exist.")
}
// Import "Telco.csv"
records, err := parseCSV(filepath.Join(path, "Telco.csv"))
if err != nil {
return nil, err
}
for _, record := range records {
if len(record) < 9 {
return nil, fmt.Errorf("Invalid record: %s\n", record)
}
// Look for duplicates
_, ok := routers[record[2]]
if ok {
return nil, fmt.Errorf("Duplicate record for: %s\n", record[2])
}
// Generate the router record
router := Router{}
router.Configuration = &RouterConfiguration{}
// Text fields
router.Organization = record[0]
router.Configuration.FQDN = record[1]
router.Name = record[2]
router.Location = record[7]
// Integers
tier, err := strconv.Atoi(record[3])
if err != nil {
return nil, fmt.Errorf("Invalid tier in: %s\n", record)
}
router.Tier = tier
asn, err := strconv.Atoi(record[5])
if err != nil {
return nil, fmt.Errorf("Invalid ASN in: %s\n", record)
}
router.Configuration.ASN = asn
priority, err := strconv.Atoi(record[8])
if err != nil {
return nil, fmt.Errorf("Invalid priority in: %s\n", record)
}
router.Priority = priority
// Booleans
if record[6] == "1" || record[6] == "yes" || record[6] == "true" {
router.Internal = true
}
// Subnet
_, netSubnet, err := net.ParseCIDR(record[4])
if err != nil {
return nil, err
}
prefix := strings.Split(netSubnet.String(), ":/")
if len(prefix) != 2 {
return nil, fmt.Errorf("Bad IPv6 subnet: %s\n", netSubnet.String())
}
ip := fmt.Sprintf("%s:%x:%x:%x:%x", prefix[0], rand.Int31n(65536), rand.Int31n(65536), rand.Int31n(65536), rand.Int31n(65536))
netLocal := net.ParseIP(ip)
if netLocal == nil {
return nil, fmt.Errorf("Bad IPv6 address: %s\n", ip)
}
if router.Configuration.FQDN != "" && router.Tier > 0 {
router.DNS = []string{fmt.Sprintf("%s IN AAAA %s", router.Configuration.FQDN, netLocal)}
}
router.Configuration.Loopback = &RouterInterface{
Addresses: []net.IP{netLocal},
Routes: []RouterInterfaceRoute{
{Subnet: netSubnet}}}
// Quagga
routerid := net.ParseIP(fmt.Sprintf("%d.%d.%d.%d", rand.Int31n(223), rand.Int31n(255), rand.Int31n(255), rand.Int31n(255)))
router.Configuration.RouterID = &routerid
router.Configuration.PasswordLogin = randStringN(rand.Intn(32-16) + 16)
router.Configuration.PasswordEnable = randStringN(rand.Intn(32-16) + 16)
routers[router.Name] = &router
}
// Import "Routers.csv"
records, err = parseCSV(filepath.Join(path, "Routers.csv"))
if err != nil {
return nil, err
}
for _, record := range records {
if len(record) < 9 {
return nil, fmt.Errorf("Invalid record: %s\n", record)
}
// Look for duplicates
_, ok := routers[record[6]]
if ok {
return nil, fmt.Errorf("Duplicate record for: %s\n", record[6])
}
// Generate the router record
router := Router{}
router.Configuration = &RouterConfiguration{}
// Text fields
router.Organization = record[0]
router.Configuration.FQDN = record[4]
router.Name = record[6]
router.Location = record[2]
// Integers
router.Tier = 4
// Subnet
_, netSubnet, err := net.ParseCIDR(record[5])
if err != nil {
return nil, err
}
router.Configuration.Loopback = &RouterInterface{
Routes: []RouterInterfaceRoute{
{Subnet: netSubnet}}}
routers[router.Name] = &router
}
// Import "Links.csv"
records, err = parseCSV(filepath.Join(path, "Links.csv"))
if err != nil {
return nil, err
}
for _, record := range records {
if len(record) < 7 {
return nil, fmt.Errorf("Invalid record: %s\n", record)
}
// Load left router
left, ok := routers[record[0]]
if !ok {
return nil, fmt.Errorf("Router doesn't exist: %s\n", record[0])
}
// Load right router
right, ok := routers[record[1]]
if !ok {
return nil, fmt.Errorf("Router doesn't exist: %s\n", record[1])
}
// Parse integers
speed, err := strconv.Atoi(record[6])
if err != nil {
return nil, fmt.Errorf("Invalid speed: %s\n", record[6])
}
leftWeight, err := strconv.Atoi(record[4])
if err != nil && record[4] != "" {
return nil, fmt.Errorf("Invalid priority: %s\n", record[4])
}
rightWeight, err := strconv.Atoi(record[5])
if err != nil && record[5] != "" {
return nil, fmt.Errorf("Invalid priority: %s\n", record[5])
}
// GPS math
delay, err := delayGPS(left.Location, right.Location)
if err != nil {
return nil, err
}
// Left peer
leftPeer := Peer{}
leftPeer.Name = right.Name
if left.Tier < 1 || left.Tier > 3 || right.Tier < 1 || right.Tier > 3 {
leftPeer.Interface = fmt.Sprintf("br%s", interfaceName(left, right))
if len(leftPeer.Interface) > 15 {
return nil, fmt.Errorf("Interface name is too long: %s", leftPeer.Interface)
}
} else {
leftPeer.Interface = fmt.Sprintf("v%s-1", interfaceName(left, right))
if len(leftPeer.Interface) > 15 {
return nil, fmt.Errorf("Interface name is too long: %s", leftPeer.Interface)
}
}
leftPeer.MAC = record[2]
linklocal, err := macToLinkLocal(record[3])
if err != nil {
return nil, err
}
leftPeer.Remote = linklocal
leftPeer.Speed = speed
leftPeer.Delay = delay
if right.Tier == 4 {
leftPeer.Routes = right.Configuration.Loopback.Routes
}
leftPeer.ASN = right.Configuration.ASN
leftPeer.Weight = leftWeight
if left.Peers == nil {
left.Peers = []Peer{}
}
left.Peers = append(left.Peers, leftPeer)
// Right peer
rightPeer := Peer{}
rightPeer.Name = left.Name
if left.Tier < 1 || left.Tier > 3 || right.Tier < 1 || right.Tier > 3 {
rightPeer.Interface = fmt.Sprintf("br%s", interfaceName(left, right))
if len(rightPeer.Interface) > 15 {
return nil, fmt.Errorf("Interface name is too long: %s", rightPeer.Interface)
}
} else {
rightPeer.Interface = fmt.Sprintf("v%s-2", interfaceName(left, right))
if len(rightPeer.Interface) > 15 {
return nil, fmt.Errorf("Interface name is too long: %s", rightPeer.Interface)
}
}
rightPeer.MAC = record[3]
linklocal, err = macToLinkLocal(record[2])
if err != nil {
return nil, err
}
rightPeer.Remote = linklocal
rightPeer.Speed = speed
rightPeer.Delay = delay
if left.Tier == 4 {
rightPeer.Routes = left.Configuration.Loopback.Routes
}
rightPeer.ASN = left.Configuration.ASN
rightPeer.Weight = rightWeight
if right.Peers == nil {
right.Peers = []Peer{}
}
right.Peers = append(right.Peers, rightPeer)
}
// Import "DNS.csv"
records, err = parseCSV(filepath.Join(path, "DNS.csv"))
if err != nil {
return nil, err
}
for _, record := range records {
if len(record) < 4 {
return nil, fmt.Errorf("Invalid record: %s\n", record)
}
router, ok := routers[record[2]]
if !ok {
return nil, fmt.Errorf("Router doesn't exist: %s\n", record[2])
}
if router.DNS == nil {
router.DNS = []string{}
}
router.DNS = append(router.DNS, fmt.Sprintf("%s IN %s %s", record[1], record[3], record[0]))
}
return routers, nil
}