Developing lightweight computation at the DSG edge

Commit 30c6317e authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[fetch] Save IPv4 addresses [properly] to Antidote



Add a few functions and improve the CLI output.
Signed-off-by: default avatarRoger Pueyo Centelles <roger.pueyo@guifi.net>
parent e10c714e
......@@ -281,54 +281,100 @@ func main() {
antidoteDevices := readDevicesFromAntidote()
// Export devices and their addresses to AntidoteDB
antidoteDevSucc := 0
antidoteDevFail := 0
//antidoteAddSucc := 0
//antidoteAddFail := 0
antidoteDelSucc := 0
antidoteDelFail := 0
antidoteAddDeviceSuccess := 0
antidoteAddDeviceFail := 0
antidoteAddAddressSuccess := 0
antidoteAddAddressFail := 0
antidoteDeleteDeviceSuccess := 0
antidoteDeleteDeviceFail := 0
antidoteDeleteAddressSuccess := 0
antidoteDeleteAddressFail := 0
// Remove from AntidoteDB those devices that are not in the CNML file anymore
for _, v := range antidoteDevices {
for k, v := range antidoteDevices {
if !isDeviceIPv4AddressesInArray(v, devsadds) {
// Remove IPv4 addresses of the removed device
for _, w := range readDeviceIpv4AddressesFromAntidote(v).Addresses {
if removeIpv4addressFromDevice(v, w) {
antidoteDeleteAddressSuccess++
} else {
antidoteDeleteAddressFail++
}
}
// Remove the device from the list
if removeDeviceFromAntidote(v) {
antidoteDelSucc++
antidoteDeleteDeviceSuccess++
} else {
antidoteDelFail++
antidoteDeleteDeviceFail++
}
// If the device is in the CNML file, check for old IPv4 addresses no longer in use and remove them
} else {
for _, w := range devsadds {
for _, x := range v.Addresses {
if !isIPv4addressInDevicesArray(w, x) {
if removeIpv4addressFromDevice(w, x) {
antidoteDeleteAddressSuccess++
} else {
antidoteDeleteAddressFail++
}
}
}
}
fmt.Printf("\r%d devices removed from AntidoteDB (%d success, %d fail)", antidoteDelSucc+antidoteDelFail, antidoteDelSucc, antidoteDelFail)
}
if k == 0 {
fmt.Printf("%d devices removed from AntidoteDB (%d success, %d fail)\n", antidoteDeleteDeviceSuccess+antidoteDeleteDeviceFail, antidoteDeleteDeviceSuccess, antidoteDeleteDeviceFail)
fmt.Printf("%d IPv4 addresses removed from AntidoteDB (%d success, %d fail)\n", antidoteDeleteAddressSuccess+antidoteDeleteAddressFail, antidoteDeleteAddressSuccess, antidoteDeleteAddressFail)
} else {
fmt.Printf("\033[2A\r%d devices removed from AntidoteDB (%d success, %d fail)\n", antidoteDeleteDeviceSuccess+antidoteDeleteDeviceFail, antidoteDeleteDeviceSuccess, antidoteDeleteDeviceFail)
fmt.Printf("\r%d IPv4 addresses removed from AntidoteDB (%d success, %d fail)\n", antidoteDeleteAddressSuccess+antidoteDeleteAddressFail, antidoteDeleteAddressSuccess, antidoteDeleteAddressFail)
}
}
fmt.Printf("\r%d devices removed from AntidoteDB (%d success, %d fail)\n", antidoteDelSucc+antidoteDelFail, antidoteDelSucc, antidoteDelFail)
fmt.Printf("\033[2A\r%d devices removed from AntidoteDB (%d success, %d fail)\n", antidoteDeleteDeviceSuccess+antidoteDeleteDeviceFail, antidoteDeleteDeviceSuccess, antidoteDeleteDeviceFail)
fmt.Printf("\r%d IPv4 addresses removed from AntidoteDB (%d success, %d fail)\n", antidoteDeleteAddressSuccess+antidoteDeleteAddressFail, antidoteDeleteAddressSuccess, antidoteDeleteAddressFail)
// Add to AntidoteDB those devices that are new in the CNML file
for _, v := range devsadds {
for k, v := range devsadds {
// Check if the device is in Antidote already and add it if missing
saveDevFail := false
if !isDeviceIPv4AddressesInArray(v, antidoteDevices) && len(v.Addresses) > 0 {
if saveDeviceToAntidote(v.ID) {
antidoteDevSucc++
// Add the device to the devices set
if saveDeviceToAntidote(v) {
antidoteAddDeviceSuccess++
} else {
antidoteDevFail++
antidoteAddDeviceFail++
saveDevFail = true
}
}
if !saveDevFail {
// Iterate through all the device's addresses and add them if new
for _, w := range v.Addresses {
if saveIpv4addresToDeviceToAntidote(v, w) {
antidoteAddAddressSuccess++
} else {
antidoteAddAddressFail++
}
}
fmt.Printf("\r%d devices exported to AntidoteDB (%d success, %d fail)", antidoteDevSucc+antidoteDevFail, antidoteDevSucc, antidoteDevFail)
}
if k == 0 {
fmt.Printf("%d devices added to AntidoteDB (%d success, %d fail)\n", antidoteAddDeviceSuccess+antidoteAddDeviceFail, antidoteAddDeviceSuccess, antidoteAddDeviceFail)
fmt.Printf("%d IPv4 addresses added or updated to AntidoteDB (%d success, %d fail)\n", antidoteAddAddressSuccess+antidoteAddAddressFail, antidoteAddAddressSuccess, antidoteAddAddressFail)
} else {
fmt.Printf("\033[2A\r%d devices added to AntidoteDB (%d success, %d fail)\n", antidoteAddDeviceSuccess+antidoteAddDeviceFail, antidoteAddDeviceSuccess, antidoteAddDeviceFail)
fmt.Printf("\r%d IPv4 addresses added or updated to AntidoteDB (%d success, %d fail)\n", antidoteAddAddressSuccess+antidoteAddAddressFail, antidoteAddAddressSuccess, antidoteAddAddressFail)
}
}
fmt.Printf("\r%d devices exported to AntidoteDB (%d success, %d fail)\n", antidoteDevSucc+antidoteDevFail, antidoteDevSucc, antidoteDevFail)
// if saveDeviceToAntidote(v.ID) {
// antidoteDevSucc++
// } else {
// antidoteDevFail++
// }
// for _, w := range v.Interfaces {
// if saveDeviceIpv4ToAntidote(v.ID, w.Ipv4) {
// antidoteAddSucc++
// } else {
// antidoteAddFail++
// }
//
// }
// fmt.Printf("\033[1A\r%d devices exported to AntidoteDB (%d success, %d fail)\n", k, antidoteDevSucc, antidoteDevFail)
// fmt.Printf("%d addresses exported to AntidoteDB (%d success, %d fail)", antidoteAddSucc+antidoteAddFail, antidoteAddSucc, antidoteAddFail)
// }
fmt.Printf("\033[2A\r%d devices added to AntidoteDB (%d success, %d fail)\n", antidoteAddDeviceSuccess+antidoteAddDeviceFail, antidoteAddDeviceSuccess, antidoteAddDeviceFail)
fmt.Printf("\r%d IPv4 addresses added or updated to AntidoteDB (%d success, %d fail)\n", antidoteAddAddressSuccess+antidoteAddAddressFail, antidoteAddAddressSuccess, antidoteAddAddressFail)
if antidoteAddDeviceSuccess+antidoteAddDeviceFail == 0 {
// fmt.Printf("\n")
}
// fmt.Printf("\033[2A\r%d devices added to AntidoteDB (%d success, %d failc)\n", antidoteAddDeviceSuccess+antidoteAddDeviceFail, antidoteAddDeviceSuccess, antidoteAddDeviceFail)
// fmt.Printf("\r%d IPv4 addresses added or updated to AntidoteDB (%d success, %d faild)\n", antidoteAddAddressSuccess+antidoteAddAddressFail, antidoteAddAddressSuccess, antidoteAddAddressFail)
}
......@@ -555,9 +601,28 @@ func readDevicesFromAntidote() []DeviceIpv4Adddresses {
return devices
}
// Read all the devices from Antidote using João's REST server
func readDeviceIpv4AddressesFromAntidote(device DeviceIpv4Adddresses) DeviceIpv4Adddresses {
for _, v := range antidoteReadItemsFromSetInBucket("guifi", fmt.Sprintf("device-%d", device.ID)) {
device.Addresses = append(device.Addresses, v)
}
return device
}
// Save a device ID to Antidote using João's REST server
func saveDeviceToAntidote(ID int) bool {
return antidoteAddItemToSetInBucket("guifi", "devices", fmt.Sprintf("%d", ID))
func saveDeviceToAntidote(device DeviceIpv4Adddresses) bool {
return antidoteAddItemToSetInBucket("guifi", "devices", fmt.Sprintf("%d", device.ID))
}
// Save an IPv4 address to a device
func saveIpv4addresToDeviceToAntidote(device DeviceIpv4Adddresses, address string) bool {
return antidoteAddItemToSetInBucket("guifi", fmt.Sprintf("device-%d", device.ID), address)
}
func removeIpv4addressFromDevice(device DeviceIpv4Adddresses, address string) bool {
return antidoteRemoveItemFromSetInBucket("guifi", fmt.Sprintf("device-%d", device.ID), address)
}
// // Save a device ID and an IPv4 address to Antidote using João's REST server
......@@ -657,6 +722,15 @@ func isDeviceIPv4AddressesInArray(device DeviceIpv4Adddresses, devices []DeviceI
return false
}
func isIPv4addressInDevicesArray(device DeviceIpv4Adddresses, address string) bool {
for _, v := range device.Addresses {
if v == address {
return true
}
}
return false
}
// Panic on error
func errCheck(e error) {
if e != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment