Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 1 | package graph |
| 2 | |
| 3 | import ( |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 4 | "context" |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 5 | "fmt" |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 6 | "sync" |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 7 | |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 8 | "github.com/digitalocean/go-netbox/netbox/client" |
| 9 | "github.com/digitalocean/go-netbox/netbox/client/dcim" |
Serge Bazanski | 9c5eac6 | 2018-10-06 12:13:57 +0100 | [diff] [blame] | 10 | "github.com/digitalocean/go-netbox/netbox/models" |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 11 | "github.com/golang/glog" |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 12 | |
Sergiusz Bazanski | 61594bb | 2019-07-21 15:20:51 +0200 | [diff] [blame] | 13 | pb "code.hackerspace.pl/hscloud/dc/topo/proto" |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | type MachinePort struct { |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 17 | Machine *Machine |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 18 | OtherEnd *SwitchPort |
| 19 | Name string |
| 20 | } |
| 21 | |
| 22 | type SwitchPort struct { |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 23 | Switch *Switch |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 24 | OtherEnd *MachinePort |
| 25 | Name string |
| 26 | } |
| 27 | |
| 28 | type Machine struct { |
| 29 | Name string |
| 30 | Complete bool |
| 31 | |
| 32 | Ports map[string]*MachinePort |
| 33 | } |
| 34 | |
| 35 | type Switch struct { |
| 36 | Name string |
| 37 | Complete bool |
| 38 | |
| 39 | Ports map[string]*SwitchPort |
| 40 | } |
| 41 | |
| 42 | type Graph struct { |
| 43 | Switches map[string]*Switch |
| 44 | Machines map[string]*Machine |
Serge Bazanski | 16e4ba2 | 2018-10-07 00:22:52 +0100 | [diff] [blame] | 45 | Mu sync.RWMutex |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | func New() *Graph { |
| 49 | return &Graph{ |
| 50 | Switches: make(map[string]*Switch), |
| 51 | Machines: make(map[string]*Machine), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func (g *Graph) RemoveMachine(name string) { |
| 56 | glog.Infof("Removed machine %q", name) |
| 57 | } |
| 58 | |
| 59 | func (g *Graph) RemoveSwitch(name string) { |
| 60 | glog.Infof("Removed switch %q", name) |
| 61 | } |
| 62 | |
Serge Bazanski | 477ffe7 | 2018-10-25 05:36:18 -0700 | [diff] [blame] | 63 | func (g *Graph) LoadConfig(conf *pb.Config) error { |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 64 | loadedMachines := make(map[string]bool) |
| 65 | loadedSwitches := make(map[string]bool) |
| 66 | |
| 67 | // Add new machines and switches. |
| 68 | for _, machinepb := range conf.Machine { |
| 69 | if machinepb.Name == "" { |
| 70 | return fmt.Errorf("empty machine name") |
| 71 | } |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 72 | if loadedMachines[machinepb.Name] { |
| 73 | return fmt.Errorf("duplicate machine name: %v", machinepb.Name) |
| 74 | } |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 75 | machine, ok := g.Machines[machinepb.Name] |
| 76 | if !ok { |
| 77 | machine = &Machine{ |
| 78 | Name: machinepb.Name, |
| 79 | Ports: make(map[string]*MachinePort), |
| 80 | } |
| 81 | for _, portpb := range machinepb.ManagedPort { |
| 82 | machine.Ports[portpb.Name] = &MachinePort{ |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 83 | Name: portpb.Name, |
| 84 | Machine: machine, |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | g.Machines[machinepb.Name] = machine |
| 88 | glog.Infof("Added machine %q with %d managed ports", machine.Name, len(machine.Ports)) |
| 89 | } |
| 90 | machine.Complete = false |
| 91 | loadedMachines[machinepb.Name] = true |
| 92 | } |
| 93 | for _, switchpb := range conf.Switch { |
| 94 | if switchpb.Name == "" { |
| 95 | return fmt.Errorf("empty switch name") |
| 96 | } |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 97 | if loadedSwitches[switchpb.Name] { |
| 98 | return fmt.Errorf("duplicate switch name: %v", switchpb.Name) |
| 99 | } |
| 100 | if loadedMachines[switchpb.Name] { |
| 101 | return fmt.Errorf("switch name collides with machine name: %v", switchpb.Name) |
| 102 | } |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 103 | sw, ok := g.Switches[switchpb.Name] |
| 104 | if !ok { |
| 105 | sw = &Switch{ |
| 106 | Name: switchpb.Name, |
| 107 | Ports: make(map[string]*SwitchPort), |
| 108 | } |
| 109 | for _, portpb := range switchpb.ManagedPort { |
| 110 | sw.Ports[portpb.Name] = &SwitchPort{ |
Serge Bazanski | c7be4a1 | 2018-10-06 13:18:05 +0100 | [diff] [blame] | 111 | Name: portpb.Name, |
| 112 | Switch: sw, |
Serge Bazanski | 28f4907 | 2018-10-06 11:31:18 +0100 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | g.Switches[switchpb.Name] = sw |
| 116 | glog.Infof("Added switch %q with %d managed ports", sw.Name, len(sw.Ports)) |
| 117 | } |
| 118 | sw.Complete = false |
| 119 | loadedSwitches[switchpb.Name] = true |
| 120 | } |
| 121 | |
| 122 | // Remove old machines and switches. |
| 123 | removeMachines := make(map[string]bool) |
| 124 | removeSwitches := make(map[string]bool) |
| 125 | for name, _ := range g.Switches { |
| 126 | if !loadedSwitches[name] { |
| 127 | removeSwitches[name] = true |
| 128 | } |
| 129 | } |
| 130 | for name, _ := range g.Machines { |
| 131 | if !loadedMachines[name] { |
| 132 | removeMachines[name] = true |
| 133 | } |
| 134 | } |
| 135 | for name, _ := range removeMachines { |
| 136 | g.RemoveMachine(name) |
| 137 | } |
| 138 | for name, _ := range removeSwitches { |
| 139 | g.RemoveSwitch(name) |
| 140 | } |
| 141 | return nil |
| 142 | |
| 143 | } |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 144 | |
| 145 | func (g *Graph) FeedFromNetbox(ctx context.Context, nb *client.NetBox) error { |
Serge Bazanski | 9c5eac6 | 2018-10-06 12:13:57 +0100 | [diff] [blame] | 146 | // Clear all connections first, because it's easier that way. |
| 147 | for _, machine := range g.Machines { |
| 148 | for _, port := range machine.Ports { |
| 149 | port.OtherEnd = nil |
| 150 | } |
| 151 | } |
| 152 | for _, sw := range g.Switches { |
| 153 | for _, port := range sw.Ports { |
| 154 | port.OtherEnd = nil |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Load new connections. |
| 159 | // Iterating over just machines should be fine if all connections are |
| 160 | // guaranteed to be between machines and switches (which is the model for |
| 161 | // now). |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 162 | for _, machine := range g.Machines { |
| 163 | req := &dcim.DcimInterfaceConnectionsListParams{ |
| 164 | Device: &machine.Name, |
| 165 | Context: ctx, |
| 166 | } |
| 167 | res, err := nb.Dcim.DcimInterfaceConnectionsList(req, nil) |
| 168 | if err != nil { |
| 169 | return fmt.Errorf("while querying information about %q: %v", machine.Name, err) |
| 170 | } |
| 171 | for _, connection := range res.Payload.Results { |
Serge Bazanski | 9c5eac6 | 2018-10-06 12:13:57 +0100 | [diff] [blame] | 172 | ia := connection.InterfaceA |
| 173 | ib := connection.InterfaceB |
| 174 | if ia == nil || ib == nil { |
| 175 | continue |
| 176 | } |
| 177 | |
| 178 | // Find which way this thing actually connects. |
| 179 | var thisSide, otherSide *models.PeerInterface |
| 180 | if ia.Device.Name == machine.Name { |
| 181 | thisSide = ia |
| 182 | otherSide = ib |
| 183 | } else if ib.Device.Name == machine.Name { |
| 184 | thisSide = ib |
| 185 | otherSide = ia |
| 186 | } else { |
| 187 | glog.Warning("Netbox connectivity for %q reported a link without it involced..?", machine.Name) |
| 188 | continue |
| 189 | } |
| 190 | |
| 191 | thisPort, ok := machine.Ports[*thisSide.Name] |
| 192 | if !ok { |
| 193 | continue |
| 194 | } |
| 195 | sw, ok := g.Switches[otherSide.Device.Name] |
| 196 | if !ok { |
| 197 | glog.Warningf("Machine %q port %q is managed but connected to unknown device %q", machine.Name, thisPort.Name, otherSide.Device.Name) |
| 198 | continue |
| 199 | } |
| 200 | otherPort, ok := sw.Ports[*otherSide.Name] |
| 201 | if !ok { |
| 202 | glog.Warningf("Machine %q port %q is managed but connected to unmanaged port %q on %q", machine.Name, thisPort.Name, otherSide.Name, sw.Name) |
| 203 | continue |
| 204 | } |
| 205 | |
| 206 | // Connect the two together! |
| 207 | thisPort.OtherEnd = otherPort |
| 208 | otherPort.OtherEnd = thisPort |
| 209 | glog.Infof("Connected: %s/%s <-> %s/%s", machine.Name, thisPort.Name, sw.Name, otherPort.Name) |
Serge Bazanski | 2c6c6d1 | 2018-10-06 11:55:04 +0100 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | return nil |
| 213 | } |