build graph from config
diff --git a/graph/.graph.go.swp b/graph/.graph.go.swp
new file mode 100644
index 0000000..14109d8
--- /dev/null
+++ b/graph/.graph.go.swp
Binary files differ
diff --git a/graph/graph.go b/graph/graph.go
new file mode 100644
index 0000000..35b1f23
--- /dev/null
+++ b/graph/graph.go
@@ -0,0 +1,123 @@
+package graph
+
+import (
+ "fmt"
+
+ confpb "code.hackerspace.pl/q3k/topo/proto/config"
+ "github.com/golang/glog"
+)
+
+type MachinePort struct {
+ OtherEnd *SwitchPort
+ Name string
+}
+
+type SwitchPort struct {
+ OtherEnd *MachinePort
+ Name string
+}
+
+type Machine struct {
+ Name string
+ Complete bool
+
+ Ports map[string]*MachinePort
+}
+
+type Switch struct {
+ Name string
+ Complete bool
+
+ Ports map[string]*SwitchPort
+}
+
+type Graph struct {
+ Switches map[string]*Switch
+ Machines map[string]*Machine
+}
+
+func New() *Graph {
+ return &Graph{
+ Switches: make(map[string]*Switch),
+ Machines: make(map[string]*Machine),
+ }
+}
+
+func (g *Graph) RemoveMachine(name string) {
+ glog.Infof("Removed machine %q", name)
+}
+
+func (g *Graph) RemoveSwitch(name string) {
+ glog.Infof("Removed switch %q", name)
+}
+
+func (g *Graph) LoadConfig(conf *confpb.Config) error {
+ loadedMachines := make(map[string]bool)
+ loadedSwitches := make(map[string]bool)
+
+ // Add new machines and switches.
+ for _, machinepb := range conf.Machine {
+ if machinepb.Name == "" {
+ return fmt.Errorf("empty machine name")
+ }
+ machine, ok := g.Machines[machinepb.Name]
+ if !ok {
+ machine = &Machine{
+ Name: machinepb.Name,
+ Ports: make(map[string]*MachinePort),
+ }
+ for _, portpb := range machinepb.ManagedPort {
+ machine.Ports[portpb.Name] = &MachinePort{
+ Name: portpb.Name,
+ }
+ }
+ g.Machines[machinepb.Name] = machine
+ glog.Infof("Added machine %q with %d managed ports", machine.Name, len(machine.Ports))
+ }
+ machine.Complete = false
+ loadedMachines[machinepb.Name] = true
+ }
+ for _, switchpb := range conf.Switch {
+ if switchpb.Name == "" {
+ return fmt.Errorf("empty switch name")
+ }
+ sw, ok := g.Switches[switchpb.Name]
+ if !ok {
+ sw = &Switch{
+ Name: switchpb.Name,
+ Ports: make(map[string]*SwitchPort),
+ }
+ for _, portpb := range switchpb.ManagedPort {
+ sw.Ports[portpb.Name] = &SwitchPort{
+ Name: portpb.Name,
+ }
+ }
+ g.Switches[switchpb.Name] = sw
+ glog.Infof("Added switch %q with %d managed ports", sw.Name, len(sw.Ports))
+ }
+ sw.Complete = false
+ loadedSwitches[switchpb.Name] = true
+ }
+
+ // Remove old machines and switches.
+ removeMachines := make(map[string]bool)
+ removeSwitches := make(map[string]bool)
+ for name, _ := range g.Switches {
+ if !loadedSwitches[name] {
+ removeSwitches[name] = true
+ }
+ }
+ for name, _ := range g.Machines {
+ if !loadedMachines[name] {
+ removeMachines[name] = true
+ }
+ }
+ for name, _ := range removeMachines {
+ g.RemoveMachine(name)
+ }
+ for name, _ := range removeSwitches {
+ g.RemoveSwitch(name)
+ }
+ return nil
+
+}