Developing lightweight computation at the DSG edge

Commit fb15bf70 authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[fetch] Remove old devices from AntidoteDB on CNML update



When the CNML file is updated, remove from AntidoteDB those devices
that have been removed from the CNML.
Signed-off-by: default avatarRoger Pueyo Centelles <roger.pueyo@guifi.net>
parent bc2d15b3
......@@ -11,6 +11,7 @@ import (
"net"
"net/http"
"os"
"strconv"
)
// The Cnml struct comprises the whole CNML
......@@ -259,29 +260,57 @@ func main() {
// }
// }
// Read current AntidoteDB devices
antidote_devices := readDevicesFromAntidote()
// Export devices and their addresses to AntidoteDB
antidote_dev_succ := 0
antidote_dev_fail := 0
antidote_add_succ := 0
antidote_add_fail := 0
//antidote_add_succ := 0
//antidote_add_fail := 0
antidote_del_succ := 0
antidote_del_fail := 0
// Remove from AntidoteDB those devices that are not in the CNML file anymore
for _, v := range antidote_devices {
if !isDeviceInArray(v, alldevices) {
if removeDeviceFromAntidote(v) {
antidote_del_succ++
} else {
antidote_del_fail++
}
fmt.Printf("\r%d devices removed from AntidoteDB (%d success, %d fail)", antidote_del_succ+antidote_del_fail, antidote_del_succ, antidote_del_fail)
}
}
// Add to AntidoteDB those devices that are new in the CNML file
for k, v := range alldevices {
if saveDeviceToAntidote(v.ID) {
antidote_dev_succ++
} else {
antidote_dev_fail++
}
for _, w := range v.Interfaces {
if saveDeviceIpv4ToAntidote(v.ID, w.Ipv4) {
antidote_add_succ++
if !isDeviceInArray(v, antidote_devices) {
if saveDeviceToAntidote(v.ID) {
antidote_dev_succ++
} else {
antidote_add_fail++
antidote_dev_fail++
}
fmt.Printf("\r%d devices exported to AntidoteDB (%d success, %d fail)", k, antidote_dev_succ, antidote_dev_fail)
//fmt.Printf("%d addresses exported to AntidoteDB (%d success, %d fail)", antidote_add_succ+antidote_add_fail, antidote_add_succ, antidote_add_fail)
}
fmt.Printf("\033[1A\r%d devices exported to AntidoteDB (%d success, %d fail)\n", k, antidote_dev_succ, antidote_dev_fail)
fmt.Printf("%d addresses exported to AntidoteDB (%d success, %d fail)", antidote_add_succ+antidote_add_fail, antidote_add_succ, antidote_add_fail)
}
// if saveDeviceToAntidote(v.ID) {
// antidote_dev_succ++
// } else {
// antidote_dev_fail++
// }
// for _, w := range v.Interfaces {
// if saveDeviceIpv4ToAntidote(v.ID, w.Ipv4) {
// antidote_add_succ++
// } else {
// antidote_add_fail++
// }
//
// }
// fmt.Printf("\033[1A\r%d devices exported to AntidoteDB (%d success, %d fail)\n", k, antidote_dev_succ, antidote_dev_fail)
// fmt.Printf("%d addresses exported to AntidoteDB (%d success, %d fail)", antidote_add_succ+antidote_add_fail, antidote_add_succ, antidote_add_fail)
// }
}
......@@ -488,6 +517,63 @@ func getZonesInZoneExc(zone Zone) []Zone {
return zones
}
// Remove a device by its ID from Antidote using João's REST server
func removeDeviceFromAntidote(device Device) bool {
request := fmt.Sprintf("http://localhost:3000/set/remove/guifi/devices/%d", device.ID)
//fmt.Println("Saving device", ID, "to AntidoteDB")
response, err := http.Get(request)
errCheck(err)
//fmt.Println(response)
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err)
rest_reply := string(bodyBytes)
if rest_reply == "ok" {
//fmt.Println("Success!")
}
return true
}
return false
}
// Read all the devices from Antidote using João's REST server
func readDevicesFromAntidote() []Device {
var devices []Device
request := fmt.Sprintf("http://localhost:3000/set/read/guifi/devices")
response, err := http.Get(request)
errCheck(err)
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err)
var string_rest_reply []string
err = json.Unmarshal(bodyBytes, &string_rest_reply)
errCheck(err)
for _, v := range string_rest_reply {
this_dev_id, err := strconv.Atoi(v)
errCheck(err)
var this_dev Device
this_dev.ID = this_dev_id
devices = append(devices, this_dev)
//println(v, this_dev_id)
}
}
return devices
}
// Save a device ID to Antidote using João's REST server
func saveDeviceToAntidote(ID int) bool {
request := fmt.Sprintf("http://localhost:3000/set/add/guifi/devices/%d", ID)
......@@ -536,6 +622,16 @@ func saveDeviceIpv4ToAntidote(ID int, Ipv4 string) bool {
return false
}
// Check if an int item is in an ints array
func isDeviceInArray(device Device, devices []Device) bool {
for _, v := range devices {
if v.ID == device.ID {
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