blob: d2a0cc89f89d92a9b2a66c5134bdea32b4a31bc3 [file] [log] [blame]
David K. Bainbridge97ee8052016-06-14 00:52:07 -07001package main
2
3import (
4 "encoding/json"
5 "log"
6 "strings"
7 "net/http"
8)
9
10type Vendors interface {
11 Switchq(mac string) (bool, error)
12}
13
14type VendorRec struct {
15 Prefix string `json:"prefix"`
16 Vendor string `json:"vendor"`
17 Provision bool `json:"provision"`
18}
19
20type VendorsData struct {
21 Vendors map[string]VendorRec
22}
23
24func NewVendors(spec string) (Vendors, error) {
25 v := VendorsData{}
26 v.Vendors = make(map[string]VendorRec)
27
28 t := &http.Transport{}
29 t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
30 c := &http.Client{Transport: t}
31 res, err := c.Get(spec)
32 if err != nil {
33 return nil, err
34 }
35 defer res.Body.Close()
36
37 data := make([]VendorRec, 0)
38 decoder := json.NewDecoder(res.Body)
39 err = decoder.Decode(&data)
40 if err != nil {
41 return nil, err
42 }
43 for _, rec := range data {
44 v.Vendors[rec.Prefix] = rec
45 }
46 log.Printf("[debug] %v", v.Vendors)
47
48 return &v, nil
49}
50
51func (v *VendorsData) Switchq(mac string) (bool, error) {
52 if len(mac) < 8 {
53 return false, nil
54 }
55 rec, ok := v.Vendors[strings.ToUpper(mac[0:8])]
56 if !ok || !rec.Provision {
57 return false, nil
58 }
59
60 return true, nil
61}