Developing lightweight computation at the DSG edge

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

[goclidote] Add RemoveDevicesFromAntidote() function



The RemoveDevicesFromAntidote() function gracefully removes from
AntidoteDB a []cnml.DeviceIpv4sGraphserver list of devices reusing a
single connection.
Signed-off-by: Roger Pueyo Centelles's avatarRoger Pueyo Centelles <rpueyo@ac.upc.edu>
parent 0da948dc
......@@ -379,6 +379,129 @@ func ReadDevicesFromAntidote(DatabaseHost string, DatabasePort int) []cnml.Devic
return devices
}
// RemoveDevicesFromAntidote removes an array of devices from AntidoteDB
func RemoveDevicesFromAntidote(oldDevices []cnml.DeviceIpv4sGraphserver, DatabaseHost string, DatabasePort int) []cnml.DeviceIpv4sGraphserver {
var remDevices []cnml.DeviceIpv4sGraphserver
// Start an AntidoteDB client
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, ERROR, "[RemoveDevicesFromAntidote]: Unable to create a new AntidoteDB go client")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, ERROR, "[RemoveDevicesFromAntidote]: Unable to start a transaction using the go client")
for _, v := range oldDevices {
// Remove the graphserver register from the device-id bucket
//fmt.Println(v)
bucketName := fmt.Sprintf("device-%d", v.ID)
registerName := "graphserver"
devError := false
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(registerName)
err = bucket.Update(tx, antidoteclient.RegPut(key, []byte("")))
errCheck(err, ERROR, fmt.Sprintf("[RemoveDevicesFromAntidote]: Unable to set a null value to register %s in bucket %s", registerName, bucketName))
regVal, err := bucket.ReadReg(tx, key)
errCheck(err, ERROR, fmt.Sprintf("[RemoveDevicesFromAntidote]: Unable to read register %s in bucket %s", registerName, bucketName))
//fmt.Println("regVal:", regVal)
if len(regVal) != 0 {
devError = true
}
// Empty the ipv4s set from the device-id bucket
//fmt.Println("Addresses:", v.Addresses)
for _, w := range v.Addresses {
// fmt.Println("Address:", w)
itemValue := w
setName := "ipv4s"
key = antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetRemove(key, []byte(itemValue)))
errCheck(err, ERROR, fmt.Sprintf("[RemoveDevicesFromAntidote]: Unable to remove item %s from set %s in bucket %s", itemValue, setName, bucketName))
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, ERROR, fmt.Sprintf("[RemoveDevicesFromAntidote]: Unable to read set %s in bucket %s", setName, bucketName))
// fmt.Println("Setval:", setVal)
itemNotInSet := true
for _, x := range setVal {
if string(x[:]) == itemValue {
itemNotInSet = false
}
}
if !itemNotInSet {
devError = true
}
}
// Empty the snmpnames set from the device-id bucket
for _, w := range v.SNMPNames {
itemValue := w
setName := "snmpnames"
key = antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetRemove(key, []byte(itemValue)))
errCheck(err, ERROR, fmt.Sprintf("[RemoveDevicesFromAntidote]: Unable to remove item %s from set %s in bucket %s", itemValue, setName, bucketName))
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, ERROR, fmt.Sprintf("[RemoveDevicesFromAntidote]: Unable to read set %s in bucket %s", setName, bucketName))
itemNotInSet := true
for _, x := range setVal {
if string(x[:]) == itemValue {
itemNotInSet = false
}
}
if !itemNotInSet {
devError = true
}
}
// Remove the device from the devices set in the guifi bucket
bucketName = "guifi"
setName := "devices"
itemValue := fmt.Sprintf("%d", v.ID)
bucket = antidoteclient.Bucket{Bucket: []byte(bucketName)}
key = antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetRemove(key, []byte(itemValue)))
errCheck(err, ERROR, fmt.Sprintf("[RemoveDevicesFromAntidote]: Unable to remove item %s from set %s in bucket %s", itemValue, setName, bucketName))
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, ERROR, fmt.Sprintf("[RemoveDevicesFromAntidote]: Unable to read set %s in bucket %s", setName, bucketName))
itemNotInSet := true
for _, w := range setVal {
if string(w[:]) == itemValue {
itemNotInSet = false
}
}
if !itemNotInSet {
devError = true
}
if devError == false {
// Device successfully removed
remDevices = append(remDevices, v)
}
}
err = tx.Commit()
errCheck(err, ERROR, "[AntidoteRemoveRegisterInBucket]: Unable to commit transaction")
return remDevices
}
// The errCheck function gracefully manages errors, warnings, etc.
func errCheck(e error, level int, message string) {
......
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