package switchy import ( "errors" "log" "sync" "github.rackspace.com/gorax/clients/dcxSwitchapi" "github.rackspace.com/gorax/clients/unipede" ) type ( SwitchNeighbor struct { Name string `json:"name"` NeighborPorts []*NeighborPort `json:"neighbor_port_data"` Direction string `json:"direction"` Chassis string `json:"chassis"` Role string `json:"role"` Service string `json:"service"` sw *SwitchClient } NeighborPort struct { Local *SwitchPort `json:"local_interface"` Remote *SwitchPort `json:"neighbor_interface"` } ) func (s *SwitchClient) UnipedeNeighborData(age int) error { if s.u == nil { return errors.New("unipded client cannot be nil.") } upNeighbor, err := s.u.UpNeighbors(s.SwitchName, nil) if err != nil { return err } nm := upNeighbor.NeighborMap() for n, p := range nm { s.AddNeighborPorts(n, p) } if err = s.UnipedeNeighborInfo(0); err != nil { return err } return nil } func (s *SwitchClient) AddNeighborPorts(name string, neighbors []*unipede.NeighborItem) { n := SwitchNeighbor{ Name: name, } for _, neighbor := range neighbors { n.NeighborPorts = append(n.NeighborPorts, &NeighborPort{ Local: func() *SwitchPort { sp := s.SwitchPortByName(neighbor.LocalInterface) if sp != nil { return sp } p := s.intMerge.PortByName(neighbor.LocalInterface) if p != nil { return &SwitchPort{ PortName: p.Name, FeInt: p.FeInt, FeAggTo: p.FeAggTo, CoreDevice: p.CoreDevice, CoreAccount: p.CoreAccount, } } return &SwitchPort{ PortName: neighbor.LocalInterface, } }(), Remote: &SwitchPort{ PortName: neighbor.RemoteInterface, }, }) } s.Neighbors = append(s.Neighbors, &n) } func (s *SwitchClient) UnipedeNeighborInfo(age int) error { if s.u == nil { return errors.New("unipded client cannot be nil.") } for _, neighbor := range s.Neighbors { sw, err := NewSwitch(neighbor.Name, "", nil, s.u) if err != nil { return err } if err = sw.SwitchModelInfo(); err != nil { log.Println(err) return nil } neighbor.Chassis = sw.Chassis neighbor.Role = sw.Role neighbor.Service = sw.Service } return nil } func (s *SwitchClient) UnipedeNeighborConfig(age int) error { var err error if s.u == nil { return errors.New("unipded client cannot be nil.") } for _, neighbor := range s.Neighbors { pl := neighbor.RemotePortList() if len(pl) == 0 { continue } if neighbor.sw == nil { neighbor.sw = newSwitchAndPorts(neighbor.Name, nil, pl, nil, s.u) if err = neighbor.sw.SwitchModelInfo(); err != nil { return err } } if err = neighbor.sw.PortNameGather(); err != nil { return err } if err = neighbor.sw.UnipedeConfigParse(age); err != nil { return err } } return nil } func (s *SwitchNeighbor) RemotePortList() (pl []*SwitchPort) { for _, port := range s.NeighborPorts { pl = append(pl, port.Remote) } return } func (s *SwitchClient) NeighborSwitchAPIStatus(swapi *dcxSwitchapi.Client) error { for _, n := range s.Neighbors { if err := n.sw.SwitchAPIStatus(swapi); err != nil { return err } } return nil } func MapSwitchNeighborUnipede(clients map[string]*SwitchClient) { swChan := make(chan *SwitchClient) wg := new(sync.WaitGroup) workerStart(swChan, func(sw *SwitchClient) { if err := sw.UnipedeNeighborData(0); err != nil { log.Println(err) sw.Errors = append(sw.Errors, err) } }, wg) for _, sw := range clients { wg.Add(1) swChan <- sw } wg.Wait() close(swChan) } func MapSwitchNeighborUnipedeConfig(clients map[string]*SwitchClient) { swChan := make(chan *SwitchClient) wg := new(sync.WaitGroup) workerStart(swChan, func(sw *SwitchClient) { if err := sw.UnipedeNeighborConfig(0); err != nil { sw.Errors = append(sw.Errors, err) } }, wg) for _, sw := range clients { wg.Add(1) swChan <- sw } wg.Wait() close(swChan) } func MapSwitchNeighborUnipedeInfo(clients map[string]*SwitchClient) { swChan := make(chan *SwitchClient) wg := new(sync.WaitGroup) workerStart(swChan, func(sw *SwitchClient) { if err := sw.UnipedeNeighborInfo(0); err != nil { sw.Errors = append(sw.Errors, err) log.Println(err) } }, wg) for _, sw := range clients { wg.Add(1) swChan <- sw } wg.Wait() close(swChan) }