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