Developing lightweight computation at the DSG edge

Commit 67d23f71 authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[goclidote] Add AddDevicesToAntidote() function



The AddDevicesToAntidote89 function gracefully adds to AntidoteDB a
[]cnml.DeviceIpv4sGraphserver list of devices reusing a single
connection, hence reducing the operation time.
Signed-off-by: Roger Pueyo Centelles's avatarRoger Pueyo Centelles <rpueyo@ac.upc.edu>
parent 075a3235
...@@ -502,6 +502,133 @@ func RemoveDevicesFromAntidote(oldDevices []cnml.DeviceIpv4sGraphserver, Databas ...@@ -502,6 +502,133 @@ func RemoveDevicesFromAntidote(oldDevices []cnml.DeviceIpv4sGraphserver, Databas
return remDevices return remDevices
} }
// AddDevicesToAntidote adds an array of DeviceIpv4sGraphservers from AntidoteDB
func AddDevicesToAntidote(newDevices []cnml.DeviceIpv4sGraphserver, DatabaseHost string, DatabasePort int) []cnml.DeviceIpv4sGraphserver {
var addedDevices []cnml.DeviceIpv4sGraphserver
// Start an AntidoteDB client
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, ERROR, "[AddDevicesToAntidote]: Unable to create a new AntidoteDB go client")
defer client.Close()
// Iterate all the new devices
for _, v := range newDevices {
// Start a transaction
tx, err := client.StartTransaction()
errCheck(err, ERROR, "[AddDevicesToAntidote]: Unable to start a transaction using the go client")
devError := false
// Add the device ID to 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.SetAdd(key, []byte(itemValue)))
errCheck(err, ERROR, fmt.Sprintf("[AddDevicesToAntidote]: Unable to add item %s from set %s in bucket %s", itemValue, setName, bucketName))
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, ERROR, fmt.Sprintf("[AddDevicesToAntidote]: Unable to read set %s in bucket %s", setName, bucketName))
itemInSet := false
// Iterate the updated set to see if the item was added
for _, w := range setVal {
if string(w[:]) == itemValue {
itemInSet = true
}
}
if !itemInSet {
devError = true
}
// Add the graphserver register to the device-id bucket
bucketName = fmt.Sprintf("device-%d", v.ID)
registerName := "graphserver"
itemValue = fmt.Sprintf("%d", v.GraphServer)
bucket = antidoteclient.Bucket{Bucket: []byte(bucketName)}
key = antidoteclient.Key(registerName)
err = bucket.Update(tx, antidoteclient.RegPut(key, []byte(itemValue)))
errCheck(err, ERROR, fmt.Sprintf("[AddDevicesToAntidote]: Unable to set a value %s to register %s in bucket %s", itemValue, registerName, bucketName))
regVal, err := bucket.ReadReg(tx, key)
errCheck(err, ERROR, fmt.Sprintf("[AddDevicesToAntidote]: Unable to read register %s in bucket %s", registerName, bucketName))
if string(regVal[:]) != itemValue {
devError = true
}
// Add the IPv4 addresses to the ipv4s set in the device-id bucket
for _, w := range v.Addresses {
setName := "ipv4s"
itemValue = w
key = antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetAdd(key, []byte(itemValue)))
errCheck(err, ERROR, fmt.Sprintf("[AddDevicesToAntidote]: Unable to add item %s from set %s in bucket %s", itemValue, setName, bucketName))
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, ERROR, fmt.Sprintf("[AddDevicesToAntidote]: Unable to read set %s in bucket %s", setName, bucketName))
itemInSet := false
for _, x := range setVal {
if string(x[:]) == itemValue {
itemInSet = true
}
}
if !itemInSet {
devError = true
}
}
// Add the SNMP names to the snmpnames set in the device-id bucket
for _, w := range v.SNMPNames {
setName := "snmpnames"
itemValue := w
key = antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetAdd(key, []byte(itemValue)))
errCheck(err, ERROR, fmt.Sprintf("[AddDevicesToAntidote]: Unable to add item %s from set %s in bucket %s", itemValue, setName, bucketName))
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, ERROR, fmt.Sprintf("[AddDevicesToAntidote]: Unable to read set %s in bucket %s", setName, bucketName))
itemInSet := false
for _, x := range setVal {
if string(x[:]) == itemValue {
itemInSet = true
}
}
if !itemInSet {
devError = true
}
}
if devError == false {
// Device successfully removed
addedDevices = append(addedDevices, v)
}
err = tx.Commit()
errCheck(err, ERROR, "[AddDevicesToAntidote]: Unable to commit transaction")
}
return addedDevices
}
// The errCheck function gracefully manages errors, warnings, etc. // The errCheck function gracefully manages errors, warnings, etc.
func errCheck(e error, level int, message string) { func errCheck(e error, level int, message string) {
......
...@@ -237,6 +237,7 @@ func main() { ...@@ -237,6 +237,7 @@ func main() {
if v.ID == w.ID { if v.ID == w.ID {
devFound = true devFound = true
break
} }
} }
...@@ -293,76 +294,102 @@ func main() { ...@@ -293,76 +294,102 @@ func main() {
fmt.Println("") fmt.Println("")
// Add or update those devices that are in the CNML file // Add or update those devices that are in the CNML file
for k, v := range cnmlDevsIpv4sGraphs { var newDevs []cnml.DeviceIpv4sGraphserver
var curDevs []cnml.DeviceIpv4sGraphserver
for _, v := range cnmlDevsIpv4sGraphs {
// Check if the device is in Antidote already and add it if missing // Check if the device is in Antidote already and add it if missing
if !isDeviceIpv4sGraphserverInArray(v, keepAntidoteDevices) && len(v.Addresses) > 0 { if !isDeviceIpv4sGraphserverInArray(v, keepAntidoteDevices) && len(v.Addresses) > 0 {
saveDevFail := false newDevs = append(newDevs, v)
// Add the device to the devices set } else {
if saveDeviceIpv4sGraphserverToAntidote(v) { curDevs = append(curDevs, v)
antidoteAddDeviceSuccess++ }
} else { }
antidoteAddDeviceFail++
saveDevFail = true
}
if !saveDevFail { addDevs := goclidote.AddDevicesToAntidote(newDevs, dbHost, dbPort)
// Save the default graph server
if saveGraphserverToDeviceIpv4sGraphserverToAntidote(v) {
antidoteAddGraphserverSuccess++
} else {
antidoteAddGraphserverFail++
}
// Iterate through all the device's addresses and add them if new for k, v := range newDevs {
for _, w := range v.Addresses { devFound := false
if saveIpv4addressToDeviceIpv4sGraphserverToAntidote(v, w) { for _, w := range addDevs {
antidoteAddAddressSuccess++
} else {
antidoteAddAddressFail++
}
}
// Iterate through all the device's SNMP names and add them if new if v.ID == w.ID {
for _, w := range v.SNMPNames { devFound = true
if saveSNMPNameToDeviceIpv4sGraphserverToAntidote(v, w) { break
antidoteAddSNMPNameSuccess++ }
} else { }
antidoteAddSNMPNameFail++
} if devFound {
} // Added the device to AntidoteDB
antidoteAddDeviceSuccess++
// Added the device's graphserver to AntidoteDB
antidoteAddGraphserverSuccess++
// Added the device's IPv4 addresses from AntidoteDB
for _ = range v.Addresses {
antidoteAddAddressSuccess++
}
// Removed the device's SNMP names from AntidoteDB
for _ = range v.SNMPNames {
antidoteAddSNMPNameSuccess++
} }
} else { } else {
// The device is in AntidoteDB; check if it has changed // Failed to add the device to AntidoteDB
if v.GraphServer != keepAntidoteDevices[posDeviceInSlice(v, keepAntidoteDevices)].GraphServer { antidoteAddDeviceFail++
// Save the new default graph server // Failed to add the device's graphserver to AntidoteDB
if saveGraphserverToDeviceIpv4sGraphserverToAntidote(v) { antidoteAddGraphserverFail++
antidoteAddGraphserverSuccess++ // Failed to add the device's IPv4 addresses to AntidoteDB
} else { for _ = range v.Addresses {
antidoteAddGraphserverFail++ antidoteAddAddressFail++
}
} }
// Failed to add the device's SNMP names to AntidoteDB
for _ = range v.SNMPNames {
antidoteAddSNMPNameFail++
}
}
ips1 := v.Addresses if k == 0 {
ips2 := keepAntidoteDevices[posDeviceInSlice(v, keepAntidoteDevices)].Addresses fmt.Printf("%d devices added to AntidoteDB (%d success, %d fail)\n", antidoteAddDeviceSuccess+antidoteAddDeviceFail, antidoteAddDeviceSuccess, antidoteAddDeviceFail)
fmt.Printf("%d graphservers added or updated to AntidoteDB (%d success, %d fail)\n", antidoteAddGraphserverSuccess+antidoteAddGraphserverFail, antidoteAddGraphserverSuccess, antidoteAddGraphserverFail)
fmt.Printf("%d IPv4 addresses added or updated to AntidoteDB (%d success, %d fail)\n", antidoteAddAddressSuccess+antidoteAddAddressFail, antidoteAddAddressSuccess, antidoteAddAddressFail)
fmt.Printf("%d SNMP names added or updated to AntidoteDB (%d success, %d fail)\n", antidoteAddSNMPNameSuccess+antidoteAddSNMPNameFail, antidoteAddSNMPNameSuccess, antidoteAddSNMPNameFail)
} else {
fmt.Printf("\033[4A\r%d devices added to AntidoteDB (%d success, %d fail)\n", antidoteAddDeviceSuccess+antidoteAddDeviceFail, antidoteAddDeviceSuccess, antidoteAddDeviceFail)
fmt.Printf("\r%d graphservers added or updated to AntidoteDB (%d success, %d fail)\n", antidoteAddGraphserverSuccess+antidoteAddGraphserverFail, antidoteAddGraphserverSuccess, antidoteAddGraphserverFail)
fmt.Printf("\r%d IPv4 addresses added or updated to AntidoteDB (%d success, %d fail)\n", antidoteAddAddressSuccess+antidoteAddAddressFail, antidoteAddAddressSuccess, antidoteAddAddressFail)
fmt.Printf("\r%d SNMP names added or updated to AntidoteDB (%d success, %d fail)\n", antidoteAddSNMPNameSuccess+antidoteAddSNMPNameFail, antidoteAddSNMPNameSuccess, antidoteAddSNMPNameFail)
}
}
sort.Strings(ips1) for k, v := range curDevs {
sort.Strings(ips2) // The device is in AntidoteDB; check if it has changed
if v.GraphServer != keepAntidoteDevices[posDeviceInSlice(v, keepAntidoteDevices)].GraphServer {
// Save the new default graph server
if saveGraphserverToDeviceIpv4sGraphserverToAntidote(v) {
antidoteAddGraphserverSuccess++
} else {
antidoteAddGraphserverFail++
}
}
if !compareStringArrays(ips1, ips2) { ips1 := v.Addresses
for _, w := range keepAntidoteDevices[posDeviceInSlice(v, keepAntidoteDevices)].Addresses { ips2 := keepAntidoteDevices[posDeviceInSlice(v, keepAntidoteDevices)].Addresses
if removeIpv4addressFromDeviceIpv4sGraphserver(v, w) {
antidoteDeleteAddressSuccess++ sort.Strings(ips1)
} else { sort.Strings(ips2)
antidoteDeleteAddressFail++
} if !compareStringArrays(ips1, ips2) {
for _, w := range keepAntidoteDevices[posDeviceInSlice(v, keepAntidoteDevices)].Addresses {
if removeIpv4addressFromDeviceIpv4sGraphserver(v, w) {
antidoteDeleteAddressSuccess++
} else {
antidoteDeleteAddressFail++
} }
// Iterate through all the device's addresses and add them if new }
for _, w := range v.Addresses { // Iterate through all the device's addresses and add them if new
if saveIpv4addressToDeviceIpv4sGraphserverToAntidote(v, w) { for _, w := range v.Addresses {
antidoteAddAddressSuccess++ if saveIpv4addressToDeviceIpv4sGraphserverToAntidote(v, w) {
} else { antidoteAddAddressSuccess++
antidoteAddAddressFail++ } else {
} antidoteAddAddressFail++
} }
} }
} }
......
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