Skip to content
Snippets Groups Projects
databaseDeviceStore.go 5.12 KiB
Newer Older
  • Learn to ignore specific revisions
  • package nucleus
    
    import (
    	"fmt"
    
    	"code.fbi.h-da.de/danet/gosdn/controller/interfaces/device"
    	"code.fbi.h-da.de/danet/gosdn/controller/nucleus/database"
    	"code.fbi.h-da.de/danet/gosdn/controller/nucleus/errors"
    	"code.fbi.h-da.de/danet/gosdn/controller/store"
    	"go.mongodb.org/mongo-driver/bson"
    	"go.mongodb.org/mongo-driver/bson/primitive"
    	"go.mongodb.org/mongo-driver/mongo/options"
    
    	"github.com/google/uuid"
    	log "github.com/sirupsen/logrus"
    )
    
    // DatabaseDeviceStore is used to store Devices
    type DatabaseDeviceStore struct {
    	storeName string
    }
    
    // NewDatabaseDeviceStore returns a DeviceStore
    
    func NewDatabaseDeviceStore(pndUUID uuid.UUID) device.Store {
    
    	return &DatabaseDeviceStore{
    		storeName: fmt.Sprintf("device-store-%s.json", pndUUID.String()),
    	}
    }
    
    // Get takes a Device's UUID or name and returns the Device.
    
    func (s *DatabaseDeviceStore) Get(query store.Query) (device.LoadedDevice, error) {
    	var loadedDevice device.LoadedDevice
    
    
    	if query.ID.String() != "" {
    
    		loadedDevice, err := s.getByID(query.ID)
    
    		if err != nil {
    
    		return loadedDevice, nil
    
    	loadedDevice, err := s.getByName(query.Name)
    
    	if err != nil {
    
    	return loadedDevice, nil
    
    func (s *DatabaseDeviceStore) getByID(idOfDevice uuid.UUID) (device.LoadedDevice, error) {
    	var loadedDevice device.LoadedDevice
    
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    
    	db := client.Database(database.DatabaseName)
    	collection := db.Collection(s.storeName)
    
    	result := collection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: idOfDevice.String()}})
    
    	if result == nil {
    
    		return loadedDevice, errors.ErrCouldNotFind{ID: idOfDevice}
    
    	}
    
    	err := result.Decode(&loadedDevice)
    	if err != nil {
    
    		log.Printf("Failed marshalling %v", err)
    
    		return loadedDevice, errors.ErrCouldNotMarshall{Identifier: idOfDevice, Type: loadedDevice, Err: err}
    
    	return loadedDevice, nil
    }
    
    
    func (s *DatabaseDeviceStore) getByName(nameOfDevice string) (device.LoadedDevice, error) {
    	var loadedDevice device.LoadedDevice
    
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    
    	db := client.Database(database.DatabaseName)
    	collection := db.Collection(s.storeName)
    	result := collection.FindOne(ctx, bson.D{primitive.E{Key: "name", Value: nameOfDevice}})
    	if result == nil {
    
    		return loadedDevice, errors.ErrCouldNotFind{Name: nameOfDevice}
    
    	err := result.Decode(&loadedDevice)
    
    	if err != nil {
    
    		log.Printf("Failed marshalling %v", err)
    
    		return loadedDevice, errors.ErrCouldNotMarshall{Identifier: nameOfDevice, Type: loadedDevice, Err: err}
    
    	return loadedDevice, nil
    
    }
    
    // GetAll returns all stored devices.
    
    func (s *DatabaseDeviceStore) GetAll() ([]device.LoadedDevice, error) {
    	var loadedDevices []device.LoadedDevice
    
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    	db := client.Database(database.DatabaseName)
    	collection := db.Collection(s.storeName)
    
    	cursor, err := collection.Find(ctx, bson.D{})
    	if err != nil {
    		return nil, err
    	}
    	defer cursor.Close(ctx)
    
    	err = cursor.All(ctx, &loadedDevices)
    	if err != nil {
    		log.Printf("Failed marshalling %v", err)
    
    
    		return nil, errors.ErrCouldNotMarshall{Type: loadedDevices, Err: err}
    
    	return loadedDevices, nil
    
    }
    
    // Add adds a device to the device store.
    func (s *DatabaseDeviceStore) Add(deviceToAdd device.Device) error {
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    
    	_, err := client.Database(database.DatabaseName).
    		Collection(s.storeName).
    		InsertOne(ctx, deviceToAdd)
    	if err != nil {
    		log.Printf("Could not create Device: %v", err)
    
    		return errors.ErrCouldNotCreate{Identifier: deviceToAdd.ID(), Type: deviceToAdd, Err: err}
    
    	}
    
    	return nil
    }
    
    // Update updates a existing device.
    func (s *DatabaseDeviceStore) Update(deviceToUpdate device.Device) error {
    
    	var updatedLoadedDevice device.LoadedDevice
    
    
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    
    
    Andre Sterba's avatar
    Andre Sterba committed
    	update := bson.D{primitive.E{Key: "$set", Value: deviceToUpdate}}
    
    
    	upsert := false
    	after := options.After
    	opt := options.FindOneAndUpdateOptions{
    		Upsert:         &upsert,
    		ReturnDocument: &after,
    	}
    
    	err := client.Database(database.DatabaseName).
    		Collection(s.storeName).
    		FindOneAndUpdate(
    
    Andre Sterba's avatar
    Andre Sterba committed
    			ctx, bson.M{"_id": deviceToUpdate.ID().String()}, update, &opt).
    		Decode(&updatedLoadedDevice)
    
    	if err != nil {
    		log.Printf("Could not update Device: %v", err)
    
    
    		return errors.ErrCouldNotUpdate{Identifier: deviceToUpdate.ID(), Type: deviceToUpdate, Err: err}
    
    	}
    
    	return nil
    }
    
    // Delete deletes a device from the device store.
    func (s *DatabaseDeviceStore) Delete(deviceToDelete device.Device) error {
    	client, ctx, cancel := database.GetMongoConnection()
    	defer cancel()
    	defer client.Disconnect(ctx)
    
    	db := client.Database(database.DatabaseName)
    	collection := db.Collection(s.storeName)
    	_, err := collection.DeleteOne(ctx, bson.D{primitive.E{Key: deviceToDelete.ID().String()}})
    	if err != nil {
    
    		return errors.ErrCouldNotDelete{Identifier: deviceToDelete.ID(), Type: deviceToDelete, Err: err}