topology-map/internal/cache/datacache.go

173 lines
3.4 KiB
Go
Raw Normal View History

2024-09-22 08:27:05 +00:00
package cache
import (
"sync"
"git.faercol.me/faercol/topology-map/internal/models"
)
func NewDataCache() *DataCache {
return &DataCache{
2024-09-23 15:34:40 +00:00
lock: sync.Mutex{},
devices: make(map[int]*models.Device),
interfaces: make(map[int]*models.Interface),
cables: make(map[int]*models.Cable),
vms: make(map[int]*models.VM),
vmInterfaces: make(map[int]*models.VMInterface),
2024-09-22 08:27:05 +00:00
}
}
type DataCache struct {
2024-09-23 15:34:40 +00:00
lock sync.Mutex
devices map[int]*models.Device
interfaces map[int]*models.Interface
cables map[int]*models.Cable
vms map[int]*models.VM
arpRecords []*models.ARPRecord
vmInterfaces map[int]*models.VMInterface
2024-09-22 08:27:05 +00:00
}
func (d *DataCache) AddDevice(device models.Device) {
d.lock.Lock()
defer d.lock.Unlock()
d.devices[device.ID] = &device
}
func (d *DataCache) GetDevices() []*models.Device {
d.lock.Lock()
defer d.lock.Unlock()
var res []*models.Device
for _, dev := range d.devices {
res = append(res, dev)
}
return res
}
func (d *DataCache) AddInterface(iface models.Interface) {
d.lock.Lock()
defer d.lock.Unlock()
d.interfaces[iface.ID] = &iface
}
func (d *DataCache) GetInterfaces() []*models.Interface {
d.lock.Lock()
defer d.lock.Unlock()
var res []*models.Interface
for _, iface := range d.interfaces {
res = append(res, iface)
}
return res
}
func (d *DataCache) AddCable(cable models.Cable) {
d.lock.Lock()
defer d.lock.Unlock()
d.cables[cable.ID] = &cable
}
func (d *DataCache) GetCables() []*models.Cable {
d.lock.Lock()
defer d.lock.Unlock()
var res []*models.Cable
for _, cable := range d.cables {
res = append(res, cable)
}
return res
}
func (d *DataCache) AddVM(vm models.VM) {
d.lock.Lock()
defer d.lock.Unlock()
d.vms[vm.ID] = &vm
}
func (d *DataCache) GetVMs() []*models.VM {
d.lock.Lock()
defer d.lock.Unlock()
var res []*models.VM
for _, vm := range d.vms {
res = append(res, vm)
}
return res
}
2024-09-23 11:45:30 +00:00
func (d *DataCache) AddARPRecords(records []*models.ARPRecord) {
d.lock.Lock()
defer d.lock.Unlock()
d.arpRecords = records
}
2024-09-22 08:27:05 +00:00
func (d *DataCache) ReconcileData() {
d.lock.Lock()
defer d.lock.Unlock()
for id, cable := range d.cables {
ifaceA, ok := d.interfaces[cable.ATerminations[0].ObjectID]
if !ok {
continue
}
cable.ATerminations[0].Object.Interface = *ifaceA
ifaceB, ok := d.interfaces[cable.BTerminations[0].ObjectID]
if !ok {
continue
}
cable.BTerminations[0].Object.Interface = *ifaceB
d.cables[id] = cable
2024-09-23 11:45:30 +00:00
}
for _, r := range d.arpRecords {
for _, iif := range d.interfaces {
if r.MacAddress == iif.MacAddress {
r.Device = &iif.Device
2024-09-23 15:34:40 +00:00
break
}
}
for _, iif := range d.vmInterfaces {
if r.MacAddress == iif.MacAddress {
r.VM = &iif.VM
break
2024-09-23 11:45:30 +00:00
}
}
}
}
func (d *DataCache) GetUnmonitoredMachines() []models.UnmonitoredDevice {
2024-09-23 15:34:40 +00:00
d.lock.Lock()
defer d.lock.Unlock()
2024-09-23 11:45:30 +00:00
var res []models.UnmonitoredDevice
2024-09-22 08:27:05 +00:00
2024-09-23 11:45:30 +00:00
for _, r := range d.arpRecords {
2024-09-23 15:34:40 +00:00
if r.Device == nil && r.VM == nil {
2024-09-23 11:45:30 +00:00
res = append(res, models.UnmonitoredDevice{Address: r.Address, MacAddress: r.MacAddress})
}
2024-09-22 08:27:05 +00:00
}
2024-09-23 11:45:30 +00:00
return res
2024-09-22 08:27:05 +00:00
}
2024-09-23 15:34:40 +00:00
func (d *DataCache) AddVMInterface(iface models.VMInterface) {
d.lock.Lock()
defer d.lock.Unlock()
d.vmInterfaces[iface.ID] = &iface
}
func (d *DataCache) GetVMInterfaces() []*models.VMInterface {
d.lock.Lock()
defer d.lock.Unlock()
res := []*models.VMInterface{}
for _, iif := range d.vmInterfaces {
res = append(res, iif)
}
return res
}